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 
17 package com.android.settings.notification;
18 
19 import android.app.AutomaticZenRule;
20 import android.app.NotificationManager;
21 import android.content.Context;
22 import android.database.ContentObserver;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.os.Handler;
26 import android.os.UserManager;
27 import android.provider.Settings;
28 import android.provider.Settings.Global;
29 import android.service.notification.ZenModeConfig;
30 import android.util.Log;
31 
32 import com.android.settings.RestrictedSettingsFragment;
33 
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38 
39 abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment {
40     protected static final String TAG = "ZenModeSettings";
41     protected static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
42 
43     private final Handler mHandler = new Handler();
44     private final SettingsObserver mSettingsObserver = new SettingsObserver();
45 
46     protected Context mContext;
47     protected Set<Map.Entry<String, AutomaticZenRule>> mRules;
48     protected int mZenMode;
49 
onZenModeChanged()50     abstract protected void onZenModeChanged();
onZenModeConfigChanged()51     abstract protected void onZenModeConfigChanged();
52 
ZenModeSettingsBase()53     public ZenModeSettingsBase() {
54         super(UserManager.DISALLOW_ADJUST_VOLUME);
55     }
56 
57     @Override
onCreate(Bundle icicle)58     public void onCreate(Bundle icicle) {
59         super.onCreate(icicle);
60         mContext = getActivity();
61         updateZenMode(false /*fireChanged*/);
62         maybeRefreshRules(true, false /*fireChanged*/);
63         if (DEBUG) Log.d(TAG, "Loaded mRules=" + mRules);
64     }
65 
66     @Override
onResume()67     public void onResume() {
68         super.onResume();
69         updateZenMode(true /*fireChanged*/);
70         maybeRefreshRules(true, true /*fireChanged*/);
71         mSettingsObserver.register();
72         if (isUiRestricted()) {
73             if (isUiRestrictedByOnlyAdmin()) {
74                 getPreferenceScreen().removeAll();
75                 return;
76             } else {
77                 finish();
78             }
79         }
80     }
81 
82     @Override
onPause()83     public void onPause() {
84         super.onPause();
85         mSettingsObserver.unregister();
86     }
87 
updateZenMode(boolean fireChanged)88     private void updateZenMode(boolean fireChanged) {
89         final int zenMode = Settings.Global.getInt(getContentResolver(), Global.ZEN_MODE, mZenMode);
90         if (zenMode == mZenMode) return;
91         mZenMode = zenMode;
92         if (DEBUG) Log.d(TAG, "updateZenMode mZenMode=" + mZenMode);
93         if (fireChanged) {
94             onZenModeChanged();
95         }
96     }
97 
addZenRule(AutomaticZenRule rule)98     protected String addZenRule(AutomaticZenRule rule) {
99         try {
100             String id = NotificationManager.from(mContext).addAutomaticZenRule(rule);
101             final AutomaticZenRule savedRule =
102                     NotificationManager.from(mContext).getAutomaticZenRule(id);
103             maybeRefreshRules(savedRule != null, true);
104             return id;
105         } catch (Exception e) {
106             return null;
107         }
108     }
109 
setZenRule(String id, AutomaticZenRule rule)110     protected boolean setZenRule(String id, AutomaticZenRule rule) {
111         final boolean success =
112                 NotificationManager.from(mContext).updateAutomaticZenRule(id, rule);
113         maybeRefreshRules(success, true);
114         return success;
115     }
116 
removeZenRule(String id)117     protected boolean removeZenRule(String id) {
118         final boolean success =
119                 NotificationManager.from(mContext).removeAutomaticZenRule(id);
120         maybeRefreshRules(success, true);
121         return success;
122     }
123 
maybeRefreshRules(boolean success, boolean fireChanged)124     protected void maybeRefreshRules(boolean success, boolean fireChanged) {
125         if (success) {
126             mRules = getZenModeRules();
127             if (DEBUG) Log.d(TAG, "Refreshed mRules=" + mRules);
128             if (fireChanged) {
129                 onZenModeConfigChanged();
130             }
131         }
132     }
133 
setZenMode(int zenMode, Uri conditionId)134     protected void setZenMode(int zenMode, Uri conditionId) {
135         NotificationManager.from(mContext).setZenMode(zenMode, conditionId, TAG);
136     }
137 
getZenModeRules()138     private Set<Map.Entry<String, AutomaticZenRule>> getZenModeRules() {
139         Map<String, AutomaticZenRule> ruleMap
140                 = NotificationManager.from(mContext).getAutomaticZenRules();
141         return ruleMap.entrySet();
142     }
143 
144     private final class SettingsObserver extends ContentObserver {
145         private final Uri ZEN_MODE_URI = Global.getUriFor(Global.ZEN_MODE);
146         private final Uri ZEN_MODE_CONFIG_ETAG_URI = Global.getUriFor(Global.ZEN_MODE_CONFIG_ETAG);
147 
SettingsObserver()148         private SettingsObserver() {
149             super(mHandler);
150         }
151 
register()152         public void register() {
153             getContentResolver().registerContentObserver(ZEN_MODE_URI, false, this);
154             getContentResolver().registerContentObserver(ZEN_MODE_CONFIG_ETAG_URI, false, this);
155         }
156 
unregister()157         public void unregister() {
158             getContentResolver().unregisterContentObserver(this);
159         }
160 
161         @Override
onChange(boolean selfChange, Uri uri)162         public void onChange(boolean selfChange, Uri uri) {
163             super.onChange(selfChange, uri);
164             if (ZEN_MODE_URI.equals(uri)) {
165                 updateZenMode(true /*fireChanged*/);
166             }
167             if (ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) {
168                 maybeRefreshRules(true, true /*fireChanged*/);
169             }
170         }
171     }
172 }
173