1623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock/*
2623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock * Copyright (C) 2014 The Android Open Source Project
3623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock *
4623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock * Licensed under the Apache License, Version 2.0 (the "License");
5623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock * you may not use this file except in compliance with the License.
6623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock * You may obtain a copy of the License at
7623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock *
8623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock *      http://www.apache.org/licenses/LICENSE-2.0
9623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock *
10623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock * Unless required by applicable law or agreed to in writing, software
11623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock * distributed under the License is distributed on an "AS IS" BASIS,
12623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock * See the License for the specific language governing permissions and
14623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock * limitations under the License.
15623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock */
16623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock
17623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlockpackage com.android.settings.notification;
18623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock
19623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlockimport android.animation.LayoutTransition;
20623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlockimport android.app.INotificationManager;
21623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlockimport android.content.Context;
22623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlockimport android.os.Handler;
23623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlockimport android.os.Message;
24623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlockimport android.os.RemoteException;
25623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlockimport android.os.ServiceManager;
26334bd70fc7e61567443cbf25b12bbf18aa5b636dSelim Cinekimport android.os.UserHandle;
27623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlockimport android.service.notification.Condition;
28623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlockimport android.service.notification.IConditionListener;
29062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monkimport android.service.notification.ZenModeConfig;
30beff087b3ac3aae4eb120feef81274d7caeafb58John Spurlockimport android.text.TextUtils;
31623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlockimport android.util.Log;
32623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlockimport android.widget.CompoundButton;
33623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlockimport android.widget.RadioButton;
34623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlockimport android.widget.RadioGroup;
35623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock
36623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlockimport com.android.settings.R;
37623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock
38062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monkimport java.util.ArrayList;
39062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monkimport java.util.List;
40062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk
41623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlockpublic class ZenModeConditionSelection extends RadioGroup {
42623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    private static final String TAG = "ZenModeConditionSelection";
43623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    private static final boolean DEBUG = true;
44623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock
45623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    private final INotificationManager mNoMan;
46623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    private final H mHandler = new H();
47623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    private final Context mContext;
48062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk    private final List<Condition> mConditions;
4945fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock    private final int mZenMode;
5045fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock
51062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk    private Condition mCondition;
52623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock
5345fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock    public ZenModeConditionSelection(Context context, int zenMode) {
54623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        super(context);
55623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        mContext = context;
5645fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock        mZenMode = zenMode;
57062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk        mConditions = new ArrayList<Condition>();
58623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        setLayoutTransition(new LayoutTransition());
59623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        final int p = mContext.getResources().getDimensionPixelSize(R.dimen.content_margin_left);
60623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        setPadding(p, p, p, 0);
61623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        mNoMan = INotificationManager.Stub.asInterface(
62623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock                ServiceManager.getService(Context.NOTIFICATION_SERVICE));
63623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        final RadioButton b = newRadioButton(null);
64062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk        b.setText(mContext.getString(com.android.internal.R.string.zen_mode_forever));
65623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        b.setChecked(true);
66062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk        for (int i = ZenModeConfig.MINUTE_BUCKETS.length - 1; i >= 0; --i) {
6724791abae1737901e3d0a3f9f3e60b7aa31055fcJohn Spurlock            handleCondition(ZenModeConfig.toTimeCondition(mContext,
68334bd70fc7e61567443cbf25b12bbf18aa5b636dSelim Cinek                    ZenModeConfig.MINUTE_BUCKETS[i], UserHandle.myUserId()));
69062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk        }
70623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    }
71623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock
72a7f8addd0982f599105aeb9611477b149860db83John Spurlock    private RadioButton newRadioButton(Condition condition) {
73623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        final RadioButton button = new RadioButton(mContext);
74a7f8addd0982f599105aeb9611477b149860db83John Spurlock        button.setTag(condition);
75623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
76623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock            @Override
77623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
78623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock                if (isChecked) {
79062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk                    setCondition((Condition) button.getTag());
80623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock                }
81623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock            }
82623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        });
83623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        addView(button);
84623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        return button;
85623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    }
86623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock
87623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    @Override
88623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    protected void onAttachedToWindow() {
89623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        super.onAttachedToWindow();
9023e221cd93a9bf2546709c2f23b1309ea5e23633John Spurlock        requestZenModeConditions(Condition.FLAG_RELEVANT_NOW);
91623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    }
92623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock
93623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    @Override
94623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    protected void onDetachedFromWindow() {
95623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        super.onDetachedFromWindow();
9623e221cd93a9bf2546709c2f23b1309ea5e23633John Spurlock        requestZenModeConditions(0 /*none*/);
97623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    }
98623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock
9923e221cd93a9bf2546709c2f23b1309ea5e23633John Spurlock    protected void requestZenModeConditions(int relevance) {
10023e221cd93a9bf2546709c2f23b1309ea5e23633John Spurlock        if (DEBUG) Log.d(TAG, "requestZenModeConditions " + Condition.relevanceToString(relevance));
101623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        try {
10223e221cd93a9bf2546709c2f23b1309ea5e23633John Spurlock            mNoMan.requestZenModeConditions(mListener, relevance);
103623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        } catch (RemoteException e) {
104623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock            // noop
105623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        }
106623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    }
107623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock
108623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    protected void handleConditions(Condition[] conditions) {
109062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk        for (Condition c : conditions) {
110062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk            handleCondition(c);
111062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk        }
112062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk    }
113062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk
114062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk    protected void handleCondition(Condition c) {
115062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk        if (mConditions.contains(c)) return;
116062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk        RadioButton v = (RadioButton) findViewWithTag(c.id);
117062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk        if (c.state == Condition.STATE_TRUE || c.state == Condition.STATE_UNKNOWN) {
118062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk            if (v == null) {
119062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk                v = newRadioButton(c);
120623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock            }
121623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        }
122062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk        if (v != null) {
123beff087b3ac3aae4eb120feef81274d7caeafb58John Spurlock            v.setText(computeConditionText(c));
124062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk            v.setEnabled(c.state == Condition.STATE_TRUE);
125062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk        }
126062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk        mConditions.add(c);
127062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk    }
128062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk
129062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk    protected void setCondition(Condition c) {
130062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk        if (DEBUG) Log.d(TAG, "setCondition " + c);
131062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk        mCondition = c;
132623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    }
133623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock
134062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk    public void confirmCondition() {
135062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk        if (DEBUG) Log.d(TAG, "confirmCondition " + mCondition);
136623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        try {
13745fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock            mNoMan.setZenMode(mZenMode, mCondition != null ? mCondition.id : null, TAG);
138623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        } catch (RemoteException e) {
139623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock            // noop
140623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        }
141623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    }
142623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock
143beff087b3ac3aae4eb120feef81274d7caeafb58John Spurlock    private static String computeConditionText(Condition c) {
144beff087b3ac3aae4eb120feef81274d7caeafb58John Spurlock        return !TextUtils.isEmpty(c.line1) ? c.line1
145beff087b3ac3aae4eb120feef81274d7caeafb58John Spurlock                : !TextUtils.isEmpty(c.summary) ? c.summary
146beff087b3ac3aae4eb120feef81274d7caeafb58John Spurlock                : "";
147beff087b3ac3aae4eb120feef81274d7caeafb58John Spurlock    }
148beff087b3ac3aae4eb120feef81274d7caeafb58John Spurlock
149623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    private final IConditionListener mListener = new IConditionListener.Stub() {
150623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        @Override
151623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        public void onConditionsReceived(Condition[] conditions) {
152623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock            if (conditions == null || conditions.length == 0) return;
153623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock            mHandler.obtainMessage(H.CONDITIONS, conditions).sendToTarget();
154623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        }
155623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    };
156623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock
157623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    private final class H extends Handler {
158623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        private static final int CONDITIONS = 1;
159623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock
160623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        @Override
161623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        public void handleMessage(Message msg) {
162062f736c8bfda9ac6ac7476ad51e52ca077f1505Jason Monk            if (msg.what == CONDITIONS) handleConditions((Condition[]) msg.obj);
163623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock        }
164623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock    }
165623a0e9f45c294ea7e714295f41ba2d8bcd7b32eJohn Spurlock}
166