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 android.app.NotificationManager.Policy; 20 import android.content.Context; 21 import android.provider.Settings; 22 import android.support.v14.preference.SwitchPreference; 23 import android.support.v7.preference.Preference; 24 import android.util.Log; 25 26 import com.android.settingslib.core.lifecycle.Lifecycle; 27 28 public class ZenModeMediaPreferenceController extends AbstractZenModePreferenceController 29 implements Preference.OnPreferenceChangeListener { 30 31 protected static final String KEY = "zen_mode_media"; 32 private final ZenModeBackend mBackend; 33 ZenModeMediaPreferenceController(Context context, Lifecycle lifecycle)34 public ZenModeMediaPreferenceController(Context context, Lifecycle lifecycle) { 35 super(context, KEY, lifecycle); 36 mBackend = ZenModeBackend.getInstance(context); 37 } 38 39 @Override getPreferenceKey()40 public String getPreferenceKey() { 41 return KEY; 42 } 43 44 @Override isAvailable()45 public boolean isAvailable() { 46 return true; 47 } 48 49 @Override updateState(Preference preference)50 public void updateState(Preference preference) { 51 super.updateState(preference); 52 53 SwitchPreference pref = (SwitchPreference) preference; 54 switch (getZenMode()) { 55 case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS: 56 pref.setEnabled(false); 57 pref.setChecked(false); 58 break; 59 case Settings.Global.ZEN_MODE_ALARMS: 60 pref.setEnabled(false); 61 pref.setChecked(true); 62 break; 63 default: 64 pref.setEnabled(true); 65 pref.setChecked(mBackend.isPriorityCategoryEnabled( 66 Policy.PRIORITY_CATEGORY_MEDIA)); 67 } 68 } 69 70 @Override onPreferenceChange(Preference preference, Object newValue)71 public boolean onPreferenceChange(Preference preference, Object newValue) { 72 final boolean allowMedia = (Boolean) newValue; 73 if (ZenModeSettingsBase.DEBUG) { 74 Log.d(TAG, "onPrefChange allowMedia=" + allowMedia); 75 } 76 mBackend.saveSoundPolicy(Policy.PRIORITY_CATEGORY_MEDIA, allowMedia); 77 return true; 78 } 79 } 80