1 /* 2 * Copyright (C) 2019 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 import android.widget.Switch; 26 27 import androidx.annotation.VisibleForTesting; 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceScreen; 30 31 import com.android.settings.R; 32 import com.android.settings.core.PreferenceControllerMixin; 33 import com.android.settings.widget.SwitchBar; 34 import com.android.settingslib.core.AbstractPreferenceController; 35 import com.android.settingslib.widget.LayoutPreference; 36 37 public class PreventRingingSwitchPreferenceController extends AbstractPreferenceController 38 implements PreferenceControllerMixin, SwitchBar.OnSwitchChangeListener { 39 40 private static final String KEY = "gesture_prevent_ringing_switch"; 41 private final Context mContext; 42 private SettingObserver mSettingObserver; 43 44 @VisibleForTesting 45 SwitchBar mSwitch; 46 PreventRingingSwitchPreferenceController(Context context)47 public PreventRingingSwitchPreferenceController(Context context) { 48 super(context); 49 mContext = context; 50 } 51 52 @Override getPreferenceKey()53 public String getPreferenceKey() { 54 return KEY; 55 } 56 57 @Override displayPreference(PreferenceScreen screen)58 public void displayPreference(PreferenceScreen screen) { 59 super.displayPreference(screen); 60 if (isAvailable()) { 61 LayoutPreference pref = screen.findPreference(getPreferenceKey()); 62 if (pref != null) { 63 mSettingObserver = new SettingObserver(pref); 64 pref.setOnPreferenceClickListener(preference -> { 65 int preventRinging = Settings.Secure.getInt(mContext.getContentResolver(), 66 Settings.Secure.VOLUME_HUSH_GESTURE, 67 Settings.Secure.VOLUME_HUSH_VIBRATE); 68 boolean isChecked = preventRinging != Settings.Secure.VOLUME_HUSH_OFF; 69 Settings.Secure.putInt(mContext.getContentResolver(), 70 Settings.Secure.VOLUME_HUSH_GESTURE, isChecked 71 ? Settings.Secure.VOLUME_HUSH_OFF 72 : Settings.Secure.VOLUME_HUSH_VIBRATE); 73 return true; 74 }); 75 mSwitch = pref.findViewById(R.id.switch_bar); 76 if (mSwitch != null) { 77 mSwitch.addOnSwitchChangeListener(this); 78 mSwitch.show(); 79 } 80 } 81 } 82 } 83 setChecked(boolean isChecked)84 public void setChecked(boolean isChecked) { 85 if (mSwitch != null) { 86 mSwitch.setChecked(isChecked); 87 } 88 } 89 90 @Override updateState(Preference preference)91 public void updateState(Preference preference) { 92 int preventRingingSetting = Settings.Secure.getInt(mContext.getContentResolver(), 93 Settings.Secure.VOLUME_HUSH_GESTURE, Settings.Secure.VOLUME_HUSH_VIBRATE); 94 setChecked(preventRingingSetting != Settings.Secure.VOLUME_HUSH_OFF); 95 } 96 97 @Override isAvailable()98 public boolean isAvailable() { 99 return mContext.getResources().getBoolean( 100 com.android.internal.R.bool.config_volumeHushGestureEnabled); 101 } 102 103 @Override onSwitchChanged(Switch switchView, boolean isChecked)104 public void onSwitchChanged(Switch switchView, boolean isChecked) { 105 final int preventRingingSetting = Settings.Secure.getInt(mContext.getContentResolver(), 106 Settings.Secure.VOLUME_HUSH_GESTURE, Settings.Secure.VOLUME_HUSH_VIBRATE); 107 final int newRingingSetting = preventRingingSetting == Settings.Secure.VOLUME_HUSH_OFF 108 ? Settings.Secure.VOLUME_HUSH_VIBRATE 109 : preventRingingSetting; 110 111 Settings.Secure.putInt(mContext.getContentResolver(), 112 Settings.Secure.VOLUME_HUSH_GESTURE, isChecked 113 ? newRingingSetting 114 : Settings.Secure.VOLUME_HUSH_OFF); 115 } 116 117 private class SettingObserver extends ContentObserver { 118 private final Uri VOLUME_HUSH_GESTURE = Settings.Secure.getUriFor( 119 Settings.Secure.VOLUME_HUSH_GESTURE); 120 121 private final Preference mPreference; 122 SettingObserver(Preference preference)123 public SettingObserver(Preference preference) { 124 super(new Handler()); 125 mPreference = preference; 126 } 127 register(ContentResolver cr)128 public void register(ContentResolver cr) { 129 cr.registerContentObserver(VOLUME_HUSH_GESTURE, false, this); 130 } 131 unregister(ContentResolver cr)132 public void unregister(ContentResolver cr) { 133 cr.unregisterContentObserver(this); 134 } 135 136 @Override onChange(boolean selfChange, Uri uri)137 public void onChange(boolean selfChange, Uri uri) { 138 super.onChange(selfChange, uri); 139 if (uri == null || VOLUME_HUSH_GESTURE.equals(uri)) { 140 updateState(mPreference); 141 } 142 } 143 } 144 } 145