@[TOC](Android11、12 动态禁用(隐藏)Home键)
概述
在实际开发中,导航栏的定制化开发比较常见,属于 SystemUI的修改常客。禁用Home键需求调用接口后隐藏Home键。
动态禁用Home键功能实现核心类
vendor\mediatek\proprietary\packages\apps\SystemUI\src\com\android\systemui\statusbar\phone\NavigationBarFragment.java
vendor\mediatek\proprietary\packages\apps\SystemUI\src\com\android\systemui\statusbar\phone\NavigationBarView.java
动态禁用Home键功能分析
在11、12的android系统中,导航栏的布局文件是NavigationBarView,而很显然,Home键的显示逻辑也在里面实现,实际上Home键是一个自定义的Button按钮。
home.xml
<com.android.systemui.statusbar.policy.KeyButtonView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:systemui="http://schemas.android.com/apk/res-auto"
android:id="@+id/home"
android:layout_width="@dimen/navigation_key_width"
android:layout_height="match_parent"
android:layout_weight="0"
systemui:keyCode="3"
android:scaleType="center"
android:contentDescription="@string/accessibility_home"
android:paddingStart="@dimen/navigation_key_padding"
android:paddingEnd="@dimen/navigation_key_padding"
/>
接下来看Home键在NavigationBarView中初始化
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
mActiveRegion.setEmpty();
updateButtonLocation(getBackButton(), mBackButtonBounds, true);
updateButtonLocation(getHomeButton(), mHomeButtonBounds, false);
updateButtonLocation(getRecentsButton(), mRecentsButtonBounds, false);
updateButtonLocation(getRotateSuggestionButton(), mRotationButtonBounds, true);
// TODO: Handle button visibility changes
mOverviewProxyService.onActiveNavBarRegionChanges(mActiveRegion);
mRecentsOnboarding.setNavBarHeight(getMeasuredHeight());
}
...
public ButtonDispatcher getHomeButton() {
return mButtonDispatchers.get(R.id.home);
}
绘制图标
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = MeasureSpec.getSize(heightMeasureSpec);
if (DEBUG) Log.d(TAG, String.format(
"onMeasure: (%dx%d) old: (%dx%d)", w, h, getMeasuredWidth(), getMeasuredHeight()));
final boolean newVertical = w > 0 && h > w
&& !isGesturalMode(mNavBarMode);
if (newVertical != mIsVertical) {
mIsVertical = newVertical;
if (DEBUG) {
Log.d(TAG, String.format("onMeasure: h=%d, w=%d, vert=%s", h, w,
mIsVertical ? "y" : "n"));
}
reorient();
notifyVerticalChangedListener(newVertical