Android 源码 修改系统默认横屏

论坛 期权论坛 脚本     
匿名网站用户   2020-12-19 19:09   11   0

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. publicintrotationForOrientationLw(intorientation,intlastRotation,
  2. booleandisplayEnabled){
  3. if(mPortraitRotation<0){
  4. //Initializetherotationanglesforeachorientationonce.
  5. Displayd=((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
  6. .getDefaultDisplay();
  7. if(d.getWidth()>d.getHeight()){
  8. mPortraitRotation=Surface.ROTATION_90;
  9. mLandscapeRotation=Surface.ROTATION_0;
  10. mUpsideDownRotation=Surface.ROTATION_270;
  11. mSeascapeRotation=Surface.ROTATION_180;
  12. }else{
  13. mPortraitRotation=Surface.ROTATION_0;
  14. mLandscapeRotation=Surface.ROTATION_90;
  15. mUpsideDownRotation=Surface.ROTATION_180;
  16. mSeascapeRotation=Surface.ROTATION_270;
  17. }
  18. }
  19. {
  20. Log.i(TAG,"MediaPlayer.isnotPlayingVideo");
  21. synchronized(mLock){
  22. switch(orientation){
  23. caseActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
  24. //alwaysreturnportraitiforientationsettoportrait
  25. //returnmPortraitRotation;
  26. returnmUpsideDownRotation;
  27. caseActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
  28. //alwaysreturnlandscapeiforientationsettolandscape
  29. returnmLandscapeRotation;
  30. caseActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
  31. //alwaysreturnportraitiforientationsettoportrait
  32. //returnmUpsideDownRotation;
  33. returnmPortraitRotation;
  34. caseActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
  35. //alwaysreturnseascapeiforientationsettoreverselandscape
  36. returnmSeascapeRotation;
  37. caseActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:
  38. //returneitherlandscaperotationbasedonthesensor
  39. mOrientationListener.setAllow180Rotation(
  40. isLandscapeOrSeascape(Surface.ROTATION_180));
  41. returngetCurrentLandscapeRotation(lastRotation);
  42. caseActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:
  43. mOrientationListener.setAllow180Rotation(
  44. !isLandscapeOrSeascape(Surface.ROTATION_180));
  45. returngetCurrentPortraitRotation(lastRotation);
  46. }
  47. mOrientationListener.setAllow180Rotation(
  48. orientation==ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
  49. ||orientation==ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
  50. //casefornosensormeaningignoresensorandconsideronlylid
  51. //ororientationsensordisabled
  52. //orcase.unspecified
  53. if(mLidOpen){
  54. returnmLidOpenRotation;
  55. }elseif(mDockMode==Intent.EXTRA_DOCK_STATE_CAR&&mCarDockRotation>=0){
  56. returnmCarDockRotation;
  57. }elseif(mDockMode==Intent.EXTRA_DOCK_STATE_DESK&&mDeskDockRotation>=0){
  58. returnmDeskDockRotation;
  59. }else{
  60. if(useSensorForOrientationLp(orientation)){
  61. returnmOrientationListener.getCurrentRotation(lastRotation);
  62. }
  63. returnSurface.ROTATION_0;
  64. }
  65. }
  66. }
  67. }


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

[java]view plaincopy

  1. if(mPortraitRotation<0){
  2. //Initializetherotationanglesforeachorientationonce.
  3. Displayd=((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;

}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP