当前位置:   article > 正文

【Android14 添加SystemUI下拉菜单控制按钮】_systemui下拉框添加选项

systemui下拉框添加选项

   Android14相对于之前的Android版本(11,12,13)StatusBar.java文件进行了拆分,所以在Android14中StatusBar的许多函数方法等都更换了位置,不过万变不离其宗,我们添加控制按钮的方法和思路也是类似的,本文以添加下拉菜单控制按钮为例,进行定制化修改。

需求:一些应用程序中读取通知但在其他应用程序中我们需要始终锁定下拉菜单,希望添加Drop Menu 控制器用于控制Menu是否正常下拉。

实现思路:

1.Settings模块:在Settings模块中显示菜单添加开关,用于开启和关闭下拉菜单,并发送相应的广播,并写入相应的系统属性。

2.SystemUI模块:添加广播接收器,用于接收控制下拉菜单显示和隐藏的广播,并执行相应的操作。同时添加系统属性读取,用于获取下拉菜单的初始状态。

Settings模块:

1.添加显示字符串,如有多语言需求请自行在该语言包添加

alps/vendor/mediatek/proprietary/packages/apps/MtkSettings/res/values/strings.xml

  1. <!-- Warning message when we try to dock an app not supporting multiple instances split into multiple sides [CHAR LIMIT=NONE] -->
  2. <!--wjj add-->
  3. <string name="ctrl_explan">Drop Down Menu</string>
  4. <!--wjj end -->

2.在设置模块的显示菜单​display_settings.xml中添加目标按钮的键值和标题以及控制类

alps/vendor/mediatek/proprietary/packages/apps/MtkSettings/res/xml/display_settings.xml

  1. <!--wjj add -->
  2. <SwitchPreference
  3. android:key="ctrl_explan"
  4. android:title="@string/ctrl_explan"
  5. settings:controller="com.android.settings.display.ExPlanPreferenceController.java"/>
  6. <!--wjj end -->

3.在Settings模块的DisplaySettings.java文件中,用于注册我们的控制开关:

alps/vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/DisplaySettings.java

  1. --- a/alps/vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/DisplaySettings.java
  2. +++ b/alps/vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/DisplaySettings.java
  3. @@ -29,6 +29,7 @@ import com.android.settings.display.TapToWakePreferenceController;
  4. import com.android.settings.display.ThemePreferenceController;
  5. import com.android.settings.display.VrDisplayPreferenceController;
  6. import com.android.settings.search.BaseSearchIndexProvider;
  7. +import com.android.settings.display.ExPlanPreferenceController;
  8. import com.android.settingslib.core.AbstractPreferenceController;
  9. import com.android.settingslib.core.lifecycle.Lifecycle;
  10. import com.android.settingslib.search.SearchIndexable;
  11. @@ -91,6 +92,9 @@ public class DisplaySettings extends DashboardFragment {
  12. /// M: add for HDMI settings @{
  13. controllers.add(new HdmiPreferenceController(context));
  14. /// @}
  15. + //wjj add
  16. + controllers.add(new ExPlanPreferenceController(context));
  17. + //wjj end
  18. return controllers;
  19. }

4.添加下拉菜单控制类并添加宏控控制控制按钮是否存在

alps/vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/display/ExPlanPreferenceController.java

  1. package com.android.settings.display;
  2. import android.content.Context;
  3. import android.provider.Settings;
  4. import androidx.preference.SwitchPreference;
  5. import androidx.preference.Preference;
  6. import androidx.preference.PreferenceScreen;
  7. import com.android.settings.core.PreferenceControllerMixin;
  8. import com.android.settingslib.core.AbstractPreferenceController;
  9. import android.content.Intent;
  10. import android.util.Log;
  11. import android.os.SystemProperties;
  12. public class ExPlanPreferenceController extends AbstractPreferenceController implements
  13. Preference.OnPreferenceChangeListener {
  14. private static final String TAG = "ExPlanCtrl";
  15. private static final boolean DEBUG = true;
  16. private static final String KEY_EXPLAN = "ctrl_explan";
  17. private static final String SYS_PROP_EXPLAN_ENABLE = "persist.sys.explan.enable";
  18. private static final String ACTION_HIDE_EXPLAN = "sys.explan.hide";
  19. private static final String ACTION_SHOW_EXPLAN = "sys.explan.show";
  20. private static final boolean HX_TS818_BUG_70579 = SystemProperties.getBoolean("ro.hx_ts818_bug_70579", false);
  21. public ExPlanPreferenceController(Context context) {
  22. super(context);
  23. }
  24. @Override
  25. public String getPreferenceKey() {
  26. return KEY_EXPLAN;
  27. }
  28. @Override
  29. public boolean isAvailable() {
  30. if(HX_TS818_BUG_70579){
  31. return true;
  32. }else{
  33. return false;
  34. }
  35. }
  36. @Override
  37. public void displayPreference(PreferenceScreen screen) {
  38. if (!isAvailable()) {
  39. setVisible(screen, KEY_EXPLAN, false);
  40. return;
  41. }
  42. final SwitchPreference mExPlanPreference = screen.findPreference(KEY_EXPLAN);
  43. if (mExPlanPreference != null) {
  44. String value = SystemProperties.get(SYS_PROP_EXPLAN_ENABLE, "false");
  45. mExPlanPreference.setChecked(value.equals("true"));
  46. mExPlanPreference.setOnPreferenceChangeListener(this);
  47. }
  48. }
  49. @Override
  50. public void updateState(Preference preference) {
  51. String value = SystemProperties.get(SYS_PROP_EXPLAN_ENABLE, "false");
  52. ((SwitchPreference) preference).setChecked(value.equals("true"));
  53. }
  54. @Override
  55. public boolean onPreferenceChange(Preference preference, Object newValue) {
  56. boolean value = (Boolean) newValue;
  57. if (DEBUG) {
  58. Log.d(TAG, "key value " + value);
  59. }
  60. Intent intent = new Intent();
  61. if (value) {
  62. intent.setAction(ACTION_SHOW_EXPLAN);
  63. } else {
  64. intent.setAction(ACTION_HIDE_EXPLAN);
  65. }
  66. mContext.sendBroadcast(intent);
  67. return true;
  68. }
  69. }

SystemUI模块

1.在SystemUI模块的AndroidManifest.xml文件中声明广播,同时更改其权限让其正常设置其逻辑。

alps/vendor/mediatek/proprietary/packages/apps/SystemUI/AndroidManifest.xml

  1. --- a/alps/vendor/mediatek/proprietary/packages/apps/SystemUI/AndroidManifest.xml
  2. +++ b/alps/vendor/mediatek/proprietary/packages/apps/SystemUI/AndroidManifest.xml
  3. @@ -19,7 +19,7 @@
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
  6. package="com.android.systemui"
  7. - android:sharedUserId="android.uid.systemui"
  8. + android:sharedUserId="android.uid.system"
  9. xmlns:tools="http://schemas.android.com/tools"
  10. coreApp="true">
  11. @@ -367,6 +367,10 @@
  12. <protected-broadcast android:name="com.android.settings.flashlight.action.FLASHLIGHT_CHANGED" />
  13. <protected-broadcast android:name="com.android.systemui.action.ACTION_LAUNCH_MEDIA_OUTPUT_BROADCAST_DIALOG" />
  14. <protected-broadcast android:name="com.android.systemui.STARTED" />
  15. + <!--wjj add -->
  16. + <protected-broadcast android:name="sys.explan.show" />
  17. + <protected-broadcast android:name="sys.explan.hide" />
  18. + <!--wjj end -->
  19. <application
  20. android:name=".SystemUIApplication"

2.在SystemUI模块的CentralSurfacesImpl.java(原StatusBar.java)文件中,我们需要添加以下代码,用于声明和初始化一些变量和方法以及逻辑判断:

alps/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java

  1. --- a/alps/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java
  2. +++ b/alps/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java
  3. @@ -298,6 +298,14 @@ public class CentralSurfacesImpl implements CoreStartable, CentralSurfaces {
  4. private static final String BANNER_ACTION_SETUP =
  5. "com.android.systemui.statusbar.banner_action_setup";
  6. + //wjj add
  7. + private static final String ACTION_HIDE_EXPLAN = "sys.explan.hide";
  8. + private static final String ACTION_SHOW_EXPLAN = "sys.explan.show";
  9. + private static final String SYS_PROPERTY_EXPLAN = "persist.sys.explan.enable";
  10. +
  11. + protected StatusBarManager mStatusBarManager;
  12. + //wjj end
  13. +
  14. private static final int MSG_OPEN_SETTINGS_PANEL = 1002;
  15. private static final int MSG_LAUNCH_TRANSITION_TIMEOUT = 1003;
  16. // 1020-1040 reserved for BaseStatusBar
  17. @@ -923,6 +931,9 @@ public class CentralSurfacesImpl implements CoreStartable, CentralSurfaces {
  18. mUserTracker = userTracker;
  19. mFingerprintManager = fingerprintManager;
  20. mActivityStarter = activityStarter;
  21. + //wjj add
  22. + mStatusBarManager = (StatusBarManager) mContext.getSystemService(Context.STATUS_BAR_SERVICE);
  23. + //wjj end
  24. mLockscreenShadeTransitionController = lockscreenShadeTransitionController;
  25. mStartingSurfaceOptional = startingSurfaceOptional;
  26. @@ -980,6 +991,15 @@ public class CentralSurfacesImpl implements CoreStartable, CentralSurfaces {
  27. bubbles.setExpandListener(listener);
  28. }
  29. + //wjj add
  30. + public void explan_show() {
  31. + mStatusBarManager.disable(StatusBarManager.DISABLE_NONE);
  32. + }
  33. + public void explan_hide() {
  34. + mStatusBarManager.disable(StatusBarManager.DISABLE_EXPAND);
  35. + }
  36. + //wjj end
  37. +
  38. @Override
  39. public void start() {
  40. mScreenLifecycle.addObserver(mScreenObserver);
  41. @@ -1322,7 +1342,11 @@ public class CentralSurfacesImpl implements CoreStartable, CentralSurfaces {
  42. mStatusBarTouchableRegionManager.setup(this, mNotificationShadeWindowView);
  43. createNavigationBar(result);
  44. -
  45. + //wjj add
  46. + if (SystemProperties.getBoolean(SYS_PROPERTY_EXPLAN, false)) {
  47. + explan_show();
  48. + }
  49. + //wjj end
  50. if (ENABLE_LOCKSCREEN_WALLPAPER && mWallpaperSupported) {
  51. mLockscreenWallpaper = mLockscreenWallpaperLazy.get();
  52. }
  53. @@ -1598,6 +1622,10 @@ public class CentralSurfacesImpl implements CoreStartable, CentralSurfaces {
  54. protected void registerBroadcastReceiver() {
  55. IntentFilter filter = new IntentFilter();
  56. filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
  57. + //wjj add
  58. + filter.addAction(ACTION_HIDE_EXPLAN);
  59. + filter.addAction(ACTION_SHOW_EXPLAN);
  60. + //wjj end
  61. filter.addAction(Intent.ACTION_SCREEN_OFF);
  62. mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, filter, null, UserHandle.ALL);
  63. }
  64. @@ -2200,6 +2228,7 @@ public class CentralSurfacesImpl implements CoreStartable, CentralSurfaces {
  65. }
  66. }
  67. +
  68. @Override
  69. public void dump(PrintWriter pwOriginal, String[] args) {
  70. IndentingPrintWriter pw = DumpUtilsKt.asIndenting(pwOriginal);
  71. @@ -2384,7 +2413,15 @@ public class CentralSurfacesImpl implements CoreStartable, CentralSurfaces {
  72. }
  73. finishBarAnimations();
  74. resetUserExpandedStates();
  75. - }
  76. + //wjj add
  77. + } else if (ACTION_HIDE_EXPLAN.equals(action)) {
  78. + explan_hide();
  79. + SystemProperties.set(SYS_PROPERTY_EXPLAN, "false");
  80. + } else if (ACTION_SHOW_EXPLAN.equals(action)) {
  81. + explan_show();
  82. + SystemProperties.set(SYS_PROPERTY_EXPLAN, "true");
  83. + }
  84. + //wjj end
  85. Trace.endSection();
  86. }
  87. };

end

总结:

后续编译检测达成目标效果,开关开启时正常下拉下拉栏,开关关闭时无法正常使用下拉功能。

如需进一步控制SystemUI状态栏以及导航栏,可以参考以下链接。

Android系统 添加动态控制SystemUI状态栏、导航栏和下拉菜单_android 下拉菜单-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/677556
推荐阅读
相关标签
  

闽ICP备14008679号