Instantly share code, notes, and snippets.
Last active
October 27, 2015 20:38
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save 4gus71n/f120aee4f120e39da788 to your computer and use it in GitHub Desktop.
A multi-thread SharedPreference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MultiProcessShared extends ContentProvider { | |
public static String PREFERENCE_AUTHORITY; | |
public static Uri BASE_URI; | |
private static final String TYPE = "type"; | |
private static final String KEY = "key"; | |
private static final String INT_TYPE = "integer"; | |
private static final String LONG_TYPE = "long"; | |
private static final String FLOAT_TYPE = "float"; | |
private static final String BOOLEAN_TYPE = "boolean"; | |
private static final String STRING_TYPE = "string"; | |
private static final int MATCH_DATA = 0x010000; | |
private static UriMatcher matcher; | |
private static void init(Context context) { | |
PREFERENCE_AUTHORITY = "com.yourcompany.yourapp.PREFERENCE_AUTHORITY"; | |
matcher = new UriMatcher(UriMatcher.NO_MATCH); | |
matcher.addURI(PREFERENCE_AUTHORITY, "*/*", MATCH_DATA); | |
BASE_URI = Uri.parse("content://" + PREFERENCE_AUTHORITY); | |
} | |
@Override | |
public boolean onCreate() { | |
if (matcher == null) { | |
init(getContext()); | |
} | |
return true; | |
} | |
@Override | |
public String getType(Uri uri) { | |
return ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd." + PREFERENCE_AUTHORITY + ".item"; | |
} | |
@Override | |
public int delete(Uri uri, String selection, String[] selectionArgs) { | |
switch (matcher.match(uri)) { | |
case MATCH_DATA: | |
PreferenceManager.getDefaultSharedPreferences(getContext().getApplicationContext()) | |
.edit().clear().commit(); | |
break; | |
default: | |
throw new IllegalArgumentException("Unsupported uri " + uri); | |
} | |
return 0; | |
} | |
@SuppressLint("NewApi") | |
@Override | |
public Uri insert(Uri uri, ContentValues values) { | |
switch (matcher.match(uri)) { | |
case MATCH_DATA: | |
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(getContext().getApplicationContext()).edit(); | |
for (Entry<String, Object> entry : values.valueSet()) { | |
final Object value = entry.getValue(); | |
final String key = entry.getKey(); | |
if (value == null) { | |
editor.remove(key); | |
} else if (value instanceof String) | |
editor.putString(key, (String) value); | |
else if (value instanceof Boolean) | |
editor.putBoolean(key, (Boolean) value); | |
else if (value instanceof Long) | |
editor.putLong(key, (Long) value); | |
else if (value instanceof Integer) | |
editor.putInt(key, (Integer) value); | |
else if (value instanceof Float) | |
editor.putFloat(key, (Float) value); | |
else { | |
throw new IllegalArgumentException("Unsupported type " + uri); | |
} | |
} | |
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) { | |
editor.apply(); | |
} else { | |
editor.commit(); | |
} | |
break; | |
default: | |
throw new IllegalArgumentException("Unsupported uri " + uri); | |
} | |
return null; | |
} | |
@Override | |
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { | |
MatrixCursor cursor = null; | |
switch (matcher.match(uri)) { | |
case MATCH_DATA: | |
final String key = uri.getPathSegments().get(0); | |
final String type = uri.getPathSegments().get(1); | |
cursor = new MatrixCursor(new String[]{key}); | |
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext().getApplicationContext()); | |
if (!sharedPreferences.contains(key)) | |
return cursor; | |
MatrixCursor.RowBuilder rowBuilder = cursor.newRow(); | |
Object object = null; | |
if (STRING_TYPE.equals(type)) { | |
object = sharedPreferences.getString(key, null); | |
} else if (BOOLEAN_TYPE.equals(type)) { | |
object = sharedPreferences.getBoolean(key, false) ? 1 : 0; | |
} else if (LONG_TYPE.equals(type)) { | |
object = sharedPreferences.getLong(key, 0l); | |
} else if (INT_TYPE.equals(type)) { | |
object = sharedPreferences.getInt(key, 0); | |
} else if (FLOAT_TYPE.equals(type)) { | |
object = sharedPreferences.getFloat(key, 0f); | |
} else { | |
throw new IllegalArgumentException("Unsupported type " + uri); | |
} | |
rowBuilder.add(object); | |
break; | |
default: | |
throw new IllegalArgumentException("Unsupported uri " + uri); | |
} | |
return cursor; | |
} | |
@Override | |
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { | |
throw new UnsupportedOperationException(); | |
} | |
private static String getStringValue(Cursor cursor, String def) { | |
if (cursor == null) | |
return def; | |
String value = def; | |
if (cursor.moveToFirst()) { | |
value = cursor.getString(0); | |
} | |
cursor.close(); | |
return value; | |
} | |
private static boolean getBooleanValue(Cursor cursor, boolean def) { | |
if (cursor == null) | |
return def; | |
boolean value = def; | |
if (cursor.moveToFirst()) { | |
value = cursor.getInt(0) > 0; | |
} | |
cursor.close(); | |
return value; | |
} | |
private static int getIntValue(Cursor cursor, int def) { | |
if (cursor == null) | |
return def; | |
int value = def; | |
if (cursor.moveToFirst()) { | |
value = cursor.getInt(0); | |
} | |
cursor.close(); | |
return value; | |
} | |
private static long getLongValue(Cursor cursor, long def) { | |
if (cursor == null) | |
return def; | |
long value = def; | |
if (cursor.moveToFirst()) { | |
value = cursor.getLong(0); | |
} | |
cursor.close(); | |
return value; | |
} | |
private static float getFloatValue(Cursor cursor, float def) { | |
if (cursor == null) | |
return def; | |
float value = def; | |
if (cursor.moveToFirst()) { | |
value = cursor.getFloat(0); | |
} | |
cursor.close(); | |
return value; | |
} | |
public static Editor edit(Context context) { | |
return new Editor(context); | |
} | |
public static MultiProcessSharedPreferences getDefaultSharedPreferences(Context context) { | |
return new MultiProcessSharedPreferences(context); | |
} | |
public static class Editor { | |
Context context; | |
private Editor(Context context) { | |
this.context = context; | |
} | |
private ContentValues values = new ContentValues(); | |
public void apply() { | |
context.getContentResolver().insert(getContentUri(context, KEY, TYPE), values); | |
} | |
public void commit() { | |
apply(); | |
} | |
public void putString(String key, String value) { | |
values.put(key, value); | |
commit(); | |
} | |
public void putLong(String key, long value) { | |
values.put(key, value); | |
commit(); | |
} | |
public void putBoolean(String key, boolean value) { | |
values.put(key, value); | |
commit(); | |
} | |
public void putInt(String key, int value) { | |
values.put(key, value); | |
commit(); | |
} | |
public void putFloat(String key, float value) { | |
values.put(key, value); | |
commit(); | |
} | |
/** | |
* Call content provider method immediately. apply or commit is not required for this case | |
* So it's sync method. | |
*/ | |
public void clear() { | |
context.getContentResolver().delete(getContentUri(context, KEY, TYPE), null, null); | |
} | |
} | |
public static class MultiProcessSharedPreferences { | |
private Context context; | |
private MultiProcessSharedPreferences(Context context) { | |
this.context = context; | |
} | |
public Editor edit() { | |
return new Editor(context); | |
} | |
public String getString(String key, String def) { | |
Cursor cursor = context.getContentResolver().query(getContentUri(context, key, STRING_TYPE), null, null, null, null); | |
return getStringValue(cursor, def); | |
} | |
public long getLong(String key, long def) { | |
Cursor cursor = context.getContentResolver().query(getContentUri(context, key, LONG_TYPE), null, null, null, null); | |
return getLongValue(cursor, def); | |
} | |
public float getFloat(String key, float def) { | |
Cursor cursor = context.getContentResolver().query(getContentUri(context, key, FLOAT_TYPE), null, null, null, null); | |
return getFloatValue(cursor, def); | |
} | |
public boolean getBoolean(String key, boolean def) { | |
Cursor cursor = context.getContentResolver().query(getContentUri(context, key, BOOLEAN_TYPE), null, null, null, null); | |
return getBooleanValue(cursor, def); | |
} | |
public int getInt(String key, int def) { | |
Cursor cursor = context.getContentResolver().query(getContentUri(context, key, INT_TYPE), null, null, null, null); | |
return getIntValue(cursor, def); | |
} | |
public Float getFloat(String key) { | |
return getFloat(key, SingleProcessShared.RESPONSE_NEGATIVE_FLOAT); | |
} | |
public Long getLong(String key) { | |
return getLong(key, SingleProcessShared.RESPONSE_NEGATIVE_LONG); | |
} | |
public Integer getInt(String key) { | |
return getInt(key, SingleProcessShared.RESPONSE_NEGATIVE_INTEGER); | |
} | |
public Boolean getBoolean(String key) { | |
return getBoolean(key, SingleProcessShared.RESPONSE_NEGATIVE_BOOLEAN); | |
} | |
public String getString(String key) { | |
return getString(key, SingleProcessShared.RESPONSE_NEGATIVE_STRING); | |
} | |
public boolean hasLong(String label) { | |
return getLong(label) != SingleProcessShared.RESPONSE_NEGATIVE_LONG; | |
} | |
public boolean hasFloat(String label) { | |
return getFloat(label) != SingleProcessShared.RESPONSE_NEGATIVE_FLOAT; | |
} | |
public boolean hasInteger(String label) { | |
return getInt(label) != SingleProcessShared.RESPONSE_NEGATIVE_INTEGER; | |
} | |
public boolean hasString(String label) { | |
return getString(label) != SingleProcessShared.RESPONSE_NEGATIVE_STRING; | |
} | |
public boolean hasBoolean(String label) { | |
return getBoolean(label) != SingleProcessShared.RESPONSE_NEGATIVE_BOOLEAN; | |
} | |
public void removeBoolean(String label) { | |
edit().putBoolean(label, SingleProcessShared.RESPONSE_NEGATIVE_BOOLEAN); | |
edit().commit(); | |
} | |
public void removeInteger(String label) { | |
edit().putInt(label, SingleProcessShared.RESPONSE_NEGATIVE_INTEGER); | |
edit().commit(); | |
} | |
public void removeFloat(String label) { | |
edit().putFloat(label, SingleProcessShared.RESPONSE_NEGATIVE_FLOAT); | |
edit().commit(); | |
} | |
public void removeString(String label) { | |
edit().putString(label, SingleProcessShared.RESPONSE_NEGATIVE_STRING); | |
edit().commit(); | |
} | |
public void removeLong(String label) { | |
edit().putLong(label, SingleProcessShared.RESPONSE_NEGATIVE_LONG); | |
edit().commit(); | |
} | |
} | |
private static final Uri getContentUri(Context context, String key, String type) { | |
if (BASE_URI == null) { | |
init(context); | |
} | |
return BASE_URI.buildUpon().appendPath(key).appendPath(type).build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment