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; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 21 | import android.database.ContentObserver; |
| 22 | import android.net.Uri; |
| 23 | import android.os.AsyncTask; |
Svetoslav | 79578b2 | 2013-04-29 16:55:57 -0700 | [diff] [blame] | 24 | import android.os.Build; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 25 | import android.os.Handler; |
| 26 | import android.os.RemoteException; |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 27 | import android.os.UserHandle; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 28 | import android.provider.Settings; |
| 29 | import android.util.Log; |
| 30 | import android.view.IWindowManager; |
| 31 | import android.view.Surface; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 32 | import android.view.WindowManagerGlobal; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Provides helper functions for configuring the display rotation policy. |
| 36 | */ |
| 37 | public final class RotationPolicy { |
| 38 | private static final String TAG = "RotationPolicy"; |
| 39 | |
| 40 | private RotationPolicy() { |
| 41 | } |
| 42 | |
| 43 | /** |
Svetoslav | 79578b2 | 2013-04-29 16:55:57 -0700 | [diff] [blame] | 44 | * Gets whether the device supports rotation. In general such a |
| 45 | * device has an accelerometer and has the portrait and landscape |
| 46 | * features. |
| 47 | * |
| 48 | * @param context Context for accessing system resources. |
| 49 | * @return Whether the device supports rotation. |
| 50 | */ |
| 51 | public static boolean isRotationSupported(Context context) { |
| 52 | PackageManager pm = context.getPackageManager(); |
| 53 | return pm.hasSystemFeature(PackageManager.FEATURE_SENSOR_ACCELEROMETER) |
| 54 | && pm.hasSystemFeature(PackageManager.FEATURE_SCREEN_PORTRAIT) |
| 55 | && pm.hasSystemFeature(PackageManager.FEATURE_SCREEN_LANDSCAPE); |
| 56 | } |
| 57 | |
| 58 | /** |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 59 | * Returns true if the device supports the rotation-lock toggle feature |
| 60 | * in the system UI or system bar. |
| 61 | * |
| 62 | * When the rotation-lock toggle is supported, the "auto-rotate screen" option in |
| 63 | * Display settings should be hidden, but it should remain available in Accessibility |
| 64 | * settings. |
| 65 | */ |
| 66 | public static boolean isRotationLockToggleSupported(Context context) { |
Svetoslav | 79578b2 | 2013-04-29 16:55:57 -0700 | [diff] [blame] | 67 | return isRotationSupported(context) |
| 68 | && context.getResources().getConfiguration().smallestScreenWidthDp >= 600; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns true if the rotation-lock toggle should be shown in the UI. |
| 73 | */ |
| 74 | public static boolean isRotationLockToggleVisible(Context context) { |
| 75 | return isRotationLockToggleSupported(context) && |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 76 | Settings.System.getIntForUser(context.getContentResolver(), |
| 77 | Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0, |
| 78 | UserHandle.USER_CURRENT) == 0; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns true if rotation lock is enabled. |
| 83 | */ |
| 84 | public static boolean isRotationLocked(Context context) { |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 85 | return Settings.System.getIntForUser(context.getContentResolver(), |
| 86 | Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT) == 0; |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Enables or disables rotation lock. |
| 91 | * |
| 92 | * Should be used by the rotation lock toggle. |
| 93 | */ |
| 94 | public static void setRotationLock(Context context, final boolean enabled) { |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 95 | Settings.System.putIntForUser(context.getContentResolver(), |
| 96 | Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0, |
| 97 | UserHandle.USER_CURRENT); |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 98 | |
| 99 | AsyncTask.execute(new Runnable() { |
| 100 | @Override |
| 101 | public void run() { |
| 102 | try { |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 103 | IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 104 | if (enabled) { |
| 105 | wm.freezeRotation(-1); |
| 106 | } else { |
| 107 | wm.thawRotation(); |
| 108 | } |
| 109 | } catch (RemoteException exc) { |
| 110 | Log.w(TAG, "Unable to save auto-rotate setting"); |
| 111 | } |
| 112 | } |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Enables or disables rotation lock and adjusts whether the rotation lock toggle |
| 118 | * should be hidden for accessibility purposes. |
| 119 | * |
| 120 | * Should be used by Display settings and Accessibility settings. |
| 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 | |
| 127 | AsyncTask.execute(new Runnable() { |
| 128 | @Override |
| 129 | public void run() { |
| 130 | try { |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 131 | IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 132 | if (enabled) { |
| 133 | wm.freezeRotation(Surface.ROTATION_0); |
| 134 | } else { |
| 135 | wm.thawRotation(); |
| 136 | } |
| 137 | } catch (RemoteException exc) { |
| 138 | Log.w(TAG, "Unable to save auto-rotate setting"); |
| 139 | } |
| 140 | } |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | /** |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 145 | * Registers a listener for rotation policy changes affecting the caller's user |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 146 | */ |
| 147 | public static void registerRotationPolicyListener(Context context, |
| 148 | RotationPolicyListener listener) { |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 149 | registerRotationPolicyListener(context, listener, UserHandle.getCallingUserId()); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Registers a listener for rotation policy changes affecting a specific user, |
| 154 | * or USER_ALL for all users. |
| 155 | */ |
| 156 | public static void registerRotationPolicyListener(Context context, |
| 157 | RotationPolicyListener listener, int userHandle) { |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 158 | context.getContentResolver().registerContentObserver(Settings.System.getUriFor( |
| 159 | Settings.System.ACCELEROMETER_ROTATION), |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 160 | false, listener.mObserver, userHandle); |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 161 | context.getContentResolver().registerContentObserver(Settings.System.getUriFor( |
| 162 | Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY), |
Christopher Tate | 5e08af0 | 2012-09-21 17:17:22 -0700 | [diff] [blame] | 163 | false, listener.mObserver, userHandle); |
Jeff Brown | 207673cd | 2012-06-05 17:47:11 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Unregisters a listener for rotation policy changes. |
| 168 | */ |
| 169 | public static void unregisterRotationPolicyListener(Context context, |
| 170 | RotationPolicyListener listener) { |
| 171 | context.getContentResolver().unregisterContentObserver(listener.mObserver); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Listener that is invoked whenever a change occurs that might affect the rotation policy. |
| 176 | */ |
| 177 | public static abstract class RotationPolicyListener { |
| 178 | final ContentObserver mObserver = new ContentObserver(new Handler()) { |
| 179 | public void onChange(boolean selfChange, Uri uri) { |
| 180 | RotationPolicyListener.this.onChange(); |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | public abstract void onChange(); |
| 185 | } |
| 186 | } |