blob: 70e2bfc6fd1185047aba3cddf553047c4deb6129 [file] [log] [blame]
Jeff Brown207673cd2012-06-05 17:47:11 -07001/*
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
17package com.android.internal.view;
18
19import android.content.Context;
Svetoslav79578b22013-04-29 16:55:57 -070020import android.content.pm.PackageManager;
Jeff Brown207673cd2012-06-05 17:47:11 -070021import android.database.ContentObserver;
22import android.net.Uri;
23import android.os.AsyncTask;
Svetoslav79578b22013-04-29 16:55:57 -070024import android.os.Build;
Jeff Brown207673cd2012-06-05 17:47:11 -070025import android.os.Handler;
26import android.os.RemoteException;
Christopher Tate5e08af02012-09-21 17:17:22 -070027import android.os.UserHandle;
Jeff Brown207673cd2012-06-05 17:47:11 -070028import android.provider.Settings;
29import android.util.Log;
30import android.view.IWindowManager;
31import android.view.Surface;
Jeff Brown98365d72012-08-19 20:30:52 -070032import android.view.WindowManagerGlobal;
Jeff Brown207673cd2012-06-05 17:47:11 -070033
34/**
35 * Provides helper functions for configuring the display rotation policy.
36 */
37public final class RotationPolicy {
38 private static final String TAG = "RotationPolicy";
39
40 private RotationPolicy() {
41 }
42
43 /**
Svetoslav79578b22013-04-29 16:55:57 -070044 * 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 Brown207673cd2012-06-05 17:47:11 -070059 * 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) {
Svetoslav79578b22013-04-29 16:55:57 -070067 return isRotationSupported(context)
68 && context.getResources().getConfiguration().smallestScreenWidthDp >= 600;
Jeff Brown207673cd2012-06-05 17:47:11 -070069 }
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 Tate5e08af02012-09-21 17:17:22 -070076 Settings.System.getIntForUser(context.getContentResolver(),
77 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0,
78 UserHandle.USER_CURRENT) == 0;
Jeff Brown207673cd2012-06-05 17:47:11 -070079 }
80
81 /**
82 * Returns true if rotation lock is enabled.
83 */
84 public static boolean isRotationLocked(Context context) {
Christopher Tate5e08af02012-09-21 17:17:22 -070085 return Settings.System.getIntForUser(context.getContentResolver(),
86 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT) == 0;
Jeff Brown207673cd2012-06-05 17:47:11 -070087 }
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 Tate5e08af02012-09-21 17:17:22 -070095 Settings.System.putIntForUser(context.getContentResolver(),
96 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0,
97 UserHandle.USER_CURRENT);
Jeff Brown207673cd2012-06-05 17:47:11 -070098
99 AsyncTask.execute(new Runnable() {
100 @Override
101 public void run() {
102 try {
Jeff Brown98365d72012-08-19 20:30:52 -0700103 IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
Jeff Brown207673cd2012-06-05 17:47:11 -0700104 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 Tate5e08af02012-09-21 17:17:22 -0700123 Settings.System.putIntForUser(context.getContentResolver(),
124 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, enabled ? 1 : 0,
125 UserHandle.USER_CURRENT);
Jeff Brown207673cd2012-06-05 17:47:11 -0700126
127 AsyncTask.execute(new Runnable() {
128 @Override
129 public void run() {
130 try {
Jeff Brown98365d72012-08-19 20:30:52 -0700131 IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
Jeff Brown207673cd2012-06-05 17:47:11 -0700132 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 Tate5e08af02012-09-21 17:17:22 -0700145 * Registers a listener for rotation policy changes affecting the caller's user
Jeff Brown207673cd2012-06-05 17:47:11 -0700146 */
147 public static void registerRotationPolicyListener(Context context,
148 RotationPolicyListener listener) {
Christopher Tate5e08af02012-09-21 17:17:22 -0700149 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 Brown207673cd2012-06-05 17:47:11 -0700158 context.getContentResolver().registerContentObserver(Settings.System.getUriFor(
159 Settings.System.ACCELEROMETER_ROTATION),
Christopher Tate5e08af02012-09-21 17:17:22 -0700160 false, listener.mObserver, userHandle);
Jeff Brown207673cd2012-06-05 17:47:11 -0700161 context.getContentResolver().registerContentObserver(Settings.System.getUriFor(
162 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY),
Christopher Tate5e08af02012-09-21 17:17:22 -0700163 false, listener.mObserver, userHandle);
Jeff Brown207673cd2012-06-05 17:47:11 -0700164 }
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}