Skip to content

Commit

Permalink
[Mod]: Changed method of setting pref defaults
Browse files Browse the repository at this point in the history
Taken from the project CareerStack, its an easier way to maintain
preference defaults
  • Loading branch information
iamovrhere committed Nov 4, 2014
1 parent 0a85f32 commit 0b59ba7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 38 deletions.
4 changes: 2 additions & 2 deletions res/values/preference_info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
limitations under the License.
-->
<resources>
<!-- Version 0.3.1 -->
<!-- Version 0.3.2 -->
<eat-comment />
<!-- The string used as the preference file name. -->
<string name="currConv_PREFERENCE_FILE_KEY" >
<string name="preferenceutil_PREFERENCE_FILE_KEY" >
com_ovrhere_currConv_PREFERENCE_FILE_KEY
</string> <!-- Cannot change internal values -->

Expand Down
37 changes: 37 additions & 0 deletions res/xml/preference_defaults.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2014 Jason J.
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.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<Preference
android:key="@string/currConv_pref_KEY_SOURCE_CURRENCY_INDEX"
android:defaultValue="@integer/currConv_pref_DEF_VALUE_SOURCE_CURRENCY_INDEX"/>

<Preference
android:key="@string/currConv_pref_KEY_DEST_CURRENCY_INDEX"
android:defaultValue="@integer/currConv_pref_DEF_VALUE_DEST_CURRENCY_INDEX"/>

<Preference
android:key="@string/currConv_pref_KEY_UPDATE_CURRENCY_INTERVAL"
android:defaultValue="@integer/currConv_pref_DEF_VALUE_UPDATE_CURRENCY_INTERVAL"/>

<CheckBoxPreference
android:key="@string/currConv_pref_KEY_USE_JSON_REQUEST"
android:defaultValue="@bool/currConv_pref_DEF_VALUE_USE_JSON_REQUEST"
/>

</PreferenceScreen>

Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;

import com.ovrhere.android.currencyconverter.R;

/**
* Preference Utility for handling the preferences and the preference container.
* Has ability to set defaults.
* Has ability to set defaults. Requires <code>preference_info.xml</code> and
* <code>preference_defaults.xml</code>.
* @author Jason J.
* @version 0.4.0-20140929
* @version 0.5.0-20141104
*/
public class PreferenceUtils {
/* The class name. */
Expand All @@ -43,6 +44,9 @@ public class PreferenceUtils {
*/
static public boolean isFirstRun(Context context){
SharedPreferences prefs = getPreferences(context);
//set any missing preferences if not set, ignores the rest
_setDefaults(context);

//if the default value not set, then true.
return (prefs.getBoolean(KEY_PREFERENCES_SET, !VALUE_PREFERENCES_SET)
== !VALUE_PREFERENCES_SET);
Expand All @@ -54,50 +58,40 @@ static public SharedPreferences getPreferences(Context context){
/* This is safe as SharedPreferences is a shared instance for the application
* and thus will not leak. */
context = context.getApplicationContext();

return context.getSharedPreferences(
context.getResources().getString(R.string.currConv_PREFERENCE_FILE_KEY),
context.getResources().getString(R.string.preferenceutil_PREFERENCE_FILE_KEY),
Context.MODE_PRIVATE);
}

/** Sets the application's preferences using the default values.
/** Resets application's preferences to the default values.
* @param context The current context to be used.
* @see res/values/preferences_info.xml */
static public void setToDefault(Context context){
SharedPreferences.Editor prefs = getPreferences(context).edit();
Resources r = context.getResources();
_setDefaults(r, prefs);
prefs.commit();
prefs.clear().commit();
_setDefaults(context.getApplicationContext());

//first run has completed.
prefs .putBoolean(KEY_PREFERENCES_SET, VALUE_PREFERENCES_SET)
.commit();
}

/////////////////////////////////////////////////////////////////////////////////////////////////
/// Utility functions
////////////////////////////////////////////////////////////////////////////////////////////////

/** Sets defaults. Does not commit.
* @param r The {@link Resources} manager to use getting strings from
* res/values/preferences_info.xml
* @param prefEdit The {@link SharedPreferences} editor to use to commit. */
static private void _setDefaults(Resources r, SharedPreferences.Editor prefEdit){
//Thought: Consider using parallel arrays in res to respect open-close?
prefEdit.putInt(
r.getString(R.string.currConv_pref_KEY_SOURCE_CURRENCY_INDEX),
r.getInteger(R.integer.currConv_pref_DEF_VALUE_SOURCE_CURRENCY_INDEX)
);
prefEdit.putInt(
r.getString(R.string.currConv_pref_KEY_DEST_CURRENCY_INDEX),
r.getInteger(R.integer.currConv_pref_DEF_VALUE_DEST_CURRENCY_INDEX)
);
prefEdit.putInt(
r.getString(R.string.currConv_pref_KEY_UPDATE_CURRENCY_INTERVAL),
r.getInteger(R.integer.currConv_pref_DEF_VALUE_UPDATE_CURRENCY_INTERVAL)
);

prefEdit.putBoolean(
r.getString(R.string.currConv_pref_KEY_USE_JSON_REQUEST),
r.getBoolean(R.bool.currConv_pref_DEF_VALUE_USE_JSON_REQUEST)
);

//first run has completed.
prefEdit.putBoolean(KEY_PREFERENCES_SET, VALUE_PREFERENCES_SET);
}
/** Sets defaults. Requires R.xml.preference_defaults xml file
* Note: does not overwrite them; must be cleared first.
* @param context The current context */
static private void _setDefaults(Context context){
PreferenceManager.setDefaultValues(
context,
context.getResources().getString(
R.string.preferenceutil_PREFERENCE_FILE_KEY),
Context.MODE_PRIVATE,
R.xml.preference_defaults,
true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class SettingsFragment extends PreferenceFragment
public void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
getPreferenceManager().setSharedPreferencesName(
getString(R.string.currConv_PREFERENCE_FILE_KEY));
getString(R.string.preferenceutil_PREFERENCE_FILE_KEY));

prefs = getPreferenceManager().getSharedPreferences();
tm = new ToastManager(getActivity());
Expand Down

0 comments on commit 0b59ba7

Please sign in to comment.