Merge "Add zen-mode exit condition dialog to settings."
diff --git a/res/values/strings.xml b/res/values/strings.xml
index be924f0..1eae3ae 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -5156,6 +5156,9 @@
     <!-- [CHAR LIMIT=20] Zen mode settings: Master switch option title, on -->
     <string name="zen_mode_option_on">On</string>
 
+     <!-- [CHAR LIMIT=30] Zen mode settings: Exit condition selection dialog, default option -->
+    <string name="zen_mode_default_option">Until you turn this off</string>
+
     <!-- [CHAR LIMIT=40] Zen mode settings: General category text -->
     <string name="zen_mode_general_category">Block all interruptions except</string>
 
diff --git a/src/com/android/settings/notification/ZenModeConditionSelection.java b/src/com/android/settings/notification/ZenModeConditionSelection.java
new file mode 100644
index 0000000..031d785
--- /dev/null
+++ b/src/com/android/settings/notification/ZenModeConditionSelection.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.notification;
+
+import android.animation.LayoutTransition;
+import android.app.INotificationManager;
+import android.content.Context;
+import android.net.Uri;
+import android.os.Handler;
+import android.os.Message;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.service.notification.Condition;
+import android.service.notification.IConditionListener;
+import android.util.Log;
+import android.widget.CompoundButton;
+import android.widget.RadioButton;
+import android.widget.RadioGroup;
+
+import com.android.settings.R;
+
+public class ZenModeConditionSelection extends RadioGroup {
+    private static final String TAG = "ZenModeConditionSelection";
+    private static final boolean DEBUG = true;
+
+    private final INotificationManager mNoMan;
+    private final H mHandler = new H();
+    private final Context mContext;
+
+    public ZenModeConditionSelection(Context context) {
+        super(context);
+        mContext = context;
+        setLayoutTransition(new LayoutTransition());
+        final int p = mContext.getResources().getDimensionPixelSize(R.dimen.content_margin_left);
+        setPadding(p, p, p, 0);
+        mNoMan = INotificationManager.Stub.asInterface(
+                ServiceManager.getService(Context.NOTIFICATION_SERVICE));
+        final RadioButton b = newRadioButton(null);
+        b.setText(R.string.zen_mode_default_option);
+        b.setChecked(true);
+    }
+
+    private RadioButton newRadioButton(Object tag) {
+        final RadioButton button = new RadioButton(mContext);
+        button.setTag(tag);
+        button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+            @Override
+            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+                if (isChecked) {
+                    handleSubscribe((Uri)button.getTag());
+                }
+            }
+        });
+        addView(button);
+        return button;
+    }
+
+    @Override
+    protected void onAttachedToWindow() {
+        super.onAttachedToWindow();
+        requestZenModeConditions(true);
+    }
+
+    @Override
+    protected void onDetachedFromWindow() {
+        super.onDetachedFromWindow();
+        requestZenModeConditions(false);
+    }
+
+    protected void requestZenModeConditions(boolean requested) {
+        if (DEBUG) Log.d(TAG, "requestZenModeConditions " + requested);
+        try {
+            mNoMan.requestZenModeConditions(mListener, requested);
+        } catch (RemoteException e) {
+            // noop
+        }
+    }
+
+    protected void handleConditions(Condition[] conditions) {
+        for (final Condition c : conditions) {
+            RadioButton v = (RadioButton) findViewWithTag(c.id);
+            if (c.state == Condition.STATE_FALSE || c.state == Condition.STATE_UNKNOWN) {
+                if (v == null) {
+                    v = newRadioButton(c.id);
+                }
+            }
+            if (v != null) {
+                v.setText(c.caption);
+                v.setEnabled(c.state == Condition.STATE_FALSE);
+            }
+        }
+    }
+
+    protected void handleSubscribe(Uri id) {
+        if (DEBUG) Log.d(TAG, "handleSubscribe " + id);
+        try {
+            mNoMan.setZenModeCondition(id);
+        } catch (RemoteException e) {
+            // noop
+        }
+    }
+
+    private final IConditionListener mListener = new IConditionListener.Stub() {
+        @Override
+        public void onConditionsReceived(Condition[] conditions) {
+            if (conditions == null || conditions.length == 0) return;
+            mHandler.obtainMessage(H.CONDITIONS, conditions).sendToTarget();
+        }
+    };
+
+    private final class H extends Handler {
+        private static final int CONDITIONS = 1;
+
+        @Override
+        public void handleMessage(Message msg) {
+            if (msg.what == CONDITIONS) handleConditions((Condition[])msg.obj);
+        }
+    }
+}
diff --git a/src/com/android/settings/notification/ZenModeSettings.java b/src/com/android/settings/notification/ZenModeSettings.java
index 26fa539..a5c720f 100644
--- a/src/com/android/settings/notification/ZenModeSettings.java
+++ b/src/com/android/settings/notification/ZenModeSettings.java
@@ -16,12 +16,14 @@
 
 package com.android.settings.notification;
 
+import android.app.AlertDialog;
 import android.app.Dialog;
 import android.app.DialogFragment;
 import android.app.FragmentManager;
 import android.app.INotificationManager;
 import android.app.TimePickerDialog;
 import android.content.Context;
+import android.content.DialogInterface;
 import android.content.res.Resources;
 import android.database.ContentObserver;
 import android.graphics.Typeface;
@@ -80,6 +82,7 @@
     private DropDownPreference mWhen;
     private TimePickerPreference mStart;
     private TimePickerPreference mEnd;
+    private AlertDialog mDialog;
 
     @Override
     public void onCreate(Bundle icicle) {
@@ -292,6 +295,46 @@
         }
     }
 
+    protected void putZenModeSetting(int value) {
+        Global.putInt(getContentResolver(), Global.ZEN_MODE, value);
+    }
+
+    protected ZenModeConditionSelection newConditionSelection() {
+        return new ZenModeConditionSelection(mContext);
+    }
+
+    private final Runnable mHideDialog = new Runnable() {
+        @Override
+        public void run() {
+            if (mDialog != null) {
+                mDialog.dismiss();
+                mDialog = null;
+            }
+        }
+    };
+
+    private final Runnable mShowDialog = new Runnable() {
+        @Override
+        public void run() {
+            mDialog = new AlertDialog.Builder(mContext)
+                    .setTitle(R.string.zen_mode_settings_title)
+                    .setView(newConditionSelection())
+                    .setNegativeButton(R.string.dlg_cancel, new DialogInterface.OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialog, int which) {
+                            putZenModeSetting(Global.ZEN_MODE_OFF);
+                        }
+                    })
+                    .setPositiveButton(R.string.dlg_ok, new DialogInterface.OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialog, int which) {
+                            // noop
+                        }
+                    })
+                    .show();
+        }
+    };
+
     private final OnPreferenceChangeListener mSwitchListener = new OnPreferenceChangeListener() {
         @Override
         public boolean onPreferenceChange(Preference preference, Object newValue) {
@@ -300,7 +343,8 @@
                 @Override
                 public void run() {
                     final int v = isChecked ? Global.ZEN_MODE_ON : Global.ZEN_MODE_OFF;
-                    Global.putInt(getContentResolver(), Global.ZEN_MODE, v);
+                    putZenModeSetting(v);
+                    mHandler.post(isChecked ? mShowDialog : mHideDialog);
                 }
             });
             return true;