Android Settings 按住电源按钮

如题,Android 原生 Settings 里有个 按住电源按钮 的选项,可以设置按住电源按钮的操作。
在这里插入图片描述

按住电源按钮

两个选项的 UI 是分离的,

电源菜单

代码在 packages/apps/Settings/src/com/android/settings/gestures/LongPressPowerForPowerMenuPreferenceController.java

/** Copyright (C) 2022 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.gestures;import android.content.Context;
import android.net.Uri;import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.widget.SelectorWithWidgetPreference;/*** Configures the behaviour of the radio selector to configure long press power button to Power* Menu.*/
public class LongPressPowerForPowerMenuPreferenceController extends BasePreferenceControllerimplements PowerMenuSettingsUtils.SettingsStateCallback,SelectorWithWidgetPreference.OnClickListener,LifecycleObserver {private SelectorWithWidgetPreference mPreference;private final PowerMenuSettingsUtils mUtils;public LongPressPowerForPowerMenuPreferenceController(Context context, String key) {super(context, key);mUtils = new PowerMenuSettingsUtils(context);}@Overridepublic int getAvailabilityStatus() {return PowerMenuSettingsUtils.isLongPressPowerSettingAvailable(mContext)? AVAILABLE: UNSUPPORTED_ON_DEVICE;}@Overridepublic void displayPreference(PreferenceScreen screen) {super.displayPreference(screen);mPreference = screen.findPreference(getPreferenceKey());if (mPreference != null) {mPreference.setOnClickListener(this);}}@Overridepublic void updateState(Preference preference) {super.updateState(preference);if (preference instanceof SelectorWithWidgetPreference) {((SelectorWithWidgetPreference) preference).setChecked(!PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext));}}@Overridepublic void onRadioButtonClicked(SelectorWithWidgetPreference preference) {PowerMenuSettingsUtils.setLongPressPowerForPowerMenu(mContext);if (mPreference != null) {updateState(mPreference);}}@Overridepublic void onChange(Uri uri) {if (mPreference != null) {updateState(mPreference);}}/** @OnLifecycleEvent(Lifecycle.Event.ON_START) */@OnLifecycleEvent(Lifecycle.Event.ON_START)public void onStart() {mUtils.registerObserver(this);}/** @OnLifecycleEvent(Lifecycle.Event.ON_STOP) */@OnLifecycleEvent(Lifecycle.Event.ON_STOP)public void onStop() {mUtils.unregisterObserver();}
}

关键代码,

    @Overridepublic void onRadioButtonClicked(SelectorWithWidgetPreference preference) {PowerMenuSettingsUtils.setLongPressPowerForPowerMenu(mContext);if (mPreference != null) {updateState(mPreference);}}

数字助理

代码在 packages/apps/Settings/src/com/android/settings/gestures/LongPressPowerForAssistantPreferenceController.java

/** Copyright (C) 2022 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.gestures;import android.content.Context;
import android.net.Uri;import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.widget.SelectorWithWidgetPreference;/*** Configures the behaviour of the radio selector to configure long press power button to Assistant.*/
public class LongPressPowerForAssistantPreferenceController extends BasePreferenceControllerimplements PowerMenuSettingsUtils.SettingsStateCallback,SelectorWithWidgetPreference.OnClickListener,LifecycleObserver {private SelectorWithWidgetPreference mPreference;private final PowerMenuSettingsUtils mUtils;public LongPressPowerForAssistantPreferenceController(Context context, String key) {super(context, key);mUtils = new PowerMenuSettingsUtils(context);}@Overridepublic int getAvailabilityStatus() {return PowerMenuSettingsUtils.isLongPressPowerSettingAvailable(mContext)? AVAILABLE: UNSUPPORTED_ON_DEVICE;}@Overridepublic void displayPreference(PreferenceScreen screen) {super.displayPreference(screen);mPreference = screen.findPreference(getPreferenceKey());if (mPreference != null) {mPreference.setOnClickListener(this);}}@Overridepublic void updateState(Preference preference) {super.updateState(preference);if (preference instanceof SelectorWithWidgetPreference) {((SelectorWithWidgetPreference) preference).setChecked(PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext));}}@Overridepublic void onRadioButtonClicked(SelectorWithWidgetPreference preference) {PowerMenuSettingsUtils.setLongPressPowerForAssistant(mContext);if (mPreference != null) {updateState(mPreference);}}@Overridepublic void onChange(Uri uri) {if (mPreference != null) {updateState(mPreference);}}/** @OnLifecycleEvent(Lifecycle.Event.ON_START) */@OnLifecycleEvent(Lifecycle.Event.ON_START)public void onStart() {mUtils.registerObserver(this);}/** @OnLifecycleEvent(Lifecycle.Event.ON_STOP) */@OnLifecycleEvent(Lifecycle.Event.ON_STOP)public void onStop() {mUtils.unregisterObserver();}
}

关键代码,

    @Overridepublic void onRadioButtonClicked(SelectorWithWidgetPreference preference) {PowerMenuSettingsUtils.setLongPressPowerForAssistant(mContext);if (mPreference != null) {updateState(mPreference);}}

功能设置

实际设置是在 packages/apps/Settings/src/com/android/settings/gestures/PowerMenuSettingsUtils.java

    private static final String POWER_BUTTON_LONG_PRESS_SETTING =Settings.Global.POWER_BUTTON_LONG_PRESS;private static final int LONG_PRESS_POWER_GLOBAL_ACTIONS = 1; // a.k.a., Power Menuprivate static final int LONG_PRESS_POWER_ASSISTANT_VALUE = 5; // Settings.Secure.ASSISTANT// ... public static boolean setLongPressPowerForAssistant(Context context) {if (Settings.Global.putInt(context.getContentResolver(),POWER_BUTTON_LONG_PRESS_SETTING,LONG_PRESS_POWER_ASSISTANT_VALUE)) {// Make power + volume up buttons to open the power menuSettings.Global.putInt(context.getContentResolver(),KEY_CHORD_POWER_VOLUME_UP_SETTING,KEY_CHORD_POWER_VOLUME_UP_GLOBAL_ACTIONS);return true;}return false;}public static boolean setLongPressPowerForPowerMenu(Context context) {if (Settings.Global.putInt(context.getContentResolver(),POWER_BUTTON_LONG_PRESS_SETTING,LONG_PRESS_POWER_GLOBAL_ACTIONS)) {// We restore power + volume up buttons to the default action.int keyChordDefaultValue =context.getResources().getInteger(KEY_CHORD_POWER_VOLUME_UP_DEFAULT_VALUE_RESOURCE);Settings.Global.putInt(context.getContentResolver(),KEY_CHORD_POWER_VOLUME_UP_SETTING,keyChordDefaultValue);return true;}return false;}

追踪初始化逻辑

android.provider.Settings 相关调用的初始化一般都在 SettingsProvider 里,

找到 frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java

    /*** Correctly sets long press power button Behavior.** The issue is that setting for LongPressPower button Behavior is not available on all devices* and actually changes default Behavior of two properties - the long press power button* and volume up + power button combo. OEM can also reconfigure these Behaviors in config.xml,* so we need to be careful that we don't irreversibly change power button Behavior when* restoring. Or switch to a non-default button Behavior.*/private void setLongPressPowerBehavior(ContentResolver cr, String value) {// We will not restore the value if the long press power setting option is unavailable.if (!mContext.getResources().getBoolean(com.android.internal.R.bool.config_longPressOnPowerForAssistantSettingAvailable)) {return;}int longPressOnPowerBehavior;try {longPressOnPowerBehavior = Integer.parseInt(value);} catch (NumberFormatException e) {return;}if (longPressOnPowerBehavior < LONG_PRESS_POWER_NOTHING|| longPressOnPowerBehavior > LONG_PRESS_POWER_FOR_ASSISTANT) {return;}// When user enables long press power for Assistant, we also switch the meaning// of Volume Up + Power key chord to the "Show power menu" option.// If the user disables long press power for Assistant, we switch back to default OEM// Behavior configured in config.xml. If the default Behavior IS "LPP for Assistant",// then we fall back to "Long press for Power Menu" Behavior.if (longPressOnPowerBehavior == LONG_PRESS_POWER_FOR_ASSISTANT) {Settings.Global.putInt(cr, Settings.Global.POWER_BUTTON_LONG_PRESS,LONG_PRESS_POWER_FOR_ASSISTANT);Settings.Global.putInt(cr, Settings.Global.KEY_CHORD_POWER_VOLUME_UP,KEY_CHORD_POWER_VOLUME_UP_GLOBAL_ACTIONS);} else {// We're restoring "LPP for Assistant Disabled" state, prefer OEM config.xml Behavior// if possible.int longPressOnPowerDeviceBehavior = mContext.getResources().getInteger(com.android.internal.R.integer.config_longPressOnPowerBehavior);if (longPressOnPowerDeviceBehavior == LONG_PRESS_POWER_FOR_ASSISTANT) {// The default on device IS "LPP for Assistant Enabled" so fall back to power menu.Settings.Global.putInt(cr, Settings.Global.POWER_BUTTON_LONG_PRESS,LONG_PRESS_POWER_GLOBAL_ACTIONS);} else {// The default is non-Assistant Behavior, so restore that default.Settings.Global.putInt(cr, Settings.Global.POWER_BUTTON_LONG_PRESS,longPressOnPowerDeviceBehavior);}// Clear and restore default power + volume up Behavior as well.int powerVolumeUpDefaultBehavior = mContext.getResources().getInteger(com.android.internal.R.integer.config_keyChordPowerVolumeUp);Settings.Global.putInt(cr, Settings.Global.KEY_CHORD_POWER_VOLUME_UP,powerVolumeUpDefaultBehavior);}}

找到 config_longPressOnPowerBehavior ,定义在 frameworks/base/core/res/res/values/config.xml

    <!-- Control the behavior when the user long presses the power button.0 - Nothing1 - Global actions menu2 - Power off (with confirmation)3 - Power off (without confirmation)4 - Go to voice assist5 - Go to assistant (Settings.Secure.ASSISTANT)--><integer name="config_longPressOnPowerBehavior">5</integer>

按住电源按钮的持续时间

代码在 packages/apps/Settings/src/com/android/settings/gestures/LongPressPowerSensitivityPreferenceController.java

/** Copyright (C) 2021 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.gestures;import android.content.Context;
import android.net.Uri;
import android.provider.Settings;import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;import com.android.settings.core.SliderPreferenceController;
import com.android.settings.widget.LabeledSeekBarPreference;/** Handles changes to the long press power button sensitivity slider. */
public class LongPressPowerSensitivityPreferenceController extends SliderPreferenceControllerimplements PowerMenuSettingsUtils.SettingsStateCallback, LifecycleObserver {@Nullableprivate final int[] mSensitivityValues;private final PowerMenuSettingsUtils mUtils;@Nullableprivate LabeledSeekBarPreference mPreference;public LongPressPowerSensitivityPreferenceController(Context context, String preferenceKey) {super(context, preferenceKey);mSensitivityValues = context.getResources().getIntArray(com.android.internal.R.array.config_longPressOnPowerDurationSettings);mUtils = new PowerMenuSettingsUtils(context);}/** @OnLifecycleEvent(Lifecycle.Event.ON_START) */@OnLifecycleEvent(Lifecycle.Event.ON_START)public void onStart() {mUtils.registerObserver(this);}/** @OnLifecycleEvent(Lifecycle.Event.ON_STOP) */@OnLifecycleEvent(Lifecycle.Event.ON_STOP)public void onStop() {mUtils.unregisterObserver();}@Overridepublic void displayPreference(PreferenceScreen screen) {super.displayPreference(screen);mPreference = screen.findPreference(getPreferenceKey());if (mPreference != null) {mPreference.setContinuousUpdates(false);mPreference.setHapticFeedbackMode(LabeledSeekBarPreference.HAPTIC_FEEDBACK_MODE_ON_TICKS);mPreference.setMin(getMin());mPreference.setMax(getMax());}}@Overridepublic void updateState(Preference preference) {super.updateState(preference);final LabeledSeekBarPreference pref = (LabeledSeekBarPreference) preference;pref.setVisible(PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext)&& getAvailabilityStatus() == AVAILABLE);pref.setProgress(getSliderPosition());}@Overridepublic int getAvailabilityStatus() {if (mSensitivityValues == null|| mSensitivityValues.length < 2|| !PowerMenuSettingsUtils.isLongPressPowerSettingAvailable(mContext)) {return UNSUPPORTED_ON_DEVICE;}return AVAILABLE;}@Overridepublic int getSliderPosition() {return mSensitivityValues == null ? 0 : closestValueIndex(mSensitivityValues,getCurrentSensitivityValue());}@Overridepublic boolean setSliderPosition(int position) {if (mSensitivityValues == null || position < 0 || position >= mSensitivityValues.length) {return false;}return Settings.Global.putInt(mContext.getContentResolver(),Settings.Global.POWER_BUTTON_LONG_PRESS_DURATION_MS,mSensitivityValues[position]);}@Overridepublic void onChange(Uri uri) {if (mPreference != null) {updateState(mPreference);}}@Overridepublic int getMax() {if (mSensitivityValues == null || mSensitivityValues.length == 0) {return 0;}return mSensitivityValues.length - 1;}@Overridepublic int getMin() {return 0;}private int getCurrentSensitivityValue() {return Settings.Global.getInt(mContext.getContentResolver(),Settings.Global.POWER_BUTTON_LONG_PRESS_DURATION_MS,mContext.getResources().getInteger(com.android.internal.R.integer.config_longPressOnPowerDurationMs));}private static int closestValueIndex(int[] values, int needle) {int minDistance = Integer.MAX_VALUE;int valueIndex = 0;for (int i = 0; i < values.length; i++) {int diff = Math.abs(values[i] - needle);if (diff < minDistance) {minDistance = diff;valueIndex = i;}}return valueIndex;}
}

R.array.config_longPressOnPowerDurationSettings 定义在 frameworks/base/core/res/res/values/config.xml

    <!-- The possible UI options to be surfaced for configuring long press power on durationaction. Value set in config_longPressOnPowerDurationMs should be one of the availableoptions to allow users to restore default. --><integer-array name="config_longPressOnPowerDurationSettings"><item>250</item><item>350</item><item>500</item><item>650</item><item>750</item></integer-array>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/438533.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

存储技术架构演进

一. 演进过程 存储技术架构的演进主要是从集中式到分布式的一种呈现&#xff0c;集中式存储模式凭借其在稳定性和可靠性方面的优势成为许多业务数据库的数据存储首选&#xff0c;顾名思义&#xff0c;集中式存储主要体现在集中性&#xff0c;一套集中式管理的存储系统&#xff…

SpringBoot项目配置SSL后,WebSocket连接失败的解决方案

SpringBoot项目配置SSL后&#xff0c;WebSocket连接应使用wss协议&#xff0c;而不是ws协议。在前端配置WebSocket时&#xff0c;URL以wss://开头。

基于springboot的房屋交易系统

文章目录 项目介绍主要功能截图&#xff1a;部分代码展示设计总结项目获取方式 &#x1f345; 作者主页&#xff1a;超级无敌暴龙战士塔塔开 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、 简历模板、学习资料、面试题库【关注我&#xff0c;都给你】 &…

批量修改图斑起始点为左上角节点(顺时针方向排列),其他方位的起始点在本案例基础上微调即可实现

目录 一、实现效果 二、实现过程 1.修改图斑节点方向 2.获取图斑左上角节点 3.重新计算图斑节点顺序 4.修改图斑的起始点到左上角 5.模板的使用 三、总结 使用FME对图斑进行批量起始点修改&#xff0c;将起始点修改到图斑的左上角&#xff0c;并且节点方向统一为顺时针…

处理Servlet生命周期事件

处理Servlet生命周期事件 接收关于 Servlet生命周期事件通知的类称为事件侦听器。这些侦听器实现Servlet API中定义的一个或多个servlet事件侦听器接口。侦听器类的逻辑分类如下: servlet请求侦听器Servlet上下文侦听器HTTP会话侦听器1. servlet请求侦听器 servlet请求侦听器…

基于springboot药房管理系统源码和论文

伴随着全球信息化发展&#xff0c;行行业业都与计算机技术相衔接&#xff0c;计算机技术普遍运用于药房管理行业。实施计算机系统来管理可以降低逍遥大药房管理成本&#xff0c;使整个逍遥大药房行业的发展有显著提升。 本论文主要面向逍遥大药房管理中出现的一些常见问题&…

Java基础数据结构之哈希表

概念 顺序结构以及平衡树 中&#xff0c;元素关键码与其存储位置之间没有对应的关系&#xff0c;因此在 查找一个元素时&#xff0c;必须要经过关键 码的多次比较 。 顺序查找时间复杂度为 O(N) &#xff0c;平衡树中为树的高度&#xff0c;即 O( log2N ) &#xff0c;搜索的效…

IndexedDB查询

Indexeddb 创建、增删改查_indexdb 删除表-CSDN博客本地数据库IndexedDB - 学员管理系统之条件筛选&#xff08;四&#xff09;_indexdb条件查询-CSDN博客 <div align"center"><input type"text" id"input_search"> <button id&q…

机器学习实验4——CNN卷积神经网络分类Minst数据集

文章目录 &#x1f9e1;&#x1f9e1;实验内容&#x1f9e1;&#x1f9e1;&#x1f9e1;&#x1f9e1; 原理&#x1f9e1;&#x1f9e1;&#x1f9e1;&#x1f9e1;CNN实现分类Minst&#x1f9e1;&#x1f9e1;代码数据预处理&#xff1a;设置基本参数&#xff1a; &#x1f9e…

操作系统-调度器与闲逛进程(调度程序与进程和线程调度)和调度算法的指标(CPU利用率 系统吞吐量 周转时间 等待时间 响应时间)

文章目录 调度器和闲逛进程调度器/调度程序进程调度线程调度 闲逛进程 调度算法的指标总览CPU利用率系统吞吐量周转时间等待时间响应时间小结 调度器和闲逛进程 调度器/调度程序 进程调度 是否让当前进程下处理机&#xff0c;让哪个进程上处理机 创建完新进程&#xff0c;此…

小程序直播項目开发流程

点击登录功能&#xff0c;创建IM个人账户 以及 创建直播间群组 第一步&#xff1a;需要获取用户唯一的标识openid。 获取流程如下-点击登录按钮-通过wx.getUserProfile这个Api返回的res.userinfo信息获取用户头像昵称等-再通过wx.login的api获取用户的code-使用code再到服务器换…

有哪些好用的洗地机?家用洗地机品牌推荐

洗地机独特的一洗一吸设计带来了卓越的清洁效果。地面上的污渍、垃圾、粉尘都无法抵挡其强大的清洁力&#xff0c;仅需短短几秒钟&#xff0c;家里的地面就能焕然一新&#xff0c;让人感觉仿佛置身于清新宜人的环境中。这种实用性和清洁效果的结合&#xff0c;让洗地机成为智能…