美文网首页
Android设备添加一个旋转屏幕开关(Android 10)

Android设备添加一个旋转屏幕开关(Android 10)

作者: gale_小米 | 来源:发表于2022-03-03 10:37 被阅读0次

    客制需求:Android 10 设备添加一个旋转屏幕开关,设备没有装重力感应,所以不能自动旋转屏幕;

    1.参考系统自动旋转屏幕:添加一个SwitchPreference ,和对应的控制器RotateScreenPreferenceController
    2.声明:persist.sys.user_rotation 属性来记录旋转状态;

    Index: android/device/softwinner/ceres-b3/overlay/packages/apps/Settings/res/xml/display_settings.xml
    ===================================================================
    --- android/device/softwinner/ceres-b3/overlay/packages/apps/Settings/res/xml/display_settings.xml
    +++ android/device/softwinner/ceres-b3/overlay/packages/apps/Settings/res/xml/display_settings.xml
    @@ -53,8 +53,13 @@
             settings:useAdminDisabledSummary="true"
             settings:controller="com.android.settings.display.WallpaperPreferenceController">
         </com.android.settingslib.RestrictedPreference>
    +     <!--    -->
    +    <SwitchPreference
    +        android:key="rotate_screen"
    +        android:title="@string/rotate_screen_title"
    +        android:summary="@string/keywords_auto_rotate1"
    +        settings:controller="com.android.settings.display.RotateScreenPreferenceController" />
     
    -
         <SwitchPreference
             android:key="dark_ui_mode"
             android:title="@string/dark_ui_mode"
    Index: android/device/softwinner/ceres-c3/overlay/packages/apps/Settings/res/xml/display_settings.xml
    
    @@ -270,6 +270,7 @@
     # set primary display orientation to 270
     PRODUCT_PROPERTY_OVERRIDES += \
         ro.surface_flinger.primary_display_orientation=ORIENTATION_$(SPRODUCT_SW_ROTATION) \
    +    persist.sys.user_rotation=3
     
     PRODUCT_SYSTEM_DEFAULT_PROPERTIES += \
         ro.minui.default_rotation=ROTATION_$(shell _a=(NONE RIGHT DOWN LEFT); _i=$$((($(SPRODUCT_SW_ROTATION)+0)/90)); echo -n $${_a[$$_i]}) \
    Index: android/packages/apps/Settings/src/com/android/settings/display/RotateScreenPreferenceController.java
    ===================================================================
    --- android/packages/apps/Settings/src/com/android/settings/display/RotateScreenPreferenceController.java
    +++ android/packages/apps/Settings/src/com/android/settings/display/RotateScreenPreferenceController.java
    @@ -0,0 +1,82 @@
    +/*
    + * Copyright (C) 2016 The Android Open Source Project
    + *
    + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
    + * except in compliance with the License. You may obtain a copy of the License at
    + *
    + *      http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software distributed under the
    + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied. See the License for the specific language governing
    + * permissions and limitations under the License.
    + */
    +package com.android.settings.display;
    +
    +import android.app.settings.SettingsEnums;
    +import android.content.Context;
    +import android.text.TextUtils;
    +
    +import androidx.preference.Preference;
    +
    +import android.util.Log;
    +import com.android.internal.view.RotationPolicy;
    +import com.android.settings.core.PreferenceControllerMixin;
    +import com.android.settings.core.TogglePreferenceController;
    +import com.android.settings.overlay.FeatureFactory;
    +import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
    +import com.android.settingslib.core.lifecycle.LifecycleObserver;
    +import com.android.settingslib.core.lifecycle.events.OnPause;
    +import com.android.settingslib.core.lifecycle.events.OnResume;
    +import android.os.SystemProperties;
    +
    +public class RotateScreenPreferenceController extends TogglePreferenceController implements
    +        PreferenceControllerMixin, Preference.OnPreferenceChangeListener, LifecycleObserver,
    +        OnResume, OnPause {
    +
    +    private Preference mPreference;
    +    private static final String TAG = "RotateScreenPreferenceController";
    +    private static final String ROTATE_SCREEN_KEY =
    +            "persist.sys.user_rotation";
    +
    +    public RotateScreenPreferenceController(Context context, String key) {
    +        super(context, key);
    +    }
    +
    +    @Override
    +    public void updateState(Preference preference) {
    +        mPreference = preference;
    +        super.updateState(preference);
    +    }
    +
    +    @Override
    +    public void onResume() {
    +    }
    +
    +    @Override
    +    public void onPause() {
    +    }
    +
    +    @Override
    +    public int getAvailabilityStatus() {
    +        return AVAILABLE;
    +    }
    +
    +    @Override
    +    public boolean isSliceable() {
    +        return TextUtils.equals(getPreferenceKey(), "rotate_screen");
    +    }
    +
    +    @Override
    +    public boolean isChecked() {
    +        int rotation = SystemProperties.getInt(ROTATE_SCREEN_KEY, 3);
    +        Log.w(TAG, "rotation="+rotation);
    +        return rotation ==0;
    +    }
    +
    +    @Override
    +    public boolean setChecked(boolean isChecked) {
    +        RotationPolicy.setRotation(mContext,isChecked);
    +        return true;
    +    }
    +}
    
    Property changes on: android/packages/apps/Settings/src/com/android/settings/display/RotateScreenPreferenceController.java
    ___________________________________________________________________
    Added: svn:executable
    ## -0,0 +1 ##
    +*
    \ No newline at end of property
    Index: android/packages/apps/Settings/res/values-zh-rCN/strings.xml
    ===================================================================
    --- android/packages/apps/Settings/res/values-zh-rCN/strings.xml
    +++ android/packages/apps/Settings/res/values-zh-rCN/strings.xml
    @@ -3162,6 +3162,8 @@
         <string name="keywords_display_auto_brightness" msgid="4561771351118904241">"降低屏幕亮度, 触摸屏, 电池, 智能亮度, 动态亮度, 自动调节亮度"</string>
         <string name="keywords_display_adaptive_sleep" msgid="6865504720946121402">"调暗屏幕, 休眠, 电池, 超时, 感知, 显示, 屏幕, 闲置"</string>
         <string name="keywords_auto_rotate" msgid="5620879898668211494">"旋转, 翻转, 纵向, 横向, 屏幕方向, 垂直, 水平"</string>
    +    <string name="rotate_screen_title">旋转屏幕</string>
    +    <string name="keywords_auto_rotate1">旋转屏幕</string>
         <string name="keywords_system_update_settings" msgid="7752189778843741773">"升级, android"</string>
         <string name="keywords_zen_mode_settings" msgid="6526742836231604995">"勿扰, 时间表, 通知, 屏蔽, 设为静音, 振动, 休眠, 工作, 焦点, 声音, 静音, 日, 工作日, 周末, 工作日晚上, 活动"</string>
         <string name="keywords_screen_timeout" msgid="8161370660970309476">"屏幕, 锁定时间, 超时, 锁屏"</string>
    Index: android/packages/apps/Settings/res/xml/display_settings.xml
    ===================================================================
    --- android/packages/apps/Settings/res/xml/display_settings.xml
    +++ android/packages/apps/Settings/res/xml/display_settings.xml
    @@ -20,7 +20,7 @@
         android:key="display_settings_screen"
         android:title="@string/display_settings"
         settings:keywords="@string/keywords_display"
    -    settings:initialExpandedChildrenCount="5">
    +    settings:initialExpandedChildrenCount="7">
     
         <com.android.settingslib.RestrictedPreference
             android:key="brightness"
    @@ -54,6 +54,12 @@
             settings:controller="com.android.settings.display.WallpaperPreferenceController">
         </com.android.settingslib.RestrictedPreference>
     
    +     <!--    -->
    +    <SwitchPreference
    +        android:key="rotate_screen"
    +        android:title="@string/rotate_screen_title"
    +        android:summary="@string/keywords_auto_rotate1"
    +        settings:controller="com.android.settings.display.RotateScreenPreferenceController" />
     
         <SwitchPreference
             android:key="dark_ui_mode"
    Index: android/packages/apps/Settings/res/values-zh-rTW/strings.xml
    ===================================================================
    --- android/packages/apps/Settings/res/values-zh-rTW/strings.xml    
    +++ android/packages/apps/Settings/res/values-zh-rTW/strings.xml    
    @@ -3162,6 +3162,8 @@
         <string name="keywords_display_auto_brightness" msgid="4561771351118904241">"調暗螢幕, 觸控螢幕, 電池, 智慧型亮度, 動態亮度, 自動亮度"</string>
         <string name="keywords_display_adaptive_sleep" msgid="6865504720946121402">"調暗螢幕, 休眠, 電池, 自動關閉, 感知, 顯示, 螢幕, 閒置"</string>
         <string name="keywords_auto_rotate" msgid="5620879898668211494">"旋轉, 翻轉, 轉動, 直向, 橫向, 螢幕方向, 垂直, 水平"</string>
    +    <string name="rotate_screen_title">旋转屏幕</string>
    +    <string name="keywords_auto_rotate1">旋转屏幕</string>
         <string name="keywords_system_update_settings" msgid="7752189778843741773">"升級, android"</string>
         <string name="keywords_zen_mode_settings" msgid="6526742836231604995">"零打擾, 時間表, 通知, 封鎖, 靜音, 震動, 休眠, 工作, 焦點, 音效, 忽略, 天, 週間, 週末, 週間晚上, 活動"</string>
         <string name="keywords_screen_timeout" msgid="8161370660970309476">"螢幕, 鎖定時間, 自動關閉, 鎖定螢幕"</string>
    Index: android/packages/apps/Settings/res/values-zh-rHK/strings.xml
    ===================================================================
    --- android/packages/apps/Settings/res/values-zh-rHK/strings.xml    
    +++ android/packages/apps/Settings/res/values-zh-rHK/strings.xml    
    @@ -3160,6 +3160,8 @@
         <string name="keywords_display_auto_brightness" msgid="4561771351118904241">"調暗螢幕, 觸控螢幕, 電池, 智能亮度, 動態亮度, 自動調整亮度"</string>
         <string name="keywords_display_adaptive_sleep" msgid="6865504720946121402">"調暗螢幕, 休眠, 電池, 逾時, 注意, 顯示屏, 螢幕, 閒置"</string>
         <string name="keywords_auto_rotate" msgid="5620879898668211494">"旋轉, 翻轉, 旋轉, 直向, 橫向, 屏幕定向, 垂直, 水平"</string>
    +    <string name="rotate_screen_title">旋转屏幕</string>
    +    <string name="keywords_auto_rotate1">旋转屏幕</string>
         <string name="keywords_system_update_settings" msgid="7752189778843741773">"升級, Android"</string>
         <string name="keywords_zen_mode_settings" msgid="6526742836231604995">"請勿騷擾, 時間表, 通知, 封鎖, 靜音, 震動, 休眠, 工作, 焦點, 音效, 忽略, 天, 平日, 週末, 平日晚上, 活動"</string>
         <string name="keywords_screen_timeout" msgid="8161370660970309476">"螢幕, 鎖定時間, 逾時, 鎖定螢幕"</string>
    Index: android/packages/apps/Settings/res/values/strings.xml
    ===================================================================
    --- android/packages/apps/Settings/res/values/strings.xml   
    +++ android/packages/apps/Settings/res/values/strings.xml   
    @@ -7392,6 +7392,8 @@
     
         <!-- List of synonyms for the auto rotate (rotate the virtual display when the device rotates) setting, used to match in settings search [CHAR LIMIT=NONE] -->
         <string name="keywords_auto_rotate">rotate, flip, rotation, portrait, landscape, orientation, vertical, horizontal</string>
    +    <string name="rotate_screen_title">Rotate screen</string>
    +    <string name="keywords_auto_rotate1">rotate screen</string>
     
         <!-- List of synonyms for the System Update (update the operating system) setting, used to match in settings search [CHAR LIMIT=NONE] -->
         <string name="keywords_system_update_settings">upgrade, android</string>
     
    Index: android/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/RotationLockControllerImpl.java
    ===================================================================
    --- android/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/RotationLockControllerImpl.java (版本 16247)
    +++ android/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/RotationLockControllerImpl.java (版本 16248)
    @@ -25,6 +25,8 @@
     
     import javax.inject.Inject;
     import javax.inject.Singleton;
    +import android.util.Log;
    +import android.os.SystemProperties;
     
     /** Platform implementation of the rotation lock controller. **/
     @Singleton
    @@ -40,6 +42,7 @@
                 notifyChanged();
             }
         };
    +    private static final String TAG = "RotationLockControllerImpl";
     
         @Inject
         public RotationLockControllerImpl(Context context) {
    @@ -68,8 +71,12 @@
             RotationPolicy.setRotationLock(mContext, locked);
         }
     
    +    private static final String ROTATE_SCREEN_KEY =
    +            "persist.sys.user_rotation";
         public void setRotationLockedAtAngle(boolean locked, int rotation){
    -        RotationPolicy.setRotationLockAtAngle(mContext, locked, rotation);
    +        int rotation_screen = SystemProperties.getInt(ROTATE_SCREEN_KEY, 3);
    +        //Log.w(TAG, " setRotationLockedAtAngle rotation_screen="+rotation_screen+",locked="+locked);
    +        RotationPolicy.setRotationLockAtAngle(mContext, locked, rotation_screen);
         }
     
         public boolean isRotationLockAffordanceVisible() {
    Index: android/frameworks/base/core/java/com/android/internal/view/RotationPolicy.java
    ===================================================================
    --- android/frameworks/base/core/java/com/android/internal/view/RotationPolicy.java
    +++ android/frameworks/base/core/java/com/android/internal/view/RotationPolicy.java 
    @@ -33,6 +33,7 @@
     import android.view.IWindowManager;
     import android.view.Surface;
     import android.view.WindowManagerGlobal;
    +import android.content.Intent;
     
     import com.android.internal.R;
     
    @@ -47,9 +48,18 @@
     
         private RotationPolicy() {
         }
    +    private static final String ROTATE_SCREEN_KEY =
    +            "persist.sys.user_rotation";
     
    +
         private static int getNaturalRotation() {
    +        /*
             int rotation = SystemProperties.getInt("ro.primary_display.user_rotation", 0);
    +        // if (rotation==90) {
    +        //    SystemProperties.set(ROTATE_SCREEN_KEY,"1");
    +        // }else{
    +        //    SystemProperties.set(ROTATE_SCREEN_KEY,"0");
    +        // }
             switch (rotation) {
                 case 90:
                     return Surface.ROTATION_90;
    @@ -61,6 +71,10 @@
                     break;
             }
             return Surface.ROTATION_0;
    +        */
    +        int rotation = SystemProperties.getInt(ROTATE_SCREEN_KEY, 3);
    +        Log.w(TAG, "getNaturalRotation = "+ rotation);
    +        return rotation;
         }
     
         /**
    @@ -133,6 +147,53 @@
             setRotationLockAtAngle(context, enabled, rotation);
         }
     
    +
    +    public static void  setRotation(Context context,final boolean enabled){       
    +        //Log.e(TAG, " static setRotation enabled="+enabled);
    +        if(enabled) {
    +                    AsyncTask.execute(new Runnable() {
    +                        @Override
    +                        public void run() {
    +                            try {
    +                                IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
    +                                wm.freezeRotation(Surface.ROTATION_0);
    +                                SystemProperties.set(ROTATE_SCREEN_KEY,"0");
    +                            } catch (RemoteException exc) {
    +                                Log.w(TAG, "Unable to save auto-rotate setting");
    +                            }
    +                            int rotation = SystemProperties.getInt(ROTATE_SCREEN_KEY, 0);
    +                            //Log.w(TAG, "setRotation rotation="+rotation);
    +                        }
    +                    });
    +                }else {
    +                    AsyncTask.execute(new Runnable() {
    +                        @Override
    +                        public void run() {
    +                            try {
    +                                IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
    +                                //wm.thawRotation();
    +                                wm.freezeRotation(Surface.ROTATION_270);
    +                                SystemProperties.set(ROTATE_SCREEN_KEY,"3");
    +                            } catch (RemoteException exc) {
    +                                Log.w(TAG, "Unable to save auto-rotate setting");
    +                            }
    +                            int rotation = SystemProperties.getInt(ROTATE_SCREEN_KEY, 0);
    +                            //Log.w(TAG, "setRotation rotation="+rotation);
    +                        }
    +                    });
    +                }
    +
    +    }
    +
    +
    +
    +    private static void disableAutoRotation(Context context) {
    +        Settings.System.putIntForUser(context.getContentResolver(),
    +                Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT);
    +    }
    +
         /**
          * Enables or disables rotation lock at a specific rotation from system UI.
          */
    @@ -141,7 +202,7 @@
             Settings.System.putIntForUser(context.getContentResolver(),
                     Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0,
                     UserHandle.USER_CURRENT);
    -
    +        //Log.w(TAG, "setRotationLockAtAngle rotation="+rotation);
             setRotationLock(enabled, rotation);
         }
     
    @@ -170,6 +231,8 @@
                         IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
                         if (params[0]) {
                             wm.freezeRotation(rotation);
    +                        //Log.w(TAG, "Unable to save auto-rotate setting rotation="+rotation);
    +                        SystemProperties.set(ROTATE_SCREEN_KEY,""+rotation);
                         } else {
                             wm.thawRotation();
                         }
    
     
    Index: android/frameworks/base/services/core/java/com/android/server/wm/DisplayRotation.java
    ===================================================================
    --- android/frameworks/base/services/core/java/com/android/server/wm/DisplayRotation.java   
    +++ android/frameworks/base/services/core/java/com/android/server/wm/DisplayRotation.java
    @@ -551,9 +551,10 @@
             final boolean deskDockEnablesAccelerometer =
                     mDisplayPolicy.isDeskDockEnablesAccelerometer();
     
    -        int user_rotation = SystemProperties.getInt("ro.primary_display.user_rotation", 0);
    -        int defaultRotation = Surface.ROTATION_0;
    +        //int user_rotation = SystemProperties.getInt("ro.primary_display.user_rotation", 0);
    +        int defaultRotation = SystemProperties.getInt("persist.sys.user_rotation", 3);
             final int preferredRotation;
    +        /*
             switch (user_rotation) {
                 case 90:
                     defaultRotation = Surface.ROTATION_90;
    @@ -566,7 +567,7 @@
                     break;
                 default:
                     break;
    -        }
    +        }*/
             if (!isDefaultDisplay) {
                 // For secondary displays we ignore things like displays sensors, docking mode and
                 // rotation lock, and always prefer user rotation.
    Index: android/frameworks/base/services/core/java/com/android/server/wm/DisplayContent.java
    ===================================================================
    --- android/frameworks/base/services/core/java/com/android/server/wm/DisplayContent.java
    +++ android/frameworks/base/services/core/java/com/android/server/wm/DisplayContent.java    
    @@ -909,7 +909,9 @@
                     * mDisplayMetrics.densityDpi / DENSITY_DEFAULT;
             isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
             if (isDefaultDisplay) {
    -            int user_rotation = SystemProperties.getInt("ro.primary_display.user_rotation", 0);
    +            //int user_rotation = SystemProperties.getInt("ro.primary_display.user_rotation", 0);
    +            mRotation = SystemProperties.getInt("persist.sys.user_rotation", 3);
    +            /*
                 switch (user_rotation) {
                     case 90:
                         mRotation = ROTATION_90;
    @@ -922,7 +924,7 @@
                         break;
                     default:
                         break;
    -            }
    +            }*/
             }
     
             mNaturalRotation = mRotation;
    Index: android/frameworks/base/core/java/android/hardware/SystemSensorManager.java
    ===================================================================
    --- android/frameworks/base/core/java/android/hardware/SystemSensorManager.java (版本 16279)
    +++ android/frameworks/base/core/java/android/hardware/SystemSensorManager.java (版本 16280)
    @@ -787,7 +787,8 @@
                 mListener = listener;
                 if (listener != null && listener.toString().startsWith("android.hardware.cts.helpers")) {
                     isCtsTest = true;
    -                user_rotation = android.os.SystemProperties.getInt("ro.primary_display.user_rotation", 0);
    +                //user_rotation = android.os.SystemProperties.getInt("ro.primary_display.user_rotation", 0);
    +                user_rotation = android.os.SystemProperties.getInt("persist.sys.user_rotation", 3);
                 }
             }
     
    @@ -831,15 +832,15 @@
                 // Copy from the values array.
                 System.arraycopy(values, 0, t.values, 0, t.values.length);
                 if (isCtsTest && sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
    -                if (user_rotation == 90) {
    +                if (user_rotation == 1) {
                         float tmp = -t.values[1];
                         t.values[1] = t.values[0];
                         t.values[0] = tmp;
    -                } else if (user_rotation == 270) {
    +                } else if (user_rotation == 3) {
                         float tmp = -t.values[0];
                         t.values[0] = t.values[1];
                         t.values[1] = tmp;
    -                } else if (user_rotation == 180) {
    +                } else if (user_rotation == 2) {
                         t.values[0] = -t.values[0];
                         t.values[1] = -t.values[1];
                     }
    
    
    
    

    相关文章

      网友评论

          本文标题:Android设备添加一个旋转屏幕开关(Android 10)

          本文链接:https://www.haomeiwen.com/subject/cygqrrtx.html