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.gestures; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.database.ContentObserver; 22 import android.net.Uri; 23 import android.os.Handler; 24 import android.provider.Settings; 25 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceCategory; 28 import androidx.preference.PreferenceScreen; 29 30 import com.android.internal.annotations.VisibleForTesting; 31 import com.android.settings.R; 32 import com.android.settings.core.PreferenceControllerMixin; 33 import com.android.settingslib.core.AbstractPreferenceController; 34 import com.android.settingslib.core.lifecycle.Lifecycle; 35 import com.android.settingslib.core.lifecycle.LifecycleObserver; 36 import com.android.settingslib.core.lifecycle.events.OnPause; 37 import com.android.settingslib.core.lifecycle.events.OnResume; 38 import com.android.settingslib.widget.SelectorWithWidgetPreference; 39 40 public class PreventRingingGesturePreferenceController extends AbstractPreferenceController 41 implements SelectorWithWidgetPreference.OnClickListener, LifecycleObserver, 42 OnResume, OnPause, PreferenceControllerMixin { 43 44 @VisibleForTesting 45 static final String KEY_VIBRATE = "prevent_ringing_option_vibrate"; 46 47 @VisibleForTesting 48 static final String KEY_MUTE = "prevent_ringing_option_mute"; 49 50 private final String PREF_KEY_VIDEO = "gesture_prevent_ringing_video"; 51 private final String KEY = "gesture_prevent_ringing_category"; 52 private final Context mContext; 53 54 @VisibleForTesting 55 PreferenceCategory mPreferenceCategory; 56 @VisibleForTesting 57 SelectorWithWidgetPreference mVibratePref; 58 @VisibleForTesting 59 SelectorWithWidgetPreference mMutePref; 60 61 private SettingObserver mSettingObserver; 62 PreventRingingGesturePreferenceController(Context context, Lifecycle lifecycle)63 public PreventRingingGesturePreferenceController(Context context, Lifecycle lifecycle) { 64 super(context); 65 mContext = context; 66 67 if (lifecycle != null) { 68 lifecycle.addObserver(this); 69 } 70 } 71 72 @Override displayPreference(PreferenceScreen screen)73 public void displayPreference(PreferenceScreen screen) { 74 super.displayPreference(screen); 75 if (!isAvailable()) { 76 return; 77 } 78 mPreferenceCategory = screen.findPreference(getPreferenceKey()); 79 mVibratePref = makeRadioPreference(KEY_VIBRATE, R.string.prevent_ringing_option_vibrate); 80 mMutePref = makeRadioPreference(KEY_MUTE, R.string.prevent_ringing_option_mute); 81 82 if (mPreferenceCategory != null) { 83 mSettingObserver = new SettingObserver(mPreferenceCategory); 84 } 85 } 86 87 @Override isAvailable()88 public boolean isAvailable() { 89 return mContext.getResources().getBoolean( 90 com.android.internal.R.bool.config_volumeHushGestureEnabled); 91 } 92 93 @Override getPreferenceKey()94 public String getPreferenceKey() { 95 return KEY; 96 } 97 getVideoPrefKey()98 public String getVideoPrefKey() { 99 return PREF_KEY_VIDEO; 100 } 101 102 @Override onRadioButtonClicked(SelectorWithWidgetPreference preference)103 public void onRadioButtonClicked(SelectorWithWidgetPreference preference) { 104 int preventRingingSetting = keyToSetting(preference.getKey()); 105 if (preventRingingSetting != Settings.Secure.getInt(mContext.getContentResolver(), 106 Settings.Secure.VOLUME_HUSH_GESTURE, Settings.Secure.VOLUME_HUSH_VIBRATE)) { 107 Settings.Secure.putInt(mContext.getContentResolver(), 108 Settings.Secure.VOLUME_HUSH_GESTURE, preventRingingSetting); 109 } 110 } 111 112 @Override updateState(Preference preference)113 public void updateState(Preference preference) { 114 int preventRingingSetting = Settings.Secure.getInt(mContext.getContentResolver(), 115 Settings.Secure.VOLUME_HUSH_GESTURE, Settings.Secure.VOLUME_HUSH_VIBRATE); 116 final boolean isVibrate = preventRingingSetting == Settings.Secure.VOLUME_HUSH_VIBRATE; 117 final boolean isMute = preventRingingSetting == Settings.Secure.VOLUME_HUSH_MUTE; 118 if (mVibratePref != null && mVibratePref.isChecked() != isVibrate) { 119 mVibratePref.setChecked(isVibrate); 120 } 121 if (mMutePref != null && mMutePref.isChecked() != isMute) { 122 mMutePref.setChecked(isMute); 123 } 124 125 if (preventRingingSetting == Settings.Secure.VOLUME_HUSH_OFF) { 126 mVibratePref.setEnabled(false); 127 mMutePref.setEnabled(false); 128 } else { 129 mVibratePref.setEnabled(true); 130 mMutePref.setEnabled(true); 131 } 132 } 133 134 @Override onResume()135 public void onResume() { 136 if (mSettingObserver != null) { 137 mSettingObserver.register(mContext.getContentResolver()); 138 mSettingObserver.onChange(false, null); 139 } 140 } 141 142 @Override onPause()143 public void onPause() { 144 if (mSettingObserver != null) { 145 mSettingObserver.unregister(mContext.getContentResolver()); 146 } 147 } 148 keyToSetting(String key)149 private int keyToSetting(String key) { 150 switch (key) { 151 case KEY_MUTE: 152 return Settings.Secure.VOLUME_HUSH_MUTE; 153 case KEY_VIBRATE: 154 return Settings.Secure.VOLUME_HUSH_VIBRATE; 155 default: 156 return Settings.Secure.VOLUME_HUSH_OFF; 157 } 158 } 159 makeRadioPreference(String key, int titleId)160 private SelectorWithWidgetPreference makeRadioPreference(String key, int titleId) { 161 SelectorWithWidgetPreference pref = new SelectorWithWidgetPreference( 162 mPreferenceCategory.getContext()); 163 pref.setKey(key); 164 pref.setTitle(titleId); 165 pref.setOnClickListener(this); 166 mPreferenceCategory.addPreference(pref); 167 return pref; 168 } 169 170 private class SettingObserver extends ContentObserver { 171 private final Uri VOLUME_HUSH_GESTURE = Settings.Secure.getUriFor( 172 Settings.Secure.VOLUME_HUSH_GESTURE); 173 174 private final Preference mPreference; 175 SettingObserver(Preference preference)176 public SettingObserver(Preference preference) { 177 super(new Handler()); 178 mPreference = preference; 179 } 180 register(ContentResolver cr)181 public void register(ContentResolver cr) { 182 cr.registerContentObserver(VOLUME_HUSH_GESTURE, false, this); 183 } 184 unregister(ContentResolver cr)185 public void unregister(ContentResolver cr) { 186 cr.unregisterContentObserver(this); 187 } 188 189 @Override onChange(boolean selfChange, Uri uri)190 public void onChange(boolean selfChange, Uri uri) { 191 super.onChange(selfChange, uri); 192 if (uri == null || VOLUME_HUSH_GESTURE.equals(uri)) { 193 updateState(mPreference); 194 } 195 } 196 } 197 } 198