Android 源码 修改系统默认横屏

 

1:WindowManagerService:

platform\frameworks\base\services\core\java\com\android\server\wm\WindowManagerService.class 

具体改动的地方看代码吧,

   public int getOrientationFromWindowsLocked() {
        ....
        -- return (mLastWindowForcedOrientation=ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        ++ return (mLastWindowForcedOrientation=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
  /*
     * Determine the new desired orientation of the display, returning
     * a non-null new Configuration if it has changed from the current
     * orientation.  IF TRUE IS RETURNED SOMEONE MUST CALL
     * setNewConfiguration() TO TELL THE WINDOW MANAGER IT CAN UNFREEZE THE
     * SCREEN.  This will typically be done for you if you call
     * sendNewConfiguration().
     *
     * The orientation is computed from non-application windows first. If none of
     * the non-application windows specify orientation, the orientation is computed from
     * application tokens.
     * @see android.view.IWindowManager#updateOrientationFromAppTokens(
     * android.os.IBinder)
     */
    boolean updateOrientationFromAppTokensLocked(boolean inTransaction) {
        long ident = Binder.clearCallingIdentity();
        try {
            int req = getOrientationFromWindowsLocked();
            if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
-- mForcedAppOrientation = req;
-- req = getOrientationFromAppTokensLocked();
         ++req=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            }

            if (req != mForcedAppOrientation) {
                -- mForcedAppOrientation = req;
                ++ mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
....
    }
    boolean computeScreenConfigurationLocked(Configuration config) {

....
        if (config != null) {
            -- config.orientation = (dw <= dh) ? Configuration.ORIENTATION_PORTRAIT:Configuration.ORIENTATION_LANDSCAPE;
            ++ config.orientation = Configuration.ORIENTATION_LANDSCAPE;
        }

....
        return true;
    }

修改这几处代码便能把系统默认的竖屏变成横屏,但是会有其他的应用出现问题,比如拨号盘原本竖屏显示的,但是在这种横屏模式下打开拨号盘会报空指针错误,进源码一看原来拨号盘也准备了两套的布局,一套横屏一套竖屏的,单独修改windowmanagerservice后不知道为什么这些系统应用没有变成竖屏模式,或许是时间仓促,并没有对windowmanagerservice的流程作很详细的分析,以后有时间再找机会研究下这个流程吧。

2:PhoneWindowManager

 

  1. public int rotationForOrientationLw(int orientation, int lastRotation,  
  2.            boolean displayEnabled) {  
  3.   
  4.        if (mPortraitRotation < 0) {  
  5.            // Initialize the rotation angles for each orientation once.  
  6.            Display d = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))  
  7.                    .getDefaultDisplay();  
  8.            if (d.getWidth() > d.getHeight()) {  
  9.                mPortraitRotation = Surface.ROTATION_90;  
  10.                mLandscapeRotation = Surface.ROTATION_0;  
  11.                mUpsideDownRotation = Surface.ROTATION_270;  
  12.                mSeascapeRotation = Surface.ROTATION_180;  
  13.            } else {  
  14.                mPortraitRotation = Surface.ROTATION_0;  
  15.                mLandscapeRotation = Surface.ROTATION_90;  
  16.                mUpsideDownRotation = Surface.ROTATION_180;  
  17.                mSeascapeRotation = Surface.ROTATION_270;  
  18.            }  
  19.        }  
  20.   
  21.     {  
  22.         Log.i(TAG, “MediaPlayer.is not PlayingVideo”);  
  23.         synchronized (mLock) {  
  24.             switch (orientation) {  
  25.                 case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:  
  26.                     //always return portrait if orientation set to portrait  
  27.                     //return mPortraitRotation;  
  28.                     return mUpsideDownRotation;  
  29.                 case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:  
  30.                     //always return landscape if orientation set to landscape  
  31.                     return mLandscapeRotation;  
  32.                 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:  
  33.                     //always return portrait if orientation set to portrait  
  34.                     //return mUpsideDownRotation;  
  35.                     return mPortraitRotation;  
  36.                 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:  
  37.                     //always return seascape if orientation set to reverse landscape  
  38.                     return mSeascapeRotation;  
  39.                 case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:  
  40.                     //return either landscape rotation based on the sensor  
  41.                     mOrientationListener.setAllow180Rotation(  
  42.                             isLandscapeOrSeascape(Surface.ROTATION_180));  
  43.                     return getCurrentLandscapeRotation(lastRotation);  
  44.                 case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:  
  45.                     mOrientationListener.setAllow180Rotation(  
  46.                             !isLandscapeOrSeascape(Surface.ROTATION_180));  
  47.                     return getCurrentPortraitRotation(lastRotation);  
  48.             }  
  49.   
  50.             mOrientationListener.setAllow180Rotation(  
  51.                    orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR  
  52.                    || orientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);  
  53.   
  54.             // case for nosensor meaning ignore sensor and consider only lid  
  55.             // or orientation sensor disabled  
  56.             //or case.unspecified  
  57.             if (mLidOpen) {  
  58.                 return mLidOpenRotation;  
  59.             } else if (mDockMode == Intent.EXTRA_DOCK_STATE_CAR && mCarDockRotation >= 0) {  
  60.                 return mCarDockRotation;  
  61.             } else if (mDockMode == Intent.EXTRA_DOCK_STATE_DESK && mDeskDockRotation >= 0) {  
  62.                 return mDeskDockRotation;  
  63.             } else {  
  64.                 if (useSensorForOrientationLp(orientation)) {  
  65.                     return mOrientationListener.getCurrentRotation(lastRotation);  
  66.                 }  
  67.                 return Surface.ROTATION_0;  
  68.             }  
  69.         }  
  70. }  
  71.    }  

修改上面倒数一行代码把return Surface.ROTATION_0改为你要的方向,记得这个要和上面的匹配,宽高不同,Surface.ROTATION_0也不同。因为代码开头就有

 

 

[java] view plaincopy

  1. if (mPortraitRotation < 0) {  
  2.     // Initialize the rotation angles for each orientation once.  
  3.     Display d = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))  
  4.             .getDefaultDisplay();  
  5.     if (d.getWidth() > d.getHeight()) {  
  6.         mPortraitRotation = Surface.ROTATION_90;  
  7.         mLandscapeRotation = Surface.ROTATION_0;  
  8.         mUpsideDownRotation = Surface.ROTATION_270;  
  9.         mSeascapeRotation = Surface.ROTATION_180;  
  10.     } else {  
  11.         mPortraitRotation = Surface.ROTATION_0;  
  12.         mLandscapeRotation = Surface.ROTATION_90;  
  13.         mUpsideDownRotation = Surface.ROTATION_180;  
  14.         mSeascapeRotation = Surface.ROTATION_270;  
  15.     }  
  16. }  

 

让所有应用都横屏显示:

frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java

 

    public int rotationForOrientationLw(int orientation, int lastRotation,

            boolean displayEnabled) {

            // Initialize the rotation angles for each orientation once.

            Display d = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))

                    .getDefaultDisplay();

            if (d.getWidth() > d.getHeight()) {

                mPortraitRotation = Surface.ROTATION_0;  //jeff. ROTATION_90;

                mLandscapeRotation = Surface.ROTATION_0;

                mUpsideDownRotation = Surface.ROTATION_90;  //jeff. 270;

                mSeascapeRotation = Surface.ROTATION_180;

            }

    原文作者:gjy_it
    原文地址: https://blog.csdn.net/gjy_it/article/details/80743448
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞