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 package com.android.settings.sound; 17 18 import android.app.settings.SettingsEnums; 19 import android.content.Context; 20 import android.graphics.drawable.Drawable; 21 import android.provider.Settings; 22 import android.text.TextUtils; 23 import android.util.ArrayMap; 24 import android.util.Log; 25 26 import androidx.annotation.VisibleForTesting; 27 28 import com.android.settings.R; 29 import com.android.settings.widget.RadioButtonPickerFragment; 30 import com.android.settingslib.widget.CandidateInfo; 31 32 import java.util.ArrayList; 33 import java.util.List; 34 import java.util.Map; 35 36 /** 37 * Fragment for changing vibrate for calls options. 38 */ 39 public class VibrateForCallsPreferenceFragment extends RadioButtonPickerFragment { 40 private static final String TAG = "VibrateForCallsPreferenceFragment"; 41 42 @VisibleForTesting 43 static final String KEY_NEVER_VIBRATE = "never_vibrate"; 44 @VisibleForTesting 45 static final String KEY_ALWAYS_VIBRATE = "always_vibrate"; 46 @VisibleForTesting 47 static final String KEY_RAMPING_RINGER = "ramping_ringer"; 48 49 private static final int ON = 1; 50 private static final int OFF = 0; 51 52 private final Map<String, VibrateForCallsCandidateInfo> mCandidates; 53 VibrateForCallsPreferenceFragment()54 public VibrateForCallsPreferenceFragment() { 55 mCandidates = new ArrayMap<>(); 56 } 57 58 @Override onAttach(Context context)59 public void onAttach(Context context) { 60 super.onAttach(context); 61 loadCandidates(context); 62 } 63 loadCandidates(Context context)64 private void loadCandidates(Context context) { 65 mCandidates.put(KEY_NEVER_VIBRATE, 66 new VibrateForCallsCandidateInfo( 67 KEY_NEVER_VIBRATE, R.string.vibrate_when_ringing_option_never_vibrate)); 68 mCandidates.put(KEY_ALWAYS_VIBRATE, 69 new VibrateForCallsCandidateInfo( 70 KEY_ALWAYS_VIBRATE, R.string.vibrate_when_ringing_option_always_vibrate)); 71 mCandidates.put(KEY_RAMPING_RINGER, 72 new VibrateForCallsCandidateInfo( 73 KEY_RAMPING_RINGER, R.string.vibrate_when_ringing_option_ramping_ringer)); 74 } 75 updateSettings(VibrateForCallsCandidateInfo candidate)76 private void updateSettings(VibrateForCallsCandidateInfo candidate) { 77 final String key = candidate.getKey(); 78 if (TextUtils.equals(key, KEY_ALWAYS_VIBRATE)) { 79 Settings.System.putInt( 80 getContext().getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING, ON); 81 Settings.Global.putInt( 82 getContext().getContentResolver(), Settings.Global.APPLY_RAMPING_RINGER, OFF); 83 } else if (TextUtils.equals(key, KEY_RAMPING_RINGER)) { 84 Settings.System.putInt( 85 getContext().getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING, OFF); 86 Settings.Global.putInt( 87 getContext().getContentResolver(), Settings.Global.APPLY_RAMPING_RINGER, ON); 88 } else { 89 Settings.System.putInt( 90 getContext().getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING, OFF); 91 Settings.Global.putInt( 92 getContext().getContentResolver(), Settings.Global.APPLY_RAMPING_RINGER, OFF); 93 } 94 } 95 96 @Override getCandidates()97 protected List<? extends CandidateInfo> getCandidates() { 98 final List<VibrateForCallsCandidateInfo> candidates = new ArrayList<>(); 99 candidates.add(mCandidates.get(KEY_NEVER_VIBRATE)); 100 candidates.add(mCandidates.get(KEY_ALWAYS_VIBRATE)); 101 candidates.add(mCandidates.get(KEY_RAMPING_RINGER)); 102 return candidates; 103 } 104 105 @Override getDefaultKey()106 protected String getDefaultKey() { 107 if (Settings.Global.getInt( 108 getContext().getContentResolver(), 109 Settings.Global.APPLY_RAMPING_RINGER, OFF) == ON) { 110 return KEY_RAMPING_RINGER; 111 } else if (Settings.System.getInt( 112 getContext().getContentResolver(), 113 Settings.System.VIBRATE_WHEN_RINGING, OFF) == ON) { 114 return KEY_ALWAYS_VIBRATE; 115 } else { 116 return KEY_NEVER_VIBRATE; 117 } 118 } 119 120 @Override setDefaultKey(String key)121 protected boolean setDefaultKey(String key) { 122 final VibrateForCallsCandidateInfo candidate = mCandidates.get(key); 123 if (candidate == null) { 124 Log.e(TAG, "Unknown vibrate for calls candidate (key = " + key + ")!"); 125 return false; 126 } 127 updateSettings(candidate); 128 return true; 129 } 130 131 @Override getPreferenceScreenResId()132 protected int getPreferenceScreenResId() { 133 return R.xml.vibrate_for_calls_settings; 134 } 135 136 @Override getMetricsCategory()137 public int getMetricsCategory() { 138 return SettingsEnums.VIBRATE_FOR_CALLS; 139 } 140 141 @VisibleForTesting 142 class VibrateForCallsCandidateInfo extends CandidateInfo { 143 private final String mKey; 144 private final int mLabelId; 145 VibrateForCallsCandidateInfo(String key, int labelId)146 VibrateForCallsCandidateInfo(String key, int labelId) { 147 super(true /* enabled */); 148 mKey = key; 149 mLabelId = labelId; 150 } 151 152 @Override loadLabel()153 public CharSequence loadLabel() { 154 return getContext().getString(mLabelId); 155 } 156 157 @Override loadIcon()158 public Drawable loadIcon() { 159 return null; 160 } 161 162 @Override getKey()163 public String getKey() { 164 return mKey; 165 } 166 } 167 } 168