Update Settings to use intensity settings as main preference keys
Updating the Settings app to allow setting the value off for key HAPTIC_FEEDBACK_INTENSITY. This setting state is also copied onto HAPTIC_FEEDBACK_ENABLED setting, so both should be in sync after this change. Similar logic is applied between RING_VIBRATION_INTENSITY and VIBRATE_WHEN_RINGING. This will not disable the hardware feedback since that one is controlled by a separate setting key now. The "vibrate for calls" was also removed and the single toggle for "vibrate first then ring gradually" was moved into the "Vibration & haptics" page. Bug: 185351540 Test: [HapticFeedback|NotificationVibration|RingVibration][Intensity|Toggle]PreferenceControllerTest and manual testing of the AOSP settings app Change-Id: I9c94cef331a1500a1272a601ba32667ca995ddab
This commit is contained in:
@@ -17,116 +17,80 @@
|
||||
package com.android.settings.accessibility;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.ContentObserver;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Vibrator;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.core.SliderPreferenceController;
|
||||
import com.android.settings.widget.SeekBarPreference;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||
|
||||
public abstract class VibrationIntensityPreferenceController extends BasePreferenceController
|
||||
/**
|
||||
* Abstract preference controller for a vibration intensity setting, that displays multiple
|
||||
* intensity levels to the user as a slider.
|
||||
*/
|
||||
public abstract class VibrationIntensityPreferenceController extends SliderPreferenceController
|
||||
implements LifecycleObserver, OnStart, OnStop {
|
||||
|
||||
protected final Vibrator mVibrator;
|
||||
private final SettingObserver mSettingsContentObserver;
|
||||
private final String mSettingKey;
|
||||
private final String mEnabledKey;
|
||||
private final boolean mSupportRampingRinger;
|
||||
protected final VibrationPreferenceConfig mPreferenceConfig;
|
||||
private final VibrationPreferenceConfig.SettingObserver mSettingsContentObserver;
|
||||
|
||||
private Preference mPreference;
|
||||
|
||||
public VibrationIntensityPreferenceController(Context context, String prefkey,
|
||||
String settingKey, String enabledKey, boolean supportRampingRinger) {
|
||||
protected VibrationIntensityPreferenceController(Context context, String prefkey,
|
||||
VibrationPreferenceConfig preferenceConfig) {
|
||||
super(context, prefkey);
|
||||
mVibrator = mContext.getSystemService(Vibrator.class);
|
||||
mSettingKey = settingKey;
|
||||
mEnabledKey = enabledKey;
|
||||
mSupportRampingRinger= supportRampingRinger;
|
||||
mSettingsContentObserver = new SettingObserver(settingKey) {
|
||||
@Override
|
||||
public void onChange(boolean selfChange, Uri uri) {
|
||||
updateState(mPreference);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public VibrationIntensityPreferenceController(Context context, String prefkey,
|
||||
String settingKey, String enabledKey) {
|
||||
this(context, prefkey, settingKey, enabledKey, /* supportRampingRinger= */ false);
|
||||
mPreferenceConfig = preferenceConfig;
|
||||
mSettingsContentObserver = new VibrationPreferenceConfig.SettingObserver(
|
||||
preferenceConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
mContext.getContentResolver().registerContentObserver(
|
||||
mSettingsContentObserver.uri,
|
||||
false /* notifyForDescendants */,
|
||||
mSettingsContentObserver);
|
||||
mSettingsContentObserver.register(mContext.getContentResolver());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
mContext.getContentResolver().unregisterContentObserver(mSettingsContentObserver);
|
||||
mSettingsContentObserver.unregister(mContext.getContentResolver());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPreference = screen.findPreference(getPreferenceKey());
|
||||
final SeekBarPreference preference = screen.findPreference(getPreferenceKey());
|
||||
mSettingsContentObserver.onDisplayPreference(this, preference);
|
||||
// TODO: remove this and replace with a different way to play the haptic preview without
|
||||
// relying on the setting being propagated to the service.
|
||||
preference.setContinuousUpdates(true);
|
||||
preference.setMin(getMin());
|
||||
preference.setMax(getMax());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getSummary() {
|
||||
final int intensity = Settings.System.getInt(mContext.getContentResolver(),
|
||||
mSettingKey, getDefaultIntensity());
|
||||
final boolean enabled = (Settings.System.getInt(mContext.getContentResolver(),
|
||||
mEnabledKey, 1) == 1) ||
|
||||
(mSupportRampingRinger && AccessibilitySettings.isRampingRingerEnabled(mContext));
|
||||
return getIntensityString(mContext, enabled ? intensity : Vibrator.VIBRATION_INTENSITY_OFF);
|
||||
public int getMin() {
|
||||
return Vibrator.VIBRATION_INTENSITY_OFF;
|
||||
}
|
||||
|
||||
public static CharSequence getIntensityString(Context context, int intensity) {
|
||||
final boolean supportsMultipleIntensities = context.getResources().getBoolean(
|
||||
R.bool.config_vibration_supports_multiple_intensities);
|
||||
if (supportsMultipleIntensities) {
|
||||
switch (intensity) {
|
||||
case Vibrator.VIBRATION_INTENSITY_OFF:
|
||||
return context.getString(R.string.accessibility_vibration_intensity_off);
|
||||
case Vibrator.VIBRATION_INTENSITY_LOW:
|
||||
return context.getString(R.string.accessibility_vibration_intensity_low);
|
||||
case Vibrator.VIBRATION_INTENSITY_MEDIUM:
|
||||
return context.getString(R.string.accessibility_vibration_intensity_medium);
|
||||
case Vibrator.VIBRATION_INTENSITY_HIGH:
|
||||
return context.getString(R.string.accessibility_vibration_intensity_high);
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
} else {
|
||||
if (intensity == Vibrator.VIBRATION_INTENSITY_OFF) {
|
||||
return context.getString(R.string.switch_off_text);
|
||||
} else {
|
||||
return context.getString(R.string.switch_on_text);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public int getMax() {
|
||||
return Vibrator.VIBRATION_INTENSITY_HIGH;
|
||||
}
|
||||
|
||||
protected abstract int getDefaultIntensity();
|
||||
@Override
|
||||
public int getSliderPosition() {
|
||||
final int position = mPreferenceConfig.readIntensity();
|
||||
return Math.min(position, getMax());
|
||||
}
|
||||
|
||||
private static class SettingObserver extends ContentObserver {
|
||||
@Override
|
||||
public boolean setSliderPosition(int position) {
|
||||
final boolean success = mPreferenceConfig.updateIntensity(position);
|
||||
|
||||
public final Uri uri;
|
||||
|
||||
public SettingObserver(String settingKey) {
|
||||
super(new Handler(Looper.getMainLooper()));
|
||||
uri = Settings.System.getUriFor(settingKey);
|
||||
if (success && (position != Vibrator.VIBRATION_INTENSITY_OFF)) {
|
||||
mPreferenceConfig.playVibrationPreview();
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user