本文主要是介绍Android12 添加设置控制导航栏显示和状态栏下拉,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
添加key值
- 位置: frameworks/base/core/java/android/provider/Settings.java
/*** Control navigation bar enabled*/@Readable@SuppressLint("NoSettingsProvider")public static final String NAVIGATIONBAR_ENABLED = "navigationbar_enabled";/*** Supoort panel bar expand*/@Readable@SuppressLint("NoSettingsProvider")public static final String SUPPORT_PANELBAR_EXPAND = "support_panelbar_expand";
Setting中添加相关设置项
- 位置:packages/apps/Settings/res/xml/display_settings.xml
<SwitchPreferenceandroid:key="navigationbar_enabled"android:title="@string/navigationbar_enabled_title"settings:controller="com.android.settings.display.NavigationbarPreferenceController"/><SwitchPreferenceandroid:key="support_panelbar_expand"android:title="@string/support_panelbar_expand_title"settings:controller="com.android.settings.display.SupportPanelbarExpandPreferenceController"/>
- 位置:packages/apps/Settings/res/values/strings.xml
<string name="navigationbar_enabled_title">Show navigation bar</string><string name="support_panelbar_expand_title">Allows status bar drop-down</string>
- 位置:packages/apps/Settings/src/com/android/settings/display/NavigationbarPreferenceController.java
package com.android.settings.display;import android.content.Context;
import android.text.TextUtils;import androidx.preference.Preference;import com.android.settings.core.TogglePreferenceController;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;import android.provider.Settings;public class NavigationbarPreferenceController extends TogglePreferenceController implementsPreference.OnPreferenceChangeListener {private static final String KEY_NAVIGATIONBAR = "navigationbar_enabled";private Context mContext;private Preference mPreference;public NavigationbarPreferenceController(Context context) {super(context, KEY_NAVIGATIONBAR);mContext = context;}@Overridepublic String getPreferenceKey() {return KEY_NAVIGATIONBAR;}@Overridepublic void updateState(Preference preference) {mPreference = preference;super.updateState(preference);}@Overridepublic int getAvailabilityStatus() {return AVAILABLE/* : UNSUPPORTED_ON_DEVICE*/;}@Overridepublic boolean isChecked() {int status = Settings.System.getInt(mContext.getContentResolver(), Settings.System.NAVIGATIONBAR_ENABLED, 0);return status==1;}@Overridepublic boolean setChecked(boolean isChecked) {Settings.System.putInt(mContext.getContentResolver(), Settings.System.NAVIGATIONBAR_ENABLED, isChecked ? 1:0);return true;}
}
在屏幕顶部和中间下滑都可以拉出状态栏,所以有两个地方需要修改。你也可以直接在CommandQueue.java的panelsEnabled()方法中做控制
- 位置:frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarView.java
@Overridepublic boolean onTouchEvent(MotionEvent event) {// add startint supportExpand = Settings.System.getInt(mContext.getContentResolver(), Settings.System.SUPPORT_PANELBAR_EXPAND, 0);if (supportExpand == 0){return true;}// endboolean barConsumedEvent = mBar.interceptTouchEvent(event);if (DEBUG_GESTURES) {if (event.getActionMasked() != MotionEvent.ACTION_MOVE) {EventLog.writeEvent(EventLogTags.SYSUI_PANELBAR_TOUCH,event.getActionMasked(), (int) event.getX(), (int) event.getY(),barConsumedEvent ? 1 : 0);}}return barConsumedEvent || super.onTouchEvent(event);}
- 位置:packages/apps/Launcher3/quickstep/src/com/android/launcher3/uioverrides/touchcontrollers/StatusBarTouchController.java
private void dispatchTouchEvent(MotionEvent ev) {// add startint supportExpand = Settings.System.getInt(mLauncher.getContentResolver(), Settings.System.SUPPORT_PANELBAR_EXPAND, 0);if (mSystemUiProxy.isActive() && supportExpand==1) {// add endmLastAction = ev.getActionMasked();mSystemUiProxy.onStatusBarMotionEvent(ev);}}
导航栏显示的控制
- frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
// add startimport android.content.ContentResolver;import android.database.ContentObserver;import android.provider.Settings;// end...// add startprivate final Object mLock = new Object();private SettingsObserver mSettingsObserver;private boolean mSupportPanelExpand;private boolean mShowNavigationbar;// end...// add startprivate final class SettingsObserver extends ContentObserver {public SettingsObserver(Handler handler) {super(handler);}@Overridepublic void onChange(boolean selfChange, Uri uri) {synchronized (mLock) {updateSettingsLocked();if (mShowNavigationbar){createNavigationBar(null);} else {removeNavigationBar();}}}}private void updateSettingsLocked() {final ContentResolver resolver = mContext.getContentResolver();mShowNavigationbar = (Settings.System.getInt(resolver,Settings.System.NAVIGATIONBAR_ENABLED, 0)) == 1;} // add end// ================================================================================// Constructing the view// ================================================================================protected void makeStatusBarView(@Nullable RegisterStatusBarResult result) {final Context context = mContext;updateDisplaySize(); // populates mDisplayMetricsupdateResources();updateTheme();...// add start// Register for settings changes.mSettingsObserver = new SettingsObserver(mHandler);final ContentResolver resolver = mContext.getContentResolver();resolver.registerContentObserver(Settings.System.getUriFor(Settings.System.NAVIGATIONBAR_ENABLED),false, mSettingsObserver);updateSettingsLocked();if (mShowNavigationbar){createNavigationBar(result);}// add end}...protected void createNavigationBar(@Nullable RegisterStatusBarResult result) {mNavigationBarController.createNavigationBars(true /* includeDefaultDisplay */, result);}// add startprotected void removeNavigationBar() {mNavigationBarController.removeNavigationBar();}// add end
- 位置:frameworks/base/packages/SystemUI/src/com/android/systemui/navigationbar/NavigationBarController.java
// add startpublic void removeNavigationBar() {Display[] displays = mDisplayManager.getDisplays();for (Display display : displays) {removeNavigationBar(display.getDisplayId());}}// add end
配置默认值
- 位置:frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
private void loadSystemSettings(SQLiteDatabase db) {SQLiteStatement stmt = null;try {stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"+ " VALUES(?,?);");loadBooleanSetting(stmt, Settings.System.DIM_SCREEN,R.bool.def_dim_screen);loadIntegerSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,R.integer.def_screen_off_timeout);// add startloadBooleanSetting(stmt, Settings.System.NAVIGATIONBAR_ENABLED,R.bool.def_navigationbar_enabled);loadBooleanSetting(stmt, Settings.System.SUPPORT_PANELBAR_EXPAND,R.bool.def_support_panelbar_expand);// end ...}
- frameworks/base/packages/SettingsProvider/res/values/defaults.xml
// add start<bool name="def_navigationbar_enabled">false</bool><bool name="def_support_panelbar_expand">false</bool>// end
这篇关于Android12 添加设置控制导航栏显示和状态栏下拉的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!