Merge "Update Settings to use intensity settings as main preference keys"

This commit is contained in:
Lais Andrade
2022-01-13 02:45:41 +00:00
committed by Android (Google) Code Review
40 changed files with 1543 additions and 1703 deletions

View File

@@ -294,11 +294,6 @@ public class AccessibilitySettings extends DashboardFragment {
return info.loadDescription(context.getPackageManager());
}
static boolean isRampingRingerEnabled(final Context context) {
return Settings.System.getInt(
context.getContentResolver(), Settings.System.APPLY_RAMPING_RINGER, 0) == 1;
}
@VisibleForTesting
void onContentChanged() {
// If the fragment is visible then update preferences immediately, else set the flag then

View File

@@ -16,31 +16,66 @@
package com.android.settings.accessibility;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import android.content.Context;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.Settings;
import androidx.annotation.VisibleForTesting;
/** Preference controller for haptic feedback intensity */
public class HapticFeedbackIntensityPreferenceController
extends VibrationIntensityPreferenceController {
@VisibleForTesting
static final String PREF_KEY = "touch_vibration_preference_screen";
/** General configuration for haptic feedback intensity settings. */
public static final class HapticFeedbackVibrationPreferenceConfig
extends VibrationPreferenceConfig {
public HapticFeedbackIntensityPreferenceController(Context context) {
super(context, PREF_KEY, Settings.System.HAPTIC_FEEDBACK_INTENSITY,
Settings.System.HAPTIC_FEEDBACK_ENABLED);
public HapticFeedbackVibrationPreferenceConfig(Context context) {
super(context, Settings.System.HAPTIC_FEEDBACK_INTENSITY,
VibrationAttributes.USAGE_TOUCH);
}
@Override
public int readIntensity() {
final int hapticFeedbackEnabled = Settings.System.getInt(mContentResolver,
Settings.System.HAPTIC_FEEDBACK_ENABLED, ON);
if (hapticFeedbackEnabled == OFF) {
// HAPTIC_FEEDBACK_ENABLED is deprecated but should still be applied if the user has
// turned it off already.
return Vibrator.VIBRATION_INTENSITY_OFF;
}
return super.readIntensity();
}
@Override
public boolean updateIntensity(int intensity) {
final boolean success = super.updateIntensity(intensity);
final boolean isIntensityOff = intensity == Vibrator.VIBRATION_INTENSITY_OFF;
Settings.System.putInt(mContentResolver, Settings.System.HAPTIC_FEEDBACK_ENABLED,
isIntensityOff ? OFF : ON);
// HAPTIC_FEEDBACK_ENABLED is deprecated but should still reflect the intensity setting.
// HARDWARE_HAPTIC_FEEDBACK_INTENSITY is dependent on this setting, but should not be
// disabled by it.
Settings.System.putInt(mContentResolver,
Settings.System.HARDWARE_HAPTIC_FEEDBACK_INTENSITY,
isIntensityOff ? getDefaultIntensity() : intensity);
return success;
}
}
public HapticFeedbackIntensityPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey, new HapticFeedbackVibrationPreferenceConfig(context));
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
protected int getDefaultIntensity() {
return mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_TOUCH);
}
}

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.HapticFeedbackIntensityPreferenceController.HapticFeedbackVibrationPreferenceConfig;
/** Preference controller for haptic feedback with only a toggle for on/off states. */
public class HapticFeedbackTogglePreferenceController extends VibrationTogglePreferenceController {
public HapticFeedbackTogglePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey, new HapticFeedbackVibrationPreferenceConfig(context));
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}

View File

@@ -20,25 +20,27 @@ import android.content.Context;
import android.os.VibrationAttributes;
import android.provider.Settings;
import androidx.annotation.VisibleForTesting;
/** Preference controller for notification vibration intensity */
public class NotificationVibrationIntensityPreferenceController
extends VibrationIntensityPreferenceController {
@VisibleForTesting
static final String PREF_KEY = "notification_vibration_preference_screen";
/** General configuration for notification vibration intensity settings. */
public static final class NotificationVibrationPreferenceConfig
extends VibrationPreferenceConfig {
public NotificationVibrationIntensityPreferenceController(Context context) {
super(context, PREF_KEY, Settings.System.NOTIFICATION_VIBRATION_INTENSITY, "");
public NotificationVibrationPreferenceConfig(Context context) {
super(context, Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
VibrationAttributes.USAGE_NOTIFICATION);
}
}
public NotificationVibrationIntensityPreferenceController(Context context,
String preferenceKey) {
super(context, preferenceKey, new NotificationVibrationPreferenceConfig(context));
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
protected int getDefaultIntensity() {
return mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_NOTIFICATION);
}
}

View File

@@ -1,63 +0,0 @@
/*
* Copyright (C) 2018 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.app.settings.SettingsEnums;
import android.media.AudioAttributes;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.Settings;
import com.android.settings.R;
/**
* Fragment for picking accessibility shortcut service
*/
public class NotificationVibrationPreferenceFragment extends VibrationPreferenceFragment {
@Override
public int getMetricsCategory() {
return SettingsEnums.ACCESSIBILITY_VIBRATION_NOTIFICATION;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.accessibility_notification_vibration_settings;
}
/**
* Get the setting string of the vibration intensity setting this preference is dealing with.
*/
@Override
protected String getVibrationIntensitySetting() {
return Settings.System.NOTIFICATION_VIBRATION_INTENSITY;
}
@Override
protected String getVibrationEnabledSetting() {
return "";
}
@Override
protected int getPreviewVibrationAudioAttributesUsage() {
return AudioAttributes.USAGE_NOTIFICATION;
}
@Override
protected int getDefaultVibrationIntensity() {
Vibrator vibrator = getContext().getSystemService(Vibrator.class);
return vibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_NOTIFICATION);
}
}

View File

@@ -0,0 +1,35 @@
/*
* 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.NotificationVibrationIntensityPreferenceController.NotificationVibrationPreferenceConfig;
/** Preference controller for notification vibration with only a toggle for on/off states. */
public class NotificationVibrationTogglePreferenceController
extends VibrationTogglePreferenceController {
public NotificationVibrationTogglePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey, new NotificationVibrationPreferenceConfig(context));
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}

View File

@@ -16,30 +16,63 @@
package com.android.settings.accessibility;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import android.content.Context;
import android.media.AudioManager;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.Settings;
import androidx.annotation.VisibleForTesting;
/** Preference controller for ringtone vibration intensity */
public class RingVibrationIntensityPreferenceController
extends VibrationIntensityPreferenceController {
@VisibleForTesting
static final String PREF_KEY = "ring_vibration_preference_screen";
/** General configuration for ringtone vibration intensity settings. */
public static final class RingVibrationPreferenceConfig extends VibrationPreferenceConfig {
private final AudioManager mAudioManager;
public RingVibrationIntensityPreferenceController(Context context) {
super(context, PREF_KEY, Settings.System.RING_VIBRATION_INTENSITY,
Settings.System.VIBRATE_WHEN_RINGING, /* supportRampingRinger= */ true);
public RingVibrationPreferenceConfig(Context context) {
super(context, Settings.System.RING_VIBRATION_INTENSITY,
VibrationAttributes.USAGE_RINGTONE);
mAudioManager = context.getSystemService(AudioManager.class);
}
@Override
public int readIntensity() {
final int vibrateWhenRinging = Settings.System.getInt(mContentResolver,
Settings.System.VIBRATE_WHEN_RINGING, ON);
if ((vibrateWhenRinging == OFF)
&& !mAudioManager.isRampingRingerEnabled()) {
// VIBRATE_WHEN_RINGING is deprecated but should still be applied if the user has
// turned it off and has not enabled the ramping ringer (old three-state setting).
return Vibrator.VIBRATION_INTENSITY_OFF;
}
return super.readIntensity();
}
@Override
public boolean updateIntensity(int intensity) {
final boolean success = super.updateIntensity(intensity);
// VIBRATE_WHEN_RINGING is deprecated but should still reflect the intensity setting.
// Ramping ringer is independent of the ring intensity and should not be affected.
Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING,
(intensity == Vibrator.VIBRATION_INTENSITY_OFF) ? OFF : ON);
return success;
}
}
public RingVibrationIntensityPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey, new RingVibrationPreferenceConfig(context));
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
protected int getDefaultIntensity() {
return mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_RINGTONE);
}
}

View File

@@ -1,67 +0,0 @@
/*
* Copyright (C) 2018 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.app.settings.SettingsEnums;
import android.media.AudioAttributes;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.Settings;
import com.android.settings.R;
/**
* Fragment for picking accessibility shortcut service
*/
public class RingVibrationPreferenceFragment extends VibrationPreferenceFragment {
@Override
public int getMetricsCategory() {
return SettingsEnums.ACCESSIBILITY_VIBRATION_RING;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.accessibility_ring_vibration_settings;
}
/**
* Get the setting string of the vibration intensity setting this preference is dealing with.
*/
@Override
protected String getVibrationIntensitySetting() {
return Settings.System.RING_VIBRATION_INTENSITY;
}
@Override
protected String getVibrationEnabledSetting() {
if (AccessibilitySettings.isRampingRingerEnabled(getContext())) {
return Settings.System.APPLY_RAMPING_RINGER;
} else {
return Settings.System.VIBRATE_WHEN_RINGING;
}
}
@Override
protected int getPreviewVibrationAudioAttributesUsage() {
return AudioAttributes.USAGE_NOTIFICATION_RINGTONE;
}
@Override
protected int getDefaultVibrationIntensity() {
Vibrator vibrator = getContext().getSystemService(Vibrator.class);
return vibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_RINGTONE);
}
}

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.RingVibrationIntensityPreferenceController.RingVibrationPreferenceConfig;
/** Preference controller for ringtone vibration with only a toggle for on/off states. */
public class RingVibrationTogglePreferenceController extends VibrationTogglePreferenceController {
public RingVibrationTogglePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey, new RingVibrationPreferenceConfig(context));
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}

View File

@@ -1,63 +0,0 @@
/*
* Copyright (C) 2018 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.app.settings.SettingsEnums;
import android.media.AudioAttributes;
import android.os.VibrationAttributes;
import android.os.Vibrator;
import android.provider.Settings;
import com.android.settings.R;
/**
* Fragment for picking accessibility shortcut service
*/
public class TouchVibrationPreferenceFragment extends VibrationPreferenceFragment {
@Override
public int getMetricsCategory() {
return SettingsEnums.ACCESSIBILITY_VIBRATION_TOUCH;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.accessibility_touch_vibration_settings;
}
/**
* Get the setting string of the vibration intensity setting this preference is dealing with.
*/
@Override
protected String getVibrationIntensitySetting() {
return Settings.System.HAPTIC_FEEDBACK_INTENSITY;
}
@Override
protected String getVibrationEnabledSetting() {
return Settings.System.HAPTIC_FEEDBACK_ENABLED;
}
@Override
protected int getDefaultVibrationIntensity() {
Vibrator vibrator = getContext().getSystemService(Vibrator.class);
return vibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_TOUCH);
}
@Override
protected int getPreviewVibrationAudioAttributesUsage() {
return AudioAttributes.USAGE_ASSISTANCE_SONIFICATION;
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,126 @@
/*
* 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.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.VibrationAttributes;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.provider.Settings;
import androidx.preference.Preference;
import com.android.settingslib.core.AbstractPreferenceController;
/**
* Vibration intensity settings configuration to be shared between different preference
* controllers that handle the same setting key.
*/
public abstract class VibrationPreferenceConfig {
protected final ContentResolver mContentResolver;
private final Vibrator mVibrator;
private final String mSettingKey;
private final int mDefaultIntensity;
private final VibrationAttributes mVibrationAttributes;
public VibrationPreferenceConfig(Context context, String settingKey, int vibrationUsage) {
mContentResolver = context.getContentResolver();
mVibrator = context.getSystemService(Vibrator.class);
mSettingKey = settingKey;
mDefaultIntensity = mVibrator.getDefaultVibrationIntensity(vibrationUsage);
mVibrationAttributes = new VibrationAttributes.Builder()
.setUsage(vibrationUsage)
.build();
}
/** Return the setting key for this setting preference. */
public String getSettingKey() {
return mSettingKey;
}
/** Returns the default intensity to be displayed when the setting value is not set. */
public int getDefaultIntensity() {
return mDefaultIntensity;
}
/** Reads setting value for corresponding {@link VibrationPreferenceConfig} */
public int readIntensity() {
return Settings.System.getInt(mContentResolver, mSettingKey, mDefaultIntensity);
}
/** Update setting value for corresponding {@link VibrationPreferenceConfig} */
public boolean updateIntensity(int intensity) {
return Settings.System.putInt(mContentResolver, mSettingKey, intensity);
}
/** Play a vibration effect with intensity just selected by the user. */
public void playVibrationPreview() {
mVibrator.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK),
mVibrationAttributes);
}
/** {@link ContentObserver} for a setting described by a {@link VibrationPreferenceConfig}. */
public static final class SettingObserver extends ContentObserver {
private final Uri mUri;
private AbstractPreferenceController mPreferenceController;
private Preference mPreference;
/** Creates observer for given preference. */
public SettingObserver(VibrationPreferenceConfig preferenceConfig) {
super(new Handler(/* async= */ true));
mUri = Settings.System.getUriFor(preferenceConfig.getSettingKey());
}
@Override
public void onChange(boolean selfChange, Uri uri) {
if (mUri.equals(uri) && mPreferenceController != null && mPreference != null) {
mPreferenceController.updateState(mPreference);
}
}
/**
* Register this observer to given {@link ContentResolver}, to be called from lifecycle
* {@code onStart} method.
*/
public void register(ContentResolver contentResolver) {
contentResolver.registerContentObserver(mUri, /* notifyForDescendants= */ false, this);
}
/**
* Unregister this observer from given {@link ContentResolver}, to be called from lifecycle
* {@code onStop} method.
*/
public void unregister(ContentResolver contentResolver) {
contentResolver.unregisterContentObserver(this);
}
/**
* Binds this observer to given controller and preference, once it has been displayed to the
* user.
*/
public void onDisplayPreference(AbstractPreferenceController controller,
Preference preference) {
mPreferenceController = controller;
mPreference = preference;
}
}
}

View File

@@ -1,78 +0,0 @@
/*
* Copyright (C) 2019 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.os.Vibrator;
import android.provider.Settings;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
public class VibrationPreferenceController extends BasePreferenceController {
private final Vibrator mVibrator;
public VibrationPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mVibrator = mContext.getSystemService(Vibrator.class);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public CharSequence getSummary() {
int ringIntensity = Settings.System.getInt(mContext.getContentResolver(),
Settings.System.RING_VIBRATION_INTENSITY,
mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_RINGTONE));
if (Settings.System.getInt(mContext.getContentResolver(),
Settings.System.VIBRATE_WHEN_RINGING, 0) == 0
&& !AccessibilitySettings.isRampingRingerEnabled(mContext)) {
ringIntensity = Vibrator.VIBRATION_INTENSITY_OFF;
}
final CharSequence ringIntensityString =
VibrationIntensityPreferenceController.getIntensityString(mContext, ringIntensity);
final int notificationIntensity = Settings.System.getInt(mContext.getContentResolver(),
Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_NOTIFICATION));
final CharSequence notificationIntensityString =
VibrationIntensityPreferenceController.getIntensityString(mContext,
notificationIntensity);
int touchIntensity = Settings.System.getInt(mContext.getContentResolver(),
Settings.System.HAPTIC_FEEDBACK_INTENSITY,
mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_TOUCH));
if (Settings.System.getInt(mContext.getContentResolver(),
Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) == 0) {
touchIntensity = Vibrator.VIBRATION_INTENSITY_OFF;
}
final CharSequence touchIntensityString =
VibrationIntensityPreferenceController.getIntensityString(mContext, touchIntensity);
if (ringIntensity == touchIntensity && ringIntensity == notificationIntensity) {
return ringIntensityString;
} else {
return mContext.getString(R.string.accessibility_vibration_summary, ringIntensityString,
notificationIntensityString, touchIntensityString);
}
}
}

View File

@@ -1,299 +0,0 @@
/*
* Copyright (C) 2018 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 static android.os.Vibrator.VibrationIntensity;
import android.content.Context;
import android.database.ContentObserver;
import android.graphics.drawable.Drawable;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Handler;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.widget.RadioButtonPickerFragment;
import com.android.settingslib.widget.CandidateInfo;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
/**
* Fragment for changing vibration settings.
*/
public abstract class VibrationPreferenceFragment extends RadioButtonPickerFragment {
private static final String TAG = "VibrationPreferenceFragment";
@VisibleForTesting
final static String KEY_INTENSITY_OFF = "intensity_off";
@VisibleForTesting
final static String KEY_INTENSITY_LOW = "intensity_low";
@VisibleForTesting
final static String KEY_INTENSITY_MEDIUM = "intensity_medium";
@VisibleForTesting
final static String KEY_INTENSITY_HIGH = "intensity_high";
// KEY_INTENSITY_ON is only used when the device doesn't support multiple intensity levels.
@VisibleForTesting
final static String KEY_INTENSITY_ON = "intensity_on";
private final Map<String, VibrationIntensityCandidateInfo> mCandidates;
private final SettingsObserver mSettingsObserver;
public VibrationPreferenceFragment() {
mCandidates = new ArrayMap<>();
mSettingsObserver = new SettingsObserver();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mSettingsObserver.register();
if (mCandidates.isEmpty()) {
loadCandidates(context);
}
}
private void loadCandidates(Context context) {
final boolean supportsMultipleIntensities = context.getResources().getBoolean(
R.bool.config_vibration_supports_multiple_intensities);
if (supportsMultipleIntensities) {
mCandidates.put(KEY_INTENSITY_OFF,
new VibrationIntensityCandidateInfo(KEY_INTENSITY_OFF,
R.string.accessibility_vibration_intensity_off,
Vibrator.VIBRATION_INTENSITY_OFF));
mCandidates.put(KEY_INTENSITY_LOW,
new VibrationIntensityCandidateInfo(KEY_INTENSITY_LOW,
R.string.accessibility_vibration_intensity_low,
Vibrator.VIBRATION_INTENSITY_LOW));
mCandidates.put(KEY_INTENSITY_MEDIUM,
new VibrationIntensityCandidateInfo(KEY_INTENSITY_MEDIUM,
R.string.accessibility_vibration_intensity_medium,
Vibrator.VIBRATION_INTENSITY_MEDIUM));
mCandidates.put(KEY_INTENSITY_HIGH,
new VibrationIntensityCandidateInfo(KEY_INTENSITY_HIGH,
R.string.accessibility_vibration_intensity_high,
Vibrator.VIBRATION_INTENSITY_HIGH));
} else {
mCandidates.put(KEY_INTENSITY_OFF,
new VibrationIntensityCandidateInfo(KEY_INTENSITY_OFF,
R.string.switch_off_text, Vibrator.VIBRATION_INTENSITY_OFF));
mCandidates.put(KEY_INTENSITY_ON,
new VibrationIntensityCandidateInfo(KEY_INTENSITY_ON,
R.string.switch_on_text, getDefaultVibrationIntensity()));
}
}
private boolean hasVibrationEnabledSetting() {
return !TextUtils.isEmpty(getVibrationEnabledSetting());
}
private void updateSettings(VibrationIntensityCandidateInfo candidate) {
boolean vibrationEnabled = candidate.getIntensity() != Vibrator.VIBRATION_INTENSITY_OFF;
if (hasVibrationEnabledSetting()) {
// Update vibration enabled setting
final String vibrationEnabledSetting = getVibrationEnabledSetting();
final boolean wasEnabled = TextUtils.equals(
vibrationEnabledSetting, Settings.System.APPLY_RAMPING_RINGER)
? true
: (Settings.System.getInt(
getContext().getContentResolver(), vibrationEnabledSetting, 1) == 1);
if (vibrationEnabled != wasEnabled) {
if (vibrationEnabledSetting.equals(Settings.System.APPLY_RAMPING_RINGER)) {
Settings.Global.putInt(getContext().getContentResolver(),
vibrationEnabledSetting, 0);
} else {
Settings.System.putInt(getContext().getContentResolver(),
vibrationEnabledSetting, vibrationEnabled ? 1 : 0);
}
int previousIntensity = Settings.System.getInt(getContext().getContentResolver(),
getVibrationIntensitySetting(), 0);
if (vibrationEnabled && previousIntensity == candidate.getIntensity()) {
// We can't play preview effect here for all cases because that causes a data
// race (VibratorService may access intensity settings before these settings
// are updated). But we can't just play it in intensity settings update
// observer, because the intensity settings are not changed if we turn the
// vibration off, then on.
//
// In this case we sould play the preview here.
// To be refactored in b/132952771
playVibrationPreview();
}
}
}
// There are two conditions that need to change the intensity.
// First: Vibration is enabled and we are changing its strength.
// Second: There is no setting to enable this vibration, change the intensity directly.
if (vibrationEnabled || !hasVibrationEnabledSetting()) {
// Update vibration intensity setting
Settings.System.putInt(getContext().getContentResolver(),
getVibrationIntensitySetting(), candidate.getIntensity());
}
}
@Override
public void onDetach() {
super.onDetach();
mSettingsObserver.unregister();
}
/**
* Get the setting string of the vibration intensity setting this preference is dealing with.
*/
protected abstract String getVibrationIntensitySetting();
/**
* Get the setting string of the vibration enabledness setting this preference is dealing with.
*/
protected abstract String getVibrationEnabledSetting();
/**
* Get the default intensity for the desired setting.
*/
protected abstract int getDefaultVibrationIntensity();
/**
* When a new vibration intensity is selected by the user.
*/
protected void onVibrationIntensitySelected(int intensity) { }
/**
* Play a vibration effect with intensity just selected by user
*/
protected void playVibrationPreview() {
Vibrator vibrator = getContext().getSystemService(Vibrator.class);
VibrationEffect effect = VibrationEffect.get(VibrationEffect.EFFECT_CLICK);
AudioAttributes.Builder builder = new AudioAttributes.Builder();
builder.setUsage(getPreviewVibrationAudioAttributesUsage());
vibrator.vibrate(effect, builder.build());
}
/**
* Get the AudioAttributes usage for vibration preview.
*/
protected int getPreviewVibrationAudioAttributesUsage() {
return AudioAttributes.USAGE_UNKNOWN;
}
@Override
protected List<? extends CandidateInfo> getCandidates() {
List<VibrationIntensityCandidateInfo> candidates = new ArrayList<>(mCandidates.values());
candidates.sort(
Comparator.comparing(VibrationIntensityCandidateInfo::getIntensity).reversed());
return candidates;
}
@Override
protected String getDefaultKey() {
int vibrationIntensity = Settings.System.getInt(getContext().getContentResolver(),
getVibrationIntensitySetting(), getDefaultVibrationIntensity());
final String vibrationEnabledSetting = getVibrationEnabledSetting();
final boolean vibrationEnabled = TextUtils.equals(
vibrationEnabledSetting, Settings.System.APPLY_RAMPING_RINGER)
? true
: (Settings.System.getInt(
getContext().getContentResolver(), vibrationEnabledSetting, 1) == 1);
if (!vibrationEnabled) {
vibrationIntensity = Vibrator.VIBRATION_INTENSITY_OFF;
}
for (VibrationIntensityCandidateInfo candidate : mCandidates.values()) {
final boolean matchesIntensity = candidate.getIntensity() == vibrationIntensity;
final boolean matchesOn = candidate.getKey().equals(KEY_INTENSITY_ON)
&& vibrationIntensity != Vibrator.VIBRATION_INTENSITY_OFF;
if (matchesIntensity || matchesOn) {
return candidate.getKey();
}
}
return null;
}
@Override
protected boolean setDefaultKey(String key) {
VibrationIntensityCandidateInfo candidate = mCandidates.get(key);
if (candidate == null) {
Log.e(TAG, "Tried to set unknown intensity (key=" + key + ")!");
return false;
}
updateSettings(candidate);
onVibrationIntensitySelected(candidate.getIntensity());
return true;
}
@VisibleForTesting
class VibrationIntensityCandidateInfo extends CandidateInfo {
private String mKey;
private int mLabelId;
@VibrationIntensity
private int mIntensity;
public VibrationIntensityCandidateInfo(String key, int labelId, int intensity) {
super(true /* enabled */);
mKey = key;
mLabelId = labelId;
mIntensity = intensity;
}
@Override
public CharSequence loadLabel() {
return getContext().getString(mLabelId);
}
@Override
public Drawable loadIcon() {
return null;
}
@Override
public String getKey() {
return mKey;
}
public int getIntensity() {
return mIntensity;
}
}
private class SettingsObserver extends ContentObserver {
public SettingsObserver() {
super(new Handler());
}
public void register() {
getContext().getContentResolver().registerContentObserver(
Settings.System.getUriFor(getVibrationIntensitySetting()), false, this);
}
public void unregister() {
getContext().getContentResolver().unregisterContentObserver(this);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
updateCandidates();
playVibrationPreview();
}
}
}

View File

@@ -0,0 +1,140 @@
/*
* 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.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;
import android.provider.Settings;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.core.TogglePreferenceController;
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}.
*
* <p>This preference depends on the {@link Settings.System#RING_VIBRATION_INTENSITY}, and it will
* be disabled and display the unchecked state when the ring intensity is set to OFF. The actual
* ramping ringer setting will not be overwritten when the ring intensity is turned off, so the
* user original value will be naturally restored when the ring intensity is enabled again.
*/
public class VibrationRampingRingerTogglePreferenceController
extends TogglePreferenceController implements LifecycleObserver, OnStart, OnStop {
@VisibleForTesting
static final String DEVICE_CONFIG_KEY = "ramping_ringer_enabled";
private final ContentObserver mSettingObserver;
private final Vibrator mVibrator;
private final AudioManager mAudioManager;
private Preference mPreference;
public VibrationRampingRingerTogglePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mVibrator = context.getSystemService(Vibrator.class);
mAudioManager = context.getSystemService(AudioManager.class);
mSettingObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
@Override
public void onChange(boolean selfChange, Uri uri) {
updateState(mPreference);
}
};
}
@Override
public int getAvailabilityStatus() {
final boolean rampingRingerEnabledOnTelephonyConfig =
DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_TELEPHONY, DEVICE_CONFIG_KEY, false);
return (Utils.isVoiceCapable(mContext) && !rampingRingerEnabledOnTelephonyConfig)
? AVAILABLE
: UNSUPPORTED_ON_DEVICE;
}
@Override
public void onStart() {
mContext.getContentResolver().registerContentObserver(
Settings.System.getUriFor(Settings.System.APPLY_RAMPING_RINGER),
/* notifyForDescendants= */ false,
mSettingObserver);
mContext.getContentResolver().registerContentObserver(
Settings.System.getUriFor(Settings.System.RING_VIBRATION_INTENSITY),
/* notifyForDescendants= */ false,
mSettingObserver);
}
@Override
public void onStop() {
mContext.getContentResolver().unregisterContentObserver(mSettingObserver);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
mPreference.setEnabled(isRingVibrationEnabled());
}
@Override
public boolean isChecked() {
return isRingVibrationEnabled() && mAudioManager.isRampingRingerEnabled();
}
@Override
public boolean setChecked(boolean isChecked) {
if (isRingVibrationEnabled()) {
// Don't update ramping ringer setting value if ring vibration is disabled.
mAudioManager.setRampingRingerEnabled(isChecked);
}
return true;
}
@Override
public int getSliceHighlightMenuRes() {
return R.string.menu_key_accessibility;
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
if (preference != null) {
preference.setEnabled(isRingVibrationEnabled());
}
}
private boolean isRingVibrationEnabled() {
final int ringIntensity = Settings.System.getInt(mContext.getContentResolver(),
Settings.System.RING_VIBRATION_INTENSITY,
mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_RINGTONE));
return ringIntensity != Vibrator.VIBRATION_INTENSITY_OFF;
}
}

View File

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

View File

@@ -0,0 +1,87 @@
/*
* 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.Vibrator;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.TogglePreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
/** Abstract preference controller for a vibration intensity setting, that has only ON/OFF states */
public abstract class VibrationTogglePreferenceController extends TogglePreferenceController
implements LifecycleObserver, OnStart, OnStop {
protected final VibrationPreferenceConfig mPreferenceConfig;
private final VibrationPreferenceConfig.SettingObserver mSettingsContentObserver;
protected VibrationTogglePreferenceController(Context context, String preferenceKey,
VibrationPreferenceConfig preferenceConfig) {
super(context, preferenceKey);
mPreferenceConfig = preferenceConfig;
mSettingsContentObserver = new VibrationPreferenceConfig.SettingObserver(
preferenceConfig);
}
@Override
public void onStart() {
mSettingsContentObserver.register(mContext.getContentResolver());
}
@Override
public void onStop() {
mSettingsContentObserver.unregister(mContext.getContentResolver());
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
final Preference preference = screen.findPreference(getPreferenceKey());
mSettingsContentObserver.onDisplayPreference(this, preference);
}
@Override
public boolean isChecked() {
final int position = mPreferenceConfig.readIntensity();
return position != Vibrator.VIBRATION_INTENSITY_OFF;
}
@Override
public boolean setChecked(boolean isChecked) {
final int newIntensity = isChecked
? mPreferenceConfig.getDefaultIntensity()
: Vibrator.VIBRATION_INTENSITY_OFF;
final boolean success = mPreferenceConfig.updateIntensity(newIntensity);
if (success && isChecked) {
mPreferenceConfig.playVibrationPreview();
}
return success;
}
@Override
public int getSliceHighlightMenuRes() {
return R.string.menu_key_accessibility;
}
}