1 /* 2 * Copyright (C) 2016 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.notification; 18 19 import static android.provider.Settings.System.VIBRATE_WHEN_RINGING; 20 21 import android.content.ContentResolver; 22 import android.content.Context; 23 import android.database.ContentObserver; 24 import android.net.Uri; 25 import android.os.Handler; 26 import android.provider.DeviceConfig; 27 import android.provider.Settings; 28 import android.text.TextUtils; 29 30 import androidx.preference.Preference; 31 import androidx.preference.PreferenceScreen; 32 33 import com.android.settings.Utils; 34 import com.android.settings.core.TogglePreferenceController; 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 39 public class VibrateWhenRingPreferenceController extends TogglePreferenceController 40 implements LifecycleObserver, OnResume, OnPause { 41 42 /** Flag for whether or not to apply ramping ringer on incoming phone calls. */ 43 private static final String RAMPING_RINGER_ENABLED = "ramping_ringer_enabled"; 44 private static final String KEY_VIBRATE_WHEN_RINGING = "vibrate_when_ringing"; 45 private final int DEFAULT_VALUE = 0; 46 private final int NOTIFICATION_VIBRATE_WHEN_RINGING = 1; 47 private SettingObserver mSettingObserver; 48 VibrateWhenRingPreferenceController(Context context, String key)49 public VibrateWhenRingPreferenceController(Context context, String key) { 50 super(context, key); 51 } 52 53 @Override isChecked()54 public boolean isChecked() { 55 return Settings.System.getInt(mContext.getContentResolver(), 56 VIBRATE_WHEN_RINGING, DEFAULT_VALUE) != DEFAULT_VALUE; 57 } 58 59 @Override setChecked(boolean isChecked)60 public boolean setChecked(boolean isChecked) { 61 return Settings.System.putInt(mContext.getContentResolver(), VIBRATE_WHEN_RINGING, 62 isChecked ? NOTIFICATION_VIBRATE_WHEN_RINGING : DEFAULT_VALUE); 63 } 64 65 @Override 66 @AvailabilityStatus getAvailabilityStatus()67 public int getAvailabilityStatus() { 68 // If ramping ringer is enabled then this setting will be injected 69 // with additional options. 70 return Utils.isVoiceCapable(mContext) && !isRampingRingerEnabled() 71 ? AVAILABLE 72 : UNSUPPORTED_ON_DEVICE; 73 } 74 75 @Override isSliceable()76 public boolean isSliceable() { 77 return TextUtils.equals(getPreferenceKey(), "vibrate_when_ringing"); 78 } 79 80 @Override displayPreference(PreferenceScreen screen)81 public void displayPreference(PreferenceScreen screen) { 82 super.displayPreference(screen); 83 Preference preference = screen.findPreference(KEY_VIBRATE_WHEN_RINGING); 84 if (preference != null) { 85 mSettingObserver = new SettingObserver(preference); 86 preference.setPersistent(false); 87 } 88 } 89 90 @Override onResume()91 public void onResume() { 92 if (mSettingObserver != null) { 93 mSettingObserver.register(true /* register */); 94 } 95 } 96 97 @Override onPause()98 public void onPause() { 99 if (mSettingObserver != null) { 100 mSettingObserver.register(false /* register */); 101 } 102 } 103 104 private final class SettingObserver extends ContentObserver { 105 106 private final Uri VIBRATE_WHEN_RINGING_URI = 107 Settings.System.getUriFor(VIBRATE_WHEN_RINGING); 108 109 private final Preference mPreference; 110 SettingObserver(Preference preference)111 public SettingObserver(Preference preference) { 112 super(new Handler()); 113 mPreference = preference; 114 } 115 register(boolean register)116 public void register(boolean register) { 117 final ContentResolver cr = mContext.getContentResolver(); 118 if (register) { 119 cr.registerContentObserver(VIBRATE_WHEN_RINGING_URI, false, this); 120 } else { 121 cr.unregisterContentObserver(this); 122 } 123 } 124 125 @Override onChange(boolean selfChange, Uri uri)126 public void onChange(boolean selfChange, Uri uri) { 127 super.onChange(selfChange, uri); 128 if (VIBRATE_WHEN_RINGING_URI.equals(uri)) { 129 updateState(mPreference); 130 } 131 } 132 } 133 isRampingRingerEnabled()134 private boolean isRampingRingerEnabled() { 135 return DeviceConfig.getBoolean( 136 DeviceConfig.NAMESPACE_TELEPHONY, RAMPING_RINGER_ENABLED, false); 137 } 138 139 } 140