• 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.app;
18 
19 import static android.app.NotificationChannel.USER_LOCKED_SOUND;
20 import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
21 
22 import android.app.NotificationChannel;
23 import android.content.Context;
24 import android.media.RingtoneManager;
25 import android.provider.Settings;
26 
27 import com.android.settings.core.PreferenceControllerMixin;
28 import com.android.settings.notification.NotificationBackend;
29 
30 import androidx.preference.Preference;
31 
32 public class ImportancePreferenceController extends NotificationPreferenceController
33         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener  {
34 
35     private static final String KEY_IMPORTANCE = "importance";
36     private NotificationSettings.DependentFieldListener mDependentFieldListener;
37 
ImportancePreferenceController(Context context, NotificationSettings.DependentFieldListener dependentFieldListener, NotificationBackend backend)38     public ImportancePreferenceController(Context context,
39             NotificationSettings.DependentFieldListener dependentFieldListener,
40             NotificationBackend backend) {
41         super(context, backend);
42         mDependentFieldListener = dependentFieldListener;
43     }
44 
45     @Override
getPreferenceKey()46     public String getPreferenceKey() {
47         return KEY_IMPORTANCE;
48     }
49 
50     @Override
isAvailable()51     public boolean isAvailable() {
52         if (!super.isAvailable()) {
53             return false;
54         }
55         if (mChannel == null) {
56             return false;
57         }
58         return !isDefaultChannel();
59     }
60 
61     @Override
isIncludedInFilter()62     boolean isIncludedInFilter() {
63         return mPreferenceFilter.contains(NotificationChannel.EDIT_IMPORTANCE);
64     }
65 
66     @Override
updateState(Preference preference)67     public void updateState(Preference preference) {
68         if (mAppRow!= null && mChannel != null) {
69             preference.setEnabled(mAdmin == null && isChannelConfigurable(mChannel));
70             ImportancePreference pref = (ImportancePreference) preference;
71             pref.setConfigurable(isChannelConfigurable(mChannel));
72             pref.setImportance(mChannel.getImportance());
73             pref.setDisplayInStatusBar(mBackend.showSilentInStatusBar(mContext.getPackageName()));
74             pref.setDisplayOnLockscreen(Settings.Secure.getInt(mContext.getContentResolver(),
75                     Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1) == 1);
76         }
77     }
78 
79     @Override
onPreferenceChange(Preference preference, Object newValue)80     public boolean onPreferenceChange(Preference preference, Object newValue) {
81         if (mChannel != null) {
82             final int importance = (Integer) newValue;
83 
84             // If you are moving from an importance level without sound to one with sound,
85             // but the sound you had selected was "Silence",
86             // then set sound for this channel to your default sound,
87             // because you probably intended to cause this channel to actually start making sound.
88             if (mChannel.getImportance() < IMPORTANCE_DEFAULT
89                     && !SoundPreferenceController.hasValidSound(mChannel)
90                     && importance >= IMPORTANCE_DEFAULT) {
91                 mChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),
92                         mChannel.getAudioAttributes());
93                 mChannel.lockFields(USER_LOCKED_SOUND);
94             }
95 
96             mChannel.setImportance(importance);
97             mChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
98             saveChannel();
99             mDependentFieldListener.onFieldValueChanged();
100         }
101         return true;
102     }
103 }
104