ZenModeControllerImpl.java revision 856edebad73560e9b1cce021a7de9a0470d07176
1/*
2 * Copyright (C) 2014 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.systemui.statusbar.policy;
18
19import android.app.INotificationManager;
20import android.content.Context;
21import android.net.Uri;
22import android.os.Handler;
23import android.os.RemoteException;
24import android.os.ServiceManager;
25import android.provider.Settings.Global;
26import android.service.notification.Condition;
27import android.service.notification.IConditionListener;
28import android.service.notification.ZenModeConfig;
29import android.util.Slog;
30
31import com.android.systemui.qs.GlobalSetting;
32
33import java.util.ArrayList;
34import java.util.LinkedHashMap;
35
36/** Platform implementation of the zen mode controller. **/
37public class ZenModeControllerImpl implements ZenModeController {
38    private static final String TAG = "ZenModeControllerImpl";
39    private static final boolean DEBUG = false;
40
41    private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
42    private final Context mContext;
43    private final GlobalSetting mModeSetting;
44    private final GlobalSetting mConfigSetting;
45    private final INotificationManager mNoMan;
46    private final LinkedHashMap<Uri, Condition> mConditions = new LinkedHashMap<Uri, Condition>();
47
48    private boolean mRequesting;
49
50    public ZenModeControllerImpl(Context context, Handler handler) {
51        mContext = context;
52        mModeSetting = new GlobalSetting(mContext, handler, Global.ZEN_MODE) {
53            @Override
54            protected void handleValueChanged(int value) {
55                fireZenChanged(value != 0);
56            }
57        };
58        mConfigSetting = new GlobalSetting(mContext, handler, Global.ZEN_MODE_CONFIG_ETAG) {
59            @Override
60            protected void handleValueChanged(int value) {
61                fireExitConditionChanged();
62            }
63        };
64        mModeSetting.setListening(true);
65        mConfigSetting.setListening(true);
66        mNoMan = INotificationManager.Stub.asInterface(
67                ServiceManager.getService(Context.NOTIFICATION_SERVICE));
68    }
69
70    @Override
71    public void addCallback(Callback callback) {
72        mCallbacks.add(callback);
73    }
74
75    @Override
76    public void removeCallback(Callback callback) {
77        mCallbacks.remove(callback);
78    }
79
80    @Override
81    public boolean isZen() {
82        return mModeSetting.getValue() != 0;
83    }
84
85    @Override
86    public void setZen(boolean zen) {
87        mModeSetting.setValue(zen ? 1 : 0);
88    }
89
90    @Override
91    public void requestConditions(boolean request) {
92        mRequesting = request;
93        try {
94            mNoMan.requestZenModeConditions(mListener, request ? Condition.FLAG_RELEVANT_NOW : 0);
95        } catch (RemoteException e) {
96            // noop
97        }
98        if (!mRequesting) {
99            mConditions.clear();
100        }
101    }
102
103    @Override
104    public void setExitConditionId(Uri exitConditionId) {
105        try {
106            mNoMan.setZenModeCondition(exitConditionId);
107        } catch (RemoteException e) {
108            // noop
109        }
110    }
111
112    @Override
113    public Uri getExitConditionId() {
114        try {
115            final ZenModeConfig config = mNoMan.getZenModeConfig();
116            if (config != null) {
117                return config.exitConditionId;
118            }
119        } catch (RemoteException e) {
120            // noop
121        }
122        return null;
123    }
124
125    private void fireZenChanged(boolean zen) {
126        for (Callback cb : mCallbacks) {
127            cb.onZenChanged(zen);
128        }
129    }
130
131    private void fireConditionsChanged(Condition[] conditions) {
132        for (Callback cb : mCallbacks) {
133            cb.onConditionsChanged(conditions);
134        }
135    }
136
137    private void fireExitConditionChanged() {
138        final Uri exitConditionId = getExitConditionId();
139        if (DEBUG) Slog.d(TAG, "exitConditionId changed: " + exitConditionId);
140        for (Callback cb : mCallbacks) {
141            cb.onExitConditionChanged(exitConditionId);
142        }
143    }
144
145    private void updateConditions(Condition[] conditions) {
146        if (conditions == null || conditions.length == 0) return;
147        for (Condition c : conditions) {
148            if ((c.flags & Condition.FLAG_RELEVANT_NOW) == 0) continue;
149            mConditions.put(c.id, c);
150        }
151        fireConditionsChanged(
152                mConditions.values().toArray(new Condition[mConditions.values().size()]));
153    }
154
155    private final IConditionListener mListener = new IConditionListener.Stub() {
156        @Override
157        public void onConditionsReceived(Condition[] conditions) {
158            if (DEBUG) Slog.d(TAG, "onConditionsReceived "
159                    + (conditions == null ? 0 : conditions.length) + " mRequesting=" + mRequesting);
160            if (!mRequesting) return;
161            updateConditions(conditions);
162        }
163    };
164}
165