• 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.NotificationManager;
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.graphics.drawable.Drawable;
24 import android.os.PersistableBundle;
25 import android.provider.Settings;
26 import android.provider.Settings.Global;
27 import android.service.notification.ZenModeConfig;
28 import android.support.annotation.VisibleForTesting;
29 
30 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
31 import com.android.settings.R;
32 import com.android.settings.core.SubSettingLauncher;
33 import com.android.settings.notification.ZenModeSettings;
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     @VisibleForTesting
46     protected ZenModeConfig mConfig;
47 
48     private int mZen;
49     private final Receiver mReceiver;
50 
DndCondition(ConditionManager manager)51     public DndCondition(ConditionManager manager) {
52         super(manager);
53         mReceiver = new Receiver();
54         mManager.getContext().registerReceiver(mReceiver, DND_FILTER);
55         mRegistered = true;
56     }
57 
58     @Override
refreshState()59     public void refreshState() {
60         NotificationManager notificationManager =
61                 mManager.getContext().getSystemService(NotificationManager.class);
62         mZen = notificationManager.getZenMode();
63         boolean zenModeEnabled = mZen != Settings.Global.ZEN_MODE_OFF;
64         if (zenModeEnabled) {
65             mConfig = notificationManager.getZenModeConfig();
66         } else {
67             mConfig = null;
68         }
69         setActive(zenModeEnabled);
70     }
71 
72     @Override
saveState(PersistableBundle bundle)73     boolean saveState(PersistableBundle bundle) {
74         bundle.putInt(KEY_STATE, mZen);
75         return super.saveState(bundle);
76     }
77 
78     @Override
restoreState(PersistableBundle bundle)79     void restoreState(PersistableBundle bundle) {
80         super.restoreState(bundle);
81         mZen = bundle.getInt(KEY_STATE, Global.ZEN_MODE_OFF);
82     }
83 
84     @Override
getIcon()85     public Drawable getIcon() {
86         return mManager.getContext().getDrawable(R.drawable.ic_do_not_disturb_on_24dp);
87     }
88 
89     @Override
getTitle()90     public CharSequence getTitle() {
91         return mManager.getContext().getString(R.string.condition_zen_title);
92     }
93 
94     @Override
getSummary()95     public CharSequence getSummary() {
96         return ZenModeConfig.getDescription(mManager.getContext(), mZen != Global.ZEN_MODE_OFF,
97                 mConfig, true);
98     }
99 
100     @Override
getActions()101     public CharSequence[] getActions() {
102         return new CharSequence[] { mManager.getContext().getString(R.string.condition_turn_off) };
103     }
104 
105     @Override
onPrimaryClick()106     public void onPrimaryClick() {
107         new SubSettingLauncher(mManager.getContext())
108                 .setDestination(ZenModeSettings.class.getName())
109                 .setSourceMetricsCategory(MetricsEvent.DASHBOARD_SUMMARY)
110                 .setTitle(R.string.zen_mode_settings_title)
111                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
112                 .launch();
113     }
114 
115     @Override
onActionClick(int index)116     public void onActionClick(int index) {
117         if (index == 0) {
118             NotificationManager notificationManager = mManager.getContext().getSystemService(
119                     NotificationManager.class);
120             notificationManager.setZenMode(Settings.Global.ZEN_MODE_OFF, null, TAG);
121             setActive(false);
122         } else {
123             throw new IllegalArgumentException("Unexpected index " + index);
124         }
125     }
126 
127     @Override
getMetricsConstant()128     public int getMetricsConstant() {
129         return MetricsEvent.SETTINGS_CONDITION_DND;
130     }
131 
132     public static class Receiver extends BroadcastReceiver {
133         @Override
onReceive(Context context, Intent intent)134         public void onReceive(Context context, Intent intent) {
135             if (NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL
136                     .equals(intent.getAction())) {
137                 final Condition condition =
138                         ConditionManager.get(context).getCondition(DndCondition.class);
139                 if (condition != null) {
140                     condition.refreshState();
141                 }
142             }
143         }
144     }
145 
146     @Override
onResume()147     public void onResume() {
148         if (!mRegistered) {
149            mManager.getContext().registerReceiver(mReceiver, DND_FILTER);
150            mRegistered = true;
151         }
152     }
153 
154     @Override
onPause()155     public void onPause() {
156         if (mRegistered) {
157             mManager.getContext().unregisterReceiver(mReceiver);
158             mRegistered = false;
159         }
160     }
161 }
162