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