Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5b8cc84c authored by Jernej Virag's avatar Jernej Virag Committed by Android (Google) Code Review
Browse files

Merge "Update power button gesture menu" into sc-dev

parents c24d5621 34cbca50
Loading
Loading
Loading
Loading
+14 −8
Original line number Diff line number Diff line
@@ -13203,23 +13203,29 @@
    <string name="cards_passes_lower">cards &amp; passes</string>
    <!-- Power menu setting name [CHAR LIMIT=NONE] -->
    <string name="power_menu_setting_name">Power menu</string>
    <string name="power_menu_setting_name">Press and hold power button</string>
    <!-- Power menu setting title [CHAR LIMIT=NONE] -->
    <string name="power_menu_setting_title">Power button menu</string>
    <!-- Power menu setting option none [CHAR LIMIT=NONE] -->
    <string name="power_menu_none">Don\u2019t show any content</string>
    <!-- Power menu setting summary to describe press and hold power button for Assistant is enabled. [CHAR LIMIT=NONE] -->
    <string name="power_menu_summary_long_press_for_assist_enabled">Press and hold for the Assistant</string>
    <!-- Power menu setting summary to describe press and hold power button for Assistant is disabled and power menu will appear. [CHAR LIMIT=NONE] -->
    <string name="power_menu_summary_long_press_for_assist_disabled_with_power_menu">Press and hold for the power menu</string>
    <!-- Power menu setting summary to describe press and hold power button for Assistant is disabled and nothing will happen. [CHAR LIMIT=NONE] -->
    <string name="power_menu_summary_long_press_for_assist_disabled_no_action">Press and hold disabled</string>
    <!-- Power menu setting privacy no secure screen lock set [CHAR_LIMIT=NONE] -->
    <string name="lockscreen_privacy_not_secure">To use, first set a screen lock</string>
    <!-- Power menu setting use long press power to invoke assistant. [CHAR_LIMIT=NONE] -->
    <!-- Power menu setting use long press power to invoke assistant. [CHAR LIMIT=NONE] -->
    <string name="power_menu_long_press_for_assist">Hold for Assistant</string>
    <!-- Power menu setting use log press power to invoke assistant summary. [CHAR_LIMIT=NONE] -->
    <!-- Power menu setting use log press power to invoke assistant summary. [CHAR LIMIT=NONE] -->
    <string name="power_menu_long_press_for_assist_summary">Trigger the Assistant by holding the power button</string>
    <!-- Power menu explanation to press power and volume up to show power and emergency menu. [CHAR LIMIT=NONE] -->
    <string name="power_menu_power_volume_up_hint">Power &amp; Emergency Menu:\nPress Power and Volume up at the same time.</string>
    <!-- Power menu explanation where to find prevent ringing option. [CHAR LIMIT=NONE] -->
    <string name="power_menu_power_prevent_ringing_hint">Prevent ringing:\nShortcut available in the volume menu.</string>
    <!-- Wallet (formerly Cards and passes) toggle name [CHAR LIMIT=60] -->
    <string name="lockscreen_privacy_wallet_setting_toggle">Show wallet</string>
    <!-- Wallet (formerly Cards and passes) summary [CHAR LIMIT=NONE] -->
+18 −15
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ Copyright (C) 2020 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.
<!-- Copyright (C) 2020 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.
-->

<PreferenceScreen
@@ -26,4 +23,10 @@
        android:summary="@string/power_menu_long_press_for_assist_summary"
        settings:controller="com.android.settings.gestures.LongPressPowerButtonPreferenceController"
        />

    <com.android.settingslib.widget.FooterPreference
        android:key="power_menu_power_volume_up_hint"
        android:title="@string/power_menu_power_volume_up_hint"
        android:selectable="false"
        settings:searchable="false" />
</PreferenceScreen>
+75 −100
Original line number Diff line number Diff line
@@ -16,45 +16,30 @@

package com.android.settings.gestures;

import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
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.internal.R;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.core.TogglePreferenceController;

import org.checkerframework.checker.nullness.qual.MonotonicNonNull;

/**
 * Configures the behaviour of long press power button action.
 */
public class LongPressPowerButtonPreferenceController extends TogglePreferenceController implements
        LifecycleObserver {
public class LongPressPowerButtonPreferenceController extends TogglePreferenceController {

    private static final String POWER_BUTTON_LONG_PRESS_SETTING =
            Settings.Global.POWER_BUTTON_LONG_PRESS;
    private static final Uri POWER_BUTTON_LONG_PRESS_SETTING_URI =
            Settings.Global.getUriFor(POWER_BUTTON_LONG_PRESS_SETTING);
    private static final String KEY_CHORD_POWER_VOLUME_UP_SETTING =
            Settings.Global.KEY_CHORD_POWER_VOLUME_UP;

    // Used for fallback to global actions if necessary.
    @VisibleForTesting
    static final String CARDS_AVAILABLE_KEY =
            Settings.Secure.GLOBAL_ACTIONS_PANEL_AVAILABLE;
    @VisibleForTesting
    static final String CARDS_ENABLED_KEY = Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED;
    private static final String FOOTER_HINT_KEY = "power_menu_power_volume_up_hint";
    private static final String ASSIST_SWITCH_KEY = "gesture_power_menu_long_press_for_assist";

    /**
     * Values used for long press power button behaviour when Assist setting is enabled.
@@ -63,9 +48,9 @@ public class LongPressPowerButtonPreferenceController extends TogglePreferenceCo
     * source of the value.
     */
    @VisibleForTesting
    static final int LONG_PRESS_POWER_GLOBAL_ACTIONS = 1;
    static final int LONG_PRESS_POWER_NO_ACTION = 0;
    @VisibleForTesting
    static final int LONG_PRESS_POWER_SHUT_OFF = 2;
    static final int LONG_PRESS_POWER_GLOBAL_ACTIONS = 1;
    @VisibleForTesting
    static final int LONG_PRESS_POWER_ASSISTANT_VALUE = 5; // Settings.Secure.ASSISTANT

@@ -89,13 +74,18 @@ public class LongPressPowerButtonPreferenceController extends TogglePreferenceCo
     * power menu, depending on their respective settings.
     */
    private static final int POWER_BUTTON_LONG_PRESS_DEFAULT_VALUE_RESOURCE =
            R.integer.config_longPressOnPowerBehavior;
            com.android.internal.R.integer.config_longPressOnPowerBehavior;

    private static final int KEY_CHORD_POWER_VOLUME_UP_DEFAULT_VALUE_RESOURCE =
            R.integer.config_keyChordPowerVolumeUp;
            com.android.internal.R.integer.config_keyChordPowerVolumeUp;

    @MonotonicNonNull
    @VisibleForTesting
    Preference mFooterHint;

    @Nullable
    private SettingObserver mSettingsObserver;
    @MonotonicNonNull
    @VisibleForTesting
    Preference mAssistSwitch;

    public LongPressPowerButtonPreferenceController(Context context, String key) {
        super(context, key);
@@ -104,26 +94,22 @@ public class LongPressPowerButtonPreferenceController extends TogglePreferenceCo
    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mSettingsObserver = new SettingObserver(screen.findPreference(getPreferenceKey()));
    }

    /**
     * Called when the settings pages resumes.
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume() {
        if (mSettingsObserver != null) {
            mSettingsObserver.register();
        }
        mFooterHint = screen.findPreference(FOOTER_HINT_KEY);
        mAssistSwitch = screen.findPreference(ASSIST_SWITCH_KEY);
        refreshStateDisplay();
    }

    /**
     * Called when the settings page pauses.
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause() {
        if (mSettingsObserver != null) {
            mSettingsObserver.unregister();
    @Override
    public CharSequence getSummary() {
        final int powerButtonValue = getPowerButtonValue();
        if (powerButtonValue == LONG_PRESS_POWER_ASSISTANT_VALUE) {
            return mContext.getString(R.string.power_menu_summary_long_press_for_assist_enabled);
        } else if (powerButtonValue == LONG_PRESS_POWER_GLOBAL_ACTIONS) {
            return mContext.getString(
                    R.string.power_menu_summary_long_press_for_assist_disabled_with_power_menu);
        } else {
            return mContext.getString(
                    R.string.power_menu_summary_long_press_for_assist_disabled_no_action);
        }
    }

@@ -136,10 +122,7 @@ public class LongPressPowerButtonPreferenceController extends TogglePreferenceCo

    @Override
    public boolean isChecked() {
        final int powerButtonValue = Settings.Global.getInt(mContext.getContentResolver(),
                POWER_BUTTON_LONG_PRESS_SETTING,
                mContext.getResources().getInteger(POWER_BUTTON_LONG_PRESS_DEFAULT_VALUE_RESOURCE));
        return powerButtonValue == LONG_PRESS_POWER_ASSISTANT_VALUE;
        return getPowerButtonValue() == LONG_PRESS_POWER_ASSISTANT_VALUE;
    }

    @Override
@@ -148,12 +131,48 @@ public class LongPressPowerButtonPreferenceController extends TogglePreferenceCo
            // The key chord value is dependant on the long press setting and it always
            // toggled in tandem. POWER_BUTTON_LONG_PRESS_SETTING is always the source
            // of truth for both.
            return setPowerVolumeChordValue(isChecked);
            setPowerVolumeChordValue(isChecked);
            refreshStateDisplay();
            return true;
        }

        return false;
    }

    private void refreshStateDisplay() {
        if (mAssistSwitch != null) {
            mAssistSwitch.setSummary(getSummary());
        }

        if (mFooterHint != null) {
            String footerHintText = mContext.getString(R.string.power_menu_power_volume_up_hint);
            // If the device supports hush gesture, we need to notify the user where to find
            // the setting.
            if (mContext.getResources().getBoolean(
                    com.android.internal.R.bool.config_volumeHushGestureEnabled)) {
                footerHintText = footerHintText + "\n\n" + mContext.getString(
                        R.string.power_menu_power_prevent_ringing_hint);
            }

            mFooterHint.setSummary(footerHintText);
            mFooterHint.setVisible(isPowerMenuKeyChordEnabled(mContext));
        }
    }

    private int getPowerButtonValue() {
        return Settings.Global.getInt(mContext.getContentResolver(),
                POWER_BUTTON_LONG_PRESS_SETTING,
                mContext.getResources().getInteger(POWER_BUTTON_LONG_PRESS_DEFAULT_VALUE_RESOURCE));
    }

    private static boolean isPowerMenuKeyChordEnabled(Context context) {
        return Settings.Global.getInt(context.getContentResolver(),
                KEY_CHORD_POWER_VOLUME_UP_SETTING,
                context.getResources().getInteger(
                        com.android.internal.R.integer.config_keyChordPowerVolumeUp))
                == KEY_CHORD_POWER_VOLUME_UP_GLOBAL_ACTIONS;
    }

    private boolean setPowerLongPressValue(boolean isChecked) {
        if (isChecked) {
            return Settings.Global.putInt(mContext.getContentResolver(),
@@ -166,10 +185,8 @@ public class LongPressPowerButtonPreferenceController extends TogglePreferenceCo
        final int defaultPowerButtonValue = mContext.getResources().getInteger(
                POWER_BUTTON_LONG_PRESS_DEFAULT_VALUE_RESOURCE);
        if (defaultPowerButtonValue == LONG_PRESS_POWER_ASSISTANT_VALUE) {
            final int fallbackValue = isCardsOrControlsAvailable() ? LONG_PRESS_POWER_GLOBAL_ACTIONS
                    : LONG_PRESS_POWER_SHUT_OFF;
            return Settings.Global.putInt(mContext.getContentResolver(),
                    POWER_BUTTON_LONG_PRESS_SETTING, fallbackValue);
                    POWER_BUTTON_LONG_PRESS_SETTING, LONG_PRESS_POWER_NO_ACTION);
        }

        return Settings.Global.putInt(mContext.getContentResolver(),
@@ -187,53 +204,11 @@ public class LongPressPowerButtonPreferenceController extends TogglePreferenceCo
                    KEY_CHORD_POWER_VOLUME_UP_SETTING, KEY_CHORD_POWER_VOLUME_UP_GLOBAL_ACTIONS);
        }

        // If the key chord defaults to mute toggle, we restore that setting if LPP option is
        // disabled. Otherwise we default to no action.
        boolean isMuteToggleKeyChordDefault = mContext.getResources().getInteger(
                KEY_CHORD_POWER_VOLUME_UP_DEFAULT_VALUE_RESOURCE)
                == KEY_CHORD_POWER_VOLUME_UP_MUTE_TOGGLE;
        // We restore key chord to the default value.
        int keyChordDefaultValue = mContext.getResources().getInteger(
                KEY_CHORD_POWER_VOLUME_UP_DEFAULT_VALUE_RESOURCE);
        return Settings.Global.putInt(mContext.getContentResolver(),
                KEY_CHORD_POWER_VOLUME_UP_SETTING, isMuteToggleKeyChordDefault
                        ? KEY_CHORD_POWER_VOLUME_UP_MUTE_TOGGLE
                        : KEY_CHORD_POWER_VOLUME_UP_NO_ACTION);
    }

    /**
     * Returns true if the global actions menu on power button click is enabled via any of the
     * content options.
     */
    private boolean isCardsOrControlsAvailable() {
        final ContentResolver resolver = mContext.getContentResolver();
        final boolean cardsAvailable = Settings.Secure.getInt(resolver, CARDS_AVAILABLE_KEY, 0)
                != 0;
        final boolean controlsAvailable = mContext.getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CONTROLS);
        return cardsAvailable || controlsAvailable;
    }

    private final class SettingObserver extends ContentObserver {

        private final Preference mPreference;

        SettingObserver(Preference preference) {
            super(new Handler(Looper.getMainLooper()));
            mPreference = preference;
        }

        public void register() {
            final ContentResolver cr = mContext.getContentResolver();
            cr.registerContentObserver(POWER_BUTTON_LONG_PRESS_SETTING_URI, false, this);
        }

        public void unregister() {
            final ContentResolver cr = mContext.getContentResolver();
            cr.unregisterContentObserver(this);
        }

        @Override
        public void onChange(boolean selfChange) {
            updateState(mPreference);
        }
                KEY_CHORD_POWER_VOLUME_UP_SETTING, keyChordDefaultValue);
    }

}
+28 −2
Original line number Diff line number Diff line
@@ -17,28 +17,54 @@
package com.android.settings.gestures;

import android.content.Context;
import android.provider.Settings;

import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;

public class PowerMenuPreferenceController extends BasePreferenceController {

    private static final String POWER_BUTTON_LONG_PRESS_SETTING =
            Settings.Global.POWER_BUTTON_LONG_PRESS;

    @VisibleForTesting
    static final int LONG_PRESS_POWER_GLOBAL_ACTIONS = 1;
    @VisibleForTesting
    static final int LONG_PRESS_POWER_ASSISTANT_VALUE = 5;

    public PowerMenuPreferenceController(Context context, String key) {
        super(context, key);
    }

    @Override
    public CharSequence getSummary() {
        return mContext.getText(R.string.power_menu_long_press_for_assist);
        final int powerButtonValue = getPowerButtonLongPressValue(mContext);
        if (powerButtonValue == LONG_PRESS_POWER_ASSISTANT_VALUE) {
            return mContext.getText(R.string.power_menu_summary_long_press_for_assist_enabled);
        } else if (powerButtonValue == LONG_PRESS_POWER_GLOBAL_ACTIONS) {
            return mContext.getText(
                    R.string.power_menu_summary_long_press_for_assist_disabled_with_power_menu);
        } else {
            return mContext.getText(
                    R.string.power_menu_summary_long_press_for_assist_disabled_no_action);
        }
    }

    @Override
    public int getAvailabilityStatus() {
        return isAssistInvocationAvailable() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
        return isAssistInvocationAvailable() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
    }

    private boolean isAssistInvocationAvailable() {
        return mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_longPressOnPowerForAssistantSettingAvailable);
    }

    private static int getPowerButtonLongPressValue(Context context) {
        return Settings.Global.getInt(context.getContentResolver(),
                POWER_BUTTON_LONG_PRESS_SETTING,
                context.getResources().getInteger(
                        com.android.internal.R.integer.config_longPressOnPowerBehavior));
    }
}
+91 −28

File changed.

Preview size limit exceeded, changes collapsed.

Loading