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
- public int rotationForOrientationLw(int orientation, int lastRotation,
- boolean displayEnabled) {
- if (mPortraitRotation < 0) {
- // 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_90;
- mLandscapeRotation = Surface.ROTATION_0;
- mUpsideDownRotation = Surface.ROTATION_270;
- mSeascapeRotation = Surface.ROTATION_180;
- } else {
- mPortraitRotation = Surface.ROTATION_0;
- mLandscapeRotation = Surface.ROTATION_90;
- mUpsideDownRotation = Surface.ROTATION_180;
- mSeascapeRotation = Surface.ROTATION_270;
- }
- }
- {
- Log.i(TAG, "MediaPlayer.is not PlayingVideo");
- synchronized (mLock) {
- switch (orientation) {
- case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
- //always return portrait if orientation set to portrait
- //return mPortraitRotation;
- return mUpsideDownRotation;
- case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
- //always return landscape if orientation set to landscape
- return mLandscapeRotation;
- case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
- //always return portrait if orientation set to portrait
- //return mUpsideDownRotation;
- return mPortraitRotation;
- case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
- //always return seascape if orientation set to reverse landscape
- return mSeascapeRotation;
- case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:
- //return either landscape rotation based on the sensor
- mOrientationListener.setAllow180Rotation(
- isLandscapeOrSeascape(Surface.ROTATION_180));
- return getCurrentLandscapeRotation(lastRotation);
- case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:
- mOrientationListener.setAllow180Rotation(
- !isLandscapeOrSeascape(Surface.ROTATION_180));
- return getCurrentPortraitRotation(lastRotation);
- }
- mOrientationListener.setAllow180Rotation(
- orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
- || orientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
- // case for nosensor meaning ignore sensor and consider only lid
- // or orientation sensor disabled
- //or case.unspecified
- if (mLidOpen) {
- return mLidOpenRotation;
- } else if (mDockMode == Intent.EXTRA_DOCK_STATE_CAR && mCarDockRotation >= 0) {
- return mCarDockRotation;
- } else if (mDockMode == Intent.EXTRA_DOCK_STATE_DESK && mDeskDockRotation >= 0) {
- return mDeskDockRotation;
- } else {
- if (useSensorForOrientationLp(orientation)) {
- return mOrientationListener.getCurrentRotation(lastRotation);
- }
- return Surface.ROTATION_0;
- }
- }
- }
- }
修改上面倒數一行程式碼把return Surface.ROTATION_0改為你要的方向,記得這個要和上面的匹配,寬高不同,Surface.ROTATION_0也不同。因為程式碼開頭就有
- if (mPortraitRotation < 0) {
- // 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_90;
- mLandscapeRotation = Surface.ROTATION_0;
- mUpsideDownRotation = Surface.ROTATION_270;
- mSeascapeRotation = Surface.ROTATION_180;
- } else {
- mPortraitRotation = Surface.ROTATION_0;
- mLandscapeRotation = Surface.ROTATION_90;
- mUpsideDownRotation = Surface.ROTATION_180;
- mSeascapeRotation = Surface.ROTATION_270;
- }
- }
讓所有應用都橫屏顯示:
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;
}