Example usage for android.util DisplayMetrics getClass

List of usage examples for android.util DisplayMetrics getClass

Introduction

In this page you can find the example usage for android.util DisplayMetrics getClass.

Prototype

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

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:Main.java

@TargetApi(4)
public static boolean isTabletDevice(android.content.Context activityContext) {
    if (!androidAPIover(4)) //VERSION
        return false; //If there is a tablet running 1.5.....GOD HELP YOU

    try {//w w w.ja  va2 s. c om

        // Verifies if the Generalized Size of the device is XLARGE to be
        // considered a Tablet
        Configuration config = activityContext.getResources().getConfiguration();
        Log.i("screenlayout",
                Integer.parseInt(
                        Configuration.class.getDeclaredField("SCREENLAYOUT_SIZE_LARGE").get(config).toString())
                        + "!!!");
        boolean xlarge =
                //activityContext.getResources().getConfiguration().screenLayout &
                Boolean.parseBoolean(config.getClass().getField("screenLayout").get(config).toString())
                        & getStaticInt(config, "SCREENLAYOUT_SIZE_MASK") >= //Changed this from == to >= because my tablet was returning 8 instead of 4.
                        Integer.parseInt(Configuration.class.getDeclaredField("SCREENLAYOUT_SIZE_LARGE")
                                .get(config).toString());
        getStaticInt(config, "SCREENLAYOUT_SIZE_MASK");

        // If XLarge, checks if the Generalized Density is at least MDPI (160dpi)
        if (xlarge) {
            android.util.DisplayMetrics metrics = new android.util.DisplayMetrics();
            Activity activity = (Activity) activityContext;
            activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

            //This next block lets us get constants that are not available in lower APIs.
            // If they aren't available, it's safe to assume that the device is not a tablet.
            // If you have a tablet or TV running Android 1.5, what the fuck is wrong with you.
            int xhigh = -1, tv = -1;
            try {
                Field f = android.util.DisplayMetrics.class.getDeclaredField("DENSITY_XHIGH");
                xhigh = (Integer) f.get(null);
                f = android.util.DisplayMetrics.class.getDeclaredField("DENSITY_TV");
                xhigh = (Integer) f.get(null);
            } catch (Exception e) {
            }

            // MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160, DENSITY_TV=213, DENSITY_XHIGH=320
            int densityDpi = Integer
                    .parseInt(metrics.getClass().getDeclaredField("densityDpi").get(metrics).toString());
            if (densityDpi == 240 || //DENSITY_HIGH
                    densityDpi == 160 || //DENSITY_MEDIUM
                    //densityDpi == android.util.DisplayMetrics.DENSITY_DEFAULT ||
                    //densityDpi == android.util.DisplayMetrics.DENSITY_HIGH ||
                    //densityDpi == android.util.DisplayMetrics.DENSITY_MEDIUM ||
                    densityDpi == tv || densityDpi == xhigh) {
                return true;
            }
        }
    } catch (Exception e) {
    }
    return false;
}