diff --git a/src/com/android/settings/gestures/DoubleTapPowerSettingsUtils.java b/src/com/android/settings/gestures/DoubleTapPowerSettingsUtils.java new file mode 100644 index 00000000000..a1bf9cbd48c --- /dev/null +++ b/src/com/android/settings/gestures/DoubleTapPowerSettingsUtils.java @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2016 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.database.ContentObserver; +import android.net.Uri; +import android.provider.Settings; + +import androidx.annotation.NonNull; + +import com.android.internal.R; + +/** Common code for double tap power settings shared between controllers. */ +final class DoubleTapPowerSettingsUtils { + + /** Setting storing whether the double tap power button gesture is enabled. */ + private static final String DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED = + Settings.Secure.DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED; + + static final Uri DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED_URI = + Settings.Secure.getUriFor(DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED); + + /** Setting storing the target action of the double tap power button gesture. */ + private static final String DOUBLE_TAP_POWER_BUTTON_GESTURE_TARGET_ACTION = + Settings.Secure.DOUBLE_TAP_POWER_BUTTON_GESTURE; + + static final Uri DOUBLE_TAP_POWER_BUTTON_GESTURE_TARGET_ACTION_URI = + Settings.Secure.getUriFor(DOUBLE_TAP_POWER_BUTTON_GESTURE_TARGET_ACTION); + + private static final int DOUBLE_TAP_POWER_BUTTON_CAMERA_LAUNCH_VALUE = 0; + private static final int DOUBLE_TAP_POWER_BUTTON_WALLET_LAUNCH_VALUE = 1; + + static final int ON = 1; + static final int OFF = 0; + + /** + * @return true if double tap power button gesture is available. + */ + public static boolean isDoubleTapPowerButtonGestureAvailable(@NonNull Context context) { + return context.getResources().getBoolean(R.bool.config_doubleTapPowerGestureEnabled); + } + + /** + * Gets double tap power button gesture enable or disable flag from Settings provider. + * + * @return true if double tap on the power button gesture is currently enabled. + * @param context App context + */ + public static boolean isDoubleTapPowerButtonGestureEnabled(@NonNull Context context) { + return Settings.Secure.getInt( + context.getContentResolver(), DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED, ON) + == ON; + } + + /** + * Sets double tap power button gesture enable or disable flag to Settings provider. + * + * @param context App context + * @param enable enable or disable double tap power button gesture. + * @return {@code true} if the setting is updated. + */ + public static boolean setDoubleTapPowerButtonGestureEnabled( + @NonNull Context context, boolean enable) { + return Settings.Secure.putInt( + context.getContentResolver(), + DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED, + enable ? ON : OFF); + } + + /** + * @return true if double tap on the power button gesture for camera launch is currently + * enabled. + * @param context App context + */ + public static boolean isDoubleTapPowerButtonGestureForCameraLaunchEnabled( + @NonNull Context context) { + return Settings.Secure.getInt( + context.getContentResolver(), + DOUBLE_TAP_POWER_BUTTON_GESTURE_TARGET_ACTION, + context.getResources() + .getInteger( + com.android.internal.R.integer + .config_defaultDoubleTapPowerGestureAction)) + == DOUBLE_TAP_POWER_BUTTON_CAMERA_LAUNCH_VALUE; + } + + /** + * Sets double tap power button gesture behavior to launch the camera. + * + * @param context App context + * @return {@code true} if the setting is updated. + */ + public static boolean setDoubleTapPowerButtonForCameraLaunch(@NonNull Context context) { + return Settings.Secure.putInt( + context.getContentResolver(), + DOUBLE_TAP_POWER_BUTTON_GESTURE_TARGET_ACTION, + DOUBLE_TAP_POWER_BUTTON_CAMERA_LAUNCH_VALUE); + } + + /** + * Sets double tap power button gesture behavior to launch the wallet. + * + * @param context App context + * @return {@code true} if the setting is updated. + */ + public static boolean setDoubleTapPowerButtonForWalletLaunch(@NonNull Context context) { + return Settings.Secure.putInt( + context.getContentResolver(), + DOUBLE_TAP_POWER_BUTTON_GESTURE_TARGET_ACTION, + DOUBLE_TAP_POWER_BUTTON_WALLET_LAUNCH_VALUE); + } + + /** + * Registers observer for settings state. + * + * @param observer Settings Content Observer + */ + public static void registerObserver( + @NonNull Context context, @NonNull ContentObserver observer) { + final ContentResolver resolver = context.getContentResolver(); + resolver.registerContentObserver( + DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED_URI, true, observer); + resolver.registerContentObserver( + DOUBLE_TAP_POWER_BUTTON_GESTURE_TARGET_ACTION_URI, true, observer); + } + + /** Unregisters observer. */ + public static void unregisterObserver( + @NonNull Context context, @NonNull ContentObserver observer) { + final ContentResolver resolver = context.getContentResolver(); + resolver.unregisterContentObserver(observer); + } +} diff --git a/tests/robotests/src/com/android/settings/gestures/DoubleTapPowerSettingsUtilsTest.java b/tests/robotests/src/com/android/settings/gestures/DoubleTapPowerSettingsUtilsTest.java new file mode 100644 index 00000000000..817f198a5b2 --- /dev/null +++ b/tests/robotests/src/com/android/settings/gestures/DoubleTapPowerSettingsUtilsTest.java @@ -0,0 +1,203 @@ +/* + * 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.android.settings.gestures.DoubleTapPowerSettingsUtils.OFF; +import static com.android.settings.gestures.DoubleTapPowerSettingsUtils.ON; + +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.content.Context; +import android.content.res.Resources; +import android.provider.Settings; + +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import com.android.internal.R; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class DoubleTapPowerSettingsUtilsTest { + + private static final int DOUBLE_TAP_POWER_BUTTON_CAMERA_LAUNCH_VALUE = 0; + private static final int DOUBLE_TAP_POWER_BUTTON_WALLET_LAUNCH_VALUE = 1; + + private Context mContext; + private Resources mResources; + + @Before + public void setUp() { + mContext = spy(ApplicationProvider.getApplicationContext()); + mResources = mock(Resources.class); + when(mContext.getResources()).thenReturn(mResources); + } + + @Test + public void isDoubleTapPowerButtonGestureAvailable_setAvailable_returnsTrue() { + when(mResources.getBoolean(R.bool.config_doubleTapPowerGestureEnabled)).thenReturn(true); + + assertThat(DoubleTapPowerSettingsUtils.isDoubleTapPowerButtonGestureAvailable(mContext)) + .isTrue(); + } + + @Test + public void isDoubleTapPowerButtonGestureAvailable_setUnavailable_returnsFalse() { + when(mResources.getBoolean(R.bool.config_doubleTapPowerGestureEnabled)).thenReturn(false); + + assertThat(DoubleTapPowerSettingsUtils.isDoubleTapPowerButtonGestureAvailable(mContext)) + .isFalse(); + } + + @Test + public void isDoubleTapPowerButtonGestureEnabled_setEnabled_returnsTrue() { + Settings.Secure.putInt( + mContext.getContentResolver(), + Settings.Secure.DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED, + ON); + + assertThat(DoubleTapPowerSettingsUtils.isDoubleTapPowerButtonGestureEnabled(mContext)) + .isTrue(); + } + + @Test + public void isDoubleTapPowerButtonGestureEnabled_setDisabled_returnsFalse() { + Settings.Secure.putInt( + mContext.getContentResolver(), + Settings.Secure.DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED, + OFF); + + assertThat(DoubleTapPowerSettingsUtils.isDoubleTapPowerButtonGestureEnabled(mContext)) + .isFalse(); + } + + @Test + public void isDoubleTapPowerButtonGestureEnabled_valueNotSet_returnsTrue() { + assertThat(DoubleTapPowerSettingsUtils.isDoubleTapPowerButtonGestureEnabled(mContext)) + .isTrue(); + } + + @Test + public void setDoubleTapPowerButtonGestureEnabled_setEnabled_returnsEnabled() { + DoubleTapPowerSettingsUtils.setDoubleTapPowerButtonGestureEnabled(mContext, true); + + assertThat( + Settings.Secure.getInt( + mContext.getContentResolver(), + Settings.Secure.DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED, + OFF)) + .isEqualTo(ON); + } + + @Test + public void setDoubleTapPowerButtonGestureEnabled_setDisabled_returnsDisabled() { + DoubleTapPowerSettingsUtils.setDoubleTapPowerButtonGestureEnabled(mContext, false); + + assertThat( + Settings.Secure.getInt( + mContext.getContentResolver(), + Settings.Secure.DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED, + ON)) + .isEqualTo(OFF); + } + + @Test + public void isDoubleTapPowerButtonGestureForCameraLaunchEnabled_valueSetToCamera_returnsTrue() { + Settings.Secure.putInt( + mContext.getContentResolver(), + Settings.Secure.DOUBLE_TAP_POWER_BUTTON_GESTURE, + DOUBLE_TAP_POWER_BUTTON_CAMERA_LAUNCH_VALUE); + + assertThat( + DoubleTapPowerSettingsUtils + .isDoubleTapPowerButtonGestureForCameraLaunchEnabled(mContext)) + .isTrue(); + } + + @Test + public void + isDoubleTapPowerButtonGestureForCameraLaunchEnabled_valueNotSetToCamera_returnsFalse() { + Settings.Secure.putInt( + mContext.getContentResolver(), + Settings.Secure.DOUBLE_TAP_POWER_BUTTON_GESTURE, + DOUBLE_TAP_POWER_BUTTON_WALLET_LAUNCH_VALUE); + + assertThat( + DoubleTapPowerSettingsUtils + .isDoubleTapPowerButtonGestureForCameraLaunchEnabled(mContext)) + .isFalse(); + } + + @Test + public void + isDoubleTapPowerButtonGestureForCameraLaunchEnabled_defaultSetToCamera_returnsTrue() { + when(mResources.getInteger(R.integer.config_defaultDoubleTapPowerGestureAction)) + .thenReturn(DOUBLE_TAP_POWER_BUTTON_CAMERA_LAUNCH_VALUE); + + assertThat( + DoubleTapPowerSettingsUtils + .isDoubleTapPowerButtonGestureForCameraLaunchEnabled(mContext)) + .isTrue(); + } + + @Test + public void + isDoubleTapPowerButtonGestureForCameraLaunchEnabled_defaultNotCamera_returnsFalse() { + when(mResources.getInteger(R.integer.config_defaultDoubleTapPowerGestureAction)) + .thenReturn(DOUBLE_TAP_POWER_BUTTON_WALLET_LAUNCH_VALUE); + + assertThat( + DoubleTapPowerSettingsUtils + .isDoubleTapPowerButtonGestureForCameraLaunchEnabled(mContext)) + .isFalse(); + } + + @Test + public void setDoubleTapPowerButtonForCameraLaunch_setGestureBehaviorToCameraLaunch() { + boolean result = + DoubleTapPowerSettingsUtils.setDoubleTapPowerButtonForCameraLaunch(mContext); + + assertThat(result).isTrue(); + assertThat( + Settings.Secure.getInt( + mContext.getContentResolver(), + Settings.Secure.DOUBLE_TAP_POWER_BUTTON_GESTURE, + DOUBLE_TAP_POWER_BUTTON_WALLET_LAUNCH_VALUE)) + .isEqualTo(DOUBLE_TAP_POWER_BUTTON_CAMERA_LAUNCH_VALUE); + } + + @Test + public void setDoubleTapPowerButtonForWalletLaunch_setGestureBehaviorToWalletLaunch() { + boolean result = + DoubleTapPowerSettingsUtils.setDoubleTapPowerButtonForWalletLaunch(mContext); + + assertThat(result).isTrue(); + assertThat( + Settings.Secure.getInt( + mContext.getContentResolver(), + Settings.Secure.DOUBLE_TAP_POWER_BUTTON_GESTURE, + DOUBLE_TAP_POWER_BUTTON_CAMERA_LAUNCH_VALUE)) + .isEqualTo(DOUBLE_TAP_POWER_BUTTON_WALLET_LAUNCH_VALUE); + } +}