OSDN Git Service

Adding zen mode condition dialog
[android-x86/packages-apps-Settings.git] / src / com / android / settings / notification / ZenModeConditionSelection.java
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
17 package com.android.settings.notification;
18
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;
33
34 import com.android.settings.R;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39 public class ZenModeConditionSelection extends RadioGroup {
40     private static final String TAG = "ZenModeConditionSelection";
41     private static final boolean DEBUG = true;
42
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;
48
49     public ZenModeConditionSelection(Context context) {
50         super(context);
51         mContext = 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));
60         b.setChecked(true);
61         for (int i = ZenModeConfig.MINUTE_BUCKETS.length - 1; i >= 0; --i) {
62             handleCondition(ZenModeConfig.toTimeCondition(ZenModeConfig.MINUTE_BUCKETS[i]));
63         }
64     }
65
66     private RadioButton newRadioButton(Condition condition) {
67         final RadioButton button = new RadioButton(mContext);
68         button.setTag(condition);
69         button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
70             @Override
71             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
72                 if (isChecked) {
73                     setCondition((Condition) button.getTag());
74                 }
75             }
76         });
77         addView(button);
78         return button;
79     }
80
81     @Override
82     protected void onAttachedToWindow() {
83         super.onAttachedToWindow();
84         requestZenModeConditions(Condition.FLAG_RELEVANT_NOW);
85     }
86
87     @Override
88     protected void onDetachedFromWindow() {
89         super.onDetachedFromWindow();
90         requestZenModeConditions(0 /*none*/);
91     }
92
93     protected void requestZenModeConditions(int relevance) {
94         if (DEBUG) Log.d(TAG, "requestZenModeConditions " + Condition.relevanceToString(relevance));
95         try {
96             mNoMan.requestZenModeConditions(mListener, relevance);
97         } catch (RemoteException e) {
98             // noop
99         }
100     }
101
102     protected void handleConditions(Condition[] conditions) {
103         for (Condition c : conditions) {
104             handleCondition(c);
105         }
106     }
107
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) {
112             if (v == null) {
113                 v = newRadioButton(c);
114             }
115         }
116         if (v != null) {
117             v.setText(c.summary);
118             v.setEnabled(c.state == Condition.STATE_TRUE);
119         }
120         mConditions.add(c);
121     }
122
123     protected void setCondition(Condition c) {
124         if (DEBUG) Log.d(TAG, "setCondition " + c);
125         mCondition = c;
126     }
127
128     public void confirmCondition() {
129         if (DEBUG) Log.d(TAG, "confirmCondition " + mCondition);
130         try {
131             mNoMan.setZenModeCondition(mCondition);
132         } catch (RemoteException e) {
133             // noop
134         }
135     }
136
137     private final IConditionListener mListener = new IConditionListener.Stub() {
138         @Override
139         public void onConditionsReceived(Condition[] conditions) {
140             if (conditions == null || conditions.length == 0) return;
141             mHandler.obtainMessage(H.CONDITIONS, conditions).sendToTarget();
142         }
143     };
144
145     private final class H extends Handler {
146         private static final int CONDITIONS = 1;
147
148         @Override
149         public void handleMessage(Message msg) {
150             if (msg.what == CONDITIONS) handleConditions((Condition[]) msg.obj);
151         }
152     }
153 }