• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 && mCurrent != sbv) {
208                 mCurrent.stopSample();
209             }
210             mCurrent = sbv;
211             if (mCurrent != null) {
212                 mHandler.removeMessages(STOP_SAMPLE);
213                 mHandler.sendEmptyMessageDelayed(STOP_SAMPLE, SAMPLE_CUTOFF);
214             }
215         }
216 
217         @Override
onStreamValueChanged(int stream, int progress)218         public void onStreamValueChanged(int stream, int progress) {
219             if (mCurrent != null) {
220                 mHandler.removeMessages(STOP_SAMPLE);
221                 mHandler.sendEmptyMessageDelayed(STOP_SAMPLE, SAMPLE_CUTOFF);
222             }
223         }
224 
stopSample()225         public void stopSample() {
226             if (mCurrent != null) {
227                 mCurrent.stopSample();
228             }
229         }
230     }
231 
buildPreferenceControllers(Context context, SoundSettings fragment, Lifecycle lifecycle)232     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
233             SoundSettings fragment, Lifecycle lifecycle) {
234         final List<AbstractPreferenceController> controllers = new ArrayList<>();
235 
236         // Volumes are added via xml
237 
238         // === Phone & notification ringtone ===
239         controllers.add(new PhoneRingtonePreferenceController(context));
240         controllers.add(new AlarmRingtonePreferenceController(context));
241         controllers.add(new NotificationRingtonePreferenceController(context));
242 
243         // === Work Sound Settings ===
244         controllers.add(new WorkSoundPreferenceController(context, fragment, lifecycle));
245 
246         // === Other Sound Settings ===
247         final DialPadTonePreferenceController dialPadTonePreferenceController =
248                 new DialPadTonePreferenceController(context, fragment, lifecycle);
249         final ScreenLockSoundPreferenceController screenLockSoundPreferenceController =
250                 new ScreenLockSoundPreferenceController(context, fragment, lifecycle);
251         final ChargingSoundPreferenceController chargingSoundPreferenceController =
252                 new ChargingSoundPreferenceController(context, fragment, lifecycle);
253         final DockingSoundPreferenceController dockingSoundPreferenceController =
254                 new DockingSoundPreferenceController(context, fragment, lifecycle);
255         final TouchSoundPreferenceController touchSoundPreferenceController =
256                 new TouchSoundPreferenceController(context, fragment, lifecycle);
257         final VibrateOnTouchPreferenceController vibrateOnTouchPreferenceController =
258                 new VibrateOnTouchPreferenceController(context, fragment, lifecycle);
259         final DockAudioMediaPreferenceController dockAudioMediaPreferenceController =
260                 new DockAudioMediaPreferenceController(context, fragment, lifecycle);
261         final BootSoundPreferenceController bootSoundPreferenceController =
262                 new BootSoundPreferenceController(context);
263         final EmergencyTonePreferenceController emergencyTonePreferenceController =
264                 new EmergencyTonePreferenceController(context, fragment, lifecycle);
265 
266         controllers.add(dialPadTonePreferenceController);
267         controllers.add(screenLockSoundPreferenceController);
268         controllers.add(chargingSoundPreferenceController);
269         controllers.add(dockingSoundPreferenceController);
270         controllers.add(touchSoundPreferenceController);
271         controllers.add(vibrateOnTouchPreferenceController);
272         controllers.add(dockAudioMediaPreferenceController);
273         controllers.add(bootSoundPreferenceController);
274         controllers.add(emergencyTonePreferenceController);
275         controllers.add(new PreferenceCategoryController(context,
276                 "other_sounds_and_vibrations_category").setChildren(
277                 Arrays.asList(dialPadTonePreferenceController,
278                         screenLockSoundPreferenceController,
279                         chargingSoundPreferenceController,
280                         dockingSoundPreferenceController,
281                         touchSoundPreferenceController,
282                         vibrateOnTouchPreferenceController,
283                         dockAudioMediaPreferenceController,
284                         bootSoundPreferenceController,
285                         emergencyTonePreferenceController)));
286 
287         return controllers;
288     }
289 
290     // === Indexing ===
291 
292     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
293             new BaseSearchIndexProvider(R.xml.sound_settings) {
294 
295                 @Override
296                 public List<AbstractPreferenceController> createPreferenceControllers(
297                         Context context) {
298                     return buildPreferenceControllers(context, null /* fragment */,
299                             null /* lifecycle */);
300                 }
301             };
302 
303     // === Work Sound Settings ===
304 
enableWorkSync()305     void enableWorkSync() {
306         final WorkSoundPreferenceController workSoundController =
307                 use(WorkSoundPreferenceController.class);
308         if (workSoundController != null) {
309             workSoundController.enableWorkSync();
310         }
311     }
312 
onPreferenceDataChanged(ListPreference preference)313     private void onPreferenceDataChanged(ListPreference preference) {
314         if (mDialogFragment != null) {
315             mDialogFragment.onListPreferenceUpdated(preference);
316         }
317     }
318 }
319