Android13 动态控制状态栏和导航栏

前言

在Android系统中,状态栏(StatusBar)、导航栏(NavigationBar)以及下拉菜单(通常称为Notification Shade或Quick Settings Panel)构成了用户界面不可或缺的三个元素,它们共同为用户提供了丰富的信息显示与便捷的交互体验。具体而言,状态栏实时展示着时间、电池电量、信号强度等关键信息;导航栏则集成了如返回、主页及多任务管理等基础操作按钮;而下拉菜单则让用户能够迅速访问快速设置项及通知详情。

值得注意的是,尽管这些元素在多数Android设备上普遍存在且至关重要,但并非所有用户或应用场景都需要它们持续显示。因此,Android系统提供了灵活的配置选项,允许用户或开发者根据实际需求控制这些元素的显示与隐藏状态。

在Android 13中,对于导航栏的动态显示与隐藏控制相较于之前版本有了更为简化的处理方式。通过利用广播机制或其他系统接口,开发者可以更加便捷地实现这些高级定制功能,为用户带来更加个性化和流畅的使用体验。同时,这也意味着Android平台在持续优化其灵活性和可定制性,以更好地满足不同用户群体的多样化需求了。

修改示例

diff --git a/frameworks/base/packages/SystemUI/src/com/android/systemui/navigationbar/NavigationBarController.java b/frameworks/base/packages/SystemUI/src/com/android/systemui/navigationbar/NavigationBarController.java
old mode 100644
new mode 100755
index 5c8d2212cef..ffd22dc73b1
--- a/frameworks/base/packages/SystemUI/src/com/android/systemui/navigationbar/NavigationBarController.java
+++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/navigationbar/NavigationBarController.java
@@ -496,4 +496,15 @@ public class NavigationBarController implements
             mNavigationBars.valueAt(i).dump(pw);
         }
     }
+
+    /**
+     * Create by yeruilai 2024-8-22 10:23:59 
+     */
+    public void removeNavigationBars() {
    
    
+        Display[] displays = mDisplayManager.getDisplays();
+        for (Display display : displays) {
    
    
+            removeNavigationBar(display.getDisplayId());
+        }
+    }
+
 }
diff --git a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java
index f76470badf1..94cca11834b 100755
--- a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java
+++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java
@@ -286,6 +286,15 @@ import dagger.Lazy;
 public class CentralSurfacesImpl extends CoreStartable implements
         CentralSurfaces {
    
    
 
+    // Create by yeruilai 2024-8-22 10:19:38 Dynamically display hidden navigation bar and status bar
+    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 SYS_PROPERTY_STATUS_BAR = "persist.sys.statusbar.enable";
+    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_NAVIGATION_BAR = "persist.sys.navigationbar.enable";
+    
     private static final String BANNER_ACTION_CANCEL =
             "com.android.systemui.statusbar.banner_action_cancel";
     private static final String BANNER_ACTION_SETUP =
@@ -1522,6 +1531,12 @@ public class CentralSurfacesImpl extends CoreStartable implements
             filter.addAction(UnisocPowerManagerUtil.ACTION_POWEREX_SAVE_MODE_CHANGED);
         }
         /* @} */
+        // Create by yeruilai 2024-8-22 10:19:38 Dynamically display hidden navigation bar and status bar
+        filter.addAction(ACTION_HIDE_STATUS_BAR);
+        filter.addAction(ACTION_SHOW_STATUS_BAR);
+        filter.addAction(ACTION_HIDE_NAVIGATION_BAR);
+        filter.addAction(ACTION_SHOW_NAVIGATION_BAR);
+
         mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, filter, null, UserHandle.ALL);
     }
 
@@ -2543,8 +2558,8 @@ public class CentralSurfacesImpl extends CoreStartable implements
     @Override
     public void createAndAddWindows(@Nullable RegisterStatusBarResult result) {
    
    
         makeStatusBarView(result);
-        mNotificationShadeWindowController.attach();
         mStatusBarWindowController.attach();
+        mNotificationShadeWindowController.attach();
     }
 
     // called by makeStatusbar and also by PhoneStatusBarView
@@ -2870,6 +2885,29 @@ public class CentralSurfacesImpl extends CoreStartable implements
                     && mDataSaverController.isDataSaverEnabled()) {
    
    
                 mDataSaverController.setDataSaverEnabled(false);
             }
+            // Create by yeruilai 2024-8-22 10:19:38 Dynamically display hidden navigation bar and status bar
+             else if(ACTION_HIDE_STATUS_BAR.equals(action)){
    
    
+                android.util.Log.d("yeruilai","yeruilai:action=" + ACTION_HIDE_STATUS_BAR);
+
+                mStatusBarWindowController.hideStatusBar();
+                SystemProperties.set(SYS_PROPERTY_STATUS_BAR, "false");
+            } else if(ACTION_SHOW_STATUS_BAR.equals(action)){
    
    
+                android.util.Log.d("yeruilai","yeruilai:action=" + ACTION_SHOW_STATUS_BAR);
+
+                mStatusBarWindowController.showStatusBar();
+                SystemProperties.set(SYS_PROPERTY_STATUS_BAR, "true");
+            } else if(ACTION_HIDE_NAVIGATION_BAR.equals(action)){
    
    
+                android.util.Log.d("yeruilai","yeruilai:action=" + ACTION_HIDE_NAVIGATION_BAR);
+
+                mNavigationBarController.removeNavigationBars();
+                SystemProperties.set(SYS_PROPERTY_NAVIGATION_BAR, "false");
+            } else if(ACTION_SHOW_NAVIGATION_BAR.equals(action)){
    
    
+                android.util.Log.d("yeruilai","yeruilai:action=" + ACTION_SHOW_NAVIGATION_BAR);
+
+                createNavigationBar(null);
+                SystemProperties.set(SYS_PROPERTY_NAVIGATION_BAR, "true");
+            }
+
             /* @} */
             Trace.endSection();
         }
diff --git a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/window/StatusBarWindowController.java b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/window/StatusBarWindowController.java
old mode 100644
new mode 100755
index e0d780a5fcd..08b4628f4c5
--- a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/window/StatusBarWindowController.java
+++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/window/StatusBarWindowController.java
@@ -316,4 +316,42 @@ public class StatusBarWindowController {
    
    
             mLpChanged.privateFlags &= ~PRIVATE_FLAG_FORCE_SHOW_STATUS_BAR;
         }
     }
+
+    /**
+     * Create by yeruilai 2024-8-22 10:23:59 
+     */
+    private void apply(State state,boolean isHideStatus) {
    
    
+        if (!mIsAttached) {
    
    
+            return;
+        }
+        applyForceStatusBarVisibleFlag(state);
+        if (!isHideStatus){
    
    
+            applyHeight(state);
+        }
+        if (mLp != null && mLp.copyFrom(mLpChanged) != 0) {
    
    
+            mWindowManager.updateViewLayout(mStatusBarWindowView, mLp);
+        }
+    }
+
+    /**
+     * Create by yeruilai 2024-8-22 10:23:59 
+     */
+    public void showStatusBar(){
    
    
+        apply(mCurrentState);
+        mStatusBarWindowView.setVisibility(View.VISIBLE);
+    }
+
+    /**
+     * Create by yeruilai 2024-8-22 10:23:59 
+     */
+    public void hideStatusBar(){
    
    
+        mStatusBarWindowView.setVisibility(View.GONE);
+        mLpChanged.height = 0;
+        for (int rot = Surface.ROTATION_0; rot <= Surface.ROTATION_270; rot++) {
    
    
+            mLpChanged.paramsForRotation[rot].height = 0;
+        }
+        apply(mCurrentState,true);
+    }
+
 }

添加相应的SELinux权限,代码示例如下:

diff --git a/system/sepolicy/public/platform_app.te b/system/sepolicy/public/platform_app.te
index 3f689d82481..21a1f30d29f 100755
--- a/system/sepolicy/public/platform_app.te
+++ b/system/sepolicy/public/platform_app.te
@@ -7,3 +7,5 @@ allow platform_app system_app_data_file:dir r_dir_perms;
 allow platform_app system_app_data_file:file r_file_perms;
 #redstone
 type platform_app, domain;
+
+allow platform_app system_prop:property_service set;
+
diff --git a/system/sepolicy/prebuilts/api/33.0/public/platform_app.te b/system/sepolicy/prebuilts/api/33.0/public/platform_app.te
index 3f689d82481..21a1f30d29f 100755
--- a/system/sepolicy/prebuilts/api/33.0/public/platform_app.te
+++ b/system/sepolicy/prebuilts/api/33.0/public/platform_app.te
@@ -7,3 +7,5 @@ allow platform_app system_app_data_file:dir r_dir_perms;
 allow platform_app system_app_data_file:file r_file_perms;
 #redstone
 type platform_app, domain;
+
+allow platform_app system_prop:property_service set;
+

猜你喜欢

转载自blog.csdn.net/u010345983/article/details/141753166