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_LOW; 20 import static android.app.NotificationManager.IMPORTANCE_MIN; 21 22 import android.app.NotificationChannel; 23 import android.content.Context; 24 25 import com.android.settings.core.PreferenceControllerMixin; 26 import com.android.settings.notification.NotificationBackend; 27 import com.android.settingslib.RestrictedSwitchPreference; 28 29 import androidx.preference.Preference; 30 31 public class MinImportancePreferenceController extends NotificationPreferenceController 32 implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener { 33 34 private static final String KEY_IMPORTANCE = "min_importance"; 35 private NotificationSettings.DependentFieldListener mDependentFieldListener; 36 MinImportancePreferenceController(Context context, NotificationSettings.DependentFieldListener dependentFieldListener, NotificationBackend backend)37 public MinImportancePreferenceController(Context context, 38 NotificationSettings.DependentFieldListener dependentFieldListener, 39 NotificationBackend backend) { 40 super(context, backend); 41 mDependentFieldListener = dependentFieldListener; 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 if (isDefaultChannel()) { 58 return false; 59 } 60 return mChannel.getImportance() <= IMPORTANCE_LOW; 61 } 62 63 @Override isIncludedInFilter()64 boolean isIncludedInFilter() { 65 return mPreferenceFilter.contains(NotificationChannel.EDIT_IMPORTANCE); 66 } 67 68 @Override updateState(Preference preference)69 public void updateState(Preference preference) { 70 if (mAppRow != null && mChannel != null) { 71 preference.setEnabled(mAdmin == null && isChannelConfigurable(mChannel)); 72 73 RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference; 74 pref.setChecked(mChannel.getImportance() == IMPORTANCE_MIN); 75 } 76 } 77 78 @Override onPreferenceChange(Preference preference, Object newValue)79 public boolean onPreferenceChange(Preference preference, Object newValue) { 80 if (mChannel != null) { 81 final boolean checked = (boolean) newValue; 82 83 mChannel.setImportance(checked ? IMPORTANCE_MIN : IMPORTANCE_LOW); 84 mChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); 85 saveChannel(); 86 mDependentFieldListener.onFieldValueChanged(); 87 } 88 return true; 89 } 90 } 91