| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.view; |
| 18 |
|
| 19 |
import android.content.Context; |
| 20 |
import android.content.pm.PackageManager; |
| 21 |
import android.content.res.Configuration; |
| 22 |
import android.database.ContentObserver; |
| 23 |
import android.net.Uri; |
| 24 |
import android.os.AsyncTask; |
| 25 |
import android.os.Handler; |
| 26 |
import android.os.RemoteException; |
| 27 |
import android.os.UserHandle; |
| 28 |
import android.provider.Settings; |
| 29 |
import android.util.DisplayMetrics; |
| 30 |
import android.util.Log; |
| 31 |
import android.view.IWindowManager; |
| 32 |
import android.view.Surface; |
| 33 |
import android.view.WindowManagerGlobal; |
| 34 |
|
| 35 |
import com.android.internal.R; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public final class RotationPolicy { |
| 41 |
private static final String TAG = "RotationPolicy"; |
| 42 |
private static final int CURRENT_ROTATION = -1; |
| 43 |
|
| 44 |
public static final int NATURAL_ROTATION = Surface.ROTATION_0; |
| 45 |
|
| 46 |
private RotationPolicy() { |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 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 |
&& context.getResources().getBoolean( |
| 63 |
com.android.internal.R.bool.config_supportAutoRotation); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public static int getRotationLockOrientation(Context context) { |
| 75 |
if (areAllRotationsAllowed(context)) { |
| 76 |
return Configuration.ORIENTATION_UNDEFINED; |
| 77 |
} |
| 78 |
final DisplayMetrics metrics = context.getResources().getDisplayMetrics(); |
| 79 |
final int rotation = |
| 80 |
context.getResources().getConfiguration().windowConfiguration.getRotation(); |
| 81 |
final boolean rotated = rotation % 2 != 0; |
| 82 |
final int w = rotated ? metrics.heightPixels : metrics.widthPixels; |
| 83 |
final int h = rotated ? metrics.widthPixels : metrics.heightPixels; |
| 84 |
return w < h ? Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public static boolean isRotationLockToggleVisible(Context context) { |
| 91 |
return isRotationSupported(context) && |
| 92 |
Settings.System.getIntForUser(context.getContentResolver(), |
| 93 |
Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0, |
| 94 |
UserHandle.USER_CURRENT) == 0; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public static boolean isRotationLocked(Context context) { |
| 101 |
return Settings.System.getIntForUser(context.getContentResolver(), |
| 102 |
Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT) == 0; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public static void setRotationLock(Context context, final boolean enabled) { |
| 109 |
final int rotation = areAllRotationsAllowed(context) ? CURRENT_ROTATION : NATURAL_ROTATION; |
| 110 |
setRotationLockAtAngle(context, enabled, rotation); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public static void setRotationLockAtAngle(Context context, final boolean enabled, |
| 117 |
final int rotation) { |
| 118 |
Settings.System.putIntForUser(context.getContentResolver(), |
| 119 |
Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0, |
| 120 |
UserHandle.USER_CURRENT); |
| 121 |
|
| 122 |
setRotationLock(enabled, rotation); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public static void setRotationLockForAccessibility(Context context, final boolean enabled) { |
| 131 |
Settings.System.putIntForUser(context.getContentResolver(), |
| 132 |
Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, enabled ? 1 : 0, |
| 133 |
UserHandle.USER_CURRENT); |
| 134 |
|
| 135 |
setRotationLock(enabled, NATURAL_ROTATION); |
| 136 |
} |
| 137 |
|
| 138 |
private static boolean areAllRotationsAllowed(Context context) { |
| 139 |
return context.getResources().getBoolean(R.bool.config_allowAllRotations); |
| 140 |
} |
| 141 |
|
| 142 |
private static void setRotationLock(final boolean enabled, final int rotation) { |
| 143 |
AsyncTask.execute(new Runnable() { |
| 144 |
@Override |
| 145 |
public void run() { |
| 146 |
try { |
| 147 |
IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); |
| 148 |
if (enabled) { |
| 149 |
wm.freezeRotation(rotation); |
| 150 |
} else { |
| 151 |
wm.thawRotation(); |
| 152 |
} |
| 153 |
} catch (RemoteException exc) { |
| 154 |
Log.w(TAG, "Unable to save auto-rotate setting"); |
| 155 |
} |
| 156 |
} |
| 157 |
}); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public static void registerRotationPolicyListener(Context context, |
| 164 |
RotationPolicyListener listener) { |
| 165 |
registerRotationPolicyListener(context, listener, UserHandle.getCallingUserId()); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public static void registerRotationPolicyListener(Context context, |
| 173 |
RotationPolicyListener listener, int userHandle) { |
| 174 |
context.getContentResolver().registerContentObserver(Settings.System.getUriFor( |
| 175 |
Settings.System.ACCELEROMETER_ROTATION), |
| 176 |
false, listener.mObserver, userHandle); |
| 177 |
context.getContentResolver().registerContentObserver(Settings.System.getUriFor( |
| 178 |
Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY), |
| 179 |
false, listener.mObserver, userHandle); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public static void unregisterRotationPolicyListener(Context context, |
| 186 |
RotationPolicyListener listener) { |
| 187 |
context.getContentResolver().unregisterContentObserver(listener.mObserver); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public static abstract class RotationPolicyListener { |
| 194 |
final ContentObserver mObserver = new ContentObserver(new Handler()) { |
| 195 |
@Override |
| 196 |
public void onChange(boolean selfChange, Uri uri) { |
| 197 |
RotationPolicyListener.this.onChange(); |
| 198 |
} |
| 199 |
}; |
| 200 |
|
| 201 |
public abstract void onChange(); |
| 202 |
} |
| 203 |
} |