Add sensitivity configuration to long press power dialog
This gives the user the ability to configure the length of the long press power gesture. The configuration values are read from config.xml and can be customized per device. Bug: 193792060 Test: Manual test on Pixel 6 and Pixel 4 hardware devices Test: Unit tests with make -j64 RunSettingsRoboTests ROBOTEST_FILTER="com.android.settings.gestures.*" Change-Id: Ia6595c1337244b01e9c8b2ef04dd97a9a0f03de2
This commit is contained in:
committed by
Govinda Wasserman
parent
f4977c5e36
commit
de2f00df41
@@ -16,6 +16,11 @@
|
||||
|
||||
package com.android.settings.gestures;
|
||||
|
||||
import static com.android.settings.gestures.PowerMenuSettingsUtils.LONG_PRESS_POWER_ASSISTANT_VALUE;
|
||||
import static com.android.settings.gestures.PowerMenuSettingsUtils.LONG_PRESS_POWER_GLOBAL_ACTIONS;
|
||||
import static com.android.settings.gestures.PowerMenuSettingsUtils.POWER_BUTTON_LONG_PRESS_DEFAULT_VALUE_RESOURCE;
|
||||
import static com.android.settings.gestures.PowerMenuSettingsUtils.POWER_BUTTON_LONG_PRESS_SETTING;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
|
||||
@@ -33,27 +38,12 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
*/
|
||||
public class LongPressPowerButtonPreferenceController extends TogglePreferenceController {
|
||||
|
||||
private static final String POWER_BUTTON_LONG_PRESS_SETTING =
|
||||
Settings.Global.POWER_BUTTON_LONG_PRESS;
|
||||
private static final String KEY_CHORD_POWER_VOLUME_UP_SETTING =
|
||||
Settings.Global.KEY_CHORD_POWER_VOLUME_UP;
|
||||
|
||||
private static final String FOOTER_HINT_KEY = "power_menu_power_volume_up_hint";
|
||||
private static final String ASSIST_SWITCH_KEY = "gesture_power_menu_long_press_for_assist";
|
||||
|
||||
/**
|
||||
* Values used for long press power button behaviour when Assist setting is enabled.
|
||||
*
|
||||
* {@link com.android.server.policy.PhoneWindowManager#LONG_PRESS_POWER_GLOBAL_ACTIONS} for
|
||||
* source of the value.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
static final int LONG_PRESS_POWER_NO_ACTION = 0;
|
||||
@VisibleForTesting
|
||||
static final int LONG_PRESS_POWER_GLOBAL_ACTIONS = 1;
|
||||
@VisibleForTesting
|
||||
static final int LONG_PRESS_POWER_ASSISTANT_VALUE = 5; // Settings.Secure.ASSISTANT
|
||||
|
||||
/**
|
||||
* Values used for volume key chord behaviour when Assist setting is enabled.
|
||||
*
|
||||
@@ -67,15 +57,6 @@ public class LongPressPowerButtonPreferenceController extends TogglePreferenceCo
|
||||
@VisibleForTesting
|
||||
static final int KEY_CHORD_POWER_VOLUME_UP_GLOBAL_ACTIONS = 2;
|
||||
|
||||
/**
|
||||
* Value used for long press power button behaviour when the Assist setting is disabled.
|
||||
*
|
||||
* If this value matches Assist setting, then it falls back to Global Actions panel or
|
||||
* power menu, depending on their respective settings.
|
||||
*/
|
||||
private static final int POWER_BUTTON_LONG_PRESS_DEFAULT_VALUE_RESOURCE =
|
||||
com.android.internal.R.integer.config_longPressOnPowerBehavior;
|
||||
|
||||
private static final int KEY_CHORD_POWER_VOLUME_UP_DEFAULT_VALUE_RESOURCE =
|
||||
com.android.internal.R.integer.config_keyChordPowerVolumeUp;
|
||||
|
||||
@@ -101,7 +82,7 @@ public class LongPressPowerButtonPreferenceController extends TogglePreferenceCo
|
||||
|
||||
@Override
|
||||
public CharSequence getSummary() {
|
||||
final int powerButtonValue = getPowerButtonValue();
|
||||
final int powerButtonValue = PowerMenuSettingsUtils.getPowerButtonSettingValue(mContext);
|
||||
if (powerButtonValue == LONG_PRESS_POWER_ASSISTANT_VALUE) {
|
||||
return mContext.getString(R.string.power_menu_summary_long_press_for_assist_enabled);
|
||||
} else if (powerButtonValue == LONG_PRESS_POWER_GLOBAL_ACTIONS) {
|
||||
@@ -122,7 +103,7 @@ public class LongPressPowerButtonPreferenceController extends TogglePreferenceCo
|
||||
|
||||
@Override
|
||||
public boolean isChecked() {
|
||||
return getPowerButtonValue() == LONG_PRESS_POWER_ASSISTANT_VALUE;
|
||||
return PowerMenuSettingsUtils.isLongPressPowerForAssistEnabled(mContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -159,12 +140,6 @@ public class LongPressPowerButtonPreferenceController extends TogglePreferenceCo
|
||||
}
|
||||
}
|
||||
|
||||
private int getPowerButtonValue() {
|
||||
return Settings.Global.getInt(mContext.getContentResolver(),
|
||||
POWER_BUTTON_LONG_PRESS_SETTING,
|
||||
mContext.getResources().getInteger(POWER_BUTTON_LONG_PRESS_DEFAULT_VALUE_RESOURCE));
|
||||
}
|
||||
|
||||
private static boolean isPowerMenuKeyChordEnabled(Context context) {
|
||||
return Settings.Global.getInt(context.getContentResolver(),
|
||||
KEY_CHORD_POWER_VOLUME_UP_SETTING,
|
||||
|
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* 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 android.content.Context;
|
||||
import android.database.ContentObserver;
|
||||
import android.os.Handler;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.core.SliderPreferenceController;
|
||||
import com.android.settings.widget.LabeledSeekBarPreference;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||
|
||||
/** Handles changes to the long press power button sensitivity slider. */
|
||||
public class LongPressPowerSensitivityPreferenceController extends
|
||||
SliderPreferenceController implements
|
||||
LifecycleObserver, OnStart, OnStop {
|
||||
|
||||
private final ContentObserver mPowerButtonObserver = new ContentObserver(Handler.getMain()) {
|
||||
@Override
|
||||
public void onChange(boolean selfChange) {
|
||||
if (mPreference != null) {
|
||||
updateState(mPreference);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@Nullable
|
||||
private final int[] mSensitivityValues;
|
||||
|
||||
@Nullable
|
||||
private LabeledSeekBarPreference mPreference;
|
||||
|
||||
public LongPressPowerSensitivityPreferenceController(Context context, String preferenceKey) {
|
||||
super(context, preferenceKey);
|
||||
mSensitivityValues = context.getResources().getIntArray(
|
||||
com.android.internal.R.array.config_longPressOnPowerDurationSettings);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
mContext.getContentResolver().registerContentObserver(
|
||||
Settings.Global.getUriFor(PowerMenuSettingsUtils.POWER_BUTTON_LONG_PRESS_SETTING),
|
||||
false, mPowerButtonObserver);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
mContext.getContentResolver().unregisterContentObserver(mPowerButtonObserver);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPreference = screen.findPreference(getPreferenceKey());
|
||||
if (mPreference != null) {
|
||||
mPreference.setContinuousUpdates(false);
|
||||
mPreference.setHapticFeedbackMode(
|
||||
LabeledSeekBarPreference.HAPTIC_FEEDBACK_MODE_ON_TICKS);
|
||||
mPreference.setMin(getMin());
|
||||
mPreference.setMax(getMax());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
final LabeledSeekBarPreference pref = (LabeledSeekBarPreference) preference;
|
||||
pref.setEnabled(
|
||||
isAvailable() && PowerMenuSettingsUtils.isLongPressPowerForAssistEnabled(mContext));
|
||||
pref.setProgress(getSliderPosition());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
if (mSensitivityValues == null || mSensitivityValues.length < 2) {
|
||||
return UNSUPPORTED_ON_DEVICE;
|
||||
}
|
||||
|
||||
if (!PowerMenuSettingsUtils.isLongPressPowerForAssistEnabled(mContext)) {
|
||||
return DISABLED_DEPENDENT_SETTING;
|
||||
}
|
||||
|
||||
return AVAILABLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSliderPosition() {
|
||||
return mSensitivityValues == null ? 0 : closestValueIndex(mSensitivityValues,
|
||||
getCurrentSensitivityValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setSliderPosition(int position) {
|
||||
if (mSensitivityValues == null || position < 0 || position >= mSensitivityValues.length) {
|
||||
return false;
|
||||
}
|
||||
return Settings.Global.putInt(mContext.getContentResolver(),
|
||||
Settings.Global.POWER_BUTTON_LONG_PRESS_DURATION_MS,
|
||||
mSensitivityValues[position]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMax() {
|
||||
if (mSensitivityValues == null || mSensitivityValues.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
return mSensitivityValues.length - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMin() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int getCurrentSensitivityValue() {
|
||||
return Settings.Global.getInt(mContext.getContentResolver(),
|
||||
Settings.Global.POWER_BUTTON_LONG_PRESS_DURATION_MS,
|
||||
mContext.getResources().getInteger(
|
||||
com.android.internal.R.integer.config_longPressOnPowerDurationMs));
|
||||
}
|
||||
|
||||
private static int closestValueIndex(int[] values, int needle) {
|
||||
int minDistance = Integer.MAX_VALUE;
|
||||
int valueIndex = 0;
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
int diff = Math.abs(values[i] - needle);
|
||||
if (diff < minDistance) {
|
||||
minDistance = diff;
|
||||
valueIndex = i;
|
||||
}
|
||||
}
|
||||
return valueIndex;
|
||||
}
|
||||
}
|
@@ -16,30 +16,23 @@
|
||||
|
||||
package com.android.settings.gestures;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
import static com.android.settings.gestures.PowerMenuSettingsUtils.LONG_PRESS_POWER_ASSISTANT_VALUE;
|
||||
import static com.android.settings.gestures.PowerMenuSettingsUtils.LONG_PRESS_POWER_GLOBAL_ACTIONS;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
|
||||
public class PowerMenuPreferenceController extends BasePreferenceController {
|
||||
|
||||
private static final String POWER_BUTTON_LONG_PRESS_SETTING =
|
||||
Settings.Global.POWER_BUTTON_LONG_PRESS;
|
||||
|
||||
@VisibleForTesting
|
||||
static final int LONG_PRESS_POWER_GLOBAL_ACTIONS = 1;
|
||||
@VisibleForTesting
|
||||
static final int LONG_PRESS_POWER_ASSISTANT_VALUE = 5;
|
||||
|
||||
public PowerMenuPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getSummary() {
|
||||
final int powerButtonValue = getPowerButtonLongPressValue(mContext);
|
||||
final int powerButtonValue = PowerMenuSettingsUtils.getPowerButtonSettingValue(mContext);
|
||||
if (powerButtonValue == LONG_PRESS_POWER_ASSISTANT_VALUE) {
|
||||
return mContext.getText(R.string.power_menu_summary_long_press_for_assist_enabled);
|
||||
} else if (powerButtonValue == LONG_PRESS_POWER_GLOBAL_ACTIONS) {
|
||||
@@ -60,11 +53,4 @@ public class PowerMenuPreferenceController extends BasePreferenceController {
|
||||
return mContext.getResources().getBoolean(
|
||||
com.android.internal.R.bool.config_longPressOnPowerForAssistantSettingAvailable);
|
||||
}
|
||||
|
||||
private static int getPowerButtonLongPressValue(Context context) {
|
||||
return Settings.Global.getInt(context.getContentResolver(),
|
||||
POWER_BUTTON_LONG_PRESS_SETTING,
|
||||
context.getResources().getInteger(
|
||||
com.android.internal.R.integer.config_longPressOnPowerBehavior));
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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 android.content.Context;
|
||||
import android.provider.Settings;
|
||||
|
||||
/** Common code for long press power settings shared between controllers. */
|
||||
final class PowerMenuSettingsUtils {
|
||||
|
||||
/**
|
||||
* Setting storing the current behaviour of long press power.
|
||||
*/
|
||||
public static final String POWER_BUTTON_LONG_PRESS_SETTING =
|
||||
Settings.Global.POWER_BUTTON_LONG_PRESS;
|
||||
|
||||
/**
|
||||
* Value used for long press power button behaviour when the Assist setting is disabled.
|
||||
*
|
||||
* If this value matches Assist setting, then it falls back to Global Actions panel or
|
||||
* power menu, depending on their respective settings.
|
||||
*/
|
||||
public static final int POWER_BUTTON_LONG_PRESS_DEFAULT_VALUE_RESOURCE =
|
||||
com.android.internal.R.integer.config_longPressOnPowerBehavior;
|
||||
|
||||
/**
|
||||
* Values used for long press power button behaviour when Assist setting is enabled.
|
||||
*
|
||||
* {@link com.android.server.policy.PhoneWindowManager#LONG_PRESS_POWER_GLOBAL_ACTIONS} for
|
||||
* source of the value.
|
||||
*/
|
||||
static final int LONG_PRESS_POWER_NO_ACTION = 0;
|
||||
static final int LONG_PRESS_POWER_GLOBAL_ACTIONS = 1;
|
||||
static final int LONG_PRESS_POWER_ASSISTANT_VALUE = 5; // Settings.Secure.ASSISTANT
|
||||
|
||||
/**
|
||||
* @return current value of power button behaviour.
|
||||
*/
|
||||
public static int getPowerButtonSettingValue(Context context) {
|
||||
return Settings.Global.getInt(context.getContentResolver(),
|
||||
POWER_BUTTON_LONG_PRESS_SETTING,
|
||||
context.getResources().getInteger(POWER_BUTTON_LONG_PRESS_DEFAULT_VALUE_RESOURCE));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if long press power for assist is currently enabled.
|
||||
*/
|
||||
public static boolean isLongPressPowerForAssistEnabled(Context context) {
|
||||
return getPowerButtonSettingValue(context) == LONG_PRESS_POWER_ASSISTANT_VALUE;
|
||||
}
|
||||
|
||||
private PowerMenuSettingsUtils() {
|
||||
}
|
||||
}
|
@@ -20,9 +20,11 @@ import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.widget.SeekBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.content.res.TypedArrayUtils;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
|
||||
@@ -35,6 +37,8 @@ public class LabeledSeekBarPreference extends SeekBarPreference {
|
||||
private final int mTextEndId;
|
||||
private final int mTickMarkId;
|
||||
private OnPreferenceChangeListener mStopListener;
|
||||
@Nullable
|
||||
private CharSequence mSummary;
|
||||
|
||||
public LabeledSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr,
|
||||
int defStyleRes) {
|
||||
@@ -52,6 +56,7 @@ public class LabeledSeekBarPreference extends SeekBarPreference {
|
||||
R.string.summary_placeholder);
|
||||
mTickMarkId = styledAttrs.getResourceId(
|
||||
R.styleable.LabeledSeekBarPreference_tickMark, /* defValue= */ 0);
|
||||
mSummary = styledAttrs.getText(R.styleable.Preference_android_summary);
|
||||
styledAttrs.recycle();
|
||||
}
|
||||
|
||||
@@ -76,6 +81,15 @@ public class LabeledSeekBarPreference extends SeekBarPreference {
|
||||
com.android.internal.R.id.seekbar);
|
||||
seekBar.setTickMark(tickMark);
|
||||
}
|
||||
|
||||
final TextView summary = (TextView) holder.findViewById(android.R.id.summary);
|
||||
if (mSummary != null) {
|
||||
summary.setText(mSummary);
|
||||
summary.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
summary.setText(null);
|
||||
summary.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
public void setOnPreferenceChangeStopListener(OnPreferenceChangeListener listener) {
|
||||
@@ -90,5 +104,24 @@ public class LabeledSeekBarPreference extends SeekBarPreference {
|
||||
mStopListener.onPreferenceChange(this, seekBar.getProgress());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSummary(CharSequence summary) {
|
||||
super.setSummary(summary);
|
||||
mSummary = summary;
|
||||
notifyChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSummary(int summaryResId) {
|
||||
super.setSummary(summaryResId);
|
||||
mSummary = getContext().getText(summaryResId);
|
||||
notifyChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getSummary() {
|
||||
return mSummary;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user