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 updateState(Preference preference)62 public void updateState(Preference preference) { 63 if (mAppRow!= null && mChannel != null) { 64 preference.setEnabled(mAdmin == null && !mChannel.isImportanceLockedByOEM()); 65 ImportancePreference pref = (ImportancePreference) preference; 66 pref.setConfigurable(!mChannel.isImportanceLockedByOEM()); 67 pref.setImportance(mChannel.getImportance()); 68 pref.setDisplayInStatusBar(mBackend.showSilentInStatusBar(mContext.getPackageName())); 69 pref.setDisplayOnLockscreen(Settings.Secure.getInt(mContext.getContentResolver(), 70 Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1) == 1); 71 } 72 } 73 74 @Override onPreferenceChange(Preference preference, Object newValue)75 public boolean onPreferenceChange(Preference preference, Object newValue) { 76 if (mChannel != null) { 77 final int importance = (Integer) newValue; 78 79 // If you are moving from an importance level without sound to one with sound, 80 // but the sound you had selected was "Silence", 81 // then set sound for this channel to your default sound, 82 // because you probably intended to cause this channel to actually start making sound. 83 if (mChannel.getImportance() < IMPORTANCE_DEFAULT 84 && !SoundPreferenceController.hasValidSound(mChannel) 85 && importance >= IMPORTANCE_DEFAULT) { 86 mChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), 87 mChannel.getAudioAttributes()); 88 mChannel.lockFields(USER_LOCKED_SOUND); 89 } 90 91 mChannel.setImportance(importance); 92 mChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); 93 saveChannel(); 94 mDependentFieldListener.onFieldValueChanged(); 95 } 96 return true; 97 } 98 } 99