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