RotationPolicy.java revision 98365d7663cbd82979a5700faf0050220b01084d
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 17package com.android.internal.view; 18 19import android.content.Context; 20import android.database.ContentObserver; 21import android.net.Uri; 22import android.os.AsyncTask; 23import android.os.Handler; 24import android.os.RemoteException; 25import android.os.ServiceManager; 26import android.provider.Settings; 27import android.util.Log; 28import android.view.IWindowManager; 29import android.view.Surface; 30import android.view.WindowManagerGlobal; 31 32/** 33 * Provides helper functions for configuring the display rotation policy. 34 */ 35public final class RotationPolicy { 36 private static final String TAG = "RotationPolicy"; 37 38 private RotationPolicy() { 39 } 40 41 /** 42 * Returns true if the device supports the rotation-lock toggle feature 43 * in the system UI or system bar. 44 * 45 * When the rotation-lock toggle is supported, the "auto-rotate screen" option in 46 * Display settings should be hidden, but it should remain available in Accessibility 47 * settings. 48 */ 49 public static boolean isRotationLockToggleSupported(Context context) { 50 return context.getResources().getConfiguration().smallestScreenWidthDp >= 600; 51 } 52 53 /** 54 * Returns true if the rotation-lock toggle should be shown in the UI. 55 */ 56 public static boolean isRotationLockToggleVisible(Context context) { 57 return isRotationLockToggleSupported(context) && 58 Settings.System.getInt(context.getContentResolver(), 59 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0) == 0; 60 } 61 62 /** 63 * Returns true if rotation lock is enabled. 64 */ 65 public static boolean isRotationLocked(Context context) { 66 return Settings.System.getInt(context.getContentResolver(), 67 Settings.System.ACCELEROMETER_ROTATION, 0) == 0; 68 } 69 70 /** 71 * Enables or disables rotation lock. 72 * 73 * Should be used by the rotation lock toggle. 74 */ 75 public static void setRotationLock(Context context, final boolean enabled) { 76 Settings.System.putInt(context.getContentResolver(), 77 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0); 78 79 AsyncTask.execute(new Runnable() { 80 @Override 81 public void run() { 82 try { 83 IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); 84 if (enabled) { 85 wm.freezeRotation(-1); 86 } else { 87 wm.thawRotation(); 88 } 89 } catch (RemoteException exc) { 90 Log.w(TAG, "Unable to save auto-rotate setting"); 91 } 92 } 93 }); 94 } 95 96 /** 97 * Enables or disables rotation lock and adjusts whether the rotation lock toggle 98 * should be hidden for accessibility purposes. 99 * 100 * Should be used by Display settings and Accessibility settings. 101 */ 102 public static void setRotationLockForAccessibility(Context context, final boolean enabled) { 103 Settings.System.putInt(context.getContentResolver(), 104 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, enabled ? 1 : 0); 105 106 AsyncTask.execute(new Runnable() { 107 @Override 108 public void run() { 109 try { 110 IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); 111 if (enabled) { 112 wm.freezeRotation(Surface.ROTATION_0); 113 } else { 114 wm.thawRotation(); 115 } 116 } catch (RemoteException exc) { 117 Log.w(TAG, "Unable to save auto-rotate setting"); 118 } 119 } 120 }); 121 } 122 123 /** 124 * Registers a listener for rotation policy changes. 125 */ 126 public static void registerRotationPolicyListener(Context context, 127 RotationPolicyListener listener) { 128 context.getContentResolver().registerContentObserver(Settings.System.getUriFor( 129 Settings.System.ACCELEROMETER_ROTATION), 130 false, listener.mObserver); 131 context.getContentResolver().registerContentObserver(Settings.System.getUriFor( 132 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY), 133 false, listener.mObserver); 134 } 135 136 /** 137 * Unregisters a listener for rotation policy changes. 138 */ 139 public static void unregisterRotationPolicyListener(Context context, 140 RotationPolicyListener listener) { 141 context.getContentResolver().unregisterContentObserver(listener.mObserver); 142 } 143 144 /** 145 * Listener that is invoked whenever a change occurs that might affect the rotation policy. 146 */ 147 public static abstract class RotationPolicyListener { 148 final ContentObserver mObserver = new ContentObserver(new Handler()) { 149 public void onChange(boolean selfChange, Uri uri) { 150 RotationPolicyListener.this.onChange(); 151 } 152 }; 153 154 public abstract void onChange(); 155 } 156}