Introduce alarm and media vibration intensity settings

Introduce toggles and sliders to configure the alarm and media
vibrations in the "Vibration & haptics" settings app.

Also update the multiple intensities configuration flag into a integer,
where the device can specify how many distinct levels are supported.
Follow existing implementation to map the intensities to higher setting
values.

Bug: 198346559
Bug: 207477604
Test: [Alarm|Media]Vibration[Intensity|Toggle]PreferenceControllerTest
Change-Id: Ie3d570b72ba1229e613ecf0c45fac81233529e32
This commit is contained in:
Lais Andrade
2022-01-07 20:12:19 +00:00
parent 8e7a57e8fa
commit eaaf5331d6
26 changed files with 1102 additions and 146 deletions

View File

@@ -0,0 +1,50 @@
/*
* Copyright (C) 2022 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.accessibility;
import android.content.Context;
import android.os.VibrationAttributes;
import android.provider.Settings;
/** Preference controller for alarm vibration intensity */
public class AlarmVibrationIntensityPreferenceController
extends VibrationIntensityPreferenceController {
/** General configuration for alarm vibration intensity settings. */
public static final class AlarmVibrationPreferenceConfig extends VibrationPreferenceConfig {
public AlarmVibrationPreferenceConfig(Context context) {
super(context, Settings.System.ALARM_VIBRATION_INTENSITY,
VibrationAttributes.USAGE_ALARM);
}
}
public AlarmVibrationIntensityPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey, new AlarmVibrationPreferenceConfig(context));
}
protected AlarmVibrationIntensityPreferenceController(Context context, String preferenceKey,
int supportedIntensityLevels) {
super(context, preferenceKey, new AlarmVibrationPreferenceConfig(context),
supportedIntensityLevels);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (C) 2022 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.accessibility;
import android.content.Context;
import com.android.settings.accessibility.AlarmVibrationIntensityPreferenceController.AlarmVibrationPreferenceConfig;
/** Preference controller for alarm vibration with only a toggle for on/off states. */
public class AlarmVibrationTogglePreferenceController extends VibrationTogglePreferenceController {
public AlarmVibrationTogglePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey, new AlarmVibrationPreferenceConfig(context));
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}

View File

@@ -74,6 +74,12 @@ public class HapticFeedbackIntensityPreferenceController
super(context, preferenceKey, new HapticFeedbackVibrationPreferenceConfig(context));
}
protected HapticFeedbackIntensityPreferenceController(Context context, String preferenceKey,
int supportedIntensityLevels) {
super(context, preferenceKey, new HapticFeedbackVibrationPreferenceConfig(context),
supportedIntensityLevels);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;

View File

@@ -0,0 +1,51 @@
/*
* Copyright (C) 2022 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.accessibility;
import android.content.Context;
import android.os.VibrationAttributes;
import android.provider.Settings;
/** Preference controller for am vibration intensity */
public class MediaVibrationIntensityPreferenceController
extends VibrationIntensityPreferenceController {
/** General configuration for alarm vibration intensity settings. */
public static final class MediaVibrationPreferenceConfig extends VibrationPreferenceConfig {
public MediaVibrationPreferenceConfig(Context context) {
super(context, Settings.System.MEDIA_VIBRATION_INTENSITY,
VibrationAttributes.USAGE_MEDIA);
}
}
public MediaVibrationIntensityPreferenceController(Context context,
String preferenceKey) {
super(context, preferenceKey, new MediaVibrationPreferenceConfig(context));
}
protected MediaVibrationIntensityPreferenceController(Context context, String preferenceKey,
int supportedIntensityLevels) {
super(context, preferenceKey, new MediaVibrationPreferenceConfig(context),
supportedIntensityLevels);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (C) 2022 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.accessibility;
import android.content.Context;
import com.android.settings.accessibility.MediaVibrationIntensityPreferenceController.MediaVibrationPreferenceConfig;
/** Preference controller for alarm vibration with only a toggle for on/off states. */
public class MediaVibrationTogglePreferenceController extends VibrationTogglePreferenceController {
public MediaVibrationTogglePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey, new MediaVibrationPreferenceConfig(context));
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}

View File

@@ -39,6 +39,12 @@ public class NotificationVibrationIntensityPreferenceController
super(context, preferenceKey, new NotificationVibrationPreferenceConfig(context));
}
protected NotificationVibrationIntensityPreferenceController(Context context,
String preferenceKey, int supportedIntensityLevels) {
super(context, preferenceKey, new NotificationVibrationPreferenceConfig(context),
supportedIntensityLevels);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;

View File

@@ -71,6 +71,12 @@ public class RingVibrationIntensityPreferenceController
super(context, preferenceKey, new RingVibrationPreferenceConfig(context));
}
protected RingVibrationIntensityPreferenceController(Context context, String preferenceKey,
int supportedIntensityLevels) {
super(context, preferenceKey, new RingVibrationPreferenceConfig(context),
supportedIntensityLevels);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;

View File

@@ -21,6 +21,7 @@ import android.os.Vibrator;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.SliderPreferenceController;
import com.android.settings.widget.SeekBarPreference;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
@@ -36,13 +37,22 @@ public abstract class VibrationIntensityPreferenceController extends SliderPrefe
protected final VibrationPreferenceConfig mPreferenceConfig;
private final VibrationPreferenceConfig.SettingObserver mSettingsContentObserver;
private final int mMaxIntensity;
protected VibrationIntensityPreferenceController(Context context, String prefkey,
VibrationPreferenceConfig preferenceConfig) {
this(context, prefkey, preferenceConfig,
context.getResources().getInteger(
R.integer.config_vibration_supported_intensity_levels));
}
protected VibrationIntensityPreferenceController(Context context, String prefkey,
VibrationPreferenceConfig preferenceConfig, int supportedIntensityLevels) {
super(context, prefkey);
mPreferenceConfig = preferenceConfig;
mSettingsContentObserver = new VibrationPreferenceConfig.SettingObserver(
preferenceConfig);
mMaxIntensity = Math.min(Vibrator.VIBRATION_INTENSITY_HIGH, supportedIntensityLevels);
}
@Override
@@ -74,7 +84,7 @@ public abstract class VibrationIntensityPreferenceController extends SliderPrefe
@Override
public int getMax() {
return Vibrator.VIBRATION_INTENSITY_HIGH;
return mMaxIntensity;
}
@Override
@@ -85,7 +95,8 @@ public abstract class VibrationIntensityPreferenceController extends SliderPrefe
@Override
public boolean setSliderPosition(int position) {
final boolean success = mPreferenceConfig.updateIntensity(position);
final int intensity = calculateVibrationIntensity(position);
final boolean success = mPreferenceConfig.updateIntensity(intensity);
if (success && (position != Vibrator.VIBRATION_INTENSITY_OFF)) {
mPreferenceConfig.playVibrationPreview();
@@ -93,4 +104,19 @@ public abstract class VibrationIntensityPreferenceController extends SliderPrefe
return success;
}
private int calculateVibrationIntensity(int position) {
int maxPosition = getMax();
if (position >= maxPosition) {
if (maxPosition == 1) {
// If there is only one intensity available besides OFF, then use the device default
// intensity to ensure no scaling will ever happen in the platform.
return mPreferenceConfig.getDefaultIntensity();
}
// If the settings granularity is lower than the platform's then map the max position to
// the highest vibration intensity, skipping intermediate values in the scale.
return Vibrator.VIBRATION_INTENSITY_HIGH;
}
return position;
}
}

View File

@@ -21,7 +21,6 @@ import android.database.ContentObserver;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.DeviceConfig;
@@ -37,8 +36,6 @@ import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
import com.google.common.annotations.VisibleForTesting;
/**
* Preference controller for the ramping ringer setting key, controlled via {@link AudioManager}.
*
@@ -50,9 +47,15 @@ import com.google.common.annotations.VisibleForTesting;
public class VibrationRampingRingerTogglePreferenceController
extends TogglePreferenceController implements LifecycleObserver, OnStart, OnStop {
@VisibleForTesting
static final String DEVICE_CONFIG_KEY = "ramping_ringer_enabled";
/** Wrapper around static {@link DeviceConfig} accessor for testing. */
protected static class DeviceConfigProvider {
public boolean isRampingRingerEnabledOnTelephonyConfig() {
return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_TELEPHONY,
"ramping_ringer_enabled", false);
}
}
private final DeviceConfigProvider mDeviceConfigProvider;
private final ContentObserver mSettingObserver;
private final Vibrator mVibrator;
private final AudioManager mAudioManager;
@@ -60,10 +63,16 @@ public class VibrationRampingRingerTogglePreferenceController
private Preference mPreference;
public VibrationRampingRingerTogglePreferenceController(Context context, String preferenceKey) {
this(context, preferenceKey, new DeviceConfigProvider());
}
protected VibrationRampingRingerTogglePreferenceController(Context context,
String preferenceKey, DeviceConfigProvider deviceConfigProvider) {
super(context, preferenceKey);
mDeviceConfigProvider = deviceConfigProvider;
mVibrator = context.getSystemService(Vibrator.class);
mAudioManager = context.getSystemService(AudioManager.class);
mSettingObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
mSettingObserver = new ContentObserver(new Handler(/* async= */ true)) {
@Override
public void onChange(boolean selfChange, Uri uri) {
updateState(mPreference);
@@ -74,7 +83,7 @@ public class VibrationRampingRingerTogglePreferenceController
@Override
public int getAvailabilityStatus() {
final boolean rampingRingerEnabledOnTelephonyConfig =
DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_TELEPHONY, DEVICE_CONFIG_KEY, false);
mDeviceConfigProvider.isRampingRingerEnabledOnTelephonyConfig();
return (Utils.isVoiceCapable(mContext) && !rampingRingerEnabledOnTelephonyConfig)
? AVAILABLE
: UNSUPPORTED_ON_DEVICE;

View File

@@ -41,9 +41,9 @@ public class VibrationSettings extends DashboardFragment {
@Override
protected int getPreferenceScreenResId() {
final boolean supportsMultipleIntensities = getContext().getResources().getBoolean(
R.bool.config_vibration_supports_multiple_intensities);
return supportsMultipleIntensities
final int supportedIntensities = getContext().getResources().getInteger(
R.integer.config_vibration_supported_intensity_levels);
return supportedIntensities > 1
? R.xml.accessibility_vibration_intensity_settings
: R.xml.accessibility_vibration_settings;
}