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.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.settingslib.RestrictedSwitchPreference; 27 28 import androidx.preference.Preference; 29 30 public class MinImportancePreferenceController extends NotificationPreferenceController 31 implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener { 32 33 private static final String KEY_IMPORTANCE = "min_importance"; 34 private NotificationSettingsBase.ImportanceListener mImportanceListener; 35 MinImportancePreferenceController(Context context, NotificationSettingsBase.ImportanceListener importanceListener, NotificationBackend backend)36 public MinImportancePreferenceController(Context context, 37 NotificationSettingsBase.ImportanceListener importanceListener, 38 NotificationBackend backend) { 39 super(context, backend); 40 mImportanceListener = importanceListener; 41 } 42 43 @Override getPreferenceKey()44 public String getPreferenceKey() { 45 return KEY_IMPORTANCE; 46 } 47 48 @Override isAvailable()49 public boolean isAvailable() { 50 if (!super.isAvailable()) { 51 return false; 52 } 53 if (mChannel == null) { 54 return false; 55 } 56 if (isDefaultChannel()) { 57 return false; 58 } 59 return mChannel.getImportance() <= IMPORTANCE_LOW; 60 } 61 62 @Override updateState(Preference preference)63 public void updateState(Preference preference) { 64 if (mAppRow != null && mChannel != null) { 65 preference.setEnabled(mAdmin == null && !mChannel.isImportanceLockedByOEM()); 66 67 RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference; 68 pref.setChecked(mChannel.getImportance() == IMPORTANCE_MIN); 69 } 70 } 71 72 @Override onPreferenceChange(Preference preference, Object newValue)73 public boolean onPreferenceChange(Preference preference, Object newValue) { 74 if (mChannel != null) { 75 final boolean checked = (boolean) newValue; 76 77 mChannel.setImportance(checked ? IMPORTANCE_MIN : IMPORTANCE_LOW); 78 mChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); 79 saveChannel(); 80 mImportanceListener.onImportanceChanged(); 81 } 82 return true; 83 } 84 } 85