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