FileDocCategorySizeDatePackage
RotationPolicy.javaAPI DocAndroid 5.1 API7799Thu Mar 12 22:22:10 GMT 2015com.android.internal.view

RotationPolicy

public final class RotationPolicy extends Object
Provides helper functions for configuring the display rotation policy.

Fields Summary
private static final String
TAG
private static final int
CURRENT_ROTATION
private static final int
NATURAL_ROTATION
Constructors Summary
private RotationPolicy()


      
    
Methods Summary
private static booleanareAllRotationsAllowed(android.content.Context context)

        return context.getResources().getBoolean(R.bool.config_allowAllRotations);
    
public static intgetRotationLockOrientation(android.content.Context context)
Returns the orientation that will be used when locking the orientation from system UI with {@link #setRotationLock}. If the device only supports locking to its natural orientation, this will be either Configuration.ORIENTATION_PORTRAIT or Configuration.ORIENTATION_LANDSCAPE, otherwise Configuration.ORIENTATION_UNDEFINED if any orientation is lockable.

        if (!areAllRotationsAllowed(context)) {
            final Point size = new Point();
            final IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
            try {
                wm.getInitialDisplaySize(Display.DEFAULT_DISPLAY, size);
                return size.x < size.y ?
                        Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
            } catch (RemoteException e) {
                Log.w(TAG, "Unable to get the display size");
            }
        }
        return Configuration.ORIENTATION_UNDEFINED;
    
public static booleanisRotationLockToggleVisible(android.content.Context context)
Returns true if the rotation-lock toggle should be shown in system UI.

        return isRotationSupported(context) &&
                Settings.System.getIntForUser(context.getContentResolver(),
                        Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0,
                        UserHandle.USER_CURRENT) == 0;
    
public static booleanisRotationLocked(android.content.Context context)
Returns true if rotation lock is enabled.

        return Settings.System.getIntForUser(context.getContentResolver(),
                Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT) == 0;
    
public static booleanisRotationSupported(android.content.Context context)
Gets whether the device supports rotation. In general such a device has an accelerometer and has the portrait and landscape features.

param
context Context for accessing system resources.
return
Whether the device supports rotation.

        PackageManager pm = context.getPackageManager();
        return pm.hasSystemFeature(PackageManager.FEATURE_SENSOR_ACCELEROMETER)
                && pm.hasSystemFeature(PackageManager.FEATURE_SCREEN_PORTRAIT)
                && pm.hasSystemFeature(PackageManager.FEATURE_SCREEN_LANDSCAPE)
                && context.getResources().getBoolean(
                        com.android.internal.R.bool.config_supportAutoRotation);
    
public static voidregisterRotationPolicyListener(android.content.Context context, com.android.internal.view.RotationPolicy$RotationPolicyListener listener)
Registers a listener for rotation policy changes affecting the caller's user

        registerRotationPolicyListener(context, listener, UserHandle.getCallingUserId());
    
public static voidregisterRotationPolicyListener(android.content.Context context, com.android.internal.view.RotationPolicy$RotationPolicyListener listener, int userHandle)
Registers a listener for rotation policy changes affecting a specific user, or USER_ALL for all users.

        context.getContentResolver().registerContentObserver(Settings.System.getUriFor(
                Settings.System.ACCELEROMETER_ROTATION),
                false, listener.mObserver, userHandle);
        context.getContentResolver().registerContentObserver(Settings.System.getUriFor(
                Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY),
                false, listener.mObserver, userHandle);
    
public static voidsetRotationLock(android.content.Context context, boolean enabled)
Enables or disables rotation lock from the system UI toggle.

        Settings.System.putIntForUser(context.getContentResolver(),
                Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0,
                UserHandle.USER_CURRENT);

        final int rotation = areAllRotationsAllowed(context) ? CURRENT_ROTATION : NATURAL_ROTATION;
        setRotationLock(enabled, rotation);
    
private static voidsetRotationLock(boolean enabled, int rotation)

        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
                    if (enabled) {
                        wm.freezeRotation(rotation);
                    } else {
                        wm.thawRotation();
                    }
                } catch (RemoteException exc) {
                    Log.w(TAG, "Unable to save auto-rotate setting");
                }
            }
        });
    
public static voidsetRotationLockForAccessibility(android.content.Context context, boolean enabled)
Enables or disables natural rotation lock from Accessibility settings. If rotation is locked for accessibility, the system UI toggle is hidden to avoid confusion.

        Settings.System.putIntForUser(context.getContentResolver(),
                Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, enabled ? 1 : 0,
                        UserHandle.USER_CURRENT);

        setRotationLock(enabled, NATURAL_ROTATION);
    
public static voidunregisterRotationPolicyListener(android.content.Context context, com.android.internal.view.RotationPolicy$RotationPolicyListener listener)
Unregisters a listener for rotation policy changes.

        context.getContentResolver().unregisterContentObserver(listener.mObserver);