关于Android6.0开机默认横屏显示,网上有一些文章提到过,但是我仔细研究了下,要么不全,要么修改方式会引入新的问题。为了让开机强制横屏显示,并且又不影响sensor的屏幕自动旋转功能,我对旋转的修改方式做了很多尝试,最后终于找到了一种可行的方式。既保证开机启动的时候按照指定的方向显示logo和开机动画,又可以在打开自动旋转开关后,通过转动设备来自动旋转屏幕。具体做法如下:
开机默认横屏显示,需要修改几个部分,包括bootloader,开机动画,SurfaceFlinger以及进入桌面后的Launcher显示方向。
1. bootloader
不同的平台,bootloader的显示部分有差异,可以根据平台的代码特点去修改,比如调换x、y的数据。
2. 开机动画
开机动画主要修改BootAnimation.cpp -> readyToRun ()
if (dinfo.w < dinfo.h)
control = session()->createSurface(String8("BootAnimation"),
dinfo.h, dinfo.w, PIXEL_FORMAT_RGB_565);
else
control = session()->createSurface(String8("BootAnimation"),
dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
3. SurfaceFlinger
SurfaceFlinger部分主要修改DisplayDevice.cpp -> DisplayDevice() 最后一行:
if (!strcmp(property, "DT370CR"))
rotate_default = DisplayState::eOrientation270;
else if (!strcmp(property, "DT317CR"))
rotate_default = DisplayState::eOrientation90;
setProjection(rotate_default, mViewport, mFrame);
然后进入setProjection()里面进行如下修改:
if (!frame.isValid()) {
// the destination frame can be invalid if it has never been set,
// in that case we assume the whole display frame.
if (w < h)
frame = Rect(h, w); //此处调换x、y顺序,若不调换,开机动画前半段会有偏移
else
frame = Rect(w, h);
}
4. Framework
(1) WindowManagerService.java
修改四个变量的初始值:
int mRotation = ("DT370CR".equals(Build.MODEL))?3:(("DT317CR".equals(Build.MODEL))?1:0);
int mForcedAppOrientation = ("DT370CR".equals(Build.MODEL))?8:(("DT317CR".equals(Build.MODEL))?0:-1);
int mLastWindowForcedOrientation = ("DT370CR".equals(Build.MODEL))?8:(("DT317CR".equals(Build.MODEL))?0:-1);
int mLastKeyguardForcedOrientation = ("DT370CR".equals(Build.MODEL))?8:(("DT317CR".equals(Build.MODEL))?0:-1);
修改boolean updateOrientationFromAppTokensLocked(boolean inTransaction),增加下面一段:
if (req == 5)
req = ("DT370CR".equals(Build.MODEL))?8:(("DT317CR".equals(Build.MODEL))?0:5);
(2) PhoneWindowManager.java
修改一个变量的初始值:
int mUserRotation = (("DT370CR".equals(Build.MODEL))?3:(("DT317CR".equals(Build.MODEL))?1:0));
修改public void updateSettings(),给userRotation重新赋值,增加如下一段代码:
int userRotation = Settings.System.getIntForUser(resolver,
Settings.System.USER_ROTATION,
(("DT370CR".equals(Build.MODEL))?3:(("DT317CR".equals(Build.MODEL))?1:0)),
UserHandle.USER_CURRENT);
if (userRotation < 0) {
userRotation = (("DT370CR".equals(Build.MODEL))?3:(("DT317CR".equals(Build.MODEL))?1:0));
}
修改public int rotationForOrientationLw(int orientation, int lastRotation)如下:
if (mForceDefaultOrientation) {
if ("DT370CR".equals(Build.MODEL))
return Surface.ROTATION_270;
else if ("DT317CR".equals(Build.MODEL))
return Surface.ROTATION_90;
else
return Surface.ROTATION_0;
}
(3) ~/framework/base/packages/SettingsProvider/res/values/defaults.xml
将def_user_rotation的值修改为-1
至此,所有代码修改完毕,重新编译代码,烧录固件后会发现屏幕已经横过来了。