From 2fd75c63684e4ada9e00e1d31304519e60f5a22a Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Mon, 14 Nov 2011 14:28:42 -0800 Subject: [PATCH] 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 --- samples/ApiDemos/AndroidManifest.xml | 9 ++++ samples/ApiDemos/res/values/strings.xml | 1 + samples/ApiDemos/res/xml/default_values.xml | 6 --- .../res/xml/fragmented_preferences.xml | 5 -- samples/ApiDemos/res/xml/preference_switch | 46 +++++++++++++++++++ samples/ApiDemos/res/xml/preferences.xml | 12 ----- .../android/apis/ApiDemosApplication.java | 18 ++------ .../apis/preference/DefaultValues.java | 22 ++++++--- .../apis/preference/LaunchingPreferences.java | 9 ++-- .../preference/PreferenceWithHeaders.java | 7 +++ .../apis/preference/SwitchPreference.java | 38 +++++++++++++++ 11 files changed, 123 insertions(+), 50 deletions(-) create mode 100644 samples/ApiDemos/res/xml/preference_switch create mode 100644 samples/ApiDemos/src/com/example/android/apis/preference/SwitchPreference.java diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml index 2bda4a747..8157e9fab 100644 --- a/samples/ApiDemos/AndroidManifest.xml +++ b/samples/ApiDemos/AndroidManifest.xml @@ -952,6 +952,15 @@ + + + + + + + diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml index b1a57b0c5..a31ad8a8f 100644 --- a/samples/ApiDemos/res/values/strings.xml +++ b/samples/ApiDemos/res/values/strings.xml @@ -525,6 +525,7 @@ Preference/6. Advanced preferences Preference/7. Fragment Preference/8. Headers + Preference/9. Switch Launch PreferenceActivity The counter value is diff --git a/samples/ApiDemos/res/xml/default_values.xml b/samples/ApiDemos/res/xml/default_values.xml index 5638c0047..ef06c3eaf 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" /> - - - - + + + + + + + + + + + + + + + + + + diff --git a/samples/ApiDemos/res/xml/preferences.xml b/samples/ApiDemos/res/xml/preferences.xml index 3316aa585..6f3faee08 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" /> - - - - * 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 066477b1a..573330c96 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 @@ public class LaunchingPreferences extends Activity implements OnClickListener { 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 6437e1ebf..b4dbdb41e 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 com.example.android.apis.R; 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 class PreferenceWithHeaders extends PreferenceActivity { 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 000000000..191c6ac7e --- /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); + } +}