Android12 添加屏幕方向旋转方案
添加屏幕方向属性值
- device/qcom/qssi/system.prop
persist.panel.orientation=0
修改开机动画方向
- frameworks/base/cmds/bootanimation/BootAnimation.cpp
status_t BootAnimation::readyToRun() {mAssets.addDefaultAssets();mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();if (mDisplayToken == nullptr)return NAME_NOT_FOUND;DisplayMode displayMode;const status_t error =SurfaceComposerClient::getActiveDisplayMode(mDisplayToken, &displayMode);if (error != NO_ERROR)return error;mMaxWidth = android::base::GetIntProperty("ro.surface_flinger.max_graphics_width", 0);mMaxHeight = android::base::GetIntProperty("ro.surface_flinger.max_graphics_height", 0);ui::Size resolution = displayMode.resolution;resolution = limitSurfaceSize(resolution.width, resolution.height);// add startchar rAngleValue[PROPERTY_VALUE_MAX];property_get("persist.panel.orientation", rAngleValue, "0");int rAngle = atoi(rAngleValue);SurfaceComposerClient::Transaction t;int d_width = resolution.getWidth(),d_height = resolution.getHeight();if (rAngle == 90) {d_width = resolution.getHeight();d_height = resolution.getWidth();Rect destRect(d_width, d_height);t.setDisplayProjection(mDisplayToken, ui::ROTATION_90, destRect, destRect);ALOGD("BootAnimation default set rotation to be 90...");} else if (rAngle == 180) {Rect destRect(d_width, d_height);t.setDisplayProjection(mDisplayToken, ui::ROTATION_180, destRect, destRect);ALOGD("BootAnimation default set rotation to be 180...");} else if (rAngle == 270) {d_width = resolution.getHeight();d_height = resolution.getWidth();Rect destRect(d_width, d_height);t.setDisplayProjection(mDisplayToken, ui::ROTATION_270, destRect, destRect);ALOGD("BootAnimation default set rotation to be 270...");}// create the native surfacesp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),d_width/*resolution.getWidth()*/, d_height/*resolution.getHeight()*/, PIXEL_FORMAT_RGB_565);//SurfaceComposerClient::Transaction t;// add end
修改导航栏位置
- frameworks/base/services/core/java/com/android/server/wm/DisplayPolicy.java
@NavigationBarPosition
int navigationBarPosition(int displayWidth, int displayHeight, int displayRotation) {// add start/*if (navigationBarCanMove() && displayWidth > displayHeight) {if (displayRotation == Surface.ROTATION_270) {return NAV_BAR_LEFT;} else if (displayRotation == Surface.ROTATION_90) {return NAV_BAR_RIGHT;}}*/// add endreturn NAV_BAR_BOTTOM;
}
应用界面横竖屏修改
- frameworks/base/services/core/java/com/android/server/wm/DisplayRotation.java
private int mDefaultOrientation = Surface.ROTATION_0;
DisplayRotation(WindowManagerService service, DisplayContent displayContent,DisplayPolicy displayPolicy, DisplayWindowSettings displayWindowSettings,Context context, Object lock) {...if (isDefaultDisplay) {...t.start();// add startString orientation = SystemProperties.get("persist.panel.orientation");if (orientation.startsWith("0")){mDefaultOrientation = Surface.ROTATION_0;} else if(orientation.startsWith("90")){mDefaultOrientation = Surface.ROTATION_90;} else if(orientation.startsWith("180")){mDefaultOrientation = Surface.ROTATION_180;} else if(orientation.startsWith("270")){mDefaultOrientation = Surface.ROTATION_270;}mRotation = mDefaultOrientation;// add end}
}
...
boolean updateRotationUnchecked(boolean forceUpdate) {...final int oldRotation = mRotation;final int lastOrientation = mLastOrientation;// add startfinal int rotation = mDefaultOrientation/*rotationForOrientation(lastOrientation, oldRotation)*/;// add end...
}
...
@VisibleForTesting
@Surface.Rotation
int rotationForOrientation(@ScreenOrientation int orientation,@Surface.Rotation int lastRotation) {ProtoLog.v(WM_DEBUG_ORIENTATION,"rotationForOrientation(orient=%s (%d), last=%s (%d)); user=%s (%d) %s",ActivityInfo.screenOrientationToString(orientation), orientation,Surface.rotationToString(lastRotation), lastRotation,Surface.rotationToString(mUserRotation), mUserRotation,mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED? "USER_ROTATION_LOCKED" : "");if (isFixedToUserRotation()) {return mUserRotation;}...default:// For USER, UNSPECIFIED, NOSENSOR, SENSOR and FULL_SENSOR,// just return the preferred orientation we already calculated.if (preferredRotation >= 0) {return preferredRotation;}// add start//return Surface.ROTATION_0;return mDefaultOrientation;// add end}
}
- frameworks/base/services/core/java/com/android/server/wm/DisplayContent.java
@ScreenOrientation
@Override
int getOrientation() {// add startint mDefaultOrientation = 0;String persistOrientation = SystemProperties.get("persist.panel.orientation");if (!persistOrientation.isEmpty()){if (persistOrientation.startsWith("0")){mDefaultOrientation = Surface.ROTATION_0;} else if(persistOrientation.startsWith("90")){mDefaultOrientation = Surface.ROTATION_90;} else if(persistOrientation.startsWith("180")){mDefaultOrientation = Surface.ROTATION_180;} else if(persistOrientation.startsWith("270")){mDefaultOrientation = Surface.ROTATION_270;}return mDefaultOrientation;}// add endmLastOrientationSource = null;