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

Commit 4f1e17eb authored by Jernej Virag's avatar Jernej Virag
Browse files

Create a setting for Assist invocation via long press power button

Adds a setting which allows the user to enable invoking the Assist app via long pressing the power button. The availability of setting is controlled by config_longPressOnPowerForAssistantSettingAvailable configuration value.

Bug: 179175321
Bug: 182983853
Test: make RunSettingsRoboTests
Change-Id: I2eb23e5b7539b2fb8e5bc85d23ca5795a08366c5
parent 2a959d47
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -12804,6 +12804,12 @@
    <!-- Power menu setting privacy no secure screen lock set [CHAR_LIMIT=NONE] -->
    <string name="power_menu_privacy_not_secure">To use, first set a screen lock</string>
    <!-- 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] -->
    <string name="power_menu_long_press_for_assist_summary">Trigger the Assistant by holding the power button</string>
    <!-- Device controls toggle name [CHAR LIMIT=60] -->
    <string name="device_controls_setting_toggle">Show device controls</string>
+7 −0
Original line number Diff line number Diff line
@@ -20,6 +20,13 @@
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:title="@string/power_menu_setting_name">

    <SwitchPreference
        android:key="gesture_power_menu_long_press_for_assist"
        android:title="@string/power_menu_long_press_for_assist"
        android:summary="@string/power_menu_long_press_for_assist_summary"
        settings:controller="com.android.settings.gestures.LongPressPowerButtonPreferenceController"
        />

    <Preference
        android:key="gesture_global_actions_panel_summary"
        android:title="@string/cards_passes_sentence"
+188 −0
Original line number Diff line number Diff line
/*
 * 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.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.core.TogglePreferenceController;

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

    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);

    // 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;

    /**
     * Value used for long press power button behaviour when Assist setting is enabled.
     *
     * {@link com.android.server.policy.PhoneWindowManager#LONG_PRESS_POWER_GLOBAL_ACTIONS} for
     * source of the value.
     */
    @VisibleForTesting
    static final int LONG_PRESS_POWER_GLOBAL_ACTIONS = 1;
    @VisibleForTesting
    static final int LONG_PRESS_POWER_SHUT_OFF = 2;
    @VisibleForTesting
    static final int LONG_PRESS_POWER_ASSISTANT_VALUE = 5; // Settings.Secure.ASSISTANT

    /**
     * Value used for long press power button behaviour when the Assist setting is disabled.
     *
     * If this value matches Assist setting, then it falls back to Global Actions panel or
     * power menu, depending on their respective settings.
     */
    private static final int POWER_BUTTON_LONG_PRESS_DEFAULT_VALUE_RESOURCE =
            R.integer.config_longPressOnPowerBehavior;

    @Nullable
    private SettingObserver mSettingsObserver;

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

    @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();
        }
    }

    /**
     * Called when the settings page pauses.
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause() {
        if (mSettingsObserver != null) {
            mSettingsObserver.unregister();
        }
    }

    @Override
    public int getAvailabilityStatus() {
        final boolean enabled = mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_longPressOnPowerForAssistantSettingAvailable);
        return enabled ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
    }

    @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;
    }

    @Override
    public boolean setChecked(boolean isChecked) {
        if (isChecked) {
            return Settings.Global.putInt(mContext.getContentResolver(),
                    POWER_BUTTON_LONG_PRESS_SETTING, LONG_PRESS_POWER_ASSISTANT_VALUE);
        }

        // We need to determine the right disabled value - we set it to device default
        // if it's different than Assist, otherwise we fallback to either global actions or power
        //menu.
        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);
        }

        return Settings.Global.putInt(mContext.getContentResolver(),
                POWER_BUTTON_LONG_PRESS_SETTING, defaultPowerButtonValue);
    }

    /**
     * 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);
        }
    }

}
+7 −1
Original line number Diff line number Diff line
@@ -57,7 +57,8 @@ public class PowerMenuPreferenceController extends BasePreferenceController {

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

    private boolean isControlsAvailable() {
@@ -68,4 +69,9 @@ public class PowerMenuPreferenceController extends BasePreferenceController {
        return Settings.Secure.getInt(mContext.getContentResolver(),
                CARDS_AVAILABLE_SETTING, 0) == 1;
    }

    private boolean isAssistInvocationAvailable() {
        return mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_longPressOnPowerForAssistantSettingAvailable);
    }
}
+129 −0
Original line number Diff line number Diff line
/*
 * 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 static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import android.app.Application;
import android.content.res.Resources;
import android.provider.Settings;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

@RunWith(RobolectricTestRunner.class)
public class LongPressPowerButtonPreferenceControllerTest {

    private static final String KEY_LONG_PRESS_POWER_BUTTON =
            "gesture_power_menu_long_press_for_assist";

    private Application mContext;
    private Resources mResources;
    private LongPressPowerButtonPreferenceController mController;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mContext = spy(RuntimeEnvironment.application);
        mResources = mock(Resources.class);
        when(mContext.getResources()).thenReturn(mResources);
        when(mResources.getBoolean(
                com.android.internal.R.bool.config_longPressOnPowerForAssistantSettingAvailable))
                .thenReturn(true);
        mController = new LongPressPowerButtonPreferenceController(mContext,
                KEY_LONG_PRESS_POWER_BUTTON);
    }

    @Test
    public void isAvailable_configIsTrue_shouldReturnTrue() {
        when(mResources.getBoolean(
                com.android.internal.R.bool.config_longPressOnPowerForAssistantSettingAvailable))
                .thenReturn(true);

        assertThat(mController.isAvailable()).isTrue();
    }

    @Test
    public void isAvailable_configIsFalse_shouldReturnFalse() {
        when(mResources.getBoolean(
                com.android.internal.R.bool.config_longPressOnPowerForAssistantSettingAvailable))
                .thenReturn(false);

        assertThat(mController.isAvailable()).isFalse();
    }

    @Test
    public void preferenceChecked_longPressPowerSettingSetToAssistant() {
        mController.onPreferenceChange(null, true);

        assertThat(Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.POWER_BUTTON_LONG_PRESS, -1)).isEqualTo(
                LongPressPowerButtonPreferenceController.LONG_PRESS_POWER_ASSISTANT_VALUE);
    }

    @Test
    public void preferenceUnchecked_longPressPowerSettingSetToDefaultValue() {
        // Value out of range chosen deliberately.
        when(mResources.getInteger(
                com.android.internal.R.integer.config_longPressOnPowerBehavior))
                .thenReturn(8);

        mController.onPreferenceChange(null, false);
        assertThat(Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.POWER_BUTTON_LONG_PRESS, -1)).isEqualTo(8);
    }

    @Test
    public void preferenceUnchecked_assistDefault_setShutOff() {
        // Value out of range chosen deliberately.
        when(mResources.getInteger(
                com.android.internal.R.integer.config_longPressOnPowerBehavior))
                .thenReturn(
                        LongPressPowerButtonPreferenceController.LONG_PRESS_POWER_ASSISTANT_VALUE);

        mController.onPreferenceChange(null, false);
        assertThat(Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.POWER_BUTTON_LONG_PRESS, -1)).isEqualTo(
                LongPressPowerButtonPreferenceController.LONG_PRESS_POWER_SHUT_OFF);
    }

    @Test
    public void preferenceUnchecked_assistDefaultGlobalActionsEnabled_setGlobalActions() {
        // Value out of range chosen deliberately.
        when(mResources.getInteger(
                com.android.internal.R.integer.config_longPressOnPowerBehavior))
                .thenReturn(
                        LongPressPowerButtonPreferenceController.LONG_PRESS_POWER_ASSISTANT_VALUE);
        Settings.Secure.putInt(mContext.getContentResolver(),
                LongPressPowerButtonPreferenceController.CARDS_AVAILABLE_KEY, 1);
        Settings.Secure.putInt(mContext.getContentResolver(),
                LongPressPowerButtonPreferenceController.CARDS_ENABLED_KEY, 1);

        mController.onPreferenceChange(null, false);
        assertThat(Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.POWER_BUTTON_LONG_PRESS, -1)).isEqualTo(
                LongPressPowerButtonPreferenceController.LONG_PRESS_POWER_GLOBAL_ACTIONS);
    }
}
Loading