安卓强制横屏或竖屏实例

 

一.  OS:安卓5.1

修改文件:frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java 

在函数updateOrientationFromAppTokensLocked,

强制为竖屏添加:req = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT     (竖屏值为 1 )

                           req = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE  (横屏值为 0 )

                           req = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR   (忽略传感器 方向)

1、屏幕方向参数:

ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED,//未指定。为默认值。由Android系统自己选择合适的方向。
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,//横屏
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,//竖屏

ActivityInfo.SCREEN_ORIENTATION_USER,//用户当前的首选方向
ActivityInfo.SCREEN_ORIENTATION_BEHIND,//继承Activity堆栈中当前Activity下面的那个Activity的方向
ActivityInfo.SCREEN_ORIENTATION_SENSOR,//由物理感应器决定显示方向
ActivityInfo.SCREEN_ORIENTATION_NOSENSOR,//忽略物理感应器——即显示方向与物理感应器无关

device/qcom/msm8916/system.prop

persist.panel.orientation=90//系统默认旋转90度

boolean updateOrientationFromAppTokensLocked(boolean inTransaction) {
        long ident = Binder.clearCallingIdentity();
        try {
            int req = getOrientationFromWindowsLocked();
            if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
                req = getOrientationFromAppTokensLocked();
            }
            String screenMode = SystemProperties.get("android.screen.mode");
              if(screenMode.equals("portrait"))
                        req = 1;  
              else if(screenMode.equals("landscape"))
                        req = 0;
                 
             else if(screenMode.equals("nosensor"))
                     req = 5;
        System.out.println("---TTT---req = "+req+",screenMode =" + screenMode);
            if (req != mForcedAppOrientation) {
                mForcedAppOrientation = req;
                //send a message to Policy indicating orientation change to take
                //action like disabling/enabling sensors etc.,
                mPolicy.setCurrentOrientationLw(req);
                if (updateRotationUncheckedLocked(inTransaction)) {
                    // changed
                    return true;
                }
            }

            return false;
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
    }

上述属性android.screen.mode 可在自启动脚本或者system.prop文件添加。

 

二 . 安卓7.1

        

diff --git a/device/qcom/msm8953_64/system.prop b/device/qcom/msm8953_64/system.prop
index 02e390c..f56c52f 100755
--- a/device/qcom/msm8953_64/system.prop
+++ b/device/qcom/msm8953_64/system.prop
@@ -201,3 +201,4 @@ camera.display.lmax=1280x720
 ro.cutoff_voltage_mv=3300
 #add by cody for enable sim contacts
 persist.env.contacts.autosync =true
+persist.panel.orientation=90
diff --git a/frameworks/base/cmds/bootanimation/BootAnimation.cpp b/frameworks/base/cmds/bootanimation/BootAnimation.cpp
old mode 100644
new mode 100755
index 37656b9..f10b53b
--- a/frameworks/base/cmds/bootanimation/BootAnimation.cpp
+++ b/frameworks/base/cmds/bootanimation/BootAnimation.cpp
@@ -315,7 +315,8 @@ status_t BootAnimation::readyToRun() {
         return -1;
     // create the native surface
     sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
-            dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
+            dinfo.orientation == 1 ? dinfo.h : dinfo.w,
+            dinfo.orientation == 1 ? dinfo.w : dinfo.h, PIXEL_FORMAT_RGB_565);
 
     SurfaceComposerClient::openGlobalTransaction();
     control->setLayer(0x40000000);
diff --git a/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java b/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
old mode 100644
new mode 100755
index ca5c4dc..f942ee6
--- a/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -471,6 +471,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
     // Default display does not rotate, apps that require non-default orientation will have to
     // have the orientation emulated.
     private boolean mForceDefaultOrientation = false;
+    private int mDefaultOrientation = Surface.ROTATION_0;
 
     int mUserRotationMode = WindowManagerPolicy.USER_ROTATION_FREE;
     int mUserRotation = Surface.ROTATION_0;
@@ -2046,6 +2047,18 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                 // $ adb shell setprop config.override_forced_orient true
                 // $ adb shell wm size reset
                 !"true".equals(SystemProperties.get("config.override_forced_orient"));
+        String defaultOrientation = SystemProperties.get("persist.panel.orientation", "0");
+        if("0".equals(defaultOrientation)) {
+            mDefaultOrientation = Surface.ROTATION_0;
+        } else if("90".equals(defaultOrientation)) {
+            mDefaultOrientation = Surface.ROTATION_90;
+        } else if("180".equals(defaultOrientation)) {
+            mDefaultOrientation = Surface.ROTATION_180;
+        } else if("270".equals(defaultOrientation)) {
+            mDefaultOrientation = Surface.ROTATION_270;
+        } else {
+            mDefaultOrientation = Surface.ROTATION_0;
+        }
     }
 
     /**
@@ -6999,7 +7012,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                     if (preferredRotation >= 0) {
                         return preferredRotation;
                     }
-                    return Surface.ROTATION_0;
+                    return mDefaultOrientation;
             }
         }
     }
diff --git a/frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java b/frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java
old mode 100644
new mode 100755
index 0e47d3f..5180765
--- a/frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java
+++ b/frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java
@@ -1073,6 +1073,18 @@ public class WindowManagerService extends IWindowManager.Stub
         }
 
         showEmulatorDisplayOverlayIfNeeded();
+        String defaultOrientation = SystemProperties.get("persist.panel.orientation", "0");
+        if("0".equals(defaultOrientation)) {
+            mRotation = Surface.ROTATION_0;
+        } else if("90".equals(defaultOrientation)) {
+            mRotation = Surface.ROTATION_90;
+        } else if("180".equals(defaultOrientation)) {
+            mRotation = Surface.ROTATION_180;
+        } else if("270".equals(defaultOrientation)) {
+            mRotation = Surface.ROTATION_270;
+        } else {
+            mRotation = Surface.ROTATION_0;
+        }
     }
 
     public InputMonitor getInputMonitor() {
diff --git a/frameworks/native/services/surfaceflinger/DisplayDevice.cpp b/frameworks/native/services/surfaceflinger/DisplayDevice.cpp
old mode 100644
new mode 100755
index f1450cf..61e1c37
--- a/frameworks/native/services/surfaceflinger/DisplayDevice.cpp
+++ b/frameworks/native/services/surfaceflinger/DisplayDevice.cpp
@@ -106,6 +106,7 @@ DisplayDevice::DisplayDevice(
     Surface* surface;
     mNativeWindow = surface = new Surface(producer, false);
     ANativeWindow* const window = mNativeWindow.get();
+    int defaultOrientation = 0;
     char property[PROPERTY_VALUE_MAX];
 
     /*
@@ -168,8 +169,28 @@ DisplayDevice::DisplayDevice(
     property_get("ro.panel.mountflip", property, "0");
     mPanelMountFlip = atoi(property);
 
+    property_get("persist.panel.orientation", property, "0");
+    defaultOrientation = atoi(property);
+    switch(defaultOrientation) {
+        case 0:
+            defaultOrientation = DisplayState::eOrientationDefault;
+            break;
+        case 90:
+            defaultOrientation = DisplayState::eOrientation90;
+            break;
+        case 180:
+            defaultOrientation = DisplayState::eOrientation180;
+            break;
+        case 270:
+            defaultOrientation = DisplayState::eOrientation270;
+            break;
+        default:
+            defaultOrientation = DisplayState::eOrientationDefault;
+            break;
+    }
+
     // initialize the display orientation transform.
-    setProjection(DisplayState::eOrientationDefault, mViewport, mFrame);
+    setProjection(defaultOrientation, mViewport, mFrame);
 
 #ifdef NUM_FRAMEBUFFER_SURFACE_BUFFERS
     surface->allocateBuffers();
@@ -536,6 +557,7 @@ void DisplayDevice::setProjection(int orientation,
             // viewport is always specified in the logical orientation
             // of the display (ie: post-rotation).
             swap(viewport.right, viewport.bottom);
+            swap(frame.right, frame.bottom);
         }
     }
 

例如旋转90度

对照上面的修改

在device\qcom\msm8953_64\system.prop

persist.panel.orientation=90

 

  相关文章:https://blog.csdn.net/eliot_shao/article/details/70766283

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kevin@1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值