1 /* 2 * Copyright (C) 2015 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 package com.android.settings.dashboard.conditional; 17 18 import android.app.ActivityManager; 19 import android.app.NotificationManager; 20 import android.app.StatusBarManager; 21 import android.content.BroadcastReceiver; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.graphics.drawable.Icon; 25 import android.os.PersistableBundle; 26 import android.provider.Settings; 27 import android.provider.Settings.Global; 28 import android.service.notification.ZenModeConfig; 29 import com.android.internal.logging.MetricsProto.MetricsEvent; 30 import com.android.settings.R; 31 32 public class DndCondition extends Condition { 33 34 private static final String TAG = "DndCondition"; 35 private static final String KEY_STATE = "state"; 36 37 private int mZen; 38 private ZenModeConfig mConfig; 39 DndCondition(ConditionManager manager)40 public DndCondition(ConditionManager manager) { 41 super(manager); 42 } 43 44 @Override refreshState()45 public void refreshState() { 46 NotificationManager notificationManager = 47 mManager.getContext().getSystemService(NotificationManager.class); 48 mZen = notificationManager.getZenMode(); 49 boolean zenModeEnabled = mZen != Settings.Global.ZEN_MODE_OFF; 50 if (zenModeEnabled) { 51 mConfig = notificationManager.getZenModeConfig(); 52 } else { 53 mConfig = null; 54 } 55 setActive(zenModeEnabled); 56 } 57 58 @Override saveState(PersistableBundle bundle)59 boolean saveState(PersistableBundle bundle) { 60 bundle.putInt(KEY_STATE, mZen); 61 return super.saveState(bundle); 62 } 63 64 @Override restoreState(PersistableBundle bundle)65 void restoreState(PersistableBundle bundle) { 66 super.restoreState(bundle); 67 mZen = bundle.getInt(KEY_STATE, Global.ZEN_MODE_OFF); 68 } 69 70 @Override getReceiverClass()71 protected Class<?> getReceiverClass() { 72 return Receiver.class; 73 } 74 getZenState()75 private CharSequence getZenState() { 76 switch (mZen) { 77 case Settings.Global.ZEN_MODE_ALARMS: 78 return mManager.getContext().getString(R.string.zen_mode_option_alarms); 79 case Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: 80 return mManager.getContext().getString( 81 R.string.zen_mode_option_important_interruptions); 82 case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS: 83 return mManager.getContext().getString(R.string.zen_mode_option_no_interruptions); 84 } 85 return null; 86 } 87 88 @Override getIcon()89 public Icon getIcon() { 90 return Icon.createWithResource(mManager.getContext(), R.drawable.ic_zen); 91 } 92 93 @Override getTitle()94 public CharSequence getTitle() { 95 return mManager.getContext().getString(R.string.condition_zen_title, getZenState()); 96 } 97 98 @Override getSummary()99 public CharSequence getSummary() { 100 final boolean isForever = mConfig != null && mConfig.manualRule != null 101 && mConfig.manualRule.conditionId == null; 102 return isForever ? mManager.getContext().getString(com.android.internal.R.string.zen_mode_forever_dnd) 103 : ZenModeConfig.getConditionSummary(mManager.getContext(), mConfig, 104 ActivityManager.getCurrentUser(), 105 false); 106 } 107 108 @Override getActions()109 public CharSequence[] getActions() { 110 return new CharSequence[] { mManager.getContext().getString(R.string.condition_turn_off) }; 111 } 112 113 @Override onPrimaryClick()114 public void onPrimaryClick() { 115 StatusBarManager statusBar = mManager.getContext().getSystemService(StatusBarManager.class); 116 statusBar.expandSettingsPanel("dnd"); 117 } 118 119 @Override onActionClick(int index)120 public void onActionClick(int index) { 121 if (index == 0) { 122 NotificationManager notificationManager = mManager.getContext().getSystemService( 123 NotificationManager.class); 124 notificationManager.setZenMode(Settings.Global.ZEN_MODE_OFF, null, TAG); 125 setActive(false); 126 } else { 127 throw new IllegalArgumentException("Unexpected index " + index); 128 } 129 } 130 131 @Override getMetricsConstant()132 public int getMetricsConstant() { 133 return MetricsEvent.SETTINGS_CONDITION_DND; 134 } 135 136 public static class Receiver extends BroadcastReceiver { 137 @Override onReceive(Context context, Intent intent)138 public void onReceive(Context context, Intent intent) { 139 if (NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL 140 .equals(intent.getAction())) { 141 ConditionManager.get(context).getCondition(DndCondition.class) 142 .refreshState(); 143 } 144 } 145 } 146 } 147