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