Reputation: 1341
I've read this post in order to check the flags for ApplicationInfo . But then I read this post in order to see if there's a way to get all the values contained in a flag, but I'm a bit confused. Can anyone tell me if there's a way to get all the values and explain it?
Thanks in advance.
Upvotes: 2
Views: 1973
Reputation: 14183
int flags work this way, you have an integer, say 48, in binary it would be 0011 0000
. this means it contains 2 flags one is 0010 0000
and the other one is 0001 0000
. if you check ApplicationInfo flags you would notice that all the values are powers of 2 (1,2,4,8..) this is because the binary representation of a power of 2 is all zeros and only one 1. so, in order to check what flags the integer 48 contains, you can do it this way (notice the &
operator, it's a bitwise AND, if you have doubts about it please read this):
int value = 48;
int[] possibleFlags = {ApplicationInfo.FLAG_FACTORY_TEST, ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING, ApplicationInfo.FLAG_ALLOW_BACKUP};
String[] flagNames = {"FLAG_FACTORY_TEST", "FLAG_ALLOW_TASK_REPARENTING", "FLAG_ALLOW_BACKUP"};
for (int i = 0; i < possibleFlags.length; i++) {
int aFlag = possibleFlags[i];
String name = flagNames[i];
boolean hasFlag = (value & aFlag) > 0;
Log.d("Flags", String.format("flag %s %s", name, hasFlag ? "PRESENT" : "NOT PRESENT"));
}
I didn't really understand the approach you're asking about, but another way to check all the flags is to iterate all powers of 2 from 1 to 31 (for 4 bytes integers like java) but you still need to link the numbers to the actual constant, would it be useful? e.g.
for (int i = 0; i < 32; i++) {
int aFlag = 1 << i;
boolean hasFlag = (value & aFlag) > 0;
Log.d("Flags", String.format("flag %s %s", aFlag, hasFlag ? "PRESENT" : "NOT PRESENT"));
}
Upvotes: 3