Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.internal.view; |
| 18 | |
| 19 | import android.content.Context; |
Svetoslav | 79578b2 | 2013-04-29 16:55:57 -0700 | [diff] [blame] | 20 | import android.content.pm.PackageManager; |
John Spurlock | 8ab172e | 2013-12-19 16:39:23 -0500 | [diff] [blame] | 21 | import android.content.res.Configuration; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 22 | import android.database.ContentObserver; |
John Spurlock | 8ab172e | 2013-12-19 16:39:23 -0500 | [diff] [blame] | 23 | import android.graphics.Point; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 24 | import android.net.Uri; |
| 25 | import android.os.AsyncTask; |
| 26 | import android.os.Handler; |
| 27 | import android.os.RemoteException; |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 28 | import android.os.UserHandle; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 29 | import android.provider.Settings; |
| 30 | import android.util.Log; |
John Spurlock | 8ab172e | 2013-12-19 16:39:23 -0500 | [diff] [blame] | 31 | import android.view.Display; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 32 | import android.view.IWindowManager; |
| 33 | import android.view.Surface; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 34 | import android.view.WindowManagerGlobal; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 35 | |
John Spurlock | 8ab172e | 2013-12-19 16:39:23 -0500 | [diff] [blame] | 36 | import com.android.internal.R; |
| 37 | |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Provides helper functions for configuring the display rotation policy. |
| 40 | */ |
| 41 | public final class RotationPolicy { |
| 42 | private static final String TAG = "RotationPolicy"; |
John Spurlock | 8ab172e | 2013-12-19 16:39:23 -0500 | [diff] [blame] | 43 | private static final int CURRENT_ROTATION = -1; |
| 44 | private static final int NATURAL_ROTATION = Surface.ROTATION_0; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 45 | |
| 46 | private RotationPolicy() { |
| 47 | } |
| 48 | |
| 49 | /** |
Svetoslav | 79578b2 | 2013-04-29 16:55:57 -0700 | [diff] [blame] | 50 | * Gets whether the device supports rotation. In general such a |
| 51 | * device has an accelerometer and has the portrait and landscape |
| 52 | * features. |
| 53 | * |
| 54 | * @param context Context for accessing system resources. |
| 55 | * @return Whether the device supports rotation. |
| 56 | */ |
| 57 | public static boolean isRotationSupported(Context context) { |
| 58 | PackageManager pm = context.getPackageManager(); |
| 59 | return pm.hasSystemFeature(PackageManager.FEATURE_SENSOR_ACCELEROMETER) |
| 60 | && pm.hasSystemFeature(PackageManager.FEATURE_SCREEN_PORTRAIT) |
| 61 | && pm.hasSystemFeature(PackageManager.FEATURE_SCREEN_LANDSCAPE); |
| 62 | } |
| 63 | |
| 64 | /** |
John Spurlock | 8ab172e | 2013-12-19 16:39:23 -0500 | [diff] [blame] | 65 | * Returns the orientation that will be used when locking the orientation from system UI |
| 66 | * with {@link #setRotationLock}. |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 67 | * |
John Spurlock | 8ab172e | 2013-12-19 16:39:23 -0500 | [diff] [blame] | 68 | * If the device only supports locking to its natural orientation, this will be either |
| 69 | * Configuration.ORIENTATION_PORTRAIT or Configuration.ORIENTATION_LANDSCAPE, |
| 70 | * otherwise Configuration.ORIENTATION_UNDEFINED if any orientation is lockable. |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 71 | */ |
John Spurlock | 8ab172e | 2013-12-19 16:39:23 -0500 | [diff] [blame] | 72 | public static int getRotationLockOrientation(Context context) { |
| 73 | if (!areAllRotationsAllowed(context)) { |
| 74 | final Point size = new Point(); |
| 75 | final IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); |
| 76 | try { |
| 77 | wm.getInitialDisplaySize(Display.DEFAULT_DISPLAY, size); |
| 78 | return size.x < size.y ? |
| 79 | Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE; |
| 80 | } catch (RemoteException e) { |
| 81 | Log.w(TAG, "Unable to get the display size"); |
| 82 | } |
| 83 | } |
| 84 | return Configuration.ORIENTATION_UNDEFINED; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
John Spurlock | 8ab172e | 2013-12-19 16:39:23 -0500 | [diff] [blame] | 88 | * Returns true if the rotation-lock toggle should be shown in system UI. |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 89 | */ |
| 90 | public static boolean isRotationLockToggleVisible(Context context) { |
John Spurlock | 8ab172e | 2013-12-19 16:39:23 -0500 | [diff] [blame] | 91 | return isRotationSupported(context) && |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 92 | Settings.System.getIntForUser(context.getContentResolver(), |
| 93 | Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0, |
| 94 | UserHandle.USER_CURRENT) == 0; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns true if rotation lock is enabled. |
| 99 | */ |
| 100 | public static boolean isRotationLocked(Context context) { |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 101 | return Settings.System.getIntForUser(context.getContentResolver(), |
| 102 | Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT) == 0; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
John Spurlock | 8ab172e | 2013-12-19 16:39:23 -0500 | [diff] [blame] | 106 | * Enables or disables rotation lock from the system UI toggle. |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 107 | */ |
| 108 | public static void setRotationLock(Context context, final boolean enabled) { |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 109 | Settings.System.putIntForUser(context.getContentResolver(), |
| 110 | Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0, |
| 111 | UserHandle.USER_CURRENT); |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 112 | |
John Spurlock | 8ab172e | 2013-12-19 16:39:23 -0500 | [diff] [blame] | 113 | final int rotation = areAllRotationsAllowed(context) ? CURRENT_ROTATION : NATURAL_ROTATION; |
| 114 | setRotationLock(enabled, rotation); |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
John Spurlock | 8ab172e | 2013-12-19 16:39:23 -0500 | [diff] [blame] | 118 | * Enables or disables natural rotation lock from Accessibility settings. |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 119 | * |
John Spurlock | 8ab172e | 2013-12-19 16:39:23 -0500 | [diff] [blame] | 120 | * If rotation is locked for accessibility, the system UI toggle is hidden to avoid confusion. |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 121 | */ |
| 122 | public static void setRotationLockForAccessibility(Context context, final boolean enabled) { |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 123 | Settings.System.putIntForUser(context.getContentResolver(), |
| 124 | Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, enabled ? 1 : 0, |
| 125 | UserHandle.USER_CURRENT); |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 126 | |
John Spurlock | 8ab172e | 2013-12-19 16:39:23 -0500 | [diff] [blame] | 127 | setRotationLock(enabled, NATURAL_ROTATION); |
| 128 | } |
| 129 | |
| 130 | private static boolean areAllRotationsAllowed(Context context) { |
| 131 | return context.getResources().getBoolean(R.bool.config_allowAllRotations); |
| 132 | } |
| 133 | |
| 134 | private static void setRotationLock(final boolean enabled, final int rotation) { |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 135 | AsyncTask.execute(new Runnable() { |
| 136 | @Override |
| 137 | public void run() { |
| 138 | try { |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 139 | IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 140 | if (enabled) { |
John Spurlock | 8ab172e | 2013-12-19 16:39:23 -0500 | [diff] [blame] | 141 | wm.freezeRotation(rotation); |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 142 | } else { |
| 143 | wm.thawRotation(); |
| 144 | } |
| 145 | } catch (RemoteException exc) { |
| 146 | Log.w(TAG, "Unable to save auto-rotate setting"); |
| 147 | } |
| 148 | } |
| 149 | }); |
| 150 | } |
| 151 | |
| 152 | /** |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 153 | * Registers a listener for rotation policy changes affecting the caller's user |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 154 | */ |
| 155 | public static void registerRotationPolicyListener(Context context, |
| 156 | RotationPolicyListener listener) { |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 157 | registerRotationPolicyListener(context, listener, UserHandle.getCallingUserId()); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Registers a listener for rotation policy changes affecting a specific user, |
| 162 | * or USER_ALL for all users. |
| 163 | */ |
| 164 | public static void registerRotationPolicyListener(Context context, |
| 165 | RotationPolicyListener listener, int userHandle) { |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 166 | context.getContentResolver().registerContentObserver(Settings.System.getUriFor( |
| 167 | Settings.System.ACCELEROMETER_ROTATION), |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 168 | false, listener.mObserver, userHandle); |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 169 | context.getContentResolver().registerContentObserver(Settings.System.getUriFor( |
| 170 | Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY), |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 171 | false, listener.mObserver, userHandle); |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Unregisters a listener for rotation policy changes. |
| 176 | */ |
| 177 | public static void unregisterRotationPolicyListener(Context context, |
| 178 | RotationPolicyListener listener) { |
| 179 | context.getContentResolver().unregisterContentObserver(listener.mObserver); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Listener that is invoked whenever a change occurs that might affect the rotation policy. |
| 184 | */ |
| 185 | public static abstract class RotationPolicyListener { |
| 186 | final ContentObserver mObserver = new ContentObserver(new Handler()) { |
| 187 | public void onChange(boolean selfChange, Uri uri) { |
| 188 | RotationPolicyListener.this.onChange(); |
| 189 | } |
| 190 | }; |
| 191 | |
| 192 | public abstract void onChange(); |
| 193 | } |
| 194 | } |