Move switch preference into its own demo.

This avoids everything breaking on pre-ICS platforms.

Also get rid of pref initialization in Application. I hate that.  Hate
hate hate.  Hate.  Totally hate.

Totally.

Change-Id: Idb3526a96eb2dff49f9de8e5ae71149cb4ed6e96
diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml
index 2bda4a7..8157e9f 100644
--- a/samples/ApiDemos/AndroidManifest.xml
+++ b/samples/ApiDemos/AndroidManifest.xml
@@ -952,6 +952,15 @@
             </intent-filter>
         </activity>
 
+        <activity android:name=".preference.SwitchPreference"
+                android:label="@string/switch_preference"
+                android:enabled="@bool/atLeastIceCreamSandwich">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.SAMPLE_CODE" />
+            </intent-filter>
+        </activity>
+
         <!-- ************************************* -->
         <!--        CONTENT PACKAGE SAMPLES        -->
         <!-- ************************************* -->
diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml
index b1a57b0..a31ad8a 100644
--- a/samples/ApiDemos/res/values/strings.xml
+++ b/samples/ApiDemos/res/values/strings.xml
@@ -525,6 +525,7 @@
     <string name="advanced_preferences">Preference/6. Advanced preferences</string>
     <string name="fragment_preferences">Preference/7. Fragment</string>
     <string name="preference_with_headers">Preference/8. Headers</string>
+    <string name="switch_preference">Preference/9. Switch</string>
 
     <string name="launch_preference_activity">Launch PreferenceActivity</string>
     <string name="counter_value_is">The counter value is</string>
diff --git a/samples/ApiDemos/res/xml/default_values.xml b/samples/ApiDemos/res/xml/default_values.xml
index 5638c00..ef06c3e 100644
--- a/samples/ApiDemos/res/xml/default_values.xml
+++ b/samples/ApiDemos/res/xml/default_values.xml
@@ -25,12 +25,6 @@
             android:title="@string/title_checkbox_preference"
             android:summary="@string/summary_checkbox_preference" />
 
-    <SwitchPreference
-            android:key="default_switch"
-            android:defaultValue="false"
-            android:title="@string/title_switch_preference"
-            android:summary="@string/summary_switch_preference" />
-
     <EditTextPreference
             android:key="default_edittext"
             android:defaultValue="@string/default_value_edittext_preference"
diff --git a/samples/ApiDemos/res/xml/fragmented_preferences.xml b/samples/ApiDemos/res/xml/fragmented_preferences.xml
index da6f3f1..71cb21c 100644
--- a/samples/ApiDemos/res/xml/fragmented_preferences.xml
+++ b/samples/ApiDemos/res/xml/fragmented_preferences.xml
@@ -27,11 +27,6 @@
                 android:title="@string/title_checkbox_preference"
                 android:summary="@string/summary_checkbox_preference" />
 
-        <SwitchPreference
-                android:key="checkbox_preference"
-                android:title="@string/title_switch_preference"
-                android:summary="@string/summary_switch_preference" />
-
     </PreferenceCategory>
 
     <PreferenceCategory
diff --git a/samples/ApiDemos/res/xml/preference_switch b/samples/ApiDemos/res/xml/preference_switch
new file mode 100644
index 0000000..fceb3a1
--- /dev/null
+++ b/samples/ApiDemos/res/xml/preference_switch
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 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.
+-->
+
+<!-- This is a primitive example showing the different types of preferences available. -->
+<!-- BEGIN_INCLUDE(preferences) -->
+<PreferenceScreen
+        xmlns:android="http://schemas.android.com/apk/res/android">
+
+    <PreferenceCategory
+            android:title="@string/inline_preferences">
+            
+        <CheckBoxPreference
+                android:key="checkbox_preference"
+                android:title="@string/title_checkbox_preference"
+                android:summary="@string/summary_checkbox_preference" />
+
+        <SwitchPreference
+                android:key="checkbox_preference"
+                android:title="@string/title_switch_preference"
+                android:summary="@string/summary_switch_preference" />
+
+        <SwitchPreference
+                android:key="checkbox_preference"
+                android:title="@string/title_switch_preference"
+                android:summary="@string/summary_switch_preference_yes_no"
+                android:switchTextOn = "YES"
+                android:switchTextOff = "NO"
+                android:defaultValue="true" />
+            
+    </PreferenceCategory>
+    
+</PreferenceScreen>
+<!-- END_INCLUDE(preferences) -->
diff --git a/samples/ApiDemos/res/xml/preferences.xml b/samples/ApiDemos/res/xml/preferences.xml
index 3316aa5..6f3faee 100644
--- a/samples/ApiDemos/res/xml/preferences.xml
+++ b/samples/ApiDemos/res/xml/preferences.xml
@@ -27,18 +27,6 @@
                 android:title="@string/title_checkbox_preference"
                 android:summary="@string/summary_checkbox_preference" />
 
-        <SwitchPreference
-                android:key="checkbox_preference"
-                android:title="@string/title_switch_preference"
-                android:summary="@string/summary_switch_preference" />
-
-        <SwitchPreference
-                android:key="checkbox_preference"
-                android:title="@string/title_switch_preference"
-                android:summary="@string/summary_switch_preference_yes_no"
-                android:switchTextOn = "YES"
-                android:switchTextOff = "NO" />
-            
     </PreferenceCategory>
                 
     <PreferenceCategory
diff --git a/samples/ApiDemos/src/com/example/android/apis/ApiDemosApplication.java b/samples/ApiDemos/src/com/example/android/apis/ApiDemosApplication.java
index 5ed1714..4ad5d35 100644
--- a/samples/ApiDemos/src/com/example/android/apis/ApiDemosApplication.java
+++ b/samples/ApiDemos/src/com/example/android/apis/ApiDemosApplication.java
@@ -17,12 +17,12 @@
 package com.example.android.apis;
 
 import android.app.Application;
-import android.preference.PreferenceManager;
 
 /**
- * This is an example of a {@link android.app.Application} class.  Ordinarily you would use
- * a class like this as a central repository for information that might be shared between multiple
- * activities.
+ * This is an example of a {@link android.app.Application} class.  This can
+ * be used as a central repository for per-process information about your app;
+ * however it is recommended to use singletons for that instead rather than merge
+ * all of these globals from across your application into one place here.
  * 
  * In this case, we have not defined any specific work for this Application.
  * 
@@ -30,17 +30,7 @@
  * of how to perform unit tests on an Application object.
  */
 public class ApiDemosApplication extends Application {
-
     @Override
     public void onCreate() {
-        /*
-         * This populates the default values from the preferences XML file. See
-         * {@link DefaultValues} for more details.
-         */
-        PreferenceManager.setDefaultValues(this, R.xml.default_values, false);
-    }
-
-    @Override
-    public void onTerminate() {
     }
 }
diff --git a/samples/ApiDemos/src/com/example/android/apis/preference/DefaultValues.java b/samples/ApiDemos/src/com/example/android/apis/preference/DefaultValues.java
index 69cf499..84cd5b2 100644
--- a/samples/ApiDemos/src/com/example/android/apis/preference/DefaultValues.java
+++ b/samples/ApiDemos/src/com/example/android/apis/preference/DefaultValues.java
@@ -16,9 +16,9 @@
 
 package com.example.android.apis.preference;
 
-import com.example.android.apis.ApiDemosApplication;
 import com.example.android.apis.R;
 
+import android.content.Context;
 import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.preference.PreferenceActivity;
@@ -34,20 +34,28 @@
  * {@link PreferenceManager#setDefaultValues(android.content.Context, int, boolean)}.
  * <p>
  * This should be called early, typically when the application is first created.
- * This ensures any of the application's activities, services, etc. will have
- * the default values present, even if the user has not wandered into the
- * application's settings. For ApiDemos, this is {@link ApiDemosApplication},
- * and you can find the call to
- * {@link PreferenceManager#setDefaultValues(android.content.Context, int, boolean)}
- * in its {@link ApiDemosApplication#onCreate() onCreate}.
+ * An easy way to do this is to have a common function for retrieving the
+ * SharedPreferences that takes care of calling it.
  */
 public class DefaultValues extends PreferenceActivity {
+    // This is the global (to the .apk) name under which we store these
+    // preferences.  We want this to be unique from other preferences so that
+    // we do not have unexpected name conflicts, and the framework can correctly
+    // determine whether these preferences' defaults have already been written.
+    static final String PREFS_NAME = "defaults";
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
+        getPrefs(this);
+        getPreferenceManager().setSharedPreferencesName(PREFS_NAME);
         addPreferencesFromResource(R.xml.default_values);
     }
 
+    static SharedPreferences getPrefs(Context context) {
+        PreferenceManager.setDefaultValues(context, PREFS_NAME, MODE_PRIVATE,
+                R.xml.default_values, false);
+        return context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
+    }
 }
diff --git a/samples/ApiDemos/src/com/example/android/apis/preference/LaunchingPreferences.java b/samples/ApiDemos/src/com/example/android/apis/preference/LaunchingPreferences.java
index 066477b..573330c 100644
--- a/samples/ApiDemos/src/com/example/android/apis/preference/LaunchingPreferences.java
+++ b/samples/ApiDemos/src/com/example/android/apis/preference/LaunchingPreferences.java
@@ -44,12 +44,9 @@
         super.onCreate(savedInstanceState);
 
         /*
-         * If this were my app's main activity, I would load the default values
-         * so they're set even if the user does not go into the preferences
-         * screen. Another good place to call this method would be from a
-         * subclass of Application, so your default values would be loaded
-         * regardless of entry into your application (for example, a service or
-         * activity).
+         * These preferences have defaults, so before using them go apply those
+         * defaults.  This will only execute once -- when the defaults are applied
+         * a boolean preference is set so they will not be applied again.
          */
         PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false);
 
diff --git a/samples/ApiDemos/src/com/example/android/apis/preference/PreferenceWithHeaders.java b/samples/ApiDemos/src/com/example/android/apis/preference/PreferenceWithHeaders.java
index 6437e1e..b4dbdb4 100644
--- a/samples/ApiDemos/src/com/example/android/apis/preference/PreferenceWithHeaders.java
+++ b/samples/ApiDemos/src/com/example/android/apis/preference/PreferenceWithHeaders.java
@@ -21,6 +21,7 @@
 import android.os.Bundle;
 import android.preference.PreferenceActivity;
 import android.preference.PreferenceFragment;
+import android.preference.PreferenceManager;
 import android.util.Log;
 import android.widget.Button;
 
@@ -60,6 +61,12 @@
         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
 
+            // Make sure default values are applied.  In a real app, you would
+            // want this in a shared function that is used to retrieve the
+            // SharedPreferences wherever they are needed.
+            PreferenceManager.setDefaultValues(getActivity(),
+                    R.xml.advanced_preferences, false);
+
             // Load the preferences from an XML resource
             addPreferencesFromResource(R.xml.fragmented_preferences);
         }
diff --git a/samples/ApiDemos/src/com/example/android/apis/preference/SwitchPreference.java b/samples/ApiDemos/src/com/example/android/apis/preference/SwitchPreference.java
new file mode 100644
index 0000000..191c6ac
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/preference/SwitchPreference.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2007 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.example.android.apis.preference;
+
+import com.example.android.apis.R;
+
+import android.os.Bundle;
+import android.preference.PreferenceActivity;
+import android.preference.PreferenceManager;
+
+public class SwitchPreference extends PreferenceActivity {
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        PreferenceManager.setDefaultValues(this, "switch", MODE_PRIVATE,
+                R.xml.default_values, false);
+
+        // Load the preferences from an XML resource
+        getPreferenceManager().setSharedPreferencesName("switch");
+        addPreferencesFromResource(R.xml.preference_switch);
+    }
+}