Android 12.0 App全屏时通过系统属性控制上划下拉是否显示虚拟导航栏和状态栏

遇到问题

厂测App触摸屏测试,需要全屏显示,禁止出现状态栏和导航栏,以免影响测试。

参考资料

https://zhuanlan.zhihu.com/p/440810585

Android 12系统源码_SystemUI(九)WindowInsetsController源码解析-CSDN博客

解决方案

1、framework修改点

+++ b/frameworks/base/services/core/java/com/android/server/wm/DisplayPolicy.java
@@ -454,6 +454,10 @@ public class DisplayPolicy {
                     public void onSwipeFromTop() {
                         synchronized (mLock) {
                             if (mStatusBar != null) {
+                                if(SystemProperties.getBoolean("persist.sys.tpw.touchtest", false)){
+                                    Slog.i(TAG, "touchtest onSwipeFromTop return");
+                                    return;
+                                }
                                 requestTransientBars(mStatusBar);
                             }
                             checkAltBarSwipeForTransientBars(ALT_BAR_TOP);
@@ -465,6 +469,10 @@ public class DisplayPolicy {
                         synchronized (mLock) {
                             if (mNavigationBar != null
                                     && mNavigationBarPosition == NAV_BAR_BOTTOM) {
+                                if(SystemProperties.getBoolean("persist.sys.tpw.touchtest", false)){
+                                    Slog.i(TAG, "touchtest onSwipeFromBottom return");
+                                    return;
+                                }
                                 requestTransientBars(mNavigationBar);
                             }
                             checkAltBarSwipeForTransientBars(ALT_BAR_BOTTOM);

2、app修改

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        ActionBar actionBar = getActionBar();
        if(actionBar != null) actionBar.hide();

        setContentView(R.layout.activity_main);

    }

    @Override
    protected void onResume() {
        super.onResume();
        //获取WindowInsetsController对象实例
        WindowInsetsController windowInsetsController = getWindow().getDecorView().getWindowInsetsController();
        //调用hide隐藏状态栏
        windowInsetsController.hide(WindowInsets.Type.statusBars());
        //调用hide隐藏导航栏
        windowInsetsController.hide(WindowInsets.Type.navigationBars());

		//设置属性值禁止手势动作
		SystemProperties.set("persist.sys.tpw.touchtest", "1");
    }

    @Override
    protected void onPause() {
        super.onPause();
        //获取WindowInsetsController对象实例
        WindowInsetsController windowInsetsController = getWindow().getDecorView().getWindowInsetsController();
        //调用show显示状态栏
        windowInsetsController.show(WindowInsets.Type.statusBars());
        //调用show显示导航栏
        windowInsetsController.show(WindowInsets.Type.navigationBars());

        //设置属性值恢复手势动作
		SystemProperties.set("persist.sys.tpw.touchtest", "0");
    }

你可能感兴趣的:(android)