ZenModeConditionSelection.java revision 0e0122a9999eb1a603c99be12846acad29ed0593
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 17package com.android.settings.notification; 18 19import android.animation.LayoutTransition; 20import android.app.INotificationManager; 21import android.content.Context; 22import android.net.Uri; 23import android.os.Handler; 24import android.os.Message; 25import android.os.RemoteException; 26import android.os.ServiceManager; 27import android.service.notification.Condition; 28import android.service.notification.IConditionListener; 29import android.util.Log; 30import android.widget.CompoundButton; 31import android.widget.RadioButton; 32import android.widget.RadioGroup; 33 34import com.android.settings.R; 35 36public 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} 134