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也不同。因为代码开头就有
[java] view plaincopy
- 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;
}
更多相关文章
- Android应用程序安装过程源代码分析(4)
- Android平台mass storage相关代码
- 使用代码为textview设置drawableLeft
- [置顶] Android 2.3.5源代码 更新至android 6.0,可以下载,度娘网盘
- cocos2dx 调用java层代码
- 安卓学习(初)第三章(3)(《第一行代码》)
- 性能优化之Java(Android)代码优化
- 通过eclipse查看Android源代码(Java)
- 【Android】注解框架(四)-- 一行代码注入微信支付