1 /* 2 * Copyright (C) 2021 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.Context; 20 import android.net.Uri; 21 import android.provider.Settings; 22 23 import androidx.annotation.Nullable; 24 import androidx.lifecycle.Lifecycle; 25 import androidx.lifecycle.LifecycleObserver; 26 import androidx.lifecycle.OnLifecycleEvent; 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceScreen; 29 30 import com.android.settings.core.SliderPreferenceController; 31 import com.android.settings.widget.LabeledSeekBarPreference; 32 33 /** Handles changes to the long press power button sensitivity slider. */ 34 public class LongPressPowerSensitivityPreferenceController extends SliderPreferenceController 35 implements PowerMenuSettingsUtils.SettingsStateCallback, LifecycleObserver { 36 37 @Nullable 38 private final int[] mSensitivityValues; 39 40 private final PowerMenuSettingsUtils mUtils; 41 42 @Nullable 43 private LabeledSeekBarPreference mPreference; 44 LongPressPowerSensitivityPreferenceController(Context context, String preferenceKey)45 public LongPressPowerSensitivityPreferenceController(Context context, String preferenceKey) { 46 super(context, preferenceKey); 47 mSensitivityValues = context.getResources().getIntArray( 48 com.android.internal.R.array.config_longPressOnPowerDurationSettings); 49 mUtils = new PowerMenuSettingsUtils(context); 50 } 51 52 /** @OnLifecycleEvent(Lifecycle.Event.ON_START) */ 53 @OnLifecycleEvent(Lifecycle.Event.ON_START) onStart()54 public void onStart() { 55 mUtils.registerObserver(this); 56 } 57 58 /** @OnLifecycleEvent(Lifecycle.Event.ON_STOP) */ 59 @OnLifecycleEvent(Lifecycle.Event.ON_STOP) onStop()60 public void onStop() { 61 mUtils.unregisterObserver(); 62 } 63 64 @Override displayPreference(PreferenceScreen screen)65 public void displayPreference(PreferenceScreen screen) { 66 super.displayPreference(screen); 67 mPreference = screen.findPreference(getPreferenceKey()); 68 if (mPreference != null) { 69 mPreference.setContinuousUpdates(false); 70 mPreference.setHapticFeedbackMode( 71 LabeledSeekBarPreference.HAPTIC_FEEDBACK_MODE_ON_TICKS); 72 mPreference.setMin(getMin()); 73 mPreference.setMax(getMax()); 74 } 75 } 76 77 @Override updateState(Preference preference)78 public void updateState(Preference preference) { 79 super.updateState(preference); 80 final LabeledSeekBarPreference pref = (LabeledSeekBarPreference) preference; 81 pref.setVisible( 82 PowerMenuSettingsUtils.isLongPressPowerForAssistantEnabled(mContext) 83 && getAvailabilityStatus() == AVAILABLE); 84 pref.setProgress(getSliderPosition()); 85 } 86 87 @Override getAvailabilityStatus()88 public int getAvailabilityStatus() { 89 if (mSensitivityValues == null 90 || mSensitivityValues.length < 2 91 || !PowerMenuSettingsUtils.isLongPressPowerSettingAvailable(mContext)) { 92 return UNSUPPORTED_ON_DEVICE; 93 } 94 return AVAILABLE; 95 } 96 97 @Override getSliderPosition()98 public int getSliderPosition() { 99 return mSensitivityValues == null ? 0 : closestValueIndex(mSensitivityValues, 100 getCurrentSensitivityValue()); 101 } 102 103 @Override setSliderPosition(int position)104 public boolean setSliderPosition(int position) { 105 if (mSensitivityValues == null || position < 0 || position >= mSensitivityValues.length) { 106 return false; 107 } 108 return Settings.Global.putInt(mContext.getContentResolver(), 109 Settings.Global.POWER_BUTTON_LONG_PRESS_DURATION_MS, 110 mSensitivityValues[position]); 111 } 112 113 @Override onChange(Uri uri)114 public void onChange(Uri uri) { 115 if (mPreference != null) { 116 updateState(mPreference); 117 } 118 } 119 120 @Override getMax()121 public int getMax() { 122 if (mSensitivityValues == null || mSensitivityValues.length == 0) { 123 return 0; 124 } 125 return mSensitivityValues.length - 1; 126 } 127 128 @Override getMin()129 public int getMin() { 130 return 0; 131 } 132 getCurrentSensitivityValue()133 private int getCurrentSensitivityValue() { 134 return Settings.Global.getInt(mContext.getContentResolver(), 135 Settings.Global.POWER_BUTTON_LONG_PRESS_DURATION_MS, 136 mContext.getResources().getInteger( 137 com.android.internal.R.integer.config_longPressOnPowerDurationMs)); 138 } 139 closestValueIndex(int[] values, int needle)140 private static int closestValueIndex(int[] values, int needle) { 141 int minDistance = Integer.MAX_VALUE; 142 int valueIndex = 0; 143 for (int i = 0; i < values.length; i++) { 144 int diff = Math.abs(values[i] - needle); 145 if (diff < minDistance) { 146 minDistance = diff; 147 valueIndex = i; 148 } 149 } 150 return valueIndex; 151 } 152 } 153