Android Settings 按住电源按钮的操作方法

 更新时间:2024年01月26日 14:22:25   作者:南国樗里疾  
这篇文章主要介绍了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 按住电源按钮内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Android RxJava创建操作符Timer的方法

    Android RxJava创建操作符Timer的方法

    这篇文章主要为大家详细介绍了Android RxJava创建操作符Timer的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • Android开发之Flutter与webview通信桥梁实现

    Android开发之Flutter与webview通信桥梁实现

    这篇文章主要为大家介绍了Android开发之Flutter与webview通信桥梁实现,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • Android程序打开和对输入法的操作(打开/关闭)

    Android程序打开和对输入法的操作(打开/关闭)

    整理了一下Android下对输入法的操作:打开输入法窗口、关闭出入法窗口、如果输入法打开则关闭,如果没打开则打开、获取输入法打开的状态
    2013-05-05
  • Android编程设计模式之命令模式详解

    Android编程设计模式之命令模式详解

    这篇文章主要介绍了Android编程设计模式之命令模式,详细分析了命令模式的概念、功能、使用场景、用法及相关操作注意事项,需要的朋友可以参考下
    2017-12-12
  • Android入门之bindService的用法详解

    Android入门之bindService的用法详解

    indService大家可以认为它是和Android的一个共生体。即这个service所属的activity如果消亡那么bindService也会消亡。本文将通过简单的例子带大家了解一下bindService的用法,感兴趣的可以了解一下
    2022-12-12
  • Android实现控件拖动效果

    Android实现控件拖动效果

    这篇文章主要为大家详细介绍了Android实现控件拖动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • Flutter实现视频压缩功能的示例代码

    Flutter实现视频压缩功能的示例代码

    移动应用程序中,视频占用了大量的存储空间和带宽,这在一定程度上影响了应用程序的性能和用户体验,所以本文为大家准备了Flutter实现视频压缩的方法,需要的可以参考一下
    2023-06-06
  • Listview的异步加载性能优化

    Listview的异步加载性能优化

    Android中ListView是使用平率最高的控件之一(GridView跟ListView是兄弟,都是继承AbsListView),ListView优化最有效的无非就是采用ViewHolder来减少频繁的对view查询和更新,缓存图片加快解码,减小图片尺寸
    2016-01-01
  • Android 两个Service的相互监视实现代码

    Android 两个Service的相互监视实现代码

    这篇文章主要介绍了Android 两个Service的相互监视实现代码的相关资料,需要的朋友可以参考下
    2016-10-10
  • Android实现九宫格抽奖

    Android实现九宫格抽奖

    这篇文章主要为大家详细介绍了Android实现九宫格抽奖,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06

最新评论