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.os.UserHandle; 27 import android.service.notification.Condition; 28 import android.service.notification.IConditionListener; 29 import android.service.notification.ZenModeConfig; 30 import android.text.TextUtils; 31 import android.util.Log; 32 import android.widget.CompoundButton; 33 import android.widget.RadioButton; 34 import android.widget.RadioGroup; 35 36 import com.android.settings.R; 37 38 import java.util.ArrayList; 39 import java.util.List; 40 41 public class ZenModeConditionSelection extends RadioGroup { 42 private static final String TAG = "ZenModeConditionSelection"; 43 private static final boolean DEBUG = true; 44 45 private final INotificationManager mNoMan; 46 private final H mHandler = new H(); 47 private final Context mContext; 48 private final List<Condition> mConditions; 49 private Condition mCondition; 50 ZenModeConditionSelection(Context context)51 public ZenModeConditionSelection(Context context) { 52 super(context); 53 mContext = context; 54 mConditions = new ArrayList<Condition>(); 55 setLayoutTransition(new LayoutTransition()); 56 final int p = mContext.getResources().getDimensionPixelSize(R.dimen.content_margin_left); 57 setPadding(p, p, p, 0); 58 mNoMan = INotificationManager.Stub.asInterface( 59 ServiceManager.getService(Context.NOTIFICATION_SERVICE)); 60 final RadioButton b = newRadioButton(null); 61 b.setText(mContext.getString(com.android.internal.R.string.zen_mode_forever)); 62 b.setChecked(true); 63 for (int i = ZenModeConfig.MINUTE_BUCKETS.length - 1; i >= 0; --i) { 64 handleCondition(ZenModeConfig.toTimeCondition(mContext, 65 ZenModeConfig.MINUTE_BUCKETS[i], UserHandle.myUserId())); 66 } 67 } 68 newRadioButton(Condition condition)69 private RadioButton newRadioButton(Condition condition) { 70 final RadioButton button = new RadioButton(mContext); 71 button.setTag(condition); 72 button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 73 @Override 74 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 75 if (isChecked) { 76 setCondition((Condition) button.getTag()); 77 } 78 } 79 }); 80 addView(button); 81 return button; 82 } 83 84 @Override onAttachedToWindow()85 protected void onAttachedToWindow() { 86 super.onAttachedToWindow(); 87 requestZenModeConditions(Condition.FLAG_RELEVANT_NOW); 88 } 89 90 @Override onDetachedFromWindow()91 protected void onDetachedFromWindow() { 92 super.onDetachedFromWindow(); 93 requestZenModeConditions(0 /*none*/); 94 } 95 requestZenModeConditions(int relevance)96 protected void requestZenModeConditions(int relevance) { 97 if (DEBUG) Log.d(TAG, "requestZenModeConditions " + Condition.relevanceToString(relevance)); 98 try { 99 mNoMan.requestZenModeConditions(mListener, relevance); 100 } catch (RemoteException e) { 101 // noop 102 } 103 } 104 handleConditions(Condition[] conditions)105 protected void handleConditions(Condition[] conditions) { 106 for (Condition c : conditions) { 107 handleCondition(c); 108 } 109 } 110 handleCondition(Condition c)111 protected void handleCondition(Condition c) { 112 if (mConditions.contains(c)) return; 113 RadioButton v = (RadioButton) findViewWithTag(c.id); 114 if (c.state == Condition.STATE_TRUE || c.state == Condition.STATE_UNKNOWN) { 115 if (v == null) { 116 v = newRadioButton(c); 117 } 118 } 119 if (v != null) { 120 v.setText(!TextUtils.isEmpty(c.line1) ? c.line1 : c.summary); 121 v.setEnabled(c.state == Condition.STATE_TRUE); 122 } 123 mConditions.add(c); 124 } 125 setCondition(Condition c)126 protected void setCondition(Condition c) { 127 if (DEBUG) Log.d(TAG, "setCondition " + c); 128 mCondition = c; 129 } 130 confirmCondition()131 public void confirmCondition() { 132 if (DEBUG) Log.d(TAG, "confirmCondition " + mCondition); 133 try { 134 mNoMan.setZenModeCondition(mCondition); 135 } catch (RemoteException e) { 136 // noop 137 } 138 } 139 140 private final IConditionListener mListener = new IConditionListener.Stub() { 141 @Override 142 public void onConditionsReceived(Condition[] conditions) { 143 if (conditions == null || conditions.length == 0) return; 144 mHandler.obtainMessage(H.CONDITIONS, conditions).sendToTarget(); 145 } 146 }; 147 148 private final class H extends Handler { 149 private static final int CONDITIONS = 1; 150 151 @Override handleMessage(Message msg)152 public void handleMessage(Message msg) { 153 if (msg.what == CONDITIONS) handleConditions((Condition[]) msg.obj); 154 } 155 } 156 } 157