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
17package com.android.settings.notification;
18
19import android.app.AutomaticZenRule;
20import android.app.NotificationManager;
21import android.content.Context;
22import android.database.ContentObserver;
23import android.net.Uri;
24import android.os.Bundle;
25import android.os.Handler;
26import android.os.UserManager;
27import android.provider.Settings;
28import android.provider.Settings.Global;
29import android.service.notification.ZenModeConfig;
30import android.util.Log;
31
32import com.android.settings.RestrictedSettingsFragment;
33
34import java.util.ArrayList;
35import java.util.List;
36import java.util.Map;
37import java.util.Set;
38
39abstract 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
50    abstract protected void onZenModeChanged();
51    abstract protected void onZenModeConfigChanged();
52
53    public ZenModeSettingsBase() {
54        super(UserManager.DISALLOW_ADJUST_VOLUME);
55    }
56
57    @Override
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
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
83    public void onPause() {
84        super.onPause();
85        mSettingsObserver.unregister();
86    }
87
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
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
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
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
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
134    protected void setZenMode(int zenMode, Uri conditionId) {
135        NotificationManager.from(mContext).setZenMode(zenMode, conditionId, TAG);
136    }
137
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
148        private SettingsObserver() {
149            super(mHandler);
150        }
151
152        public void register() {
153            getContentResolver().registerContentObserver(ZEN_MODE_URI, false, this);
154            getContentResolver().registerContentObserver(ZEN_MODE_CONFIG_ETAG_URI, false, this);
155        }
156
157        public void unregister() {
158            getContentResolver().unregisterContentObserver(this);
159        }
160
161        @Override
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