OSDN Git Service

am 3ba14771: (-s ours) am bd042089: DO NOT MERGE Fix build for notification listener...
[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.net.Uri;
23 import android.os.Handler;
24 import android.os.Message;
25 import android.os.RemoteException;
26 import android.os.ServiceManager;
27 import android.service.notification.Condition;
28 import android.service.notification.IConditionListener;
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 public class ZenModeConditionSelection extends RadioGroup {
37     private static final String TAG = "ZenModeConditionSelection";
38     private static final boolean DEBUG = true;
39
40     private final INotificationManager mNoMan;
41     private final H mHandler = new H();
42     private final Context mContext;
43
44     public ZenModeConditionSelection(Context context) {
45         super(context);
46         mContext = context;
47         setLayoutTransition(new LayoutTransition());
48         final int p = mContext.getResources().getDimensionPixelSize(R.dimen.content_margin_left);
49         setPadding(p, p, p, 0);
50         mNoMan = INotificationManager.Stub.asInterface(
51                 ServiceManager.getService(Context.NOTIFICATION_SERVICE));
52         final RadioButton b = newRadioButton(null);
53         b.setText(R.string.zen_mode_default_option);
54         b.setChecked(true);
55     }
56
57     private RadioButton newRadioButton(Object tag) {
58         final RadioButton button = new RadioButton(mContext);
59         button.setTag(tag);
60         button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
61             @Override
62             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
63                 if (isChecked) {
64                     handleSubscribe((Uri)button.getTag());
65                 }
66             }
67         });
68         addView(button);
69         return button;
70     }
71
72     @Override
73     protected void onAttachedToWindow() {
74         super.onAttachedToWindow();
75         requestZenModeConditions(Condition.FLAG_RELEVANT_NOW);
76     }
77
78     @Override
79     protected void onDetachedFromWindow() {
80         super.onDetachedFromWindow();
81         requestZenModeConditions(0 /*none*/);
82     }
83
84     protected void requestZenModeConditions(int relevance) {
85         if (DEBUG) Log.d(TAG, "requestZenModeConditions " + Condition.relevanceToString(relevance));
86         try {
87             mNoMan.requestZenModeConditions(mListener, relevance);
88         } catch (RemoteException e) {
89             // noop
90         }
91     }
92
93     protected void handleConditions(Condition[] conditions) {
94         for (final Condition c : conditions) {
95             RadioButton v = (RadioButton) findViewWithTag(c.id);
96             if (c.state == Condition.STATE_TRUE || c.state == Condition.STATE_UNKNOWN) {
97                 if (v == null) {
98                     v = newRadioButton(c.id);
99                 }
100             }
101             if (v != null) {
102                 v.setText(c.summary);
103                 v.setEnabled(c.state == Condition.STATE_TRUE);
104             }
105         }
106     }
107
108     protected void handleSubscribe(Uri id) {
109         if (DEBUG) Log.d(TAG, "handleSubscribe " + id);
110         try {
111             mNoMan.setZenModeCondition(id);
112         } catch (RemoteException e) {
113             // noop
114         }
115     }
116
117     private final IConditionListener mListener = new IConditionListener.Stub() {
118         @Override
119         public void onConditionsReceived(Condition[] conditions) {
120             if (conditions == null || conditions.length == 0) return;
121             mHandler.obtainMessage(H.CONDITIONS, conditions).sendToTarget();
122         }
123     };
124
125     private final class H extends Handler {
126         private static final int CONDITIONS = 1;
127
128         @Override
129         public void handleMessage(Message msg) {
130             if (msg.what == CONDITIONS) handleConditions((Condition[])msg.obj);
131         }
132     }
133 }