Android Open Source - GreenGear Droid Prefs






From Project

Back to project page GreenGear.

License

The source code is released under:

MIT License

If you think the Android project GreenGear listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bradleycurran.droidprefs;
//w ww  . ja  va 2s  . c  o  m
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import com.google.gson.Gson;

public class DroidPrefs {

    public static boolean contains(Context context, String key) {
        return getDefaultInstance(context).contains(key);
    }

    public static <T> T get(Context context, String key, Class<T> cls) {
        return getDefaultInstance(context).get(key, cls);
    }

    public static void apply(Context context, String key, Object value) {
        getDefaultInstance(context).apply(key, value);
    }

    public static void commit(Context context, String key, Object value) {
        getDefaultInstance(context).commit(key, value);
    }

    private static DroidPrefs getDefaultInstance(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return new DroidPrefs(sp);
    }

    private Gson              mGson;

    private SharedPreferences mSharedPrefs;

    public DroidPrefs(SharedPreferences sharedPrefs) {
        mGson = new Gson();
        mSharedPrefs = sharedPrefs;
    }

    public boolean contains(String key) {
        return mSharedPrefs.contains(key);
    }

    public <T> T get(String key, Class<T> cls) {
        if (contains(key)) {
            return mGson.fromJson(mSharedPrefs.getString(key, null), cls);
        }

        try {
            return cls.newInstance();
        }
        catch (Exception e) {
            throw new IllegalArgumentException("class must have an empty constructor");
        }
    }

    public void apply(String key, Object value) {
        put(key, value).apply();
    }

    public void commit(String key, Object value) {
        put(key, value).commit();
    }

    @SuppressLint("CommitPrefEdits")
    private Editor put(String key, Object value) {
        Editor e = mSharedPrefs.edit();
        e.putString(key, mGson.toJson(value));
        return e;
    }
}




Java Source Code List

com.bradleycurran.droidprefs.DroidPrefs.java
com.bradleycurran.greengear.model.KeyValue.java
com.bradleycurran.greengear.util.AssetUtil.java
com.bradleycurran.greengear.util.Log.java
com.bradleycurran.greengear.util.ViewUtil.java
com.bradleycurran.velocadapter.ArrayVelocAdapter.java
com.bradleycurran.velocadapter.VelocAdapter.java
com.bradleycurran.velocadapter.ViewBinder.java
com.bradleycurran.viewquery.Operation.java
com.bradleycurran.viewquery.ViewQuery.java
com.bradleycurran.viewquery.animation.hc.BounceTouchListener.java