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.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;
34 import com.android.settings.R;
36 public class ZenModeConditionSelection extends RadioGroup {
37 private static final String TAG = "ZenModeConditionSelection";
38 private static final boolean DEBUG = true;
40 private final INotificationManager mNoMan;
41 private final H mHandler = new H();
42 private final Context mContext;
44 public ZenModeConditionSelection(Context 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);
57 private RadioButton newRadioButton(Object tag) {
58 final RadioButton button = new RadioButton(mContext);
60 button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
62 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
64 handleSubscribe((Uri)button.getTag());
73 protected void onAttachedToWindow() {
74 super.onAttachedToWindow();
75 requestZenModeConditions(Condition.FLAG_RELEVANT_NOW);
79 protected void onDetachedFromWindow() {
80 super.onDetachedFromWindow();
81 requestZenModeConditions(0 /*none*/);
84 protected void requestZenModeConditions(int relevance) {
85 if (DEBUG) Log.d(TAG, "requestZenModeConditions " + Condition.relevanceToString(relevance));
87 mNoMan.requestZenModeConditions(mListener, relevance);
88 } catch (RemoteException e) {
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) {
98 v = newRadioButton(c.id);
102 v.setText(c.summary);
103 v.setEnabled(c.state == Condition.STATE_TRUE);
108 protected void handleSubscribe(Uri id) {
109 if (DEBUG) Log.d(TAG, "handleSubscribe " + id);
111 mNoMan.setZenModeCondition(id);
112 } catch (RemoteException e) {
117 private final IConditionListener mListener = new IConditionListener.Stub() {
119 public void onConditionsReceived(Condition[] conditions) {
120 if (conditions == null || conditions.length == 0) return;
121 mHandler.obtainMessage(H.CONDITIONS, conditions).sendToTarget();
125 private final class H extends Handler {
126 private static final int CONDITIONS = 1;
129 public void handleMessage(Message msg) {
130 if (msg.what == CONDITIONS) handleConditions((Condition[])msg.obj);