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_DEFAULT; 20 import static android.app.NotificationManager.IMPORTANCE_LOW; 21 import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED; 22 23 import android.app.NotificationChannel; 24 import android.content.Context; 25 import android.util.Log; 26 27 import androidx.preference.Preference; 28 29 import com.android.settings.core.PreferenceControllerMixin; 30 import com.android.settingslib.RestrictedSwitchPreference; 31 32 public class AllowSoundPreferenceController extends NotificationPreferenceController 33 implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener { 34 35 private static final String TAG = "AllowSoundPrefContr"; 36 private static final String KEY_IMPORTANCE = "allow_sound"; 37 private NotificationSettingsBase.ImportanceListener mImportanceListener; 38 AllowSoundPreferenceController(Context context, NotificationSettingsBase.ImportanceListener importanceListener, NotificationBackend backend)39 public AllowSoundPreferenceController(Context context, 40 NotificationSettingsBase.ImportanceListener importanceListener, 41 NotificationBackend backend) { 42 super(context, backend); 43 mImportanceListener = importanceListener; 44 } 45 46 @Override getPreferenceKey()47 public String getPreferenceKey() { 48 return KEY_IMPORTANCE; 49 } 50 51 @Override isAvailable()52 public boolean isAvailable() { 53 if (!super.isAvailable()) { 54 return false; 55 } 56 return mChannel != null && NotificationChannel.DEFAULT_CHANNEL_ID.equals(mChannel.getId()); 57 58 } 59 60 @Override updateState(Preference preference)61 public void updateState(Preference preference) { 62 if (mChannel != null) { 63 RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference; 64 pref.setDisabledByAdmin(mAdmin); 65 pref.setEnabled(!pref.isDisabledByAdmin()); 66 pref.setChecked(mChannel.getImportance() >= IMPORTANCE_DEFAULT 67 || mChannel.getImportance() == IMPORTANCE_UNSPECIFIED); 68 } else { Log.i(TAG, "tried to updatestate on a null channel?!"); } 69 } 70 71 @Override onPreferenceChange(Preference preference, Object newValue)72 public boolean onPreferenceChange(Preference preference, Object newValue) { 73 if (mChannel != null) { 74 final int importance = 75 ((Boolean) newValue ? IMPORTANCE_UNSPECIFIED : IMPORTANCE_LOW); 76 mChannel.setImportance(importance); 77 mChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); 78 saveChannel(); 79 mImportanceListener.onImportanceChanged(); 80 } 81 return true; 82 } 83 } 84