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