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 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(); } }
关键代码,
@Override public 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 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(); } }
关键代码,
@Override public 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 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
相关调用的初始化一般都在 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>
按住电源按钮的持续时间
代码在 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>
到此这篇关于Android Settings 按住电源按钮的文章就介绍到这了,更多相关Android Settings 按住电源按钮内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!