android10动态显示隐藏导航栏,状态栏

Posted 红尘六欲

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android10动态显示隐藏导航栏,状态栏相关的知识,希望对你有一定的参考价值。

实现方式:在frameworks里增加一个自定义 的service,接收上层应用广播,然后通过这个service来发送控制显示隐藏的功能,也可以上层应用发送广播直接控制显示隐藏,因为还有其它功能,所以单独加了一个service
修改:frameworks/base/core/res/androidManifest.xml

@@ -634,6 +634,12 @@
     <!-- For tether entitlement recheck-->
     <protected-broadcast
         android:name="com.android.server.connectivity.tethering.PROVISIONING_RECHECK_ALARM" />
+
+	<!--add by hclydao for broadcast -->
+	<protected-broadcast android:name="com.gzease.action.hide_nav_bar" />
+	<protected-broadcast android:name="com.gzease.action.show_nav_bar" />
+	<protected-broadcast android:name="com.gzease.action.hide_status_bar" />
+	<protected-broadcast android:name="com.gzease.action.show_status_bar" />
     <!-- ====================================================================== -->
     <!--                          RUNTIME PERMISSIONS                           -->
     <!-- ====================================================================== -->

修改: frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java

--- a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
+++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
@@ -316,7 +316,14 @@ public class StatusBar extends SystemUI implements DemoMode,
 
     /** If true, the lockscreen will show a distinct wallpaper */
     public static final boolean ENABLE_LOCKSCREEN_WALLPAPER = true;
-
+	//add by hclydao
+	private final String ACTION_HIDE_NAV_BAR = "com.gzease.action.hide_nav_bar";
+	private final String ACTION_SHOW_NAV_BAR = "com.gzease.action.show_nav_bar";
+	private final String ACTION_HIDE_STATUS_BAR = "com.gzease.action.hide_status_bar";
+	private final String ACTION_SHOW_STATUS_BAR = "com.gzease.action.show_status_bar";
+
+	private boolean isHideNavBar = false;
+	private boolean isHideStatusBar = false;
     static 
         boolean onlyCoreApps;
         try 
@@ -1011,6 +1018,11 @@ public class StatusBar extends SystemUI implements DemoMode,
         IntentFilter filter = new IntentFilter();
         filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
         filter.addAction(Intent.ACTION_SCREEN_OFF);
+		//add by hclydao
+		filter.addAction(ACTION_HIDE_NAV_BAR);
+		filter.addAction(ACTION_SHOW_NAV_BAR);
+		filter.addAction(ACTION_HIDE_STATUS_BAR);
+		filter.addAction(ACTION_SHOW_STATUS_BAR);
         filter.addAction(DevicePolicyManager.ACTION_SHOW_DEVICE_MONITORING_DIALOG);
         context.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null);
 
@@ -2535,7 +2547,7 @@ public class StatusBar extends SystemUI implements DemoMode,
             return deferred;
         , cancelAction, afterKeyguardGone);
     
-
+	
     private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() 
         @Override
         public void onReceive(Context context, Intent intent) 
@@ -2570,7 +2582,34 @@ public class StatusBar extends SystemUI implements DemoMode,
             
             else if (DevicePolicyManager.ACTION_SHOW_DEVICE_MONITORING_DIALOG.equals(action)) 
                 mQSPanel.showDeviceMonitoringDialog();
-            
+             else if(action.equals(ACTION_HIDE_NAV_BAR))  //add by hclydao
+				Log.d(TAG, "ACTION_HIDE_NAV_BAR isHideNavBar: " + isHideNavBar);
+				if(!isHideNavBar) 
+					isHideNavBar = true;
+					mNavigationBarController.onDisplayRemoved(mDisplayId);
+				
+			 else if(action.equals(ACTION_SHOW_NAV_BAR)) 
+				Log.d(TAG, "ACTION_SHOW_NAV_BAR isHideNavBar: " + isHideNavBar);
+				if(isHideNavBar) 
+					isHideNavBar = false;
+					mNavigationBarController.onDisplayReady(mDisplayId);
+				
+			 else if(action.equals(ACTION_HIDE_STATUS_BAR)) 
+				Log.d(TAG, "ACTION_HIDE_STATUS_BAR isHideStatusBar: " + isHideStatusBar);
+				if(!isHideStatusBar) 
+					isHideStatusBar = true;
+					//mStatusBarView.setVisibility(View.GONE);//如果同时要隐藏导航栏和状态栏可以只使用这一个,不会出现bug
+					mStatusBarWindowController.setBarHeight(0);//一定要加这个,不然removeViewImmediate后更新mStatusBarWindowController会出现bug
+					mWindowManager.removeViewImmediate(mStatusBarWindow);
+				
+			 else if(action.equals(ACTION_SHOW_STATUS_BAR)) 
+				Log.d(TAG, "ACTION_SHOW_STATUS_BAR isHideStatusBar: " + isHideStatusBar);
+				if(isHideStatusBar) 
+					isHideStatusBar = false;
+					mStatusBarWindowController.add(mStatusBarWindow, getStatusBarHeight());
+					//mStatusBarView.setVisibility(View.VISIBLE);//如果同时要隐藏导航栏和状态栏可以只使用这一个,不会出现bug
+				
+			 //add end
         
     ;
 
@@ -3666,17 +3705,23 @@ public class StatusBar extends SystemUI implements DemoMode,
     final ScreenLifecycle.Observer mScreenObserver = new ScreenLifecycle.Observer() 
         @Override
         public void onScreenTurningOn() 
+			if(isHideStatusBar) //add by hclydao 使用setVisibility隐藏时不用加
+				return;
             mFalsingManager.onScreenTurningOn();
             mNotificationPanel.onScreenTurningOn();
         
 
         @Override
         public void onScreenTurnedOn() 
+			if(isHideStatusBar) //add by hclydao 使用setVisibility隐藏时不用加
+				return;
             mScrimController.onScreenTurnedOn();
         
 
         @Override
         public void onScreenTurnedOff() 
+			if(isHideStatusBar) //add by hclydao 使用setVisibility隐藏时不用加
+				return;
             mFalsingManager.onScreenOff();
             mScrimController.onScreenTurnedOff();
             updateIsKeyguard();

状态栏显示隐藏说明
如果只使用mStatusBarView.setVisibility(View.GONE);进行隐藏,使用mStatusBarView.setVisibility(View.VISIBLE);进行显示,隐藏时状态栏里的内容是全部进行了隐藏,但状态栏的那个高度还在,还在那占地方,上层应用需要设置为全屏显示才能完全隐藏掉状态栏的位置,即要在上层应用中加上

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);

这种修改不会出现systemui异常的风险。
但是有的客户要求显示导航栏,但隐藏状态栏,此时就不能使用这种方式了,隐藏时需要彻底去掉状态栏,使用这种方式时还需要修改几个地方,不然systemui会出现异常。
使用完全隐藏方式时还需要修改
修改:frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhone.java

@@ -328,6 +328,12 @@ public class HeadsUpManagerPhone extends HeadsUpManager implements Dumpable,
     
 
     private void updateRegionForNotch(Region region) 
+		//add by hclydao
+		if(mStatusBarWindowView == null)
+			return;
+		if(mStatusBarWindowView.getRootWindowInsets() == null)
+			return;
+		//add end
         DisplayCutout cutout = mStatusBarWindowView.getRootWindowInsets().getDisplayCutout();
         if (cutout == null) 
             return;

修改frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowController.java

@@ -322,7 +322,8 @@ public class StatusBarWindowController implements Callback, Dumpable, Configurat
         applyNotTouchable(state);
         applyStatusBarColorSpaceAgnosticFlag(state);
         if (mLp != null && mLp.copyFrom(mLpChanged) != 0) 
-            mWindowManager.updateViewLayout(mStatusBarView, mLp);
+			if(mBarHeight != 0) //add by hclydao
+	            mWindowManager.updateViewLayout(mStatusBarView, mLp);
         
         if (mHasTopUi != mHasTopUiChanged) 
             try 

关闭应用全屏时的提示

修改frameworks/base/services/core/java/com/android/server/wm/ImmersiveModeConfirmation.java

@@ -54,7 +54,7 @@ import android.widget.Button;
 import android.widget.FrameLayout;
 
 import com.android.internal.R;
-
+import android.os.SystemProperties; //add by hclydao
 /**
  *  Helper to manage showing/hiding a confirmation prompt when the navigation bar is hidden
  *  entering immersive mode.
@@ -326,6 +326,9 @@ public class ImmersiveModeConfirmation 
 
             mContext.registerReceiver(mReceiver,
                     new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED));
+			if(!SystemProperties.getBoolean("gzease.fullnotify",false))  //add by hclydao
+				mConfirm.run();//add by hclydao no fullscreen notify
+			
         
 
         @Override

目前测试动态显示隐藏,暂时没有发现什么问题

以上是关于android10动态显示隐藏导航栏,状态栏的主要内容,如果未能解决你的问题,请参考以下文章

android10动态显示隐藏导航栏,状态栏

Android 动态隐藏显示导航栏,状态栏

android 动态控制状态栏显示和隐藏的方法实例

Android 显示、隐藏状态栏和导航栏

Android SystemUI 导航栏动态显示隐藏

Android SystemUI 导航栏动态显示隐藏