How to manage your Android Application Settings

Standard

Here is my best practice for managing your Android application settings using AppSettings class. by using this class you can save your different variables in shared preferences and also load them to ram when ever needed and use them in memory.

String methods sample:

AppSettings.setValue(this, KEY_NAME, name);
AppSettings.getString(this, KEY_NAME, "");
AppSettings.clearValue(this, KEY_NAME);

Source Code:

package com.mirhoseini.appsettings;

import android.content.Context;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.util.Log;

import java.util.HashMap;

public class AppSettings {
    private final static String tag = AppSettings.class.getSimpleName();

    // Boolean Values
    public static HashMap<String, Boolean> booleanValues;
    // Float Values
    public static HashMap<String, Float> floatValues;
    // Long Values
    public static HashMap<String, Long> longValues;
    // String Values
    public static HashMap<String, String> stringValues;
    // Integer Values
    public static HashMap<String, Integer> intValues;

    public static Boolean getBoolean(Context context, String key) {
        return getBoolean(context, key, null);
    }

    public static Boolean getBoolean(Context context, String key,
                                     Boolean defaultValue) {
        Log.i(tag, "Get " + key);
        if (booleanValues == null)
            booleanValues = new HashMap<String, Boolean>();

        if (!booleanValues.containsKey(key))
            booleanValues.put(key,
                    PreferenceManager.getDefaultSharedPreferences(context)
                            .getBoolean(key, defaultValue));

        return booleanValues.get(key);
    }

    public static void setValue(Context context, String key, Boolean value) {
        Log.i(tag, "Set " + key + ":" + value);

        if (booleanValues == null)
            booleanValues = new HashMap<String, Boolean>();

        Editor editor = PreferenceManager.getDefaultSharedPreferences(context)
                .edit();
        editor.putBoolean(key, value);
        editor.commit();

        booleanValues.put(key, value);
    }

    public static Float getFloat(Context context, String key) {
        return getFloat(context, key, null);
    }

    public static Float getFloat(Context context, String key, Float defaultValue) {
        Log.i(tag, "Get " + key);
        if (floatValues == null)
            floatValues = new HashMap<String, Float>();

        if (!floatValues.containsKey(key))
            floatValues.put(key,
                    PreferenceManager.getDefaultSharedPreferences(context)
                            .getFloat(key, defaultValue));

        return floatValues.get(key);
    }

    public static void setValue(Context context, String key, Float value) {
        Log.i(tag, "Set " + key + ":" + value);

        if (floatValues == null)
            floatValues = new HashMap<String, Float>();

        Editor editor = PreferenceManager.getDefaultSharedPreferences(context)
                .edit();
        editor.putFloat(key, value);
        editor.commit();

        floatValues.put(key, value);
    }

    public static Long getLong(Context context, String key) {
        return getLong(context, key, null);
    }

    public static Long getLong(Context context, String key, Long defaultValue) {
        Log.i(tag, "Get " + key);
        if (longValues == null)
            longValues = new HashMap<String, Long>();

        if (!longValues.containsKey(key))
            longValues.put(key,
                    PreferenceManager.getDefaultSharedPreferences(context)
                            .getLong(key, defaultValue));

        return longValues.get(key);
    }

    public static void setValue(Context context, String key, Long value) {
        Log.i(tag, "Set " + key + ":" + value);

        if (longValues == null)
            longValues = new HashMap<String, Long>();

        Editor editor = PreferenceManager.getDefaultSharedPreferences(context)
                .edit();
        editor.putLong(key, value);
        editor.commit();

        longValues.put(key, value);
    }

    public static String getString(Context context, String key) {
        return getString(context, key, null);
    }

    public static String getString(Context context, String key,
                                   String defaultValue) {
        Log.i(tag, "Get " + key);
        if (stringValues == null)
            stringValues = new HashMap<String, String>();

        if (!stringValues.containsKey(key))
            stringValues.put(key,
                    PreferenceManager.getDefaultSharedPreferences(context)
                            .getString(key, defaultValue));

        return stringValues.get(key);
    }

    public static void setValue(Context context, String key, String value) {
        Log.i(tag, "Set " + key + ":" + value);

        if (stringValues == null)
            stringValues = new HashMap<String, String>();

        Editor editor = PreferenceManager.getDefaultSharedPreferences(context)
                .edit();
        editor.putString(key, value);
        editor.commit();

        stringValues.put(key, value);
    }

    public static Integer getInt(Context context, String key) {
        return getInt(context, key, null);
    }

    public static Integer getInt(Context context, String key,
                                 Integer defaultValue) {
        Log.i(tag, "Get " + key);
        if (intValues == null)
            intValues = new HashMap<String, Integer>();

        if (!intValues.containsKey(key))
            intValues.put(key,
                    PreferenceManager.getDefaultSharedPreferences(context)
                            .getInt(key, defaultValue));

        return intValues.get(key);
    }

    public static void setValue(Context context, String key, Integer value) {
        Log.i(tag, "Set " + key + ":" + value);

        if (intValues == null)
            intValues = new HashMap<String, Integer>();

        Editor editor = PreferenceManager.getDefaultSharedPreferences(context)
                .edit();
        editor.putInt(key, value);
        editor.commit();

        intValues.put(key, value);
    }

    public static void clearValue(Context context, String key) {
        Log.i(tag, "Clear " + key);

        if (intValues == null)
            intValues = new HashMap<String, Integer>();

        if (booleanValues == null)
            booleanValues = new HashMap<String, Boolean>();

        if (floatValues == null)
            floatValues = new HashMap<String, Float>();

        if (stringValues == null)
            stringValues = new HashMap<String, String>();

        if (longValues == null)
            longValues = new HashMap<String, Long>();

        Editor editor = PreferenceManager.getDefaultSharedPreferences(context)
                .edit();
        editor.remove(key);
        editor.commit();

        intValues.remove(key);
        booleanValues.remove(key);
        floatValues.remove(key);
        stringValues.remove(key);
        longValues.remove(key);
    }

}

 

Sample project available at GitHub:
http://github.com/mirhoseini/app_settings

Leave a Reply

Your email address will not be published. Required fields are marked *