RK3568 Android12动态控制SystemUI状态栏和导航栏-frameworks/base【一】
要实现一个需求,在Android12上实现动态控制状态栏和导航栏的显示及隐藏,基本思路:在frameworks/base 中增加想要的显示控制,在Settings增加开关按钮进行功能出发。本文主要讲解frameworks/base 的改动思路。
下面是实现的具体方法和步骤
-
增加系统属性,用于保存显示状态:true 表示显示,false表示隐藏
可在编译固件时,在mk文件中增加这两个属性persist.sys.statusbar.enable=true persist.sys.navigationbar.enable=true
-
用于控制显示、隐藏的广播
com.systemui.statusbar.show ----显示状态栏 com.systemui.statusbar.hide ----隐藏状态栏 com.systemui.navigationbar.show ----显示导航栏 com.systemui.navigationbar.show ----隐藏导航栏
-
在frameworks/base/packages/SystemUI 增加功能代码
修改代码涉及的文件如下:
AndroidManifest.xml
src/com/android/systemui/navigationbar/NavigationBarController.java
src/com/android/systemui/statusbar/phone/StatusBar.java
src/com/android/systemui/statusbar/window/StatusBarWindowController.java
(1)AndroidManifest.xml 增加广播的应用
+ <protected-broadcast android:name="com.systemui.statusbar.show" />
+ <protected-broadcast android:name="com.systemui.statusbar.hide" />
+
+ <!-- For NavigationBar show or not -->
+ <protected-broadcast android:name="com.systemui.navigationbar.show" />
+ <protected-broadcast android:name="com.systemui.navigationbar.hide" />
(2)statusbar/phone/StatusBar.java 初始化时根据系统属性显示或者隐藏状态栏和导航栏,监听广播显示或隐藏状态栏和导航栏
@@ -313,6 +313,12 @@ public class StatusBar extends SystemUI implements
public static final int[] CAMERA_LAUNCH_GESTURE_VIBRATION_AMPLITUDES =
new int[]{
39, 82, 139, 213, 0, 127};
+ private static final String ACTION_HIDE_STATUS_BAR = "com.systemui.statusbar.hide";
+ private static final String ACTION_SHOW_STATUS_BAR = "com.systemui.statusbar.show";
+ private static final String ACTION_HIDE_NAVIGATION_BAR = "com.systemui.navigationbar.hide";
+ private static final String ACTION_SHOW_NAVIGATION_BAR = "com.systemui.navigationbar.show";
+ private static final String SYS_PROPERTY_STATUS_BAR = "persist.sys.statusbar.enable";
+ private static final String SYS_PROPERTY_NAVIGATION_BAR = "persist.sys.navigationbar.enable";
+
@@ -966,6 +972,9 @@ public class StatusBar extends SystemUI implements
}
createAndAddWindows(result);
+ if (!SystemProperties.getBoolean(SYS_PROPERTY_STATUS_BAR, false)) {
+ mStatusBarWindowController.setBarVisibility(View.GONE);
+ }
if (mWallpaperSupported) {
// Make sure we always have the most current wallpaper info.
@@ -1178,7 +1187,9 @@ public class StatusBar extends SystemUI implements
mHeadsUpManager.addListener(mVisualStabilityManager);
mNotificationPanelViewController.setHeadsUpManager(mHeadsUpManager);
- createNavigationBar(result);
+ if (SystemProperties.getBoolean(SYS_PROPERTY_NAVIGATION_BAR, false)) {
+ createNavigationBar(result);
+ }
if (ENABLE_LOCKSCREEN_WALLPAPER && mWallpaperSupported) {
mLockscreenWallpaper = mLockscreenWallpaperLazy.get();
@@ -1406,6 +1417,10 @@ public class StatusBar extends SystemUI implements
filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(DevicePolicyManager.ACTION_SHOW_DEVICE_MONITORING_DIALOG);
+ filter.addAction(ACTION_HIDE_NAVIGATION_BAR);
+ filter.addAction(ACTION_SHOW_NAVIGATION_BAR);
+ filter.addAction(ACTION_HIDE_STATUS_BAR);
+ filter.addAction(ACTION_SHOW_STATUS_BAR);
mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, filter, null, UserHandle.ALL);
}
@@ -2666,6 +2681,18 @@ public class StatusBar extends SystemUI implements
}
else if (DevicePolicyManager.ACTION_SHOW_DEVICE_MONITORING_DIALOG.equals(action)) {
mQSPanelController.showDeviceMonitoringDialog();
+ } else if (ACTION_HIDE_NAVIGATION_BAR.equals(action)) {
+ mNavigationBarController.removeNavigationBars();
+ SystemProperties.set(SYS_PROPERTY_NAVIGATION_BAR, "false");
+ } else if (ACTION_SHOW_NAVIGATION_BAR.equals(action)) {
+ createNavigationBar(null);
+ SystemProperties.set(SYS_PROPERTY_NAVIGATION_BAR, "true");
+ } else if (ACTION_HIDE_STATUS_BAR.equals(action)) {
+ mStatusBarWindowController.setBarVisibility(View.GONE);
+ SystemProperties.set(SYS_PROPERTY_STATUS_BAR, "false");
+ } else if (ACTION_SHOW_STATUS_BAR.equals(action)) {
+ mStatusBarWindowController.setBarVisibility(View.VISIBLE);
+ SystemProperties.set(SYS_PROPERTY_STATUS_BAR, "true");
}
Trace.endSection();
}
(3)navigationbar/NavigationBarController.java 增加导航栏的销毁接口
@@ -241,6 +241,13 @@ public class NavigationBarController implements
}
}
+ public void removeNavigationBars() {
+ Display[] displays = mDisplayManager.getDisplays();
+ for (Display display : displays) {
+ removeNavigationBar(display.getDisplayId());
+ }
+ }
(4)statusbar/window/StatusBarWindowController.java 增加状态的显示控制接口
@@ -315,4 +315,8 @@ public class StatusBarWindowController {
mLpChanged.privateFlags &= ~PRIVATE_FLAG_FORCE_SHOW_STATUS_BAR;
}
}
+
+ public void setBarVisibility(int visibility) {
+ mStatusBarWindowView.setVisibility(visibility);
+ }
-
测试
在Settings还没添加功能之前,可通过发生广播命令进行测试:命令如下am broadcast -a com.systemui.statusbar.show #状态显示 am broadcast -a com.systemui.statusbar.hide #状态栏隐藏 am broadcast -a com.systemui.navigationbar.show #导航栏显示 am broadcast -a com.systemui.navigationbar.hide #导航栏隐藏