2018年9月18日 星期二

Use reflection to get framework hide variable value or resource value

Code snippet:

1. Get hide constant value
public static int getHideIntConstants(String classAndPkgName, String fieldName, int defaultValue){
    int fieldValue = defaultValue;

    Class<?> c = null;
    try {
        c = Class.forName(classAndPkgName);
        Field field = c.getDeclaredField(fieldName);
        field.setAccessible(true);
        fieldValue = field.getInt(null);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (Exception e){
        e.printStackTrace();
    }
    String hesString = Integer.toHexString(fieldValue);
    Log.d(TAG, "fieldValue " + fieldName + ": " + fieldValue + " ; hex: " + hesString);
    return fieldValue;
}

2. Get hide constant value declared in inner class
public static int getTypeDisplayOverlayInt(Window window) {
    int typeDisplayOverlay = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;

    Class<?> c = null;
    try {
        c = Class.forName("android.view.WindowManager");
        Class<?>[] classes = c.getDeclaredClasses();
        for(Class innerClass: classes){
            if(innerClass.getName().contains("LayoutParams")){
                Field field = innerClass.getDeclaredField("TYPE_DISPLAY_OVERLAY");
                field.setAccessible(true);
                typeDisplayOverlay = field.getInt(window.getAttributes());
                break;
            }
        }
    } catch (Exception e){
        e.printStackTrace();
    }
    return typeDisplayOverlay;
}

3. Get resource value
public static int getStatusHeight(Context context) {
    int statusBarHeight = -1;
    try {
        Class<?> clazz = Class.forName("com.android.internal.R$dimen");
        Object object = clazz.newInstance();
        int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
        statusBarHeight = context.getResources().getDimensionPixelSize(height);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return statusBarHeight;
}

4. Get SystemUI apk recource value
public static String getDateFormat(Context context) {
    PackageManager pm = context.getPackageManager();
    try {
        Resources resources = pm.getResourcesForApplication("com.android.systemui");
        int id = resources.getIdentifier("abbrev_wday_month_day_no_year", "string", "com.android.systemui");
        if (id > 0) {
            return resources.getString(id);
        } else {
            return null;
        }

    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

1 則留言:

eleanejacci 提到...

Play Live Dealer Casino Games Online - Jackson Hole Casino
Play live dealer 경산 출장샵 casino 부산광역 출장마사지 games online at Jackson Hole Casino – your 남원 출장안마 home 아산 출장마사지 for great gaming fun, safety, 보령 출장마사지 and bonuses! We have hand-picked the best games