Example usage for android.app Activity getWindowManager

List of usage examples for android.app Activity getWindowManager

Introduction

In this page you can find the example usage for android.app Activity getWindowManager.

Prototype

public WindowManager getWindowManager() 

Source Link

Document

Retrieve the window manager for showing custom windows.

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 {/*from www  .  j  a  va 2  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;
}

From source file:Main.java

public static int getOrientationHint(Activity activity, int cameraId) {
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;//from  w ww .j  a  v a 2s  .c  om
        break;
    case Surface.ROTATION_90:
        degrees = 90;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        break;
    case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

    int result = 0;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation - degrees + 360) % 360;
    } else { // back-facing camera
        result = (info.orientation + degrees) % 360;
    }
    return result;
}

From source file:Main.java

public static int getCameraDisplayOrientation(Activity activity, int cameraId) {
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;//from  www. j  a  v  a 2s  . co m
        break;
    case Surface.ROTATION_90:
        degrees = 90;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        break;
    case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360; // compensate the mirror
    } else { // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }
    return result;
}

From source file:Main.java

/**
 * Locks specified activity's orientation until it is unlocked or recreated.
 *
 * @param activity activity//from  w  w w .  ja va  2 s. c  o m
 */
public static void lockOrientation(Activity activity) {
    Configuration config = activity.getResources().getConfiguration();
    final int deviceOrientation = config.orientation;
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();

    int orientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
    if (deviceOrientation == Configuration.ORIENTATION_PORTRAIT) {
        orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_180)
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
    } else if (deviceOrientation == Configuration.ORIENTATION_LANDSCAPE) {
        orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270)
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
    }

    activity.setRequestedOrientation(orientation);
}

From source file:Main.java

public static int setCameraDisplayOrientation(Activity activity, int cameraId, Camera camera) {
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;/* w w  w  . j a v  a2s.  com*/
        break;
    case Surface.ROTATION_90:
        degrees = 90;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        break;
    case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360; // compensate the mirror
    } else { // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }
    Log.d(TAG, "camera display orientation: " + result);
    camera.setDisplayOrientation(result);

    return result;
}

From source file:Main.java

public static Bitmap downSampleBitmap(Uri uri, Activity act, Boolean needRotate) {
    DisplayMetrics displaymetrics = new DisplayMetrics();
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    Resources r = act.getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, r.getDisplayMetrics()); // 50: magic num
    int targetWidth = displaymetrics.heightPixels;
    int targetHeight = displaymetrics.widthPixels - px;

    Bitmap resizedBitmap = decodeSampledBitmap(uri, targetWidth, targetHeight, act);
    Bitmap returnBitmap = null;/*w ww  .j  a  v a2 s  .c o m*/
    ExifInterface exif;
    try {
        float degree = 0;
        exif = new ExifInterface(uri.toString());
        int orient = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        if (resizedBitmap != null && needRotate) {
            degree = getDegree(orient);
            if (degree != 0) {
                returnBitmap = createRotatedBitmap(resizedBitmap, degree);
            }
            returnBitmap = returnBitmap == null ? resizedBitmap : returnBitmap;
        }
    } catch (IOException e) {
        Log.v(TAG, "not found file at downsample");
        e.printStackTrace();
    }
    return returnBitmap;
}

From source file:Main.java

/**
 * @Thach Feb 21, 2014//w  ww  . j  a va  2s .  c om
 * @Desc lock Orientation
 * @param b
 */
@SuppressWarnings("deprecation")
public static void lockOrientation(Activity act, boolean isLock) {
    if (act == null) {
        return;
    }
    if (isLock) {
        Display display = act.getWindowManager().getDefaultDisplay();
        int rotation = display.getRotation();
        int height;
        int width;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
            height = display.getHeight();
            width = display.getWidth();
        } else {
            Point size = new Point();
            display.getSize(size);
            height = size.y;
            width = size.x;
        }
        switch (rotation) {
        case Surface.ROTATION_90:
            if (width > height)
                act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            else
                act.setRequestedOrientation(9/* reversePortait */);
            break;
        case Surface.ROTATION_180:
            if (height > width)
                act.setRequestedOrientation(9/* reversePortait */);
            else
                act.setRequestedOrientation(8/* reverseLandscape */);
            break;
        case Surface.ROTATION_270:
            if (width > height)
                act.setRequestedOrientation(8/* reverseLandscape */);
            else
                act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            break;
        default:
            if (height > width)
                act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            else
                act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    } else {
        act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }
}

From source file:jp.seikeidenron.androidtv.common.Utils.java

public static int getDisplayWidthInPx(Activity activity) {
    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return metrics.widthPixels;
}

From source file:jp.seikeidenron.androidtv.common.Utils.java

public static int getDisplayHeightInPx(Activity activity) {
    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return metrics.heightPixels;
}

From source file:Main.java

public static int getScreenWidth(Activity context) {
    //        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    //        return wm.getDefaultDisplay().getWidth();

    DisplayMetrics dpMetrics = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(dpMetrics);
    return dpMetrics.widthPixels;
}