当前位置: 首页 > 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);...
}

http://www.xdnf.cn/news/205813.html

相关文章:

  • Go 语言中的 `os.Truncate` 函数详解
  • 2025年4月最新Cursor续杯详细步骤
  • 分治而不割裂—分治协同式敏捷工作模式
  • 若依后台管理系统-v3.8.8-登录模块--个人笔记
  • HarmonyOS NEXT 诗词元服务项目开发上架全流程实战(二、元服务与应用APP签名打包步骤详解)
  • 【工具】Elasticsearch:强大的开源搜索与分析引擎
  • 通信协议:数字世界的隐形语言——从基础认知到工程实践-优雅草卓伊凡
  • Uniapp:设置TabBar
  • HarmonyOS ArkUI安全控件开发指南:粘贴、保存与位置控件的实现与隐私保护实践
  • Android项目中使用ComposeUI
  • GTC Taipei 2025 医疗域前瞻:从AI代理到主权生态,解码医疗健康与生命科学的未来图景
  • DeepSeek提示词技巧
  • 如何防止 ES 被 Linux OOM Killer 杀掉
  • uniapp 支付宝小程序自定义 navbar 无效解决方案
  • 区块链密码学核心
  • 海外社交App的Web3革命:去中心化社交与Token经济实战指南
  • GAEA商业前景和生态系统扩展
  • MongoDB的下载安装与启动
  • 【Linux】服务自启动设置的方式
  • 【计算机网络】面试常考——GET 和 POST 的区别
  • 强化学习之基于模型的算法之动态规划
  • Windows 10系统中找回MySQL 8的root密码
  • stm32 g031g8 flash擦除函数被坑
  • 从SOA到微服务:架构演进之路与实践示例
  • Docker 仓库管理
  • 【Linux网络】深入解析I/O多路转接 - Select
  • 探索PyTorch中的空间与通道双重注意力机制:实现concise的scSE模块
  • HotSpot的算法细节
  • 数据库原理及应用mysql版陈业斌实验三
  • IOS 国际化词条 Python3 脚本