• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.NotificationChannel;
20 import android.app.NotificationManager;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.net.Uri;
24 import android.preference.PreferenceManager;
25 import android.support.v7.preference.Preference;
26 import android.support.v7.preference.PreferenceScreen;
27 
28 import com.android.settings.SettingsPreferenceFragment;
29 import com.android.settings.core.PreferenceControllerMixin;
30 
31 public class SoundPreferenceController extends NotificationPreferenceController
32         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
33         PreferenceManager.OnActivityResultListener {
34 
35     private static final String KEY_SOUND = "ringtone";
36     private final SettingsPreferenceFragment mFragment;
37     private final NotificationSettingsBase.ImportanceListener mListener;
38     private NotificationSoundPreference mPreference;
39     protected static final int CODE = 200;
40 
SoundPreferenceController(Context context, SettingsPreferenceFragment hostFragment, NotificationSettingsBase.ImportanceListener importanceListener, NotificationBackend backend)41     public SoundPreferenceController(Context context, SettingsPreferenceFragment hostFragment,
42             NotificationSettingsBase.ImportanceListener importanceListener,
43             NotificationBackend backend) {
44         super(context, backend);
45         mFragment = hostFragment;
46         mListener = importanceListener;
47     }
48 
49     @Override
getPreferenceKey()50     public String getPreferenceKey() {
51         return KEY_SOUND;
52     }
53 
54     @Override
isAvailable()55     public boolean isAvailable() {
56         if (!super.isAvailable()) {
57             return false;
58         }
59         if (mChannel == null) {
60             return false;
61         }
62         return checkCanBeVisible(NotificationManager.IMPORTANCE_DEFAULT) && !isDefaultChannel();
63     }
64 
65     @Override
displayPreference(PreferenceScreen screen)66     public void displayPreference(PreferenceScreen screen) {
67         super.displayPreference(screen);
68 
69         mPreference = (NotificationSoundPreference) screen.findPreference(getPreferenceKey());
70     }
71 
updateState(Preference preference)72     public void updateState(Preference preference) {
73         if (mAppRow!= null && mChannel != null) {
74             NotificationSoundPreference pref = (NotificationSoundPreference) preference;
75             pref.setEnabled(mAdmin == null && isChannelConfigurable());
76             pref.setRingtone(mChannel.getSound());
77         }
78     }
79 
80     @Override
onPreferenceChange(Preference preference, Object newValue)81     public boolean onPreferenceChange(Preference preference, Object newValue) {
82         if (mChannel != null) {
83             mChannel.setSound((Uri) newValue, mChannel.getAudioAttributes());
84             saveChannel();
85         }
86         return true;
87     }
88 
89     @Override
handlePreferenceTreeClick(Preference preference)90     public boolean handlePreferenceTreeClick(Preference preference) {
91         if (KEY_SOUND.equals(preference.getKey()) && mFragment != null) {
92             NotificationSoundPreference pref = (NotificationSoundPreference) preference;
93             pref.onPrepareRingtonePickerIntent(pref.getIntent());
94             mFragment.startActivityForResult(preference.getIntent(), CODE);
95             return true;
96         }
97         return false;
98     }
99 
100     @Override
onActivityResult(int requestCode, int resultCode, Intent data)101     public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
102         if (CODE == requestCode) {
103             if (mPreference != null) {
104                 mPreference.onActivityResult(requestCode, resultCode, data);
105             }
106             // the importance hasn't changed, but the importance description might as a result of
107             // user's selection.
108             mListener.onImportanceChanged();
109             return true;
110         }
111         return false;
112     }
113 
hasValidSound(NotificationChannel channel)114     protected static boolean hasValidSound(NotificationChannel channel) {
115         return channel != null
116                 && channel.getSound() != null && !Uri.EMPTY.equals(channel.getSound());
117     }
118 }
119