2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.notification;
19 import android.animation.LayoutTransition;
20 import android.app.INotificationManager;
21 import android.content.Context;
22 import android.os.Handler;
23 import android.os.Message;
24 import android.os.RemoteException;
25 import android.os.ServiceManager;
26 import android.service.notification.Condition;
27 import android.service.notification.IConditionListener;
28 import android.service.notification.ZenModeConfig;
29 import android.util.Log;
30 import android.widget.CompoundButton;
31 import android.widget.RadioButton;
32 import android.widget.RadioGroup;
34 import com.android.settings.R;
36 import java.util.ArrayList;
37 import java.util.List;
39 public class ZenModeConditionSelection extends RadioGroup {
40 private static final String TAG = "ZenModeConditionSelection";
41 private static final boolean DEBUG = true;
43 private final INotificationManager mNoMan;
44 private final H mHandler = new H();
45 private final Context mContext;
46 private final List<Condition> mConditions;
47 private Condition mCondition;
49 public ZenModeConditionSelection(Context context) {
52 mConditions = new ArrayList<Condition>();
53 setLayoutTransition(new LayoutTransition());
54 final int p = mContext.getResources().getDimensionPixelSize(R.dimen.content_margin_left);
55 setPadding(p, p, p, 0);
56 mNoMan = INotificationManager.Stub.asInterface(
57 ServiceManager.getService(Context.NOTIFICATION_SERVICE));
58 final RadioButton b = newRadioButton(null);
59 b.setText(mContext.getString(com.android.internal.R.string.zen_mode_forever));
61 for (int i = ZenModeConfig.MINUTE_BUCKETS.length - 1; i >= 0; --i) {
62 handleCondition(ZenModeConfig.toTimeCondition(ZenModeConfig.MINUTE_BUCKETS[i]));
66 private RadioButton newRadioButton(Condition condition) {
67 final RadioButton button = new RadioButton(mContext);
68 button.setTag(condition);
69 button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
71 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
73 setCondition((Condition) button.getTag());
82 protected void onAttachedToWindow() {
83 super.onAttachedToWindow();
84 requestZenModeConditions(Condition.FLAG_RELEVANT_NOW);
88 protected void onDetachedFromWindow() {
89 super.onDetachedFromWindow();
90 requestZenModeConditions(0 /*none*/);
93 protected void requestZenModeConditions(int relevance) {
94 if (DEBUG) Log.d(TAG, "requestZenModeConditions " + Condition.relevanceToString(relevance));
96 mNoMan.requestZenModeConditions(mListener, relevance);
97 } catch (RemoteException e) {
102 protected void handleConditions(Condition[] conditions) {
103 for (Condition c : conditions) {
108 protected void handleCondition(Condition c) {
109 if (mConditions.contains(c)) return;
110 RadioButton v = (RadioButton) findViewWithTag(c.id);
111 if (c.state == Condition.STATE_TRUE || c.state == Condition.STATE_UNKNOWN) {
113 v = newRadioButton(c);
117 v.setText(c.summary);
118 v.setEnabled(c.state == Condition.STATE_TRUE);
123 protected void setCondition(Condition c) {
124 if (DEBUG) Log.d(TAG, "setCondition " + c);
128 public void confirmCondition() {
129 if (DEBUG) Log.d(TAG, "confirmCondition " + mCondition);
131 mNoMan.setZenModeCondition(mCondition);
132 } catch (RemoteException e) {
137 private final IConditionListener mListener = new IConditionListener.Stub() {
139 public void onConditionsReceived(Condition[] conditions) {
140 if (conditions == null || conditions.length == 0) return;
141 mHandler.obtainMessage(H.CONDITIONS, conditions).sendToTarget();
145 private final class H extends Handler {
146 private static final int CONDITIONS = 1;
149 public void handleMessage(Message msg) {
150 if (msg.what == CONDITIONS) handleConditions((Condition[]) msg.obj);