Last active
December 20, 2015 16:59
-
-
Save angelix/6165603 to your computer and use it in GitHub Desktop.
ManagedDataHelper is a Helper class that gives you the ability to override strings (or array of strings) saved on Android Resources (xml) with data from a remote JSON document.
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 ManagedDataHelper { | |
static ManagedDataHelper sSelf = null; | |
static final String SHARED_PREFERENCES_FILENAME = "managed_data"; | |
Context mContext; | |
Resources mResources; | |
SharedPreferences mSharedPreferences; | |
private ManagedDataHelper(Context context) { | |
mContext = context; | |
mResources = mContext.getResources(); | |
mSharedPreferences = mContext.getSharedPreferences( | |
SHARED_PREFERENCES_FILENAME, 0); | |
} | |
public static ManagedDataHelper factory(Context context) { | |
if (sSelf == null) { | |
sSelf = new ManagedDataHelper(context); | |
} | |
return sSelf; | |
} | |
public String getString(String key) { | |
String value = null; | |
boolean hasKey = mSharedPreferences.contains(key); | |
if (hasKey) { | |
value = mSharedPreferences.getString(key, null); | |
if (value != null) { | |
return value; | |
} | |
} | |
return value; | |
} | |
public String getString(String key, String defVal) { | |
String value = getString(key); | |
if (value != null) { | |
return value; | |
} else { | |
return defVal; | |
} | |
} | |
/* | |
* If resourceID is zero 0 then we try to find the string resource with the | |
* same name, it is better to use resourceID for speed reasons | |
*/ | |
public String getString(String key, int resourceID) { | |
String value = getString(key); | |
if (value == null) { | |
if (resourceID == 0) { | |
// try to find the resID from key name | |
resourceID = mResources.getIdentifier(key, "string", | |
mContext.getPackageName()); | |
} | |
if (resourceID > 0) { | |
value = mResources.getString(resourceID); | |
} | |
} | |
return value; | |
} | |
public String[] getStringArray(String key) { | |
String[] values = null; | |
boolean hasKey = mSharedPreferences.contains(key); | |
if (hasKey) { | |
String json = mSharedPreferences.getString(key, null); | |
if (json != null) { | |
try { | |
JSONArray a = new JSONArray(json); | |
values = new String[a.length()]; | |
for (int i = 0; i < a.length(); i++) { | |
values[i] = a.optString(i); | |
} | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
return values; | |
} | |
/* | |
* If resourceID is zero 0 then we try to find the string resource with the | |
* same name, it is better to use resourceID for speed reasons | |
*/ | |
public String[] getStringArray(String key, int resourceID) { | |
String[] values = getStringArray(key); | |
if (values == null) { | |
if (resourceID == 0) { | |
// try to find the resID from key name | |
resourceID = mResources.getIdentifier(key, "string", | |
mContext.getPackageName()); | |
} | |
if (resourceID > 0) { | |
values = mResources.getStringArray(resourceID); | |
} | |
} | |
return values; | |
} | |
public void updateData(JSONObject json) { | |
Editor editor = mSharedPreferences.edit(); | |
if (json != null) { | |
Iterator<String> it = json.keys(); | |
String key = null; | |
while (it.hasNext()) { | |
String value = null; | |
key = it.next(); | |
// if value is null then remove the key from xml | |
if (json.isNull(key)) { | |
editor.remove(key); | |
} else { | |
// try to see if it is an array of strings | |
try { | |
JSONArray jsonArray = json.getJSONArray(key); | |
editor.putString(key, jsonArray.toString()); | |
} catch (Exception e) { | |
value = null; | |
} | |
if (value == null) { | |
try { | |
value = json.getString(key); | |
if (value != null) { | |
editor.putString(key, value); | |
} | |
} catch (Exception e) { | |
value = null; | |
} | |
} | |
} | |
} | |
editor.apply(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment