Create DoubleTapPowerSettingsUtils

Create Utils class to store common methods used by Preference
controllers

Android Settings Feature Request: b/380287172

Bug: 378130789
Test: atest DoubleTapPowerSettingsUtilsTest
FLAG: android.service.quickaccesswallet.launch_wallet_option_on_power_double_tap
Change-Id: I0bd47579bd3c8163e2e48ac572577c72f2aab785
This commit is contained in:
Lorenzo Lucena Maguire
2024-11-08 19:34:03 +00:00
parent 70e95a24f2
commit c09909489f
2 changed files with 352 additions and 0 deletions

View File

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

View File

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