Android

Intent flags part01

2019-04-08  本文已影响0人  JaedenKil

May refer to here.

| (bitwise logically or)
Example: 100 | 001 = 101
The result has a 1 on the position where at least one value has a 1.
& (bitwise logically and)
Example: 100 & 101 = 100
The result has a 1 on the position where both values have a 1.
~ (bitwise inverse)
Example: ~100 = 011
All bits get inverted.
^ (bitwise exclusive or)
Example: 100^101 = 001
The result has a 1 on the position where one value has a 1 whereas the other value has a 0.
private boolean containsFlag(int flagSet, int flag){
    return (flagSet|flag) == flagSet;
}
    @Test
    public void testFlag() throws PackageManager.NameNotFoundException {
        Context ctx = InstrumentationRegistry.getTargetContext();
        int flag = ctx.getPackageManager().getPackageInfo(packageName, 0).applicationInfo.flags;
        int debuggable = ApplicationInfo.FLAG_DEBUGGABLE;
        int finalFlag = flag | debuggable;
        Log.d(TAG, "Flag: 0x" + Integer.toHexString(flag));
        Log.d(TAG, "Debuggable flag: 0x" + Integer.toHexString(debuggable));
        Log.d(TAG, "Final flag: 0x" + Integer.toHexString(finalFlag));
        Log.d(TAG, "Is debuggable? " + (finalFlag == flag));
    }
Flag: 0x38a83e46
Debuggable flag: 0x2
Final flag: 0x38a83e46
Is debuggable? true

Which means this app version is debuggable.

private int addFlag(int flagSet, int flag){
    return flagSet|flag;
}
private int toggleFlag(int flagSet, int flag){
    return flagSet^flag;
}

Since the exclusive or-operator ^ only keeps the bits where both bits are different, it will remove the bits that are set in the flag when they are also already set in the flag-set. And it will add them if they are not set in the flag-set.

private int removeFlag(int flagSet, int flag){
    return flagSet&(~flag);
}

First we invert the flag that we want to remove. Then we combine this with the current flag-set. This means only where the bit in the flag was set, we now have a 0. And that’s why we also have a 0 at this position in the final result. The rest of the result is the same as in the original flag-set.

上一篇 下一篇

猜你喜欢

热点阅读