Use SwitchBar for DoNoDisturb Settings

- follow up CL to 41937766981423c9252e12e3319b2e7532739627

Related to bug #14898161 On/Off switches must move down from Action Bar

Change-Id: If1ce0eb147ade8485107ee948b0d03cf688bcf06
diff --git a/res/layout/zen_mode_section_first.xml b/res/layout/zen_mode_section_first.xml
new file mode 100644
index 0000000..6054be4
--- /dev/null
+++ b/res/layout/zen_mode_section_first.xml
@@ -0,0 +1,28 @@
+<!--
+     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.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:orientation="vertical" >
+
+    <TextView
+        android:id="@android:id/title"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:textAllCaps="true"
+        android:textStyle="bold" />
+
+</LinearLayout>
\ No newline at end of file
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 535e502..aa0c77d 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -5175,12 +5175,6 @@
     <!-- [CHAR LIMIT=20] Notification settings: App notifications dialog dismiss button caption -->
     <string name="app_notifications_dialog_done">Done</string>
 
-    <!-- [CHAR LIMIT=20] Zen mode settings: Master switch option title, off -->
-    <string name="zen_mode_option_off">Off</string>
-
-    <!-- [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>
 
diff --git a/res/xml/zen_mode_settings.xml b/res/xml/zen_mode_settings.xml
index b1ec8ad..35732d6 100644
--- a/res/xml/zen_mode_settings.xml
+++ b/res/xml/zen_mode_settings.xml
@@ -19,15 +19,9 @@
     android:key="zen_mode_settings"
     android:title="@string/zen_mode_settings_title" >
 
-    <SwitchPreference
-        android:key="zen_mode"
-        android:persistent="false"
-        android:switchTextOff=""
-        android:switchTextOn="" />
-
     <PreferenceCategory
         android:key="general"
-        android:layout="@layout/zen_mode_section"
+        android:layout="@layout/zen_mode_section_first"
         android:title="@string/zen_mode_general_category" >
         <SwitchPreference
             android:key="phone_calls"
diff --git a/src/com/android/settings/notification/ZenModeSettings.java b/src/com/android/settings/notification/ZenModeSettings.java
index ff1a5ab..e5ffd67 100644
--- a/src/com/android/settings/notification/ZenModeSettings.java
+++ b/src/com/android/settings/notification/ZenModeSettings.java
@@ -47,25 +47,28 @@
 import android.util.Log;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.Switch;
 import android.widget.TextView;
 import android.widget.TimePicker;
 
 import com.android.settings.R;
+import com.android.settings.SettingsActivity;
 import com.android.settings.SettingsPreferenceFragment;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.search.Indexable;
 import com.android.settings.search.SearchIndexableRaw;
+import com.android.settings.widget.SwitchBar;
 
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.List;
 import java.util.Objects;
 
-public class ZenModeSettings extends SettingsPreferenceFragment implements Indexable {
+public class ZenModeSettings extends SettingsPreferenceFragment implements Indexable,
+        SwitchBar.OnSwitchChangeListener {
     private static final String TAG = "ZenModeSettings";
     private static final boolean DEBUG = true;
 
-    private static final String KEY_ZEN_MODE = "zen_mode";
     private static final String KEY_GENERAL = "general";
     private static final String KEY_CALLS = "phone_calls";
     private static final String KEY_MESSAGES = "messages";
@@ -80,7 +83,8 @@
     private final Handler mHandler = new Handler();
     private final SettingsObserver mSettingsObserver = new SettingsObserver();
 
-    private SwitchPreference mSwitch;
+    private SwitchBar mSwitchBar;
+    private Switch mSwitch;
     private Context mContext;
     private PackageManager mPM;
     private ZenModeConfig mConfig;
@@ -98,6 +102,26 @@
     private boolean mIgnoreNext;
 
     @Override
+    public void onSwitchChanged(Switch switchView, final boolean isChecked) {
+        if (DEBUG) Log.d(TAG, "onPreferenceChange isChecked=" + isChecked
+                + " mIgnoreNext=" + mIgnoreNext);
+        if (mIgnoreNext) {
+            mIgnoreNext = false;
+        }
+        AsyncTask.execute(new Runnable() {
+            @Override
+            public void run() {
+                final int v = isChecked ? Global.ZEN_MODE_ON : Global.ZEN_MODE_OFF;
+                putZenModeSetting(v);
+                final int n = ConditionProviderSettings.getEnabledProviderCount(mContext);
+                if (n > 0) {
+                    mHandler.post(isChecked ? mShowDialog : mHideDialog);
+                }
+            }
+        });
+    }
+
+    @Override
     public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         mContext = getActivity();
@@ -111,8 +135,8 @@
         mConfig = getZenModeConfig();
         if (DEBUG) Log.d(TAG, "Loaded mConfig=" + mConfig);
 
-        mSwitch = (SwitchPreference) root.findPreference(KEY_ZEN_MODE);
-        mSwitch.setOnPreferenceChangeListener(mSwitchListener);
+        mSwitchBar = ((SettingsActivity) mContext).getSwitchBar();
+        mSwitch = mSwitchBar.getSwitch();
 
         final PreferenceCategory general = (PreferenceCategory) root.findPreference(KEY_GENERAL);
 
@@ -324,12 +348,16 @@
         updateControls();
         updateZenMode();
         mSettingsObserver.register();
+        mSwitchBar.addOnSwitchChangeListener(this);
+        mSwitchBar.show();
     }
 
     @Override
     public void onPause() {
         super.onPause();
         mSettingsObserver.unregister();
+        mSwitchBar.removeOnSwitchChangeListener(this);
+        mSwitchBar.hide();
     }
 
     private void updateZenMode() {
@@ -339,7 +367,6 @@
             mSwitch.setChecked(zenMode);
             mIgnoreNext = true;
         }
-        mSwitch.setTitle(zenMode ? R.string.zen_mode_option_on : R.string.zen_mode_option_off);
     }
 
     private void updateZenModeConfig() {
@@ -417,31 +444,6 @@
         }
     };
 
-    private final OnPreferenceChangeListener mSwitchListener = new OnPreferenceChangeListener() {
-        @Override
-        public boolean onPreferenceChange(Preference preference, Object newValue) {
-            final boolean isChecked = (Boolean) newValue;
-            if (DEBUG) Log.d(TAG, "onPreferenceChange isChecked=" + isChecked
-                    + " mIgnoreNext=" + mIgnoreNext);
-            if (mIgnoreNext) {
-                mIgnoreNext = false;
-                return true;
-            }
-            AsyncTask.execute(new Runnable() {
-                @Override
-                public void run() {
-                    final int v = isChecked ? Global.ZEN_MODE_ON : Global.ZEN_MODE_OFF;
-                    putZenModeSetting(v);
-                    final int n = ConditionProviderSettings.getEnabledProviderCount(mContext);
-                    if (n > 0) {
-                        mHandler.post(isChecked ? mShowDialog : mHideDialog);
-                    }
-                }
-            });
-            return true;
-        }
-    };
-
     // Enable indexing of searchable data
     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
         new BaseSearchIndexProvider() {