Android11动态设置显示隐藏导航栏
1.setting中添加一个显示导航栏的开关控件,可以参考自动旋转屏幕这个做法来添加:
路径:packages/apps/Settings
1.1 布局xml文件:
diff --git a/res/values-zh-rCN/strings.xml b/res/values-zh-rCN/strings.xml old mode 100644 new mode 100755 index f93320c..a63588a --- a/res/values-zh-rCN/strings.xml +++ b/res/values-zh-rCN/strings.xml @@ -4972,6 +4972,10 @@ <string name="screenshot_version">截屏管理器版本</string> <string name="later">秒以后截屏</string> + <!--navigation_bar--> + <string name="system_navibar">显示导航栏</string> + <string name="system_navibar_tip">勾选打开导航栏</string> + <!--hdmi settings--> <string name="system_rotation">"系统方向"</string> <string name="screen_title">"屏幕设置"</string> diff --git a/res/values/strings.xml b/res/values/strings.xml old mode 100644 new mode 100755 index cdcb81f..11067d9 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -12243,6 +12243,10 @@ <string name="screenshot_version">Show the version of screenshot manager.</string> <string name="later">s later capture</string> + <!--navigation_bar--> + <string name="system_navibar" >navi_bar</string> + <string name="system_navibar_tip" >Select navi_bar</string> + <!--hdmi settings--> <string name="hdmi_settings">HDMI</string> <string name="system_rotation">System Direction</string> diff --git a/res/xml/display_settings.xml b/res/xml/display_settings.xml old mode 100644 new mode 100755 index 77ff247..842e1e5 --- a/res/xml/display_settings.xml +++ b/res/xml/display_settings.xml @@ -71,6 +71,12 @@ android:entryValues="@array/dream_timeout_values" settings:keywords="@string/keywords_screen_timeout" /> + <SwitchPreference + android:key="system_navi_bar_preference" + android:title="@string/system_navibar" + settings:keywords="@string/system_navibar_tip" + settings:controller="com.android.settings.display.NavigationBarController" /> + <Preference android:key="adaptive_sleep_entry" android:title="@string/adaptive_sleep_title"
1.2 Java代码:
diff --git a/src/com/android/settings/display/NavigationBarController.java b/src/com/android/settings/display/NavigationBarController.java new file mode 100755 index 0000000..83bdacd --- /dev/null +++ b/src/com/android/settings/display/NavigationBarController.java /* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package com.android.settings.display; import android.app.settings.SettingsEnums; import android.content.Context; import android.text.TextUtils; import androidx.preference.Preference; import com.android.internal.view.RotationPolicy; import com.android.settings.core.PreferenceControllerMixin; import com.android.settings.core.TogglePreferenceController; import com.android.settings.overlay.FeatureFactory; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import com.android.settingslib.core.lifecycle.LifecycleObserver; import android.util.Log; import android.os.UserHandle; import android.content.Intent; import android.os.SystemProperties; public class NavigationBarController extends TogglePreferenceController implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener{ private final MetricsFeatureProvider mMetricsFeatureProvider; private Preference mPreference; private RotationPolicy.RotationPolicyListener mRotationPolicyListener; public NavigationBarController(Context context, String key) { super(context, key); mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); } @Override public void updateState(Preference preference) { mPreference = preference; super.updateState(preference); } @Override public int getAvailabilityStatus() { return RotationPolicy.isRotationLockToggleVisible(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE; } @Override public boolean isSliceable() { return TextUtils.equals(getPreferenceKey(), "system_navi_bar_preference"); } @Override public boolean isPublicSlice() { return true; } // @Override // public boolean isChecked() { // return !RotationPolicy.isRotationLocked(mContext); // } public boolean isChecked() { boolean isChecked; String nvbval = SystemProperties.get("persist.sys.nvbshow"); if("1".equals(nvbval)){ isChecked = true; }else{ isChecked = false; } return isChecked; } @Override public boolean setChecked(boolean isChecked) { boolean value = isChecked; if (value == false) { Log.d("wmc", "hide----navigation"); SystemProperties.set("persist.sys.nvbshow", "0"); Intent intent = new Intent(); intent.setAction("com.android.navigation.status"); intent.putExtra("hide", true); mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT); } else{ Log.d("wmc", "show----navigation"); SystemProperties.set("persist.sys.nvbshow", "1"); Intent intent = new Intent(); intent.setAction("com.android.navigation.status"); intent.putExtra("hide", false); mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT); } return true; } }
这里面实现了按钮的开关,发送相对应的广播给framework层,实现导航栏的显示或者隐藏。添加完成设置中显示如下:
2.framework层的实现:
2.1 增加一个移除导航栏方法:
NavigationBarController.java有一个onDisplayRemoved方法:
@Override public void onDisplayRemoved(int displayId) { removeNavigationBar(displayId); }
这个是删除导航栏的,但需要传入一个id号,所以可以构造一个类似的函数:
private int mdisplayId; public void removeNavigationBarView() { removeNavigationBar(mdisplayId); }
定义一个全局的mdisplayId放在createNavigationBar中赋值即可:
![](https://img2022.cnblogs.com/blog/1461604/202206/1461604-20220614102930140-1866008791.png)
2.1 增加监听的广播:
@@ -2770,6 +2776,8 @@ public class StatusBar extends SystemUI implements DemoMode, }, cancelAction, afterKeyguardGone); } + RegisterStatusBarResult mresult; + private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -2805,6 +2813,20 @@ public class StatusBar extends SystemUI implements DemoMode, else if (DevicePolicyManager.ACTION_SHOW_DEVICE_MONITORING_DIALOG.equals(action)) { mQSPanel.showDeviceMonitoringDialog(); } + else if(intent.getAction().equals("com.android.navigation.status")){ + if(intent.getBooleanExtra("hide",false)) + { + NavigationBarView mNavigationBarView = mNavigationBarController.getDefaultNavigationBarView(); + if(mNavigationBarView == null) return; + mNavigationBarController.removeNavigationBarView(); + } + else + { + NavigationBarView mNavigationBarView = mNavigationBarController.getDefaultNavigationBarView(); + if(mNavigationBarView != null) return; + createNavigationBar(mresult); + } + } } };
接受到广播做相应的隐藏或者显示处理。
3.开机初始化显示/隐藏导航栏:
在makeStatusBarView方法中添加判断是否显示导航栏:
index f125b7d..c20bce1 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java @@ -1099,7 +1099,12 @@ public class StatusBar extends SystemUI implements DemoMode, mNotificationPanelViewController.setHeadsUpManager(mHeadsUpManager); mNotificationLogger.setHeadsUpManager(mHeadsUpManager); - createNavigationBar(result); + mresult = result; + + String nvbval = SystemProperties.get("persist.sys.nvbshow"); + if("1".equals(nvbval)){ + createNavigationBar(result); + } if (ENABLE_LOCKSCREEN_WALLPAPER && mWallpaperSupported) { mLockscreenWallpaper = mLockscreenWallpaperLazy.get();
其中persist.sys.nvbshow这个属性的定义是从build.prop读取的。
device/rockchip/rk356x
diff --git a/rk3568_r/rk3568_r.mk b/rk3568_r/rk3568_r.mk index 0d04028..96b0493 100755 --- a/rk3568_r/rk3568_r.mk +++ b/rk3568_r/rk3568_r.mk @@ -46,3 +46,4 @@ PRODUCT_PROPERTY_OVERRIDES += persist.wifi.sleep.delay.ms=0 PRODUCT_PROPERTY_OVERRIDES += persist.bt.power.down=true PRODUCT_PROPERTY_OVERRIDES += persist.sys.rotation.einit-1=1 PRODUCT_PROPERTY_OVERRIDES += persist.sys.rotation.efull-1=true +PRODUCT_PROPERTY_OVERRIDES += persist.sys.nvbshow=0
rk3568_r.mk定义的这些属性编译后会集成到build.prop中