Example usage for android.view Window getClass

List of usage examples for android.view Window getClass

Introduction

In this page you can find the example usage for android.view Window getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.xxxifan.devbox.core.util.ViewUtils.java

/**
 * set status bar icon to light theme, which is called dark mode.
 * should be called in onCreate()/*from   ww  w  .ja  va  2s .  c o m*/
 */
public static void setStatusBarLightMode(Activity activity, boolean lightMode) {
    if (activity == null || activity.getWindow() == null) {
        return;
    }

    Window window = activity.getWindow();
    boolean changed = false;
    // try miui
    try {
        Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
        Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
        int darkIcon = field.getInt(layoutParams);
        Method extraFlagField = window.getClass().getMethod("setExtraFlags", int.class, int.class);
        extraFlagField.invoke(window, lightMode ? darkIcon : 0, darkIcon);
        changed = true;
    } catch (Exception ignored) {
    }

    // try flyme
    try {
        WindowManager.LayoutParams lp = window.getAttributes();
        Field darkIcon = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
        Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
        darkIcon.setAccessible(true);
        meizuFlags.setAccessible(true);
        int bit = darkIcon.getInt(null);
        int value = meizuFlags.getInt(lp);
        if (lightMode) {
            value |= bit;
        } else {
            value &= ~bit;
        }
        meizuFlags.setInt(lp, value);
        window.setAttributes(lp);
        changed = true;
    } catch (Exception ignored) {
    }

    if (!changed && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int visibility = window.getDecorView().getSystemUiVisibility();
        if (lightMode) {
            visibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
        } else {
            visibility &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
        }
        window.getDecorView().setSystemUiVisibility(visibility);
    }
}

From source file:com.efithealth.app.activity.BaseActivity.java

@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);

    // ????//from  w  w  w  .  j ava  2  s. co m
    Window window = getWindow();
    Class<? extends Window> clazz = window.getClass();
    int tranceFlag = 0;
    int darkModeFlag = 0;
    Class<?> layoutParams;
    try {
        layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
        Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_TRANSPARENT");
        tranceFlag = field.getInt(layoutParams);
        field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
        darkModeFlag = field.getInt(layoutParams);
        Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
        // ?????
        // extraFlagField.invoke(window, tranceFlag, tranceFlag);
        // ???
        extraFlagField.invoke(window, tranceFlag | darkModeFlag, tranceFlag | darkModeFlag);
        // 
        // extraFlagField.invoke(window, 0, darkModeFlag);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.dw.bartinter.BarTinter.java

private void statusBar(final String colorPref) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (colorPref != null && !colorPref.isEmpty()) {
            final Window window = cordova.getActivity().getWindow();
            window.clearFlags(0x04000000);
            window.addFlags(0x80000000);
            try {
                window.getClass().getDeclaredMethod("setStatusBarColor", int.class).invoke(window,
                        Color.parseColor(colorPref));
            } catch (IllegalArgumentException ignore) {
                Log.e(PLUGIN, "Invalid hexString argument.");
            } catch (Exception ignore) {
                Log.w(PLUGIN,//from   w  w w . j a  va 2  s  .  co m
                        "Method window.setStatusBarColor not found for SDK level " + Build.VERSION.SDK_INT);
            }
        }
    }
}

From source file:com.dw.bartinter.BarTinter.java

private void navigationBar(final String colorPref) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (colorPref != null && !colorPref.isEmpty()) {
            final Window window = cordova.getActivity().getWindow();
            window.clearFlags(0x04000000);
            window.addFlags(0x80000000);
            try {
                window.getClass().getDeclaredMethod("setNavigationBarColor", int.class).invoke(window,
                        Color.parseColor(colorPref));
            } catch (IllegalArgumentException ignore) {
                Log.e(PLUGIN, "Invalid hexString argument.");
            } catch (Exception ignore) {
                Log.w(PLUGIN,// w  w w  .j  av a  2s  .c o  m
                        "Method window.setNavigationBarColor not found for SDK level " + Build.VERSION.SDK_INT);
            }
        }
    }
}

From source file:com.phonegap.bossbolo.plugin.statusbar.StatusBar.java

private void setStatusBarBackgroundColor(final String colorPref) {
    if (Build.VERSION.SDK_INT >= 21) {
        if (colorPref != null && !colorPref.isEmpty()) {
            final Window window = cordova.getActivity().getWindow();
            // Method and constants not available on all SDKs but we want to be able to compile this code with any SDK
            window.clearFlags(0x04000000); // SDK 19: WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(0x80000000); // SDK 21: WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            try {
                // Using reflection makes sure any 5.0+ device will work without having to compile with SDK level 21
                window.getClass().getDeclaredMethod("setStatusBarColor", int.class).invoke(window,
                        Color.parseColor(colorPref));
            } catch (IllegalArgumentException ignore) {
                Log.e(TAG, "Invalid hexString argument, use f.i. '#999999'");
            } catch (Exception ignore) {
                // this should not happen, only in case Android removes this method in a version > 21
                Log.w(TAG, "Method window.setStatusBarColor not found for SDK level " + Build.VERSION.SDK_INT);
            }/*from w w w . j  a v  a  2 s  . c  om*/
        }
    }
}

From source file:com.github.michaelins.lightstatusbar.LightStatusBar.java

private void setStatusBarBackgroundColor(final String colorPref) {
    if (colorPref != null && !colorPref.isEmpty()) {
        final Window window = cordova.getActivity().getWindow();
        // Method and constants not available on all SDKs but we want to
        // be able to compile this code with any SDK
        window.clearFlags(0x04000000); // SDK 19:
        // WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(0x80000000); // SDK 21:
        // WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        try {// w w  w  .j a va 2  s  .c o m
            // Using reflection makes sure any 5.0+ device will work
            // without having to compile with SDK level 21
            window.getClass().getDeclaredMethod("setStatusBarColor", int.class).invoke(window,
                    Color.parseColor(colorPref));
        } catch (IllegalArgumentException ignore) {
            LOG.e(TAG, "Invalid hexString argument, use f.i. '#999999'");
        } catch (Exception ignore) {
            // this should not happen, only in case Android removes this
            // method in a version > 21
            LOG.w(TAG, "Method window.setStatusBarColor not found for SDK level " + Build.VERSION.SDK_INT);
        }
    }
}

From source file:com.nttec.everychan.ui.theme.CustomThemeHelper.java

private static void processWindow(Context context, SparseIntArray attrs, int textColorPrimaryOriginal,
        int textColorPrimaryOverridden) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
        return;//from w w w. j  a  va 2 s . c  o  m
    boolean isLollipop = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;

    int materialPrimaryIndex = attrs.indexOfKey(R.attr.materialPrimary);
    int materialPrimaryDarkIndex = attrs.indexOfKey(R.attr.materialPrimaryDark);
    int materialNavigationBarIndex = attrs.indexOfKey(R.attr.materialNavigationBar);
    boolean overrideActionbarColor = materialPrimaryIndex >= 0;
    boolean overridePanelsColor = Math.max(materialPrimaryDarkIndex, materialNavigationBarIndex) >= 0;

    boolean overrideTextColor = textColorPrimaryOriginal != textColorPrimaryOverridden;
    if (!overrideTextColor && !overrideActionbarColor && !overridePanelsColor)
        return;

    Window window = ((Activity) context).getWindow();
    final View decorView = window.getDecorView();
    Resources resources = context.getResources();

    if (overrideActionbarColor) {
        try {
            Drawable background = new ColorDrawable(attrs.valueAt(materialPrimaryIndex));
            Object actionBar = context.getClass().getMethod("getActionBar").invoke(context);
            actionBar.getClass().getMethod("setBackgroundDrawable", Drawable.class).invoke(actionBar,
                    background);
        } catch (Exception e) {
            Logger.e(TAG, e);
        }
    }

    if (overrideTextColor) {
        int id = resources.getIdentifier("action_bar_title", "id", "android");
        if (id != 0) {
            View v = decorView.findViewById(id);
            if (v instanceof TextView)
                ((TextView) v).setTextColor(textColorPrimaryOverridden);
        }
    }

    if (isLollipop && overrideTextColor) {
        try {
            int id = resources.getIdentifier("action_bar", "id", "android");
            if (id == 0)
                throw new Exception("'android:id/action_bar' identifier not found");
            View v = decorView.findViewById(id);
            if (v == null)
                throw new Exception("view with id 'android:id/action_bar' not found");
            Class<?> toolbarClass = Class.forName("android.widget.Toolbar");
            if (!toolbarClass.isInstance(v))
                throw new Exception("view 'android:id/action_bar' is not instance of android.widget.Toolbar");
            toolbarClass.getMethod("setTitleTextColor", int.class).invoke(v, textColorPrimaryOverridden);
            setLollipopMenuOverflowIconColor((ViewGroup) v, textColorPrimaryOverridden);
        } catch (Exception e) {
            Logger.e(TAG, e);
        }
    }

    if (isLollipop && overridePanelsColor) {
        try {
            if (materialPrimaryDarkIndex >= 0) {
                window.getClass().getMethod("setStatusBarColor", int.class).invoke(window,
                        attrs.valueAt(materialPrimaryDarkIndex));
            }
            if (materialNavigationBarIndex >= 0) {
                window.getClass().getMethod("setNavigationBarColor", int.class).invoke(window,
                        attrs.valueAt(materialNavigationBarIndex));
            }
        } catch (Exception e) {
            Logger.e(TAG, e);
        }
    }
}