Android 13 自动旋转屏幕 重启后关闭的问题

介绍

近期在Android 13 上发现当打开自动旋转屏幕后关机,重新启动后自动旋转屏幕关闭了

修改

路径:vendor/mediatek/proprietary/frameworks/base/settingsprovider/java/com/mediatek/provider/MtkSettingsExt.java

        public static final String ACCELEROMETER_ROTATION_RESTORE
            = "accelerometer_rotation_restore";

实际上这里我们排查时发现 settings list system 

accelerometer_rotation_restore      //这个值通过代码找到发现是在关机动画的patch中后加入的问题就出在这里。

accelerometer_rotation   //自动旋转开关的值

路径:vendor/mediatek/proprietary/frameworks/base/services/core/java/com/mediatek/server/MtkShutdownThread.java

    private boolean showShutdownAnimation(Context context) {
        beginAnimationTime = 0;
        if (isCustBootAnim() == ANIMATION_MODE) {
            configShutdownAnimation(context);
            // Show Shutdown Animation
            bootanimCust(context);
            return true;
        }
        return false;
    }


    private static void bootanimCust(Context context) {
        boolean isRotaionEnabled = false;
        // [MTK] fix shutdown animation timing issue
        SystemProperties.set("service.shutanim.running", "0");
        Log.i(TAG, "set service.shutanim.running to 0");
        try {
            isRotaionEnabled = Settings.System.getInt(context.getContentResolver(),
                    Settings.System.ACCELEROMETER_ROTATION, 1) != 0;
            //这里是在关机时如果自动旋转开启,则先关闭自动旋转在执行关机动画,其中还记录了一个ACCELEROMETER_ROTATION_RESTORE的值但是没有其他地方调用
            if (isRotaionEnabled) {
                final IWindowManager wm = IWindowManager.Stub.asInterface(
                        ServiceManager.getService(Context.WINDOW_SERVICE));
                if (wm != null) {
                    wm.freezeRotation(Surface.ROTATION_0);
                }
                Settings.System.putInt(context.getContentResolver(),
                        Settings.System.ACCELEROMETER_ROTATION, 0);
                Settings.System.putInt(context.getContentResolver(),
                        MtkSettingsExt.System.ACCELEROMETER_ROTATION_RESTORE, 1);
            //既然没有用到这个值,我们就用这个值来判断什么时候保存自动旋转的状态,当自动旋转关闭时我们put 0
            //*/soda water.20231228 Automatic rotation save
            }else{
                Settings.System.putInt(context.getContentResolver(),
                        MtkSettingsExt.System.ACCELEROMETER_ROTATION_RESTORE, 0);
            }
            //*/
        } catch (NullPointerException ex) {
            Log.e(TAG, "check Rotation: context object is null when get Rotation");
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        beginAnimationTime = SystemClock.elapsedRealtime() + MIN_SHUTDOWN_ANIMATION_PLAY_TIME;
        //Disable key dispatch
        try {
            final IWindowManager wm = IWindowManager.Stub.asInterface(
                    ServiceManager.getService(Context.WINDOW_SERVICE));
            if (wm != null) {
                wm.setEventDispatching(false);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        //Disable key dispatch
        startBootAnimation();
    }

上面我们已经用ACCELEROMETER_ROTATION_RESTORE 记录了自动旋转的值,这里为什么不直接删掉

                Settings.System.putInt(context.getContentResolver(),
                        Settings.System.ACCELEROMETER_ROTATION, 0);

,因为执行关机动画时不希望屏幕可以旋转。

接下来我们执行在开机时finishBooting的中读ACCELEROMETER_ROTATION_RESTORE的值当为1时来putACCELEROMETER_ROTATION的值为1即可。

路径:frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

//*/soda water.20231228 Automatic rotation save
import android.provider.Settings;
//*/

public class ActivityManagerService extends IActivityManager.Stub
        implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback, ActivityManagerGlobalLock {
    //.....略
    final void finishBooting() {
        //*/soda water.20231228 Automatic rotation save
        try {
        boolean rotation = Settings.System.getInt(mContext.getContentResolver(),
                    "accelerometer_rotation_restore", 0) == 1;
            if(rotation){
            Settings.System.putInt(mContext.getContentResolver(),Settings.System.ACCELEROMETER_ROTATION,1);
            }
        } catch (Exception ex) {
        }
        //*/
    }
}

你可能感兴趣的:(Android,13,android)