我们通常在手机上旋转屏幕可以应用主动旋转,也可以让应用随系统旋转。这里我们先分析应用随系统旋转的情况。
设置选项(旋转屏幕)
随系统选装的话,我们先要在手机设置中选择显示项,设备旋转时自动旋转。最终会调用RotationPolicy.setRotationLockForAccessibility函数,我们先来看下这个函数
public static void setRotationLockForAccessibility(Context context, final boolean enabled) {
Settings.System.putIntForUser(context.getContentResolver(),
Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, enabled ? 1 : 0,
UserHandle.USER_CURRENT);
setRotationLock(enabled, NATURAL_ROTATION);
}
setRotationLock函数会根据参数调用WMS的freezeRotation和thawRotation函数。
private static void setRotationLock(final boolean enabled, final int rotation) {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
try {
IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
if (enabled) {
wm.freezeRotation(rotation);
} else {
wm.thawRotation();
}
} catch (RemoteException exc) {
Log.w(TAG, "Unable to save auto-rotate setting");
}
}
});
}
写入设置数据库
我们先来看看WMS的thawRotation函数,先会调用PhoneWindowManager的setUserRotationMode函数,然后调用upateRotationUnchecked(这里处理设备旋转,我们下篇博客分析)。
public void thawRotation() {
if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
"thawRotation()")) {
throw new SecurityException("Requires SET_ORIENTATION permission");
}
if (DEBUG_ORIENTATION) Slog.v(TAG, "thawRotation: mRotation=" + mRotation);
long origId = Binder.clearCallingIdentity();
try {
mPolicy.setUserRotationMode(WindowManagerPolicy.USER_ROTATION_FREE,
777); // rot not used
} finally {
Binder.restoreCallingIdentity(origId);
}
updateRotationUnchecked(false, false);//后续分析
}
我们再来看看freezeRotation函数,只是调用PhoneWindowManager的setUserRotationMode的参数不一样,这里是Locked,而thawRotation传下去的参数是free
@Override
public void freezeRotation(int rotation)