MTK Android13 霸屏实现

一、背景

在台式POS场景下,经常有应用会需要获取霸屏的权限,隐藏状态栏或者导航栏,且不能被划出,其实系统已经系统了隐藏状态栏也导航栏的接口,但是无法做到禁止滑出。

java 复制代码
View decorView = ((Activity) context).getWindow().getDecorView();
      int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
          | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
      decorView.setSystemUiVisibility(uiOptions);

二、霸屏实现

通过广播形式实现:

1.在systemui中增加广播定义

java 复制代码
diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/AndroidManifest.xml b/vendor/mediatek/proprietary/packages/apps/SystemUI/AndroidManifest.xml
index 6420244..9dab3d4 100644
--- a/vendor/mediatek/proprietary/packages/apps/SystemUI/AndroidManifest.xml
+++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/AndroidManifest.xml
@@ -342,6 +342,12 @@
     <protected-broadcast android:name="com.android.settingslib.action.UNREGISTER_SLICE_RECEIVER" />
     <protected-broadcast android:name="com.android.settings.flashlight.action.FLASHLIGHT_CHANGED" />

+    <protected-broadcast android:name="com.systemui.statusbar.show" />
+    <protected-broadcast android:name="com.systemui.statusbar.hide" />
+
+    <protected-broadcast android:name="com.systemui.navigationbar.show" />
+    <protected-broadcast android:name="com.systemui.navigationbar.hide" />
+
     <application
         android:name=".SystemUIApplication"
         android:persistent="true"

2.增加广播的监听

java 复制代码
diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java b/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java
index a853747..70d1586 100644
--- a/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java
+++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java
@@ -287,6 +287,13 @@ public class CentralSurfacesImpl extends CoreStartable implements
     private static final int MSG_LAUNCH_TRANSITION_TIMEOUT = 1003;
     // 1020-1040 reserved for BaseStatusBar

+
+    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";
     /**
      * The delay to reset the hint text when the hint animation is finished running.
      */
@@ -969,6 +976,10 @@ public class CentralSurfacesImpl extends CoreStartable 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.
             IntentFilter wallpaperChangedFilter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED);
@@ -1216,7 +1227,9 @@ public class CentralSurfacesImpl extends CoreStartable implements
         }
         mNotificationPanelViewController.setHeadsUpManager(mHeadsUpManager);

-        createNavigationBar(result);
+        if (SystemProperties.getBoolean(SYS_PROPERTY_NAVIGATION_BAR, false)) {
+            createNavigationBar(result);
+        }

         if (ENABLE_LOCKSCREEN_WALLPAPER && mWallpaperSupported) {
             mLockscreenWallpaper = mLockscreenWallpaperLazy.get();
@@ -1454,6 +1467,10 @@ public class CentralSurfacesImpl extends CoreStartable implements
         IntentFilter filter = new IntentFilter();
         filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
         filter.addAction(Intent.ACTION_SCREEN_OFF);
+        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);
     }

@@ -2729,6 +2746,18 @@ public class CentralSurfacesImpl extends CoreStartable implements
                 }
                 finishBarAnimations();
                 resetUserExpandedStates();
+            } 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.针对广播监听增加对应接口实现

java 复制代码
diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBarController.java b/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBarController.java
index d756af7..97a7744 100644
--- a/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBarController.java
+++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/NavigationBarController.java
@@ -353,6 +353,13 @@ public class NavigationBarController implements
         }
     }

+    public void removeNavigationBars() {
+        Display[] displays = mDisplayManager.getDisplays();
+        for (Display display : displays) {
+            removeNavigationBar(display.getDisplayId());
+        }
+    }
+
     /** @see NavigationBar#checkNavBarModes() */
     public void checkNavBarModes(int displayId) {
         NavigationBar navBar = mNavigationBars.get(displayId);
java 复制代码
diff --git a/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/window/StatusBarWindowController.java b/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/window/StatusBarWindowController.java
index e0d780a..beef9aa 100644
--- a/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/window/StatusBarWindowController.java
+++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/window/StatusBarWindowController.java
@@ -250,6 +250,10 @@ public class StatusBarWindowController {
         apply(mCurrentState);
     }

+    public void setBarVisibility(int visibility) {
+        mStatusBarWindowView.setVisibility(visibility);
+    }
+
     /**
      * Sets whether an ongoing process requires the status bar to be forced visible.
      *

三、重新编译systemui,并测试

java 复制代码
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  #导航栏隐藏

注意广播需要root权限或者system权限,如果普通应用想具备此权限,需要修改广播的定义,大家可根据自己需求修改