Android11 强制所有APP 横屏显示

464 阅读1分钟

前言

由于平板项目没有 Gsensor,需要将所有第三方APP强制横屏, 无视 android:screenOrientation="portrait" 属性

效果图

修改前

mno2z8.png

修改后

mnoV5q.png

修改源码

alps\frameworks\base\services\core\java\com\android\server\wm\DisplayRotation.java

    @Surface.Rotation
-    private int mRotation;
+    private int mRotation = 3;


 @Override
       boolean updateRotationUnchecked(boolean forceUpdate) {
        //add
+        if (true) {
+            return true;
+        }//end
        final int displayId = mDisplayContent.getDisplayId();
        if (!forceUpdate) {
            if (mDeferredRotationPauseCount > 0) {
                // Rotation updates have been paused temporarily. Defer the update until updates
                // have been resumed.
                ProtoLog.v(WM_DEBUG_ORIENTATION, "Deferring rotation, rotation is paused.");
                return false;
            }

alps\frameworks\base\services\core\java\com\android\server\wm\DisplayContent.java

    @ScreenOrientation
    @Override
    int getOrientation() {
		 //add
+        if (true) {
+            return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
+        }//end
        mLastOrientationSource = null;

        if (mIgnoreRotationForApps) {
            return SCREEN_ORIENTATION_USER;
        }

        if (mWmService.mDisplayFrozen) {
            if (mWmService.mPolicy.isKeyguardLocked()) {
                // Use the last orientation the while the display is frozen with the keyguard
                // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
                // window. We don't want to check the show when locked window directly though as
                // things aren't stable while the display is frozen, for example the window could be
                // momentarily unavailable due to activity relaunch.
                ProtoLog.v(WM_DEBUG_ORIENTATION,
                        "Display id=%d is frozen while keyguard locked, return %d",
                        mDisplayId, getLastOrientation());
                return getLastOrientation();
            }
        }
        final int rootOrientation = mRootDisplayArea.getOrientation();
        mLastOrientationSource = mRootDisplayArea.getLastOrientationSource();
        return rootOrientation;
    }