当前位置: 首页 > news >正文

[Android]导航栏中插入电源菜单

1. 新增 frameworks/base/packages/SystemUI/res/layout/power.xml

<?xml version="1.0" encoding="utf-8"?>
<com.android.systemui.navigationbar.buttons.KeyButtonView xmlns:android="http://schemas.android.com/apk/res/android"xmlns:systemui="http://schemas.android.com/apk/res-auto"android:id="@+id/power"android:layout_width="@dimen/navigation_key_width"android:layout_height="match_parent"android:layout_weight="0"android:contentDescription="@string/accessibility_power"android:paddingStart="@dimen/navigation_key_padding"android:paddingEnd="@dimen/navigation_key_padding"android:scaleType="center"systemui:keyCode="0" />

2. frameworks/base/packages/SystemUI/res/values/config.xml

<!-- Insert power menu in navigation bar default if value is false--><bool name="config_power_menu_show_enable">false</bool>

3.frameworks/base/packages/SystemUI/res/values/strings.xml

<!-- Content description of the power button for accessibility (not shown on the screen). [CHAR LIMIT=NONE] -->
<string name="accessibility_power">Power</string>

4.frameworks/base/packages/SystemUI/src/com/android/systemui/navigationbar/NavigationBar.java

private void prepareNavigationBarView() {mView.reorient();ButtonDispatcher recentsButton = mView.getRecentsButton();recentsButton.setOnClickListener(this::onRecentsClick);recentsButton.setOnTouchListener(this::onRecentsTouch);ButtonDispatcher homeButton = mView.getHomeButton();homeButton.setOnTouchListener(this::onHomeTouch);homeButton.setNavBarButtonClickLogger(mNavBarButtonClickLogger);ButtonDispatcher backButton = mView.getBackButton();backButton.setNavBarButtonClickLogger(mNavBarButtonClickLogger);reconfigureHomeLongClick();ButtonDispatcher accessibilityButton = mView.getAccessibilityButton();accessibilityButton.setOnClickListener(this::onAccessibilityClick);accessibilityButton.setOnLongClickListener(this::onAccessibilityLongClick);updateAccessibilityStateFlags();ButtonDispatcher imeSwitcherButton = mView.getImeSwitchButton();imeSwitcherButton.setOnClickListener(this::onImeSwitcherClick);// Insert a Power menu to the navigation bar 20250411 add startButtonDispatcher powerButton = mView.getPowerButton();powerButton.setOnClickListener(mPowerClickListener);// Insert a Power menu to the navigation bar 20250411 add endupdateScreenPinningGestures();
}// Insert a Power menu to the navigation bar 20250411 add start
private View.OnClickListener mPowerClickListener = new View.OnClickListener() {public void onClick(View v) {Intent intent = new Intent("android.intent.action.POWER_MENU");mContext.sendBroadcast(intent);}
};
//Insert a Power menu to the navigation bar 20250411 add end

5.frameworks/base/packages/SystemUI/src/com/android/systemui/navigationbar/NavigationBarInflaterView.java

// Insert a Power menu to the navigation bar 20250411 add
public static final String POWER = "power";
private Context mContext;
private boolean mPowerMenuShowEnable = false;
public static final int SOHO_NAVIGATION_BAR_SIZE = 4;
// Insert a Power menu to the navigation bar 20250411 endpublic NavigationBarInflaterView(Context context, AttributeSet attrs) {super(context, attrs);// Insert a Power menu to the navigation bar 20250411 addmContext = context;if (mContext != null) {mPowerMenuShowEnable = mContext.getResources().getBoolean(R.bool.config_power_menu_show_enable);}// Insert a Power menu to the navigation bar 20250411 endcreateInflaters();mOverviewProxyService = Dependency.get(OverviewProxyService.class);mListener = new Listener(this);mNavBarMode = Dependency.get(NavigationModeController.class).addListener(mListener);
}protected void inflateLayout(String newLayout) {mCurrentLayout = newLayout;if (newLayout == null) {newLayout = getDefaultLayout();}// Insert a Power menu to the navigation bar 20250411 modify startif (mPowerMenuShowEnable) {String[] sets = newLayout.split(GRAVITY_SEPARATOR, SOHO_NAVIGATION_BAR_SIZE);if (sets.length != SOHO_NAVIGATION_BAR_SIZE) {Log.d(TAG, "Invalid layout.");newLayout = getDefaultLayout();sets = newLayout.split(GRAVITY_SEPARATOR, SOHO_NAVIGATION_BAR_SIZE);}String[] start = sets[0].split(BUTTON_SEPARATOR);String[] center = sets[1].split(BUTTON_SEPARATOR);String[] end = sets[2].split(BUTTON_SEPARATOR);String[] add = sets[3].split(BUTTON_SEPARATOR);inflateButtons(start, mHorizontal.findViewById(R.id.ends_group),false /* landscape */, true /* start */);inflateButtons(start, mVertical.findViewById(R.id.ends_group),true /* landscape */, true /* start */);inflateButtons(center, mHorizontal.findViewById(R.id.ends_group),false /* landscape */, false /* start */);inflateButtons(center, mVertical.findViewById(R.id.ends_group),true /* landscape */, false /* start */);inflateButtons(end, mHorizontal.findViewById(R.id.ends_group),false /* landscape */, false /* start */);inflateButtons(end, mVertical.findViewById(R.id.ends_group),true /* landscape */, false /* start */);inflateButtons(add, mHorizontal.findViewById(R.id.ends_group),false /* landscape */, false /* start */);inflateButtons(add, mVertical.findViewById(R.id.ends_group),true /* landscape */, false /* start */);} else {String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);if (sets.length != 3) {Log.d(TAG, "Invalid layout.");newLayout = getDefaultLayout();sets = newLayout.split(GRAVITY_SEPARATOR, 3);}String[] start = sets[0].split(BUTTON_SEPARATOR);String[] center = sets[1].split(BUTTON_SEPARATOR);String[] end = sets[2].split(BUTTON_SEPARATOR);// Inflate these in start to end order or accessibility traversal will be messed up.inflateButtons(start, mHorizontal.findViewById(R.id.ends_group),false /* landscape */, true /* start */);inflateButtons(start, mVertical.findViewById(R.id.ends_group),true /* landscape */, true /* start */);inflateButtons(center, mHorizontal.findViewById(R.id.center_group),false /* landscape */, false /* start */);inflateButtons(center, mVertical.findViewById(R.id.center_group),true /* landscape */, false /* start */);addGravitySpacer(mHorizontal.findViewById(R.id.ends_group));addGravitySpacer(mVertical.findViewById(R.id.ends_group));inflateButtons(end, mHorizontal.findViewById(R.id.ends_group),false /* landscape */, false /* start */);inflateButtons(end, mVertical.findViewById(R.id.ends_group),true /* landscape */, false /* start */);}// Insert a Power menu to the navigation bar 20250411 modify endupdateButtonDispatchersCurrentView();}View createView(String buttonSpec, ViewGroup parent, LayoutInflater inflater) {View v = null;String button = extractButton(buttonSpec);if (LEFT.equals(button)) {button = extractButton(NAVSPACE);} else if (RIGHT.equals(button)) {button = extractButton(MENU_IME_ROTATE);}if (HOME.equals(button)) {v = inflater.inflate(R.layout.home, parent, false);} else if (BACK.equals(button)) {v = inflater.inflate(R.layout.back, parent, false);} else if (RECENT.equals(button)) {v = inflater.inflate(R.layout.recent_apps, parent, false);} else if (MENU_IME_ROTATE.equals(button)) {v = inflater.inflate(R.layout.menu_ime, parent, false);} else if (NAVSPACE.equals(button)) {v = inflater.inflate(R.layout.nav_key_space, parent, false);} else if (CLIPBOARD.equals(button)) {v = inflater.inflate(R.layout.clipboard, parent, false);} else if (CONTEXTUAL.equals(button)) {v = inflater.inflate(R.layout.contextual, parent, false);} else if (HOME_HANDLE.equals(button)) {v = inflater.inflate(R.layout.home_handle, parent, false);} else if (IME_SWITCHER.equals(button)) {v = inflater.inflate(R.layout.ime_switcher, parent, false);// Insert a Power menu to the navigation bar 20250411 add start} else if (POWER.equals(button)) {v = inflater.inflate(R.layout.power, parent, false);// Insert a Power menu to the navigation bar 20250411 add end} else if (button.startsWith(KEY)) {String uri = extractImage(button);int code = extractKeycode(button);v = inflater.inflate(R.layout.custom_key, parent, false);((KeyButtonView) v).setCode(code);if (uri != null) {if (uri.contains(":")) {((KeyButtonView) v).loadAsync(Icon.createWithContentUri(uri));} else if (uri.contains("/")) {int index = uri.indexOf('/');String pkg = uri.substring(0, index);int id = Integer.parseInt(uri.substring(index + 1));((KeyButtonView) v).loadAsync(Icon.createWithResource(pkg, id));}}}return v;}

6.frameworks/base/packages/SystemUI/src/com/android/systemui/navigationbar/NavigationBarView.java

// Insert a Power menu to the navigation bar 20250411 add
private KeyButtonDrawable mPowerIcon;public NavigationBarView(Context context, AttributeSet attrs) {super(context, attrs);// Insert a Power menu to the navigation bar 20250411 addmButtonDispatchers.put(R.id.power, new ButtonDispatcher(R.id.power));
}// Insert a Power menu to the navigation bar 20250411 add start
public ButtonDispatcher getPowerButton() {return mButtonDispatchers.get(R.id.power);
}
// Insert a Power menu to the navigation bar 20250411 add endprivate void updateIcons(Configuration oldConfig) {final boolean orientationChange = oldConfig.orientation != mConfiguration.orientation;final boolean densityChange = oldConfig.densityDpi != mConfiguration.densityDpi;final boolean dirChange = oldConfig.getLayoutDirection() != mConfiguration.getLayoutDirection();if (orientationChange || densityChange) {mDockedIcon = getDrawable(R.drawable.ic_sysbar_docked);mHomeDefaultIcon = getHomeDrawable();}if (densityChange || dirChange) {mRecentIcon = getDrawable(R.drawable.ic_sysbar_recent);mContextualButtonGroup.updateIcons(mLightIconColor, mDarkIconColor);}if (orientationChange || densityChange || dirChange) {mBackIcon = getBackDrawable();}// Insert a Power menu to the navigation bar 20250411 addmPowerIcon = getDrawable(R.drawable.ic_settings_power);
}public void updateNavButtonIcons() {
...//Insert a Power menu to the navigation bar 20250411 addgetPowerButton().setImageDrawable(mPowerIcon);}

7.frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

void init(Injector injector) {mContext = injector.getContext();...//Insert a Power menu to the navigation bar 20250411 add startIntentFilter mPower = new IntentFilter("android.intent.action.POWER_MENU");mContext.registerReceiver(new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {//show global actions dialogshowGlobalActionsInternal();}}, mPower);//Insert a Power menu to the navigation bar 20250411 add end// register for multiuser-relevant broadcastsfilter = new IntentFilter(Intent.ACTION_USER_SWITCHED);mContext.registerReceiver(mMultiuserReceiver, filter);...
}

相关文章:

  • 【深度学习新浪潮】ISP芯片算法技术简介及关键技术分析
  • 强化学习之基于无模型的算法之蒙特卡洛方法
  • 【题解-Acwing】871. 约数之和
  • LeetCode 2962.统计最大元素出现至少 K 次的子数组:滑动窗口
  • QT控件 参考Qt的PIMPL设计模式实现使用QWidget控件绘制3D饼状图表和3D柱状图表,使用QChartView绘制圆柱体图表
  • 论文导读 - 基于特征融合的电子鼻多任务深度学习模型研究
  • 从 0 到 1:ComfyUI AI 工作流抠图构建全实践
  • Redis核心与底层实现场景题深度解析
  • C++函数模板基础
  • A2A与MCP:理解它们的区别以及何时使用
  • 机器学习实操 第一部分 机器学习基础 第5章 支持向量机(SVM)
  • 【行业特化篇3】制造业简历优化指南:技术参数与标准化流程的关键词植入艺术
  • 【Linux】第十三章 访问Linux文件系统
  • 【和春笋一起学C++】函数——C++的编程模块
  • 第十六届蓝桥杯 2025 C/C++组 旗帜
  • 蓝桥杯 10. 凯撒加密
  • Pytest中的fixture装饰器详解
  • 优化PCB Via Stub系列(2) – 运用U-Turn Via设计破解阻抗匹配困境,改善信号完整性
  • android开发中的多线程、数据存储同步功能实现方案和应用场景
  • 人事管理系统6
  • 辽宁省委书记郝鹏、省长王新伟赶到辽阳火灾事故现场指导善后处置工作
  • 长三角议事厅·周报|长三角游戏出海,关键在“生态输出”
  • 最近这75年,谁建造了上海?
  • 江西省国资委原副主任李键主动向组织交代问题,接受审查调查
  • 上海“生育友好岗”已让4000余人受益,今年将推产假社保补贴政策
  • 一季度全国城镇新增就业308万人