From adda1b0022f12dd15a802ec36e6efee0f9c6bff2 Mon Sep 17 00:00:00 2001 From: John Spurlock Date: Thu, 27 Feb 2014 15:37:13 -0500 Subject: [PATCH] Zen mode settings. Step 1: Surface zen mode value under sound settings. This is in keeping with the principle that quick settings are accelerators (not replacements) for settings in the Settings app. Adding configuration to "Limited interruptions" will be done as a followup, and will serve as the target for the configuration icon in SystemUI. Change-Id: Ia2c7351454b17df3e27926ff593eebff284ebef8 --- res/values/arrays.xml | 13 +++ res/values/strings.xml | 7 ++ res/xml/sound_settings.xml | 9 ++ src/com/android/settings/SoundSettings.java | 5 + .../settings/ZenModeListPreference.java | 92 +++++++++++++++++++ 5 files changed, 126 insertions(+) create mode 100644 src/com/android/settings/ZenModeListPreference.java diff --git a/res/values/arrays.xml b/res/values/arrays.xml index 2e22d46088e..6d0cd764867 100644 --- a/res/values/arrays.xml +++ b/res/values/arrays.xml @@ -1229,4 +1229,17 @@ 6 + + + Off + Limited interruptions + Zero interruptions + + + + + 0 + 1 + 2 + diff --git a/res/values/strings.xml b/res/values/strings.xml index b42c88ce99c..612677b9b65 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -5005,4 +5005,11 @@ shown in notifications shown on a secure lock screen [CHAR LIMIT=50] --> All notification contents will be shown when locked + + + Zen mode + + %1$s + + Zen mode diff --git a/res/xml/sound_settings.xml b/res/xml/sound_settings.xml index 26e2e20537d..580b6d5cf8a 100644 --- a/res/xml/sound_settings.xml +++ b/res/xml/sound_settings.xml @@ -109,4 +109,13 @@ android:title="@string/dock_sounds_enable_title" android:defaultValue="false" /> + + diff --git a/src/com/android/settings/SoundSettings.java b/src/com/android/settings/SoundSettings.java index 28d93f16ab7..d50686486b6 100644 --- a/src/com/android/settings/SoundSettings.java +++ b/src/com/android/settings/SoundSettings.java @@ -75,6 +75,7 @@ public class SoundSettings extends SettingsPreferenceFragment implements private static final String KEY_DOCK_AUDIO_SETTINGS = "dock_audio"; private static final String KEY_DOCK_SOUNDS = "dock_sounds"; private static final String KEY_DOCK_AUDIO_MEDIA_ENABLED = "dock_audio_media_enabled"; + private static final String KEY_ZEN_MODE = "zen_mode"; private static final String[] NEED_VOICE_CAPABILITY = { KEY_RINGTONE, KEY_DTMF_TONE, KEY_CATEGORY_CALLS, @@ -101,6 +102,7 @@ public class SoundSettings extends SettingsPreferenceFragment implements private CheckBoxPreference mDockSounds; private Intent mDockIntent; private CheckBoxPreference mDockAudioMediaEnabled; + private ZenModeListPreference mZenModeListPreference; private Handler mHandler = new Handler() { public void handleMessage(Message msg) { @@ -227,6 +229,9 @@ public class SoundSettings extends SettingsPreferenceFragment implements }; initDockSettings(); + + mZenModeListPreference = (ZenModeListPreference) findPreference(KEY_ZEN_MODE); + mZenModeListPreference.init(); } @Override diff --git a/src/com/android/settings/ZenModeListPreference.java b/src/com/android/settings/ZenModeListPreference.java new file mode 100644 index 00000000000..0315a1335fd --- /dev/null +++ b/src/com/android/settings/ZenModeListPreference.java @@ -0,0 +1,92 @@ +/* + * 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; + +import android.content.ContentResolver; +import android.content.Context; +import android.database.ContentObserver; +import android.os.Handler; +import android.preference.ListPreference; +import android.preference.Preference; +import android.provider.Settings; +import android.util.AttributeSet; +import android.util.Log; + +public class ZenModeListPreference extends ListPreference { + private static final String TAG = "ZenModeListPreference"; + private static final boolean DEBUG = false; + + private final Handler mHandler = new Handler(); + private final ContentResolver mResolver; + + public ZenModeListPreference(Context context, AttributeSet attrs) { + super(context, attrs); + if (DEBUG) Log.d(TAG, "new ZenModeListPreference()"); + mResolver = context.getContentResolver(); + } + + public void init() { + if (DEBUG) Log.d(TAG, "init"); + loadZenModeSetting("init"); + setOnPreferenceChangeListener(new OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if (DEBUG) Log.d(TAG, "onPreferenceChange " + newValue); + final boolean updateWithNewValue = saveZenModeSetting((String)newValue); + return updateWithNewValue; + } + }); + mResolver.registerContentObserver( + Settings.Global.getUriFor(Settings.Global.ZEN_MODE), + false, new SettingsObserver()); + } + + private void loadZenModeSetting(String reason) { + if (DEBUG) Log.d(TAG, "loadZenModeSetting " + reason); + setValue(Integer.toString(Settings.Global.getInt(mResolver, + Settings.Global.ZEN_MODE, Settings.Global.ZEN_MODE_OFF))); + } + + private boolean saveZenModeSetting(String value) { + if (DEBUG) Log.d(TAG, "saveZenModeSetting " + value); + try { + final int v = Integer.valueOf(value); + checkZenMode(v); + return Settings.Global.putInt(mResolver, Settings.Global.ZEN_MODE, v); + } catch (Throwable t) { + Log.w(TAG, "Failed to update zen mode with value: " + value, t); + return false; + } + } + + private static void checkZenMode(int mode) { + if (mode < Settings.Global.ZEN_MODE_OFF || mode > Settings.Global.ZEN_MODE_FULL) { + throw new IllegalArgumentException("Invalid zen mode: " + mode); + } + } + + private final class SettingsObserver extends ContentObserver { + public SettingsObserver() { + super(mHandler); + } + + @Override + public void onChange(boolean selfChange) { + loadZenModeSetting("change"); + } + } +}