ZenModeControllerImpl.java revision ccb6b9a90f228cc4e31a9442ed28756ff474c080
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.util.Slog; 29 30import com.android.systemui.qs.GlobalSetting; 31 32import java.util.ArrayList; 33import java.util.LinkedHashMap; 34 35/** Platform implementation of the zen mode controller. **/ 36public class ZenModeControllerImpl implements ZenModeController { 37 private static final String TAG = "ZenModeControllerImpl"; 38 39 private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>(); 40 private final Context mContext; 41 private final GlobalSetting mSetting; 42 private final INotificationManager mNoMan; 43 private final LinkedHashMap<Uri, Condition> mConditions = new LinkedHashMap<Uri, Condition>(); 44 45 private boolean mRequesting; 46 47 public ZenModeControllerImpl(Context context, Handler handler) { 48 mContext = context; 49 mSetting = new GlobalSetting(mContext, handler, Global.ZEN_MODE) { 50 @Override 51 protected void handleValueChanged(int value) { 52 fireZenChanged(value != 0); 53 } 54 }; 55 mSetting.setListening(true); 56 mNoMan = INotificationManager.Stub.asInterface( 57 ServiceManager.getService(Context.NOTIFICATION_SERVICE)); 58 } 59 60 @Override 61 public void addCallback(Callback callback) { 62 mCallbacks.add(callback); 63 } 64 65 @Override 66 public void removeCallback(Callback callback) { 67 mCallbacks.remove(callback); 68 } 69 70 @Override 71 public boolean isZen() { 72 return mSetting.getValue() != 0; 73 } 74 75 @Override 76 public void setZen(boolean zen) { 77 mSetting.setValue(zen ? 1 : 0); 78 } 79 80 @Override 81 public void requestConditions(boolean request) { 82 mRequesting = request; 83 try { 84 mNoMan.requestZenModeConditions(mListener, request ? Condition.FLAG_RELEVANT_NOW : 0); 85 } catch (RemoteException e) { 86 // noop 87 } 88 if (!mRequesting) { 89 mConditions.clear(); 90 } 91 } 92 93 @Override 94 public void select(Condition condition) { 95 try { 96 mNoMan.setZenModeCondition(condition == null ? null : condition.id); 97 } catch (RemoteException e) { 98 // noop 99 } 100 } 101 102 private void fireZenChanged(boolean zen) { 103 for (Callback cb : mCallbacks) { 104 cb.onZenChanged(zen); 105 } 106 } 107 108 private void fireConditionsChanged(Condition[] conditions) { 109 for (Callback cb : mCallbacks) { 110 cb.onConditionsChanged(conditions); 111 } 112 } 113 114 private void updateConditions(Condition[] conditions) { 115 if (conditions == null || conditions.length == 0) return; 116 for (Condition c : conditions) { 117 if ((c.flags & Condition.FLAG_RELEVANT_NOW) == 0) continue; 118 mConditions.put(c.id, c); 119 } 120 fireConditionsChanged( 121 mConditions.values().toArray(new Condition[mConditions.values().size()])); 122 } 123 124 private final IConditionListener mListener = new IConditionListener.Stub() { 125 @Override 126 public void onConditionsReceived(Condition[] conditions) { 127 Slog.d(TAG, "onConditionsReceived " + (conditions == null ? 0 : conditions.length) 128 + " mRequesting=" + mRequesting); 129 if (!mRequesting) return; 130 updateConditions(conditions); 131 } 132 }; 133} 134