1

Not all that sure how I would describe this question, so I'll jump right into the example code.

I have a Constants.java

   package com.t3hh4xx0r.poc;


public class Constants {

    //RootzWiki Device Forum Constants
    public static final String RWFORUM = "http://rootzwiki.com/forum/";
    public static final String TORO = "362-cdma-galaxy-nexus-developer-forum";

    public static String DEVICE;
}

In trying to determine the device type, I use this method.

public void getDevice() {
    Constants.DEVICE = android.os.Build.DEVICE.toUpperCase();
    String thread = Constants.(Constants.DEVICE);
}

Thats not correct though, but thats how I would think it would have worked.

Im setting the Constants.DEVICE to TORO in my case on the Galaxy Nexus. I want to then set the thread String to Constants.TORO.

I dont think I'm explaining this well, but you shoudl be able to understand what I'm trying to do fromt he example code. I want

Constants.(VALUE OF WHAT CONSTANTS.DEVICE IS) set for the String thread.

Another way to put it,

I want to get Constants.(//value of android.os.Build.DEVICE.toUpperCase())

I apologies for the poorly worded question, i dont know of any better way to explain what Im trying to achieve.

Im trying to determine the thread based on the device type. I could go in and do an

if (Constants.DEVICE.equals("TORO"){
    String thread = Constants.TORO;
}

But I plan on adding a lot more device options in the future and would like to make it as easy as adding a string to the Constants.java rather than having to add another if clause.

r2DoesInc
  • 3,759
  • 3
  • 29
  • 60
  • Wait, why are you trying to set this yourself? Doesn't an Android device already have a way to provide its identity? – fge Jan 03 '12 at 11:04
  • It seems that reflection might be of help to you: http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful http://java.sun.com/developer/technicalArticles/ALT/Reflection/ – Jave Jan 03 '12 at 11:07
  • android.os.Build.DEVICE.toUpperCase() will return TORO. Thats not the problem. The problem is then setting thread as Constants.TORO – r2DoesInc Jan 03 '12 at 11:13

3 Answers3

4

I would suggest using an enum instead of just strings - then you can use:

String name = android.os.Build.DEVICE.toUpperCase();
// DeviceType is the new enum
DeviceType type = Enum.valueOf(DeviceType.class, name);

You can put the value of the string in a field for the enum, and expose it via a property:

public enum DeviceType {
    RWFORUM("http://rootzwiki.com/forum/"),
    TORO("362-cdma-galaxy-nexus-developer-forum");

    private final String forumUrl;

    private DeviceType(String forumUrl) {
        this.forumUrl = forumUrl;
    }

    public String getForumUrl() {
        return forumUrl;
    }
}

(I'm guessing at the meaning of the string value - not a great guess, but hopefully it gives the right idea so you can make your actual code more meaningful.)

EDIT: Or to use a map:

Map<String, String> deviceToForumMap = new HashMap<String, String>();
deviceToForumMap.put("RWFORUM", "http://rootzwiki.com/forum/");
deviceToForumMap.put("TORO", "362-cdma-galaxy-nexus-developer-forum");

...

String forum = deviceToForumMap.get(android.os.Build.DEVICE.toUpperCase());
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Ive never used an enum before, would you mind showing a little more example code of how i would use "type" after setting it? – r2DoesInc Jan 03 '12 at 11:09
  • @r2DoesInc: Well it's not clear what you're trying to achieve, but you can call `type.getForumUrl()` to get the string value. – Jon Skeet Jan 03 '12 at 11:10
  • I added another explanation of what im trying to achieve. – r2DoesInc Jan 03 '12 at 11:15
  • @r2DoesInc: Well, you've explained that you want to get a value - and I've told you how to get it using the enum. You haven't explained the wider goal. – Jon Skeet Jan 03 '12 at 11:16
  • I want to be able to determine the forum url based on only the device type. Im tying it in with some of the other stuff ive been asking about previously to create a zip scraper. depending on your device type, it parses thread links out of the corresponding sub forum for your device. – r2DoesInc Jan 03 '12 at 11:20
  • @r2DoesInc: Okay, so either of the solutions I've given should work... although you'll need some more defensive code in case the device isn't one you're already aware of. – Jon Skeet Jan 03 '12 at 11:32
  • While either of these Im sure would have worked, Im unfamiliar with enums and have only used hashmaps once before. the accepted answer using reflection was the simplest and one i was most comfortable using. Thank you for you input though, its given me insight into how enums work. :) – r2DoesInc Jan 03 '12 at 11:37
  • @r2DoesInc: Reflection is a really ugly hack here, IMO. You should *not* use reflection for this situation - it's generally only a good fit when there are no other alternatives. If you *only* want to map the device type to a string value, then use a hash map. If there are other things you want to do with the device type, then use an enum. – Jon Skeet Jan 03 '12 at 11:43
  • What would the downsides of using reflection be? Im new to the world of developing apps, Ive been doing roms for about two years now, but just recently got into apps and Im still playing catch up with a lot of things. Im all self taught as well, so much of the knowledge that most java developers take for granted, i may not have learned yet. – r2DoesInc Jan 03 '12 at 11:53
  • @r2DoesInc: The reflection APIs can throw all kinds of exceptions which are really nothing to do with what you're trying to do. You don't get strong typing at compile-time. They're slow (although that's irrelevant here, probably) and they're just generally *not* the way to do what you're trying to do. It's very much a case of trying to use a screwdriver to bang in a nail, because you happen to have seen a screwdriver. It's not the right tool for the job - and you *will* want to at least understand maps (and `HashMap` in particular) soon anyway. – Jon Skeet Jan 03 '12 at 12:04
  • Alright, I see what you mean. I was able to get the enum working with the sample code you provided me. Switched the accepted answer as well. Thank you for your explanation. – r2DoesInc Jan 03 '12 at 12:13
  • `Enum.valueOf(DeviceType.class, name)` can be written as `DeviceType.valueOf(name)` – user102008 May 08 '12 at 20:28
1

You can use reflection:

Constants.DEVICE = android.os.Build.DEVICE.toUpperCase();
String thread = (String) Constants.class.getField(Constants.DEVICE).get(null);
njzk2
  • 38,969
  • 7
  • 69
  • 107
0

Not sure I've understood the question properly, but I feel that it's a right place to use a Map. The outcome will be something like this:

    HashMap<String, String> map;

    map.put(TORO, DEVICE);

    Constants.DEVICE = android.os.Build.DEVICE.toUpperCase();
    String thread = map.get(Constants.DEVICE);

Sorry for a possible misunderstanding or your question, but I hope you've got the idea.

P.S. You can find more info about Maps in the Java documentation: Map, HashMap.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • Close, but the thread string needs to be set to the value of TORO, and TORO is only set if DEVICE returns toro. Sorry for the poorly worded question, Im not sure exactly how to explain what i needed. – r2DoesInc Jan 03 '12 at 11:11
  • @r2DoesInc: I've edited my answer to give a solution using maps as well, which I think will be a bit more concrete than this. – Jon Skeet Jan 03 '12 at 11:18