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;
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);
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);
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;
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;
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) {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
- handleSubscribe((Condition) button.getTag());
+ setCondition((Condition) button.getTag());
}
}
});
}
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
}
@Override
public void handleMessage(Message msg) {
- if (msg.what == CONDITIONS) handleConditions((Condition[])msg.obj);
+ if (msg.what == CONDITIONS) handleConditions((Condition[]) msg.obj);
}
}
}
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;
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:
private PreferenceCategory mAutomationCategory;
private Preference mEntry;
private Preference mConditionProviders;
+ private AlertDialog mDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
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);
}
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
}
};
+ 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);