1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.gestures; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.database.ContentObserver; 22 import android.net.Uri; 23 import android.provider.Settings; 24 25 import androidx.annotation.NonNull; 26 27 import com.android.internal.R; 28 29 /** Common code for double tap power settings shared between controllers. */ 30 final class DoubleTapPowerSettingsUtils { 31 32 /** Configuration value indicating double tap power button gesture is disabled. */ 33 static final int DOUBLE_TAP_POWER_DISABLED_MODE = 0; 34 /** Configuration value indicating double tap power button gesture should launch camera. */ 35 static final int DOUBLE_TAP_POWER_LAUNCH_CAMERA_MODE = 1; 36 /** 37 * Configuration value indicating double tap power button gesture should launch one of many 38 * target actions. 39 */ 40 static final int DOUBLE_TAP_POWER_MULTI_TARGET_MODE = 2; 41 42 /** Setting storing whether the double tap power button gesture is enabled. */ 43 private static final String DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED = 44 Settings.Secure.DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED; 45 46 static final Uri DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED_URI = 47 Settings.Secure.getUriFor(DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED); 48 49 /** Setting storing the target action of the double tap power button gesture. */ 50 private static final String DOUBLE_TAP_POWER_BUTTON_GESTURE_TARGET_ACTION = 51 Settings.Secure.DOUBLE_TAP_POWER_BUTTON_GESTURE; 52 53 static final Uri DOUBLE_TAP_POWER_BUTTON_GESTURE_TARGET_ACTION_URI = 54 Settings.Secure.getUriFor(DOUBLE_TAP_POWER_BUTTON_GESTURE_TARGET_ACTION); 55 56 private static final int DOUBLE_TAP_POWER_BUTTON_CAMERA_LAUNCH_VALUE = 0; 57 private static final int DOUBLE_TAP_POWER_BUTTON_WALLET_LAUNCH_VALUE = 1; 58 59 static final int ON = 1; 60 static final int OFF = 0; 61 62 /** 63 * @return true if double tap power button gesture is available. 64 */ isMultiTargetDoubleTapPowerButtonGestureAvailable( @onNull Context context)65 public static boolean isMultiTargetDoubleTapPowerButtonGestureAvailable( 66 @NonNull Context context) { 67 return context.getResources() 68 .getInteger( 69 R.integer.config_doubleTapPowerGestureMode) 70 == DOUBLE_TAP_POWER_MULTI_TARGET_MODE; 71 } 72 73 /** 74 * Gets double tap power button gesture enable or disable flag from Settings provider. 75 * 76 * @param context App context 77 * @return true if double tap on the power button gesture is currently enabled. 78 */ isDoubleTapPowerButtonGestureEnabled(@onNull Context context)79 public static boolean isDoubleTapPowerButtonGestureEnabled(@NonNull Context context) { 80 return Settings.Secure.getInt( 81 context.getContentResolver(), DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED, ON) 82 == ON; 83 } 84 85 /** 86 * Sets double tap power button gesture enable or disable flag to Settings provider. 87 * 88 * @param context App context 89 * @param enable enable or disable double tap power button gesture. 90 * @return {@code true} if the setting is updated. 91 */ setDoubleTapPowerButtonGestureEnabled( @onNull Context context, boolean enable)92 public static boolean setDoubleTapPowerButtonGestureEnabled( 93 @NonNull Context context, boolean enable) { 94 return Settings.Secure.putInt( 95 context.getContentResolver(), 96 DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED, 97 enable ? ON : OFF); 98 } 99 100 /** 101 * @param context App context 102 * @return true if double tap on the power button gesture for camera launch is currently 103 * enabled. 104 */ isDoubleTapPowerButtonGestureForCameraLaunchEnabled( @onNull Context context)105 public static boolean isDoubleTapPowerButtonGestureForCameraLaunchEnabled( 106 @NonNull Context context) { 107 return Settings.Secure.getInt( 108 context.getContentResolver(), 109 DOUBLE_TAP_POWER_BUTTON_GESTURE_TARGET_ACTION, 110 context.getResources() 111 .getInteger( 112 com.android.internal.R.integer 113 .config_doubleTapPowerGestureMultiTargetDefaultAction)) 114 == DOUBLE_TAP_POWER_BUTTON_CAMERA_LAUNCH_VALUE; 115 } 116 117 /** 118 * Sets double tap power button gesture behavior to launch the camera. 119 * 120 * @param context App context 121 * @return {@code true} if the setting is updated. 122 */ setDoubleTapPowerButtonForCameraLaunch(@onNull Context context)123 public static boolean setDoubleTapPowerButtonForCameraLaunch(@NonNull Context context) { 124 return Settings.Secure.putInt( 125 context.getContentResolver(), 126 DOUBLE_TAP_POWER_BUTTON_GESTURE_TARGET_ACTION, 127 DOUBLE_TAP_POWER_BUTTON_CAMERA_LAUNCH_VALUE); 128 } 129 130 /** 131 * Sets double tap power button gesture behavior to launch the wallet. 132 * 133 * @param context App context 134 * @return {@code true} if the setting is updated. 135 */ setDoubleTapPowerButtonForWalletLaunch(@onNull Context context)136 public static boolean setDoubleTapPowerButtonForWalletLaunch(@NonNull Context context) { 137 return Settings.Secure.putInt( 138 context.getContentResolver(), 139 DOUBLE_TAP_POWER_BUTTON_GESTURE_TARGET_ACTION, 140 DOUBLE_TAP_POWER_BUTTON_WALLET_LAUNCH_VALUE); 141 } 142 143 /** 144 * Registers observer for settings state. 145 * 146 * @param observer Settings Content Observer 147 */ registerObserver( @onNull Context context, @NonNull ContentObserver observer)148 public static void registerObserver( 149 @NonNull Context context, @NonNull ContentObserver observer) { 150 final ContentResolver resolver = context.getContentResolver(); 151 resolver.registerContentObserver( 152 DOUBLE_TAP_POWER_BUTTON_GESTURE_ENABLED_URI, true, observer); 153 resolver.registerContentObserver( 154 DOUBLE_TAP_POWER_BUTTON_GESTURE_TARGET_ACTION_URI, true, observer); 155 } 156 157 /** Unregisters observer. */ unregisterObserver( @onNull Context context, @NonNull ContentObserver observer)158 public static void unregisterObserver( 159 @NonNull Context context, @NonNull ContentObserver observer) { 160 final ContentResolver resolver = context.getContentResolver(); 161 resolver.unregisterContentObserver(observer); 162 } 163 } 164