diff --git a/res/layout/preference_labeled_slider.xml b/res/layout/preference_labeled_slider.xml index 75902cf3949..f120a44adeb 100644 --- a/res/layout/preference_labeled_slider.xml +++ b/res/layout/preference_labeled_slider.xml @@ -37,9 +37,18 @@ android:textAppearance="?android:attr/textAppearanceListItem" android:textColor="?android:attr/textColorPrimary" /> + + Prevent ringing:\nShortcut available in the volume menu. + + Press & hold duration + + Adjust sensitivity by choosing how long to press & hold the power button + + Short + + Long + Show wallet diff --git a/res/xml/power_menu_settings.xml b/res/xml/power_menu_settings.xml index caaeb979387..121a2dac472 100644 --- a/res/xml/power_menu_settings.xml +++ b/res/xml/power_menu_settings.xml @@ -22,11 +22,21 @@ android:title="@string/power_menu_long_press_for_assist" android:summary="@string/power_menu_long_press_for_assist_summary" settings:controller="com.android.settings.gestures.LongPressPowerButtonPreferenceController" - /> + /> + + + settings:searchable="false"/> diff --git a/src/com/android/settings/gestures/LongPressPowerButtonPreferenceController.java b/src/com/android/settings/gestures/LongPressPowerButtonPreferenceController.java index a52be15f62e..44494eeced2 100644 --- a/src/com/android/settings/gestures/LongPressPowerButtonPreferenceController.java +++ b/src/com/android/settings/gestures/LongPressPowerButtonPreferenceController.java @@ -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, diff --git a/src/com/android/settings/gestures/LongPressPowerSensitivityPreferenceController.java b/src/com/android/settings/gestures/LongPressPowerSensitivityPreferenceController.java new file mode 100644 index 00000000000..a1d681cd475 --- /dev/null +++ b/src/com/android/settings/gestures/LongPressPowerSensitivityPreferenceController.java @@ -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; + } +} diff --git a/src/com/android/settings/gestures/PowerMenuPreferenceController.java b/src/com/android/settings/gestures/PowerMenuPreferenceController.java index 0d5f810d884..b4f97aa3ff2 100644 --- a/src/com/android/settings/gestures/PowerMenuPreferenceController.java +++ b/src/com/android/settings/gestures/PowerMenuPreferenceController.java @@ -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)); - } } diff --git a/src/com/android/settings/gestures/PowerMenuSettingsUtils.java b/src/com/android/settings/gestures/PowerMenuSettingsUtils.java new file mode 100644 index 00000000000..444b7c1e565 --- /dev/null +++ b/src/com/android/settings/gestures/PowerMenuSettingsUtils.java @@ -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() { + } +} diff --git a/src/com/android/settings/widget/LabeledSeekBarPreference.java b/src/com/android/settings/widget/LabeledSeekBarPreference.java index 3a9ac18beb0..b34ea1946f9 100644 --- a/src/com/android/settings/widget/LabeledSeekBarPreference.java +++ b/src/com/android/settings/widget/LabeledSeekBarPreference.java @@ -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; + } } diff --git a/tests/robotests/src/com/android/settings/gestures/LabeledSeekBarPreferenceTest.java b/tests/robotests/src/com/android/settings/gestures/LabeledSeekBarPreferenceTest.java index 4fb94f8cad5..3d83d58141b 100644 --- a/tests/robotests/src/com/android/settings/gestures/LabeledSeekBarPreferenceTest.java +++ b/tests/robotests/src/com/android/settings/gestures/LabeledSeekBarPreferenceTest.java @@ -16,6 +16,8 @@ package com.android.settings.gestures; +import static com.google.common.truth.Truth.assertThat; + import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -24,9 +26,12 @@ import android.view.LayoutInflater; import android.view.View; import android.widget.LinearLayout; import android.widget.SeekBar; +import android.widget.TextView; import androidx.preference.Preference; +import androidx.preference.PreferenceViewHolder; +import com.android.internal.R; import com.android.settings.widget.LabeledSeekBarPreference; import org.junit.Before; @@ -41,7 +46,9 @@ import org.robolectric.RuntimeEnvironment; public class LabeledSeekBarPreferenceTest { private Context mContext; + private PreferenceViewHolder mViewHolder; private SeekBar mSeekBar; + private TextView mSummary; private LabeledSeekBarPreference mSeekBarPreference; @Mock @@ -57,7 +64,9 @@ public class LabeledSeekBarPreferenceTest { final View view = inflater.inflate(mSeekBarPreference.getLayoutResource(), new LinearLayout(mContext), false); - mSeekBar = view.findViewById(com.android.internal.R.id.seekbar); + mViewHolder = PreferenceViewHolder.createInstanceForTests(view); + mSeekBar = (SeekBar) mViewHolder.findViewById(R.id.seekbar); + mSummary = (TextView) mViewHolder.findViewById(R.id.summary); } @Test @@ -69,4 +78,23 @@ public class LabeledSeekBarPreferenceTest { verify(mListener, times(1)).onPreferenceChange(mSeekBarPreference, 2); } + + @Test + public void seekBarPreferenceSummarySet_returnsValue() { + final String summary = "this is a summary"; + mSeekBarPreference.setSummary(summary); + mSeekBarPreference.onBindViewHolder(mViewHolder); + + assertThat(mSeekBarPreference.getSummary()).isEqualTo(summary); + assertThat(mSummary.getText()).isEqualTo(summary); + } + + @Test + public void seekBarPreferenceSummaryNull_hidesView() { + mSeekBarPreference.setSummary(null); + mSeekBarPreference.onBindViewHolder(mViewHolder); + + assertThat(mSummary.getText()).isEqualTo(""); + assertThat(mSummary.getVisibility()).isEqualTo(View.GONE); + } } diff --git a/tests/robotests/src/com/android/settings/gestures/LongPressPowerButtonPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/gestures/LongPressPowerButtonPreferenceControllerTest.java index 8ff50abff8f..fa97754491c 100644 --- a/tests/robotests/src/com/android/settings/gestures/LongPressPowerButtonPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/gestures/LongPressPowerButtonPreferenceControllerTest.java @@ -137,7 +137,7 @@ public class LongPressPowerButtonPreferenceControllerTest { when(mResources.getInteger( com.android.internal.R.integer.config_keyChordPowerVolumeUp)) .thenReturn( - LongPressPowerButtonPreferenceController.KEY_CHORD_POWER_VOLUME_UP_NO_ACTION); + LongPressPowerButtonPreferenceController.KEY_CHORD_POWER_VOLUME_UP_NO_ACTION); verify(mController.mFooterHint).setVisible(false); } @@ -148,7 +148,7 @@ public class LongPressPowerButtonPreferenceControllerTest { assertThat(Settings.Global.getInt(mContext.getContentResolver(), Settings.Global.POWER_BUTTON_LONG_PRESS, -1)).isEqualTo( - LongPressPowerButtonPreferenceController.LONG_PRESS_POWER_ASSISTANT_VALUE); + PowerMenuSettingsUtils.LONG_PRESS_POWER_ASSISTANT_VALUE); assertThat(Settings.Global.getInt(mContext.getContentResolver(), Settings.Global.KEY_CHORD_POWER_VOLUME_UP, -1)).isEqualTo( LongPressPowerButtonPreferenceController.KEY_CHORD_POWER_VOLUME_UP_GLOBAL_ACTIONS); @@ -162,19 +162,19 @@ public class LongPressPowerButtonPreferenceControllerTest { when(mResources.getInteger( com.android.internal.R.integer.config_longPressOnPowerBehavior)) .thenReturn( - LongPressPowerButtonPreferenceController.LONG_PRESS_POWER_GLOBAL_ACTIONS); + PowerMenuSettingsUtils.LONG_PRESS_POWER_GLOBAL_ACTIONS); mController.onPreferenceChange(null, false); assertThat(Settings.Global.getInt(mContext.getContentResolver(), Settings.Global.POWER_BUTTON_LONG_PRESS, -1)).isEqualTo( - LongPressPowerButtonPreferenceController.LONG_PRESS_POWER_GLOBAL_ACTIONS); + PowerMenuSettingsUtils.LONG_PRESS_POWER_GLOBAL_ACTIONS); assertThat(Settings.Global.getInt(mContext.getContentResolver(), Settings.Global.KEY_CHORD_POWER_VOLUME_UP, -1)).isEqualTo( LongPressPowerButtonPreferenceController.KEY_CHORD_POWER_VOLUME_UP_NO_ACTION); verify(mController.mAssistSwitch).setSummary( getString( - R.string.power_menu_summary_long_press_for_assist_disabled_with_power_menu)); + R.string.power_menu_summary_long_press_for_assist_disabled_with_power_menu)); } @Test @@ -203,13 +203,13 @@ public class LongPressPowerButtonPreferenceControllerTest { when(mResources.getInteger( com.android.internal.R.integer.config_longPressOnPowerBehavior)) .thenReturn( - LongPressPowerButtonPreferenceController.LONG_PRESS_POWER_ASSISTANT_VALUE); + PowerMenuSettingsUtils.LONG_PRESS_POWER_ASSISTANT_VALUE); mController.onPreferenceChange(null, false); assertThat(Settings.Global.getInt(mContext.getContentResolver(), Settings.Global.POWER_BUTTON_LONG_PRESS, -1)).isEqualTo( - LongPressPowerButtonPreferenceController.LONG_PRESS_POWER_GLOBAL_ACTIONS); + PowerMenuSettingsUtils.LONG_PRESS_POWER_GLOBAL_ACTIONS); assertThat(Settings.Global.getInt(mContext.getContentResolver(), Settings.Global.KEY_CHORD_POWER_VOLUME_UP, -1)).isEqualTo( LongPressPowerButtonPreferenceController.KEY_CHORD_POWER_VOLUME_UP_NO_ACTION); diff --git a/tests/robotests/src/com/android/settings/gestures/LongPressPowerSensitivityPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/gestures/LongPressPowerSensitivityPreferenceControllerTest.java new file mode 100644 index 00000000000..9c99092849d --- /dev/null +++ b/tests/robotests/src/com/android/settings/gestures/LongPressPowerSensitivityPreferenceControllerTest.java @@ -0,0 +1,143 @@ +/* + * 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.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.app.Application; +import android.content.res.Resources; +import android.provider.Settings; + +import androidx.test.core.app.ApplicationProvider; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +@RunWith(RobolectricTestRunner.class) +public class LongPressPowerSensitivityPreferenceControllerTest { + + private static final String KEY_LONG_PRESS_SENSITIVITY = + "gesture_power_menu_long_press_for_assist_sensitivity"; + + private static final int[] SENSITIVITY_VALUES = {250, 350, 500, 750, 850}; + + private Application mContext; + private Resources mResources; + private LongPressPowerSensitivityPreferenceController mController; + + @Before + public void setUp() { + mContext = spy(ApplicationProvider.getApplicationContext()); + mResources = mock(Resources.class); + when(mContext.getResources()).thenReturn(mResources); + + when(mResources.getIntArray( + com.android.internal.R.array.config_longPressOnPowerDurationSettings)) + .thenReturn(SENSITIVITY_VALUES); + + mController = new LongPressPowerSensitivityPreferenceController(mContext, + KEY_LONG_PRESS_SENSITIVITY); + } + + @Test + public void getSliderPosition_returnsDefaultValue() { + when(mResources.getInteger( + com.android.internal.R.integer.config_longPressOnPowerDurationMs)) + .thenReturn(750); + assertThat(mController.getSliderPosition()).isEqualTo(3); + } + + @Test + public void getSliderPosition_returnsSetValue() { + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.POWER_BUTTON_LONG_PRESS_DURATION_MS, 350); + assertThat(mController.getSliderPosition()).isEqualTo(1); + } + + @Test + public void setSliderPosition_setsValue() { + mController.setSliderPosition(4); + assertThat(Settings.Global.getInt(mContext.getContentResolver(), + Settings.Global.POWER_BUTTON_LONG_PRESS_DURATION_MS, 0)).isEqualTo(850); + } + + @Test + public void setSliderPositionOutOfBounds_returnsFalse() { + assertThat(mController.setSliderPosition(-1)).isFalse(); + assertThat(mController.setSliderPosition(10)).isFalse(); + } + + @Test + public void getMin_isZero() { + assertThat(mController.getMin()).isEqualTo(0); + } + + @Test + public void getMax_isEqualToLastValueIndex() { + assertThat(mController.getMax()).isEqualTo(4); + } + + @Test + public void longPressForAssistEnabled_isAvailable() { + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.POWER_BUTTON_LONG_PRESS, + PowerMenuSettingsUtils.LONG_PRESS_POWER_ASSISTANT_VALUE); + + assertThat(mController.getAvailabilityStatus()).isEqualTo( + LongPressPowerSensitivityPreferenceController.AVAILABLE); + } + + @Test + public void longPressForAssistDisabled_isNotAvailableDueToDependentSetting() { + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.POWER_BUTTON_LONG_PRESS, + PowerMenuSettingsUtils.LONG_PRESS_POWER_NO_ACTION); + + assertThat(mController.getAvailabilityStatus()).isEqualTo( + LongPressPowerSensitivityPreferenceController.DISABLED_DEPENDENT_SETTING); + } + + @Test + public void sensitivityValuesAreNull_notAvailable() { + when(mResources.getIntArray( + com.android.internal.R.array.config_longPressOnPowerDurationSettings)) + .thenReturn(null); + mController = new LongPressPowerSensitivityPreferenceController(mContext, + KEY_LONG_PRESS_SENSITIVITY); + + assertThat(mController.getAvailabilityStatus()).isEqualTo( + LongPressPowerSensitivityPreferenceController.UNSUPPORTED_ON_DEVICE); + } + + @Test + public void sensitivityValuesArrayTooShort_notAvailable() { + when(mResources.getIntArray( + com.android.internal.R.array.config_longPressOnPowerDurationSettings)) + .thenReturn(new int[]{200}); + mController = new LongPressPowerSensitivityPreferenceController(mContext, + KEY_LONG_PRESS_SENSITIVITY); + + assertThat(mController.getAvailabilityStatus()).isEqualTo( + LongPressPowerSensitivityPreferenceController.UNSUPPORTED_ON_DEVICE); + } +} diff --git a/tests/robotests/src/com/android/settings/gestures/PowerMenuSettingsUtilsTest.java b/tests/robotests/src/com/android/settings/gestures/PowerMenuSettingsUtilsTest.java new file mode 100644 index 00000000000..25f03207284 --- /dev/null +++ b/tests/robotests/src/com/android/settings/gestures/PowerMenuSettingsUtilsTest.java @@ -0,0 +1,91 @@ +/* + * 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.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 org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +@RunWith(RobolectricTestRunner.class) +public class PowerMenuSettingsUtilsTest { + + 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 longPressBehaviourValuePresent_returnsValue() { + when(mResources.getInteger( + com.android.internal.R.integer.config_longPressOnPowerBehavior)) + .thenReturn(0); + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.POWER_BUTTON_LONG_PRESS, 1); + + assertThat(PowerMenuSettingsUtils.getPowerButtonSettingValue(mContext)).isEqualTo(1); + } + + @Test + public void longPressBehaviourValueNotPresent_returnsDefault() { + when(mResources.getInteger( + com.android.internal.R.integer.config_longPressOnPowerBehavior)) + .thenReturn(2); + + assertThat(PowerMenuSettingsUtils.getPowerButtonSettingValue(mContext)).isEqualTo(2); + } + + @Test + public void longPressBehaviourValueSetToAssistant_isAssistEnabledReturnsTrue() { + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.POWER_BUTTON_LONG_PRESS, 5); + assertThat(PowerMenuSettingsUtils.isLongPressPowerForAssistEnabled(mContext)).isTrue(); + } + + @Test + public void longPressBehaviourValueNotSetToAssistant_isAssistEnabledReturnsFalse() { + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.POWER_BUTTON_LONG_PRESS, 3); + assertThat(PowerMenuSettingsUtils.isLongPressPowerForAssistEnabled(mContext)).isFalse(); + } + + @Test + public void longPressBehaviourDefaultSetToAssistant_isAssistEnabledReturnsFalse() { + when(mResources.getInteger( + com.android.internal.R.integer.config_longPressOnPowerBehavior)) + .thenReturn(3); + + assertThat(PowerMenuSettingsUtils.isLongPressPowerForAssistEnabled(mContext)).isFalse(); + } +}