1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.accessibility; 18 19 import android.content.Context; 20 import android.database.ContentObserver; 21 import android.net.Uri; 22 import android.os.Handler; 23 import android.os.Looper; 24 import android.os.Vibrator; 25 import android.provider.Settings; 26 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceScreen; 29 30 import com.android.settings.R; 31 import com.android.settings.core.BasePreferenceController; 32 import com.android.settingslib.core.lifecycle.LifecycleObserver; 33 import com.android.settingslib.core.lifecycle.events.OnStart; 34 import com.android.settingslib.core.lifecycle.events.OnStop; 35 36 public abstract class VibrationIntensityPreferenceController extends BasePreferenceController 37 implements LifecycleObserver, OnStart, OnStop { 38 39 protected final Vibrator mVibrator; 40 private final SettingObserver mSettingsContentObserver; 41 private final String mSettingKey; 42 private final String mEnabledKey; 43 private final boolean mSupportRampingRinger; 44 45 private Preference mPreference; 46 VibrationIntensityPreferenceController(Context context, String prefkey, String settingKey, String enabledKey, boolean supportRampingRinger)47 public VibrationIntensityPreferenceController(Context context, String prefkey, 48 String settingKey, String enabledKey, boolean supportRampingRinger) { 49 super(context, prefkey); 50 mVibrator = mContext.getSystemService(Vibrator.class); 51 mSettingKey = settingKey; 52 mEnabledKey = enabledKey; 53 mSupportRampingRinger= supportRampingRinger; 54 mSettingsContentObserver = new SettingObserver(settingKey) { 55 @Override 56 public void onChange(boolean selfChange, Uri uri) { 57 updateState(mPreference); 58 } 59 }; 60 } 61 VibrationIntensityPreferenceController(Context context, String prefkey, String settingKey, String enabledKey)62 public VibrationIntensityPreferenceController(Context context, String prefkey, 63 String settingKey, String enabledKey) { 64 this(context, prefkey, settingKey, enabledKey, /* supportRampingRinger= */ false); 65 } 66 67 @Override onStart()68 public void onStart() { 69 mContext.getContentResolver().registerContentObserver( 70 mSettingsContentObserver.uri, 71 false /* notifyForDescendants */, 72 mSettingsContentObserver); 73 } 74 75 @Override onStop()76 public void onStop() { 77 mContext.getContentResolver().unregisterContentObserver(mSettingsContentObserver); 78 } 79 80 @Override displayPreference(PreferenceScreen screen)81 public void displayPreference(PreferenceScreen screen) { 82 super.displayPreference(screen); 83 mPreference = screen.findPreference(getPreferenceKey()); 84 } 85 86 @Override getSummary()87 public CharSequence getSummary() { 88 final int intensity = Settings.System.getInt(mContext.getContentResolver(), 89 mSettingKey, getDefaultIntensity()); 90 final boolean enabled = (Settings.System.getInt(mContext.getContentResolver(), 91 mEnabledKey, 1) == 1) || 92 (mSupportRampingRinger && AccessibilitySettings.isRampingRingerEnabled(mContext)); 93 return getIntensityString(mContext, enabled ? intensity : Vibrator.VIBRATION_INTENSITY_OFF); 94 } 95 getIntensityString(Context context, int intensity)96 public static CharSequence getIntensityString(Context context, int intensity) { 97 final boolean supportsMultipleIntensities = context.getResources().getBoolean( 98 R.bool.config_vibration_supports_multiple_intensities); 99 if (supportsMultipleIntensities) { 100 switch (intensity) { 101 case Vibrator.VIBRATION_INTENSITY_OFF: 102 return context.getString(R.string.accessibility_vibration_intensity_off); 103 case Vibrator.VIBRATION_INTENSITY_LOW: 104 return context.getString(R.string.accessibility_vibration_intensity_low); 105 case Vibrator.VIBRATION_INTENSITY_MEDIUM: 106 return context.getString(R.string.accessibility_vibration_intensity_medium); 107 case Vibrator.VIBRATION_INTENSITY_HIGH: 108 return context.getString(R.string.accessibility_vibration_intensity_high); 109 default: 110 return ""; 111 } 112 } else { 113 if (intensity == Vibrator.VIBRATION_INTENSITY_OFF) { 114 return context.getString(R.string.switch_off_text); 115 } else { 116 return context.getString(R.string.switch_on_text); 117 } 118 } 119 } 120 getDefaultIntensity()121 protected abstract int getDefaultIntensity(); 122 123 private static class SettingObserver extends ContentObserver { 124 125 public final Uri uri; 126 SettingObserver(String settingKey)127 public SettingObserver(String settingKey) { 128 super(new Handler(Looper.getMainLooper())); 129 uri = Settings.System.getUriFor(settingKey); 130 } 131 } 132 } 133