1 /* 2 * Copyright (C) 2014 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 android.app.settings.SettingsEnums; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.Bundle; 23 import android.os.Handler; 24 import android.os.Looper; 25 import android.os.Message; 26 import android.os.UserHandle; 27 import android.preference.SeekBarVolumizer; 28 import android.text.TextUtils; 29 30 import androidx.annotation.VisibleForTesting; 31 import androidx.preference.ListPreference; 32 import androidx.preference.Preference; 33 34 import com.android.settings.R; 35 import com.android.settings.RingtonePreference; 36 import com.android.settings.core.OnActivityResultListener; 37 import com.android.settings.dashboard.DashboardFragment; 38 import com.android.settings.search.BaseSearchIndexProvider; 39 import com.android.settings.sound.HandsFreeProfileOutputPreferenceController; 40 import com.android.settings.widget.PreferenceCategoryController; 41 import com.android.settingslib.core.AbstractPreferenceController; 42 import com.android.settingslib.core.instrumentation.Instrumentable; 43 import com.android.settingslib.core.lifecycle.Lifecycle; 44 import com.android.settingslib.search.SearchIndexable; 45 import com.android.settingslib.widget.UpdatableListPreferenceDialogFragment; 46 47 import java.util.ArrayList; 48 import java.util.Arrays; 49 import java.util.List; 50 51 @SearchIndexable 52 public class SoundSettings extends DashboardFragment implements OnActivityResultListener { 53 private static final String TAG = "SoundSettings"; 54 55 private static final String SELECTED_PREFERENCE_KEY = "selected_preference"; 56 private static final int REQUEST_CODE = 200; 57 private static final int SAMPLE_CUTOFF = 2000; // manually cap sample playback at 2 seconds 58 59 @VisibleForTesting 60 static final int STOP_SAMPLE = 1; 61 62 @VisibleForTesting 63 final VolumePreferenceCallback mVolumeCallback = new VolumePreferenceCallback(); 64 @VisibleForTesting 65 final Handler mHandler = new Handler(Looper.getMainLooper()) { 66 @Override 67 public void handleMessage(Message msg) { 68 switch (msg.what) { 69 case STOP_SAMPLE: 70 mVolumeCallback.stopSample(); 71 break; 72 } 73 } 74 }; 75 76 private RingtonePreference mRequestPreference; 77 private UpdatableListPreferenceDialogFragment mDialogFragment; 78 private String mHfpOutputControllerKey; 79 private String mVibrationPreferencesKey = "vibration_preference_screen"; 80 81 @Override getMetricsCategory()82 public int getMetricsCategory() { 83 return SettingsEnums.SOUND; 84 } 85 86 @Override onCreate(Bundle savedInstanceState)87 public void onCreate(Bundle savedInstanceState) { 88 super.onCreate(savedInstanceState); 89 if (savedInstanceState != null) { 90 String selectedPreference = savedInstanceState.getString(SELECTED_PREFERENCE_KEY, null); 91 if (!TextUtils.isEmpty(selectedPreference)) { 92 mRequestPreference = (RingtonePreference) findPreference(selectedPreference); 93 } 94 95 UpdatableListPreferenceDialogFragment dialogFragment = 96 (UpdatableListPreferenceDialogFragment) getFragmentManager() 97 .findFragmentByTag(TAG); 98 mDialogFragment = dialogFragment; 99 } 100 } 101 102 @Override getHelpResource()103 public int getHelpResource() { 104 return R.string.help_url_sound; 105 } 106 107 @Override onPause()108 public void onPause() { 109 super.onPause(); 110 mVolumeCallback.stopSample(); 111 } 112 113 @Override onPreferenceTreeClick(Preference preference)114 public boolean onPreferenceTreeClick(Preference preference) { 115 if (preference instanceof RingtonePreference) { 116 writePreferenceClickMetric(preference); 117 mRequestPreference = (RingtonePreference) preference; 118 mRequestPreference.onPrepareRingtonePickerIntent(mRequestPreference.getIntent()); 119 getActivity().startActivityForResultAsUser( 120 mRequestPreference.getIntent(), 121 REQUEST_CODE, 122 null, 123 UserHandle.of(mRequestPreference.getUserId())); 124 return true; 125 } 126 return super.onPreferenceTreeClick(preference); 127 } 128 129 @Override onDisplayPreferenceDialog(Preference preference)130 public void onDisplayPreferenceDialog(Preference preference) { 131 if (TextUtils.equals(mVibrationPreferencesKey, preference.getKey())) { 132 super.onDisplayPreferenceDialog(preference); 133 return; 134 } 135 final int metricsCategory; 136 if (mHfpOutputControllerKey.equals(preference.getKey())) { 137 metricsCategory = SettingsEnums.DIALOG_SWITCH_HFP_DEVICES; 138 } else { 139 metricsCategory = Instrumentable.METRICS_CATEGORY_UNKNOWN; 140 } 141 142 mDialogFragment = UpdatableListPreferenceDialogFragment. 143 newInstance(preference.getKey(), metricsCategory); 144 mDialogFragment.setTargetFragment(this, 0); 145 mDialogFragment.show(getFragmentManager(), TAG); 146 } 147 148 @Override getLogTag()149 protected String getLogTag() { 150 return TAG; 151 } 152 153 @Override getPreferenceScreenResId()154 protected int getPreferenceScreenResId() { 155 return R.xml.sound_settings; 156 } 157 158 @Override createPreferenceControllers(Context context)159 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { 160 return buildPreferenceControllers(context, this, getSettingsLifecycle()); 161 } 162 163 @Override onActivityResult(int requestCode, int resultCode, Intent data)164 public void onActivityResult(int requestCode, int resultCode, Intent data) { 165 if (mRequestPreference != null) { 166 mRequestPreference.onActivityResult(requestCode, resultCode, data); 167 mRequestPreference = null; 168 } 169 } 170 171 @Override onSaveInstanceState(Bundle outState)172 public void onSaveInstanceState(Bundle outState) { 173 super.onSaveInstanceState(outState); 174 if (mRequestPreference != null) { 175 outState.putString(SELECTED_PREFERENCE_KEY, mRequestPreference.getKey()); 176 } 177 } 178 179 @Override onAttach(Context context)180 public void onAttach(Context context) { 181 super.onAttach(context); 182 ArrayList<VolumeSeekBarPreferenceController> volumeControllers = new ArrayList<>(); 183 volumeControllers.add(use(AlarmVolumePreferenceController.class)); 184 volumeControllers.add(use(MediaVolumePreferenceController.class)); 185 volumeControllers.add(use(RingVolumePreferenceController.class)); 186 volumeControllers.add(use(NotificationVolumePreferenceController.class)); 187 volumeControllers.add(use(CallVolumePreferenceController.class)); 188 189 use(HandsFreeProfileOutputPreferenceController.class).setCallback(listPreference -> 190 onPreferenceDataChanged(listPreference)); 191 mHfpOutputControllerKey = 192 use(HandsFreeProfileOutputPreferenceController.class).getPreferenceKey(); 193 194 for (VolumeSeekBarPreferenceController controller : volumeControllers) { 195 controller.setCallback(mVolumeCallback); 196 getSettingsLifecycle().addObserver(controller); 197 } 198 } 199 200 // === Volumes === 201 202 final class VolumePreferenceCallback implements VolumeSeekBarPreference.Callback { 203 private SeekBarVolumizer mCurrent; 204 205 @Override onSampleStarting(SeekBarVolumizer sbv)206 public void onSampleStarting(SeekBarVolumizer sbv) { 207 if (mCurrent != null) { 208 mHandler.removeMessages(STOP_SAMPLE); 209 mHandler.sendEmptyMessageDelayed(STOP_SAMPLE, SAMPLE_CUTOFF); 210 } 211 } 212 213 @Override onStreamValueChanged(int stream, int progress)214 public void onStreamValueChanged(int stream, int progress) { 215 if (mCurrent != null) { 216 mHandler.removeMessages(STOP_SAMPLE); 217 mHandler.sendEmptyMessageDelayed(STOP_SAMPLE, SAMPLE_CUTOFF); 218 } 219 } 220 221 @Override onStartTrackingTouch(SeekBarVolumizer sbv)222 public void onStartTrackingTouch(SeekBarVolumizer sbv) { 223 // stop the ringtone when other seek bar is adjust 224 if (mCurrent != null && mCurrent != sbv) { 225 mCurrent.stopSample(); 226 } 227 mCurrent = sbv; 228 } 229 stopSample()230 public void stopSample() { 231 if (mCurrent != null) { 232 mCurrent.stopSample(); 233 } 234 } 235 } 236 buildPreferenceControllers(Context context, SoundSettings fragment, Lifecycle lifecycle)237 private static List<AbstractPreferenceController> buildPreferenceControllers(Context context, 238 SoundSettings fragment, Lifecycle lifecycle) { 239 final List<AbstractPreferenceController> controllers = new ArrayList<>(); 240 241 // Volumes are added via xml 242 243 // === Phone & notification ringtone === 244 controllers.add(new PhoneRingtonePreferenceController(context)); 245 controllers.add(new AlarmRingtonePreferenceController(context)); 246 controllers.add(new NotificationRingtonePreferenceController(context)); 247 248 // === Other Sound Settings === 249 final DialPadTonePreferenceController dialPadTonePreferenceController = 250 new DialPadTonePreferenceController(context, fragment, lifecycle); 251 final ScreenLockSoundPreferenceController screenLockSoundPreferenceController = 252 new ScreenLockSoundPreferenceController(context, fragment, lifecycle); 253 final ChargingSoundPreferenceController chargingSoundPreferenceController = 254 new ChargingSoundPreferenceController(context, fragment, lifecycle); 255 final DockingSoundPreferenceController dockingSoundPreferenceController = 256 new DockingSoundPreferenceController(context, fragment, lifecycle); 257 final TouchSoundPreferenceController touchSoundPreferenceController = 258 new TouchSoundPreferenceController(context, fragment, lifecycle); 259 final VibrateOnTouchPreferenceController vibrateOnTouchPreferenceController = 260 new VibrateOnTouchPreferenceController(context, fragment, lifecycle); 261 final DockAudioMediaPreferenceController dockAudioMediaPreferenceController = 262 new DockAudioMediaPreferenceController(context, fragment, lifecycle); 263 final BootSoundPreferenceController bootSoundPreferenceController = 264 new BootSoundPreferenceController(context); 265 final EmergencyTonePreferenceController emergencyTonePreferenceController = 266 new EmergencyTonePreferenceController(context, fragment, lifecycle); 267 268 controllers.add(dialPadTonePreferenceController); 269 controllers.add(screenLockSoundPreferenceController); 270 controllers.add(chargingSoundPreferenceController); 271 controllers.add(dockingSoundPreferenceController); 272 controllers.add(touchSoundPreferenceController); 273 controllers.add(vibrateOnTouchPreferenceController); 274 controllers.add(dockAudioMediaPreferenceController); 275 controllers.add(bootSoundPreferenceController); 276 controllers.add(emergencyTonePreferenceController); 277 controllers.add(new PreferenceCategoryController(context, 278 "other_sounds_and_vibrations_category").setChildren( 279 Arrays.asList(dialPadTonePreferenceController, 280 screenLockSoundPreferenceController, 281 chargingSoundPreferenceController, 282 dockingSoundPreferenceController, 283 touchSoundPreferenceController, 284 vibrateOnTouchPreferenceController, 285 dockAudioMediaPreferenceController, 286 bootSoundPreferenceController, 287 emergencyTonePreferenceController))); 288 289 return controllers; 290 } 291 292 // === Indexing === 293 294 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 295 new BaseSearchIndexProvider(R.xml.sound_settings) { 296 297 @Override 298 public List<AbstractPreferenceController> createPreferenceControllers( 299 Context context) { 300 return buildPreferenceControllers(context, null /* fragment */, 301 null /* lifecycle */); 302 } 303 }; 304 onPreferenceDataChanged(ListPreference preference)305 private void onPreferenceDataChanged(ListPreference preference) { 306 if (mDialogFragment != null) { 307 mDialogFragment.onListPreferenceUpdated(preference); 308 } 309 } 310 } 311