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.app;
18 
19 import static android.media.AudioAttributes.USAGE_ALARM;
20 import static android.media.AudioAttributes.USAGE_NOTIFICATION_RINGTONE;
21 
22 import android.app.NotificationChannel;
23 import android.app.NotificationManager;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.media.RingtoneManager;
27 import android.net.Uri;
28 import android.preference.PreferenceManager;
29 
30 import androidx.preference.Preference;
31 import androidx.preference.PreferenceScreen;
32 
33 import com.android.settings.SettingsPreferenceFragment;
34 import com.android.settings.core.PreferenceControllerMixin;
35 import com.android.settings.notification.NotificationBackend;
36 
37 public class SoundPreferenceController extends NotificationPreferenceController
38         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
39         PreferenceManager.OnActivityResultListener {
40 
41     private static final String KEY_SOUND = "ringtone";
42     private final SettingsPreferenceFragment mFragment;
43     private final NotificationSettings.DependentFieldListener mListener;
44     private NotificationSoundPreference mPreference;
45     protected static final int CODE = 200;
46 
SoundPreferenceController(Context context, SettingsPreferenceFragment hostFragment, NotificationSettings.DependentFieldListener dependentFieldListener, NotificationBackend backend)47     public SoundPreferenceController(Context context, SettingsPreferenceFragment hostFragment,
48             NotificationSettings.DependentFieldListener dependentFieldListener,
49             NotificationBackend backend) {
50         super(context, backend);
51         mFragment = hostFragment;
52         mListener = dependentFieldListener;
53     }
54 
55     @Override
getPreferenceKey()56     public String getPreferenceKey() {
57         return KEY_SOUND;
58     }
59 
60     @Override
isAvailable()61     public boolean isAvailable() {
62         if (!super.isAvailable()) {
63             return false;
64         }
65         if (mChannel == null) {
66             return false;
67         }
68         return checkCanBeVisible(NotificationManager.IMPORTANCE_DEFAULT) && !isDefaultChannel();
69     }
70 
71     @Override
isIncludedInFilter()72     boolean isIncludedInFilter() {
73         return mPreferenceFilter.contains(NotificationChannel.EDIT_SOUND);
74     }
75 
76     @Override
displayPreference(PreferenceScreen screen)77     public void displayPreference(PreferenceScreen screen) {
78         super.displayPreference(screen);
79 
80         mPreference = screen.findPreference(getPreferenceKey());
81     }
82 
updateState(Preference preference)83     public void updateState(Preference preference) {
84         if (mAppRow!= null && mChannel != null) {
85             NotificationSoundPreference pref = (NotificationSoundPreference) preference;
86             pref.setEnabled(mAdmin == null);
87             pref.setRingtone(mChannel.getSound());
88         }
89     }
90 
91     @Override
onPreferenceChange(Preference preference, Object newValue)92     public boolean onPreferenceChange(Preference preference, Object newValue) {
93         if (mChannel != null) {
94             mChannel.setSound((Uri) newValue, mChannel.getAudioAttributes());
95             saveChannel();
96         }
97         return true;
98     }
99 
100     @Override
handlePreferenceTreeClick(Preference preference)101     public boolean handlePreferenceTreeClick(Preference preference) {
102         if (KEY_SOUND.equals(preference.getKey()) && mFragment != null) {
103             NotificationSoundPreference pref = (NotificationSoundPreference) preference;
104             if (mChannel != null && mChannel.getAudioAttributes() != null) {
105                 if (USAGE_ALARM == mChannel.getAudioAttributes().getUsage()) {
106                     pref.setRingtoneType(RingtoneManager.TYPE_ALARM);
107                 } else if (USAGE_NOTIFICATION_RINGTONE
108                         == mChannel.getAudioAttributes().getUsage()) {
109                     pref.setRingtoneType(RingtoneManager.TYPE_RINGTONE);
110                 } else {
111                     pref.setRingtoneType(RingtoneManager.TYPE_NOTIFICATION);
112                 }
113             }
114             pref.onPrepareRingtonePickerIntent(pref.getIntent());
115             mFragment.startActivityForResult(preference.getIntent(), CODE);
116             return true;
117         }
118         return false;
119     }
120 
121     @Override
onActivityResult(int requestCode, int resultCode, Intent data)122     public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
123         if (CODE == requestCode) {
124             if (mPreference != null) {
125                 mPreference.onActivityResult(requestCode, resultCode, data);
126             }
127             // the importance hasn't changed, but the importance description might as a result of
128             // user's selection.
129             mListener.onFieldValueChanged();
130             return true;
131         }
132         return false;
133     }
134 
hasValidSound(NotificationChannel channel)135     protected static boolean hasValidSound(NotificationChannel channel) {
136         return channel != null
137                 && channel.getSound() != null && !Uri.EMPTY.equals(channel.getSound());
138     }
139 }
140