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.NotificationManager.IMPORTANCE_DEFAULT;
20 import static android.app.NotificationManager.IMPORTANCE_LOW;
21 import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
22 
23 import android.app.NotificationChannel;
24 import android.content.Context;
25 import android.util.Log;
26 
27 import androidx.preference.Preference;
28 
29 import com.android.settings.core.PreferenceControllerMixin;
30 import com.android.settings.notification.NotificationBackend;
31 import com.android.settingslib.RestrictedSwitchPreference;
32 
33 public class AllowSoundPreferenceController extends NotificationPreferenceController
34         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
35 
36     private static final String TAG = "AllowSoundPrefContr";
37     private static final String KEY_IMPORTANCE = "allow_sound";
38     private NotificationSettings.DependentFieldListener mDependentFieldListener;
39 
AllowSoundPreferenceController(Context context, NotificationSettings.DependentFieldListener dependentFieldListener, NotificationBackend backend)40     public AllowSoundPreferenceController(Context context,
41             NotificationSettings.DependentFieldListener dependentFieldListener,
42             NotificationBackend backend) {
43         super(context, backend);
44         mDependentFieldListener = dependentFieldListener;
45     }
46 
47     @Override
getPreferenceKey()48     public String getPreferenceKey() {
49         return KEY_IMPORTANCE;
50     }
51 
52     @Override
isAvailable()53     public boolean isAvailable() {
54         if (!super.isAvailable()) {
55             return false;
56         }
57         return mChannel != null && NotificationChannel.DEFAULT_CHANNEL_ID.equals(mChannel.getId());
58 
59     }
60 
61     @Override
isIncludedInFilter()62     boolean isIncludedInFilter() {
63         return mPreferenceFilter.contains(NotificationChannel.EDIT_SOUND);
64     }
65 
66     @Override
updateState(Preference preference)67     public void updateState(Preference preference) {
68         if (mChannel != null) {
69             RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference;
70             pref.setDisabledByAdmin(mAdmin);
71             pref.setEnabled(!pref.isDisabledByAdmin());
72             pref.setChecked(mChannel.getImportance() >= IMPORTANCE_DEFAULT
73                     || mChannel.getImportance() == IMPORTANCE_UNSPECIFIED);
74         } else { Log.i(TAG, "tried to updatestate on a null channel?!"); }
75     }
76 
77     @Override
onPreferenceChange(Preference preference, Object newValue)78     public boolean onPreferenceChange(Preference preference, Object newValue) {
79         if (mChannel != null) {
80             final int importance =
81                     ((Boolean) newValue ? IMPORTANCE_UNSPECIFIED : IMPORTANCE_LOW);
82             mChannel.setImportance(importance);
83             mChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
84             saveChannel();
85             mDependentFieldListener.onFieldValueChanged();
86         }
87         return true;
88     }
89 }
90