OSDN Git Service

Adding zen mode condition dialog
authorJason Monk <jmonk@google.com>
Mon, 8 Sep 2014 14:16:06 +0000 (10:16 -0400)
committerDan Sandler <dsandler@android.com>
Wed, 10 Sep 2014 20:32:39 +0000 (20:32 +0000)
So that when you turn on priority or none in settings, you can
select the end condition.

Depends on framework changes: I9300af4f8b6d80042452b75db3418b20c5c5cc81

Bug: 15454830
Change-Id: I7856beabdd54de2ef10fa5c3d38bf27f36014693

src/com/android/settings/notification/SettingPref.java
src/com/android/settings/notification/ZenModeConditionSelection.java
src/com/android/settings/notification/ZenModeSettings.java

index de01def..a06c35a 100644 (file)
@@ -33,15 +33,15 @@ public class SettingPref {
     public static final int TYPE_GLOBAL = 1;
     public static final int TYPE_SYSTEM = 2;
 
-    private final int mType;
+    protected final int mType;
     private final String mKey;
-    private final String mSetting;
-    private final int mDefault;
+    protected final String mSetting;
+    protected final int mDefault;
     private final int[] mValues;
     private final Uri mUri;
 
-    private TwoStatePreference mTwoState;
-    private DropDownPreference mDropDown;
+    protected TwoStatePreference mTwoState;
+    protected DropDownPreference mDropDown;
 
     public SettingPref(int type, String key, String setting, int def, int... values) {
         mType = type;
@@ -129,7 +129,7 @@ public class SettingPref {
         throw new IllegalArgumentException();
     }
 
-    private static boolean putInt(int type, ContentResolver cr, String setting, int value) {
+    protected static boolean putInt(int type, ContentResolver cr, String setting, int value) {
         switch(type) {
             case TYPE_GLOBAL:
                 return Global.putInt(cr, setting, value);
@@ -139,7 +139,7 @@ public class SettingPref {
         throw new IllegalArgumentException();
     }
 
-    private static int getInt(int type, ContentResolver cr, String setting, int def) {
+    protected static int getInt(int type, ContentResolver cr, String setting, int def) {
         switch(type) {
             case TYPE_GLOBAL:
                 return Global.getInt(cr, setting, def);
index 610baba..856a7f6 100644 (file)
@@ -19,13 +19,13 @@ 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.service.notification.ZenModeConfig;
 import android.util.Log;
 import android.widget.CompoundButton;
 import android.widget.RadioButton;
@@ -33,6 +33,9 @@ import android.widget.RadioGroup;
 
 import com.android.settings.R;
 
+import java.util.ArrayList;
+import java.util.List;
+
 public class ZenModeConditionSelection extends RadioGroup {
     private static final String TAG = "ZenModeConditionSelection";
     private static final boolean DEBUG = true;
@@ -40,18 +43,24 @@ public class ZenModeConditionSelection extends RadioGroup {
     private final INotificationManager mNoMan;
     private final H mHandler = new H();
     private final Context mContext;
+    private final List<Condition> mConditions;
+    private Condition mCondition;
 
     public ZenModeConditionSelection(Context context) {
         super(context);
         mContext = context;
+        mConditions = new ArrayList<Condition>();
         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.setText(mContext.getString(com.android.internal.R.string.zen_mode_forever));
         b.setChecked(true);
+        for (int i = ZenModeConfig.MINUTE_BUCKETS.length - 1; i >= 0; --i) {
+            handleCondition(ZenModeConfig.toTimeCondition(ZenModeConfig.MINUTE_BUCKETS[i]));
+        }
     }
 
     private RadioButton newRadioButton(Condition condition) {
@@ -61,7 +70,7 @@ public class ZenModeConditionSelection extends RadioGroup {
             @Override
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                 if (isChecked) {
-                    handleSubscribe((Condition) button.getTag());
+                    setCondition((Condition) button.getTag());
                 }
             }
         });
@@ -91,24 +100,35 @@ public class ZenModeConditionSelection extends RadioGroup {
     }
 
     protected void handleConditions(Condition[] conditions) {
-        for (final Condition c : conditions) {
-            RadioButton v = (RadioButton) findViewWithTag(c.id);
-            if (c.state == Condition.STATE_TRUE || c.state == Condition.STATE_UNKNOWN) {
-                if (v == null) {
-                    v = newRadioButton(c);
-                }
-            }
-            if (v != null) {
-                v.setText(c.summary);
-                v.setEnabled(c.state == Condition.STATE_TRUE);
+        for (Condition c : conditions) {
+            handleCondition(c);
+        }
+    }
+
+    protected void handleCondition(Condition c) {
+        if (mConditions.contains(c)) return;
+        RadioButton v = (RadioButton) findViewWithTag(c.id);
+        if (c.state == Condition.STATE_TRUE || c.state == Condition.STATE_UNKNOWN) {
+            if (v == null) {
+                v = newRadioButton(c);
             }
         }
+        if (v != null) {
+            v.setText(c.summary);
+            v.setEnabled(c.state == Condition.STATE_TRUE);
+        }
+        mConditions.add(c);
+    }
+
+    protected void setCondition(Condition c) {
+        if (DEBUG) Log.d(TAG, "setCondition " + c);
+        mCondition = c;
     }
 
-    protected void handleSubscribe(Condition c) {
-        if (DEBUG) Log.d(TAG, "handleSubscribe " + c);
+    public void confirmCondition() {
+        if (DEBUG) Log.d(TAG, "confirmCondition " + mCondition);
         try {
-            mNoMan.setZenModeCondition(c);
+            mNoMan.setZenModeCondition(mCondition);
         } catch (RemoteException e) {
             // noop
         }
@@ -127,7 +147,7 @@ public class ZenModeConditionSelection extends RadioGroup {
 
         @Override
         public void handleMessage(Message msg) {
-            if (msg.what == CONDITIONS) handleConditions((Condition[])msg.obj);
+            if (msg.what == CONDITIONS) handleConditions((Condition[]) msg.obj);
         }
     }
 }
index 9ea3c93..d26b128 100644 (file)
@@ -46,11 +46,13 @@ import android.service.notification.ZenModeConfig;
 import android.text.format.DateFormat;
 import android.util.Log;
 import android.util.SparseArray;
+import android.widget.ScrollView;
 import android.widget.TimePicker;
 
 import com.android.settings.R;
 import com.android.settings.SettingsPreferenceFragment;
 import com.android.settings.Utils;
+import com.android.settings.notification.DropDownPreference.Callback;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.search.Indexable;
 import com.android.settings.search.SearchIndexableRaw;
@@ -81,9 +83,10 @@ public class ZenModeSettings extends SettingsPreferenceFragment implements Index
     private static final String KEY_ENTRY = "entry";
     private static final String KEY_CONDITION_PROVIDERS = "manage_condition_providers";
 
-    private static final SettingPref PREF_ZEN_MODE = new SettingPref(SettingPref.TYPE_GLOBAL,
-            KEY_ZEN_MODE, Global.ZEN_MODE, Global.ZEN_MODE_OFF, Global.ZEN_MODE_OFF,
-            Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, Global.ZEN_MODE_NO_INTERRUPTIONS) {
+    private static final SettingPrefWithCallback PREF_ZEN_MODE = new SettingPrefWithCallback(
+            SettingPref.TYPE_GLOBAL, KEY_ZEN_MODE, Global.ZEN_MODE, Global.ZEN_MODE_OFF,
+            Global.ZEN_MODE_OFF, Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS,
+            Global.ZEN_MODE_NO_INTERRUPTIONS) {
         protected String getCaption(Resources res, int value) {
             switch (value) {
                 case Global.ZEN_MODE_NO_INTERRUPTIONS:
@@ -135,6 +138,7 @@ public class ZenModeSettings extends SettingsPreferenceFragment implements Index
     private PreferenceCategory mAutomationCategory;
     private Preference mEntry;
     private Preference mConditionProviders;
+    private AlertDialog mDialog;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -149,6 +153,14 @@ public class ZenModeSettings extends SettingsPreferenceFragment implements Index
         if (DEBUG) Log.d(TAG, "Loaded mConfig=" + mConfig);
 
         final Preference zenMode = PREF_ZEN_MODE.init(this);
+        PREF_ZEN_MODE.setCallback(new SettingPrefWithCallback.Callback() {
+            @Override
+            public void onSettingSelected(int value) {
+                if (value != Global.ZEN_MODE_OFF) {
+                    showConditionSelection(value);
+                }
+            }
+        });
         if (!Utils.isVoiceCapable(mContext)) {
             zenMode.setTitle(R.string.zen_mode_option_title_novoice);
         }
@@ -467,8 +479,44 @@ public class ZenModeSettings extends SettingsPreferenceFragment implements Index
         Global.putInt(getContentResolver(), Global.ZEN_MODE, value);
     }
 
-    protected ZenModeConditionSelection newConditionSelection() {
-        return new ZenModeConditionSelection(mContext);
+    protected void showConditionSelection(final int newSettingsValue) {
+        if (mDialog != null) return;
+
+        final ZenModeConditionSelection zenModeConditionSelection =
+                new ZenModeConditionSelection(mContext);
+        DialogInterface.OnClickListener positiveListener = new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                zenModeConditionSelection.confirmCondition();
+                mDialog = null;
+            }
+        };
+        final int oldSettingsValue = PREF_ZEN_MODE.getValue(mContext);
+        ScrollView scrollView = new ScrollView(mContext);
+        scrollView.addView(zenModeConditionSelection);
+        mDialog = new AlertDialog.Builder(getActivity())
+                .setTitle(PREF_ZEN_MODE.getCaption(getResources(), newSettingsValue))
+                .setView(scrollView)
+                .setPositiveButton(R.string.okay, positiveListener)
+                .setNegativeButton(R.string.cancel_all_caps, new DialogInterface.OnClickListener() {
+                    @Override
+                    public void onClick(DialogInterface dialog, int which) {
+                        cancelDialog(oldSettingsValue);
+                    }
+                })
+                .setOnCancelListener(new DialogInterface.OnCancelListener() {
+                    @Override
+                    public void onCancel(DialogInterface dialog) {
+                        cancelDialog(oldSettingsValue);
+                    }
+                }).create();
+        mDialog.show();
+    }
+
+    protected void cancelDialog(int oldSettingsValue) {
+        // If not making a decision, reset drop down to current setting.
+        PREF_ZEN_MODE.setValueWithoutCallback(mContext, oldSettingsValue);
+        mDialog = null;
     }
 
     // Enable indexing of searchable data
@@ -499,6 +547,60 @@ public class ZenModeSettings extends SettingsPreferenceFragment implements Index
             }
         };
 
+    private static class SettingPrefWithCallback extends SettingPref {
+
+        private Callback mCallback;
+        private int mValue;
+
+        public SettingPrefWithCallback(int type, String key, String setting, int def,
+                int... values) {
+            super(type, key, setting, def, values);
+        }
+
+        public void setCallback(Callback callback) {
+            mCallback = callback;
+        }
+
+        @Override
+        public void update(Context context) {
+            // Avoid callbacks from non-user changes.
+            mValue = getValue(context);
+            super.update(context);
+        }
+
+        @Override
+        protected boolean setSetting(Context context, int value) {
+            if (value == mValue) return true;
+            mValue = value;
+            if (mCallback != null) {
+                mCallback.onSettingSelected(value);
+            }
+            return super.setSetting(context, value);
+        }
+
+        @Override
+        public Preference init(SettingsPreferenceFragment settings) {
+            Preference ret = super.init(settings);
+            mValue = getValue(settings.getActivity());
+
+            return ret;
+        }
+
+        public boolean setValueWithoutCallback(Context context, int value) {
+            // Set the current value ahead of time, this way we won't trigger a callback.
+            mValue = value;
+            return putInt(mType, context.getContentResolver(), mSetting, value);
+        }
+
+        public int getValue(Context context) {
+            return getInt(mType, context.getContentResolver(), mSetting, mDefault);
+        }
+
+        public interface Callback {
+            void onSettingSelected(int value);
+        }
+    }
+
     private final class SettingsObserver extends ContentObserver {
         private final Uri ZEN_MODE_URI = Global.getUriFor(Global.ZEN_MODE);
         private final Uri ZEN_MODE_CONFIG_ETAG_URI = Global.getUriFor(Global.ZEN_MODE_CONFIG_ETAG);