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 import static android.app.NotificationManager.IMPORTANCE_HIGH; 22 import static android.app.NotificationManager.IMPORTANCE_MIN; 23 import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED; 24 25 import android.app.NotificationChannel; 26 import android.app.NotificationManager; 27 import android.content.Context; 28 import android.media.RingtoneManager; 29 import android.support.v7.preference.Preference; 30 31 import com.android.settings.R; 32 import com.android.settings.RestrictedListPreference; 33 import com.android.settings.core.PreferenceControllerMixin; 34 35 public class ImportancePreferenceController extends NotificationPreferenceController 36 implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener { 37 38 private static final String KEY_IMPORTANCE = "importance"; 39 private NotificationSettingsBase.ImportanceListener mImportanceListener; 40 ImportancePreferenceController(Context context, NotificationSettingsBase.ImportanceListener importanceListener, NotificationBackend backend)41 public ImportancePreferenceController(Context context, 42 NotificationSettingsBase.ImportanceListener importanceListener, 43 NotificationBackend backend) { 44 super(context, backend); 45 mImportanceListener = importanceListener; 46 } 47 48 @Override getPreferenceKey()49 public String getPreferenceKey() { 50 return KEY_IMPORTANCE; 51 } 52 53 @Override isAvailable()54 public boolean isAvailable() { 55 if (!super.isAvailable()) { 56 return false; 57 } 58 if (mChannel == null) { 59 return false; 60 } 61 return !isDefaultChannel(); 62 } 63 64 @Override updateState(Preference preference)65 public void updateState(Preference preference) { 66 if (mAppRow!= null && mChannel != null) { 67 preference.setEnabled(mAdmin == null && isChannelConfigurable()); 68 preference.setSummary(getImportanceSummary(mChannel)); 69 70 int importances = IMPORTANCE_HIGH - IMPORTANCE_MIN + 1; 71 CharSequence[] entries = new CharSequence[importances]; 72 CharSequence[] values = new CharSequence[importances]; 73 74 int index = 0; 75 for (int i = IMPORTANCE_HIGH; i >= IMPORTANCE_MIN; i--) { 76 NotificationChannel channel = new NotificationChannel("", "", i); 77 entries[index] = getImportanceSummary(channel); 78 values[index] = String.valueOf(i); 79 index++; 80 } 81 82 RestrictedListPreference pref = (RestrictedListPreference) preference; 83 pref.setEntries(entries); 84 pref.setEntryValues(values); 85 pref.setValue(String.valueOf(mChannel.getImportance())); 86 } 87 } 88 89 @Override onPreferenceChange(Preference preference, Object newValue)90 public boolean onPreferenceChange(Preference preference, Object newValue) { 91 if (mChannel != null) { 92 final int importance = Integer.parseInt((String) newValue); 93 94 // If you are moving from an importance level without sound to one with sound, 95 // but the sound you had selected was "Silence", 96 // then set sound for this channel to your default sound, 97 // because you probably intended to cause this channel to actually start making sound. 98 if (mChannel.getImportance() < IMPORTANCE_DEFAULT 99 && !SoundPreferenceController.hasValidSound(mChannel) 100 && importance >= IMPORTANCE_DEFAULT) { 101 mChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), 102 mChannel.getAudioAttributes()); 103 mChannel.lockFields(USER_LOCKED_SOUND); 104 } 105 106 mChannel.setImportance(importance); 107 mChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); 108 saveChannel(); 109 mImportanceListener.onImportanceChanged(); 110 } 111 return true; 112 } 113 getImportanceSummary(NotificationChannel channel)114 protected String getImportanceSummary(NotificationChannel channel) { 115 String summary = ""; 116 int importance = channel.getImportance(); 117 switch (importance) { 118 case IMPORTANCE_UNSPECIFIED: 119 summary = mContext.getString(R.string.notification_importance_unspecified); 120 break; 121 case NotificationManager.IMPORTANCE_MIN: 122 summary = mContext.getString(R.string.notification_importance_min); 123 break; 124 case NotificationManager.IMPORTANCE_LOW: 125 summary = mContext.getString(R.string.notification_importance_low); 126 break; 127 case NotificationManager.IMPORTANCE_DEFAULT: 128 if (SoundPreferenceController.hasValidSound(channel)) { 129 summary = mContext.getString(R.string.notification_importance_default); 130 } else { 131 summary = mContext.getString(R.string.notification_importance_low); 132 } 133 break; 134 case NotificationManager.IMPORTANCE_HIGH: 135 case NotificationManager.IMPORTANCE_MAX: 136 if (SoundPreferenceController.hasValidSound(channel)) { 137 summary = mContext.getString(R.string.notification_importance_high); 138 } else { 139 summary = mContext.getString(R.string.notification_importance_high_silent); 140 } 141 break; 142 default: 143 return ""; 144 } 145 146 return summary; 147 } 148 } 149