Android Settings 按住電源按鈕的操作方法
如題,Android 原生 Settings 里有個(gè) 按住電源按鈕 的選項(xiàng),可以設(shè)置按住電源按鈕的操作。
按住電源按鈕
兩個(gè)選項(xiàng)的 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 BasePreferenceController implements 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); } @Override public int getAvailabilityStatus() { return PowerMenuSettingsUtils.isLongPressPowerSettingAvailable(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE; } @Override public void displayPreference(PreferenceScreen screen) { super.displayPreference(screen); mPreference = screen.findPreference(getPreferenceKey()); if (mPreference != null) { mPreference.setOnClickListener(this); } } @Override public void updateState(Preference preference) { super.updateState(preference); if (preference instanceof SelectorWithWidgetPreference) { ((SelectorWithWidgetPreference) preference) .setChecked( !PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext)); } } @Override public void onRadioButtonClicked(SelectorWithWidgetPreference preference) { PowerMenuSettingsUtils.setLongPressPowerForPowerMenu(mContext); if (mPreference != null) { updateState(mPreference); } } @Override public 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(); } }
關(guān)鍵代碼,
@Override public void onRadioButtonClicked(SelectorWithWidgetPreference preference) { PowerMenuSettingsUtils.setLongPressPowerForPowerMenu(mContext); if (mPreference != null) { updateState(mPreference); } }
數(shù)字助理
代碼在 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 BasePreferenceController implements 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); } @Override public int getAvailabilityStatus() { return PowerMenuSettingsUtils.isLongPressPowerSettingAvailable(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE; } @Override public void displayPreference(PreferenceScreen screen) { super.displayPreference(screen); mPreference = screen.findPreference(getPreferenceKey()); if (mPreference != null) { mPreference.setOnClickListener(this); } } @Override public void updateState(Preference preference) { super.updateState(preference); if (preference instanceof SelectorWithWidgetPreference) { ((SelectorWithWidgetPreference) preference) .setChecked( PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext)); } } @Override public void onRadioButtonClicked(SelectorWithWidgetPreference preference) { PowerMenuSettingsUtils.setLongPressPowerForAssistant(mContext); if (mPreference != null) { updateState(mPreference); } } @Override public 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(); } }
關(guān)鍵代碼,
@Override public void onRadioButtonClicked(SelectorWithWidgetPreference preference) { PowerMenuSettingsUtils.setLongPressPowerForAssistant(mContext); if (mPreference != null) { updateState(mPreference); } }
功能設(shè)置
實(shí)際設(shè)置是在 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 Menu private 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 menu Settings.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
相關(guān)調(diào)用的初始化一般都在 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 - Nothing 1 - Global actions menu 2 - Power off (with confirmation) 3 - Power off (without confirmation) 4 - Go to voice assist 5 - Go to assistant (Settings.Secure.ASSISTANT) --> <integer name="config_longPressOnPowerBehavior">5</integer>
按住電源按鈕的持續(xù)時(shí)間
代碼在 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 SliderPreferenceController implements PowerMenuSettingsUtils.SettingsStateCallback, LifecycleObserver { @Nullable private final int[] mSensitivityValues; private final PowerMenuSettingsUtils mUtils; @Nullable private 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(); } @Override public 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()); } } @Override public void updateState(Preference preference) { super.updateState(preference); final LabeledSeekBarPreference pref = (LabeledSeekBarPreference) preference; pref.setVisible( PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext) && getAvailabilityStatus() == AVAILABLE); pref.setProgress(getSliderPosition()); } @Override public int getAvailabilityStatus() { if (mSensitivityValues == null || mSensitivityValues.length < 2 || !PowerMenuSettingsUtils.isLongPressPowerSettingAvailable(mContext)) { return UNSUPPORTED_ON_DEVICE; } return AVAILABLE; } @Override public int getSliderPosition() { return mSensitivityValues == null ? 0 : closestValueIndex(mSensitivityValues, getCurrentSensitivityValue()); } @Override public 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]); } @Override public void onChange(Uri uri) { if (mPreference != null) { updateState(mPreference); } } @Override public int getMax() { if (mSensitivityValues == null || mSensitivityValues.length == 0) { return 0; } return mSensitivityValues.length - 1; } @Override public 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 duration action. Value set in config_longPressOnPowerDurationMs should be one of the available options 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>
到此這篇關(guān)于Android Settings 按住電源按鈕的文章就介紹到這了,更多相關(guān)Android Settings 按住電源按鈕內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android 給RecyclerView添加分割線的具體步驟(分享)
下面小編就為大家?guī)硪黄狝ndroid 給RecyclerView添加分割線的具體步驟(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04Android自定義View繪制隨機(jī)生成圖片驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了Android自定義View繪制隨機(jī)生成圖片驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Android編程出現(xiàn)Button點(diǎn)擊事件無效的解決方法示例
這篇文章主要介紹了Android編程出現(xiàn)Button點(diǎn)擊事件無效的解決方法,結(jié)合實(shí)例形式分析了Android編程中出現(xiàn)Button點(diǎn)擊事件無效的原因及相關(guān)的解決方法,需要的朋友可以參考下2018-02-02Android中SharedPreferences簡單使用實(shí)例
這篇文章主要介紹了Android中SharedPreferences簡單使用案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android使用文件進(jìn)行數(shù)據(jù)存儲(chǔ)的方法
這篇文章主要介紹了Android使用文件進(jìn)行數(shù)據(jù)存儲(chǔ)的方法,較為詳細(xì)的分析了Android基于文件實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)所涉及的相關(guān)概念與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09android dialog根據(jù)彈窗等級(jí)排序顯示的示例代碼
這篇文章主要介紹了android dialog根據(jù)彈窗等級(jí)排序顯示,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10android顯示TextView文字的倒影效果實(shí)現(xiàn)代碼
這篇文章主要介紹了android顯示TextView文字的倒影效果實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-02-02淺談Android ASM自動(dòng)埋點(diǎn)方案實(shí)踐
本篇文章主要介紹了淺談Android ASM自動(dòng)埋點(diǎn)方案實(shí)踐,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01