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_IMPORTANCE; 20 import static android.app.NotificationChannel.USER_LOCKED_SOUND; 21 import static android.app.NotificationManager.IMPORTANCE_DEFAULT; 22 import static android.app.NotificationManager.IMPORTANCE_HIGH; 23 import static android.app.NotificationManager.IMPORTANCE_LOW; 24 import static android.app.NotificationManager.IMPORTANCE_MAX; 25 import static android.app.NotificationManager.IMPORTANCE_MIN; 26 27 import android.content.Context; 28 import android.media.RingtoneManager; 29 import android.provider.SearchIndexableResource; 30 import android.support.v7.preference.Preference; 31 import android.support.v7.preference.PreferenceScreen; 32 import android.text.TextUtils; 33 import android.util.Log; 34 35 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 36 import com.android.settings.R; 37 import com.android.settings.search.BaseSearchIndexProvider; 38 import com.android.settings.search.Indexable; 39 import com.android.settings.widget.RadioButtonPreference; 40 41 import java.util.ArrayList; 42 import java.util.List; 43 44 public class ChannelImportanceSettings extends NotificationSettingsBase 45 implements RadioButtonPreference.OnClickListener, Indexable { 46 private static final String TAG = "NotiImportance"; 47 48 private static final String KEY_IMPORTANCE_HIGH = "importance_high"; 49 private static final String KEY_IMPORTANCE_DEFAULT = "importance_default"; 50 private static final String KEY_IMPORTANCE_LOW = "importance_low"; 51 private static final String KEY_IMPORTANCE_MIN = "importance_min"; 52 53 List<RadioButtonPreference> mImportances = new ArrayList<>(); 54 55 @Override 56 public int getMetricsCategory() { 57 return MetricsEvent.NOTIFICATION_CHANNEL_IMPORTANCE; 58 } 59 60 @Override 61 public void onResume() { 62 super.onResume(); 63 if (mUid < 0 || TextUtils.isEmpty(mPkg) || mPkgInfo == null || mChannel == null) { 64 Log.w(TAG, "Missing package or uid or packageinfo or channel"); 65 finish(); 66 return; 67 } 68 createPreferenceHierarchy(); 69 } 70 71 @Override 72 void setupBadge() {} 73 74 @Override 75 void updateDependents(boolean banned) {} 76 77 @Override 78 public void onPause() { 79 super.onPause(); 80 } 81 82 private PreferenceScreen createPreferenceHierarchy() { 83 PreferenceScreen root = getPreferenceScreen(); 84 if (root != null) { 85 root.removeAll(); 86 } 87 addPreferencesFromResource(R.xml.notification_importance); 88 root = getPreferenceScreen(); 89 90 for (int i = 0; i < root.getPreferenceCount(); i++) { 91 Preference pref = root.getPreference(i); 92 if (pref instanceof RadioButtonPreference) { 93 RadioButtonPreference radioPref = (RadioButtonPreference) pref; 94 radioPref.setOnClickListener(this); 95 mImportances.add(radioPref); 96 } 97 } 98 99 switch (mChannel.getImportance()) { 100 case IMPORTANCE_MIN: 101 updateRadioButtons(KEY_IMPORTANCE_MIN); 102 break; 103 case IMPORTANCE_LOW: 104 updateRadioButtons(KEY_IMPORTANCE_LOW); 105 break; 106 case IMPORTANCE_DEFAULT: 107 updateRadioButtons(KEY_IMPORTANCE_DEFAULT); 108 break; 109 case IMPORTANCE_HIGH: 110 case IMPORTANCE_MAX: 111 updateRadioButtons(KEY_IMPORTANCE_HIGH); 112 break; 113 } 114 115 return root; 116 } 117 118 private void updateRadioButtons(String selectionKey) { 119 for (RadioButtonPreference pref : mImportances) { 120 if (selectionKey.equals(pref.getKey())) { 121 pref.setChecked(true); 122 } else { 123 pref.setChecked(false); 124 } 125 } 126 } 127 128 @Override 129 public void onRadioButtonClicked(RadioButtonPreference clicked) { 130 int oldImportance = mChannel.getImportance(); 131 switch (clicked.getKey()) { 132 case KEY_IMPORTANCE_HIGH: 133 mChannel.setImportance(IMPORTANCE_HIGH); 134 break; 135 case KEY_IMPORTANCE_DEFAULT: 136 mChannel.setImportance(IMPORTANCE_DEFAULT); 137 break; 138 case KEY_IMPORTANCE_LOW: 139 mChannel.setImportance(IMPORTANCE_LOW); 140 break; 141 case KEY_IMPORTANCE_MIN: 142 mChannel.setImportance(IMPORTANCE_MIN); 143 break; 144 } 145 updateRadioButtons(clicked.getKey()); 146 147 // If you are moving from an importance level without sound to one with sound, 148 // but the sound you had selected was "Silence", 149 // then set sound for this channel to your default sound, 150 // because you probably intended to cause this channel to actually start making sound. 151 if (oldImportance < IMPORTANCE_DEFAULT && !hasValidSound(mChannel) && 152 mChannel.getImportance() >= IMPORTANCE_DEFAULT) { 153 mChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), 154 mChannel.getAudioAttributes()); 155 mChannel.lockFields(USER_LOCKED_SOUND); 156 } 157 mChannel.lockFields(USER_LOCKED_IMPORTANCE); 158 mBackend.updateChannel(mAppRow.pkg, mAppRow.uid, mChannel); 159 } 160 161 // This page exists per notification channel; should not be included 162 // in search 163 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 164 new BaseSearchIndexProvider() { 165 @Override 166 public List<SearchIndexableResource> getXmlResourcesToIndex( 167 Context context, boolean enabled) { 168 return null; 169 } 170 }; 171 } 172