Monday, April 15, 2013

[android help] Best practice to store keys associated with a class


I want to share what I did for preferences storage, I use android's build in feature called SharedPreferences. For easy access, I create a wrapper around SharedPreferences called UserModel that have codes like this:



package tv.gsgw.android.garusuta.model;

import id.flwi.util.ActivityUtil;
import tv.gsgw.android.garusuta.constant.DataConstant;
import android.content.Context;

public class UserModel implements DataConstant {
Context _context;

private String authKey = "";
private String email = "";
private String password = "";
private String birthdate = "";
private String prefacture = "";
private String bloodType = "";
private String idealBodyType = "";
private String duration = "";
private String stability = "";
private String skin = "";
private String relationship = "";
private String height = "";
private String weight = "";
private boolean registerDataChange = false;

public UserModel(Context context){
this._context = context;
loadFromPreferences();
}

public void loadFromPreferences(){
setAuthKey(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_AUTHKEY, ""));
setEmail(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_EMAIL, ""));
setPassword(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_PASSWORD, ""));
setBirthdate(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_BIRTHDATE, ""));
setPrefacture(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_PREFACTURE, ""));
setBloodType(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_BLOODTYPE, ""));
setIdealBodyType(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_BODYTYPE, ""));
setPeriod(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_DURATIONPERIOD, ""));
setStability(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_STABILITY, ""));
setSkin(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_SKINTYPE, ""));
setRelationship(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_RELATIONSHIP, ""));
setHeight(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_HEIGHT, ""));
setWeight(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_WEIGHT, ""));
setRegisterDataChange(ActivityUtil.getSharedPreferenceBoolean(_context, USER_DATA_FIELD_REGISTER_DATACHANGE, false));
}

public void saveIntoPreferences(){
saveIntoPreferences(true);
}
public void saveIntoPreferences(boolean datachanged){
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_AUTHKEY, getAuthKey());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_EMAIL, getEmail());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_PASSWORD, getPassword());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_BIRTHDATE, getBirthdate());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_PREFACTURE, getPrefacture());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_BLOODTYPE, getBloodType());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_BODYTYPE, getIdealBodyType());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_DURATIONPERIOD, getPeriod());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_STABILITY, getStability());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_SKINTYPE, getSkin());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_RELATIONSHIP, getRelationship());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_HEIGHT, getHeight());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_WEIGHT, getWeight());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_REGISTER_DATACHANGE, datachanged);
}

public String getAuthKey() {
return authKey;
}

public void setAuthKey(String authKey) {
this.authKey = authKey;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getBirthdate() {
return birthdate;
}

public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
}

public String getPrefacture() {
return prefacture;
}

public void setPrefacture(String prefacture) {
this.prefacture = prefacture;
}

public String getBloodType() {
return bloodType;
}

public void setBloodType(String bloodType) {
this.bloodType = bloodType;
}

public String getIdealBodyType() {
return idealBodyType;
}

public void setIdealBodyType(String idealBodyType) {
this.idealBodyType = idealBodyType;
}

public String getPeriod() {
return duration;
}

public void setPeriod(String period) {
this.duration = period;
}

public String getSkin() {
return skin;
}

public void setSkin(String skin) {
this.skin = skin;
}

public String getRelationship() {
return relationship;
}

public void setRelationship(String relationship) {
this.relationship = relationship;
}

public String getHeight() {
return height;
}

public void setHeight(String height) {
this.height = height;
}

public String getWeight() {
return weight;
}

public void setWeight(String weight) {
this.weight = weight;
}

public boolean isRegisterDataChange() {
return registerDataChange;
}

public void setRegisterDataChange(boolean registerDataChange) {
this.registerDataChange = registerDataChange;
}

public String getStability() {
return stability;
}

public void setStability(String stability) {
this.stability = stability;
}

public void setRegistrationChanged(boolean dataChange) {
this.registerDataChange = dataChange;
}

public String toString(){
String str = "";

str += "authKey: " + authKey + "\n";
str += "email: " + email + "\n";
str += "password: " + password + "\n";
str += "birthdate: " + birthdate + "\n";
str += "prefacture: " + prefacture + "\n";
str += "bloodType: " + bloodType + "\n";
str += "idealBodyType: " + idealBodyType + "\n";
str += "duration: " + duration + "\n";
str += "stability: " + stability + "\n";
str += "skin: " + skin + "\n";
str += "relationship: " + relationship + "\n";
str += "height: " + height + "\n";
str += "weight: " + weight + "\n";
str += "registerDataChange: " + (registerDataChange ? "true" : "false") + "\n";

return str;
}

}


DataConstant in code above is just a class that contain constant variable like this:



public static final String USER_DATA_FIELD_NAME_AUTHKEY = "new_AuthKey";
public static final String USER_DATA_FIELD_NAME_EMAIL = "new_Email";
public static final String USER_DATA_FIELD_NAME_PASSWORD = "new_Password";
public static final String USER_DATA_FIELD_NAME_BIRTHDATE = "new_Birthday";
public static final String USER_DATA_FIELD_NAME_PREFACTURE = "new_Prefacture";
public static final String USER_DATA_FIELD_NAME_BLOODTYPE = "new_BloodType";
public static final String USER_DATA_FIELD_NAME_BODYTYPE = "new_IdealBodyType";
public static final String USER_DATA_FIELD_NAME_DURATIONPERIOD = "new_Duration";
public static final String USER_DATA_FIELD_NAME_STABILITY = "new_Stability";
public static final String USER_DATA_FIELD_NAME_SKINTYPE = "new_Skin";
public static final String USER_DATA_FIELD_NAME_RELATIONSHIP = "new_Relationship";
public static final String USER_DATA_FIELD_NAME_HEIGHT = "new_Height";
public static final String USER_DATA_FIELD_NAME_WEIGHT = "new_Weight";
public static final String USER_DATA_FIELD_NAME_PHY_1 = "new_phy1";
public static final String USER_DATA_FIELD_NAME_PHY_2 = "new_phy2";


This use an utility class that also create to help working with SharedPreferences easier:



package id.flwi.util;

/**
* @author Arief Bayu Purwanto
*/
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Calendar;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

public class ActivityUtil {
public static final String PREFS_NAME = "____MyPrefsFile";

public static void debugExtra(Bundle extras){
if( extras != null) {
Log.i("Log", "printing all extras information");
java.util.Set keys = extras.keySet();
java.util.Iterator keyIterator = keys.iterator();
int index = 0;
while(keyIterator.hasNext()) {
Log.i("log", " extras #" + (++index) + ": " + keyIterator.next());
}
} else {
Log.i("Log", "empty extras");
}
}

public static boolean getSharedPreferenceBoolean(Context c, String preference){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
return settings.getBoolean(preference, false);
}
public static boolean getSharedPreferenceBoolean(Context c, String preference, boolean defaultValue){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
return settings.getBoolean(preference, defaultValue);
}
public static String getSharedPreferenceString(Context c, String preference){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
return settings.getString(preference, "");
}
public static String getSharedPreferenceString(Context c, String preference, String defaultValue){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
return settings.getString(preference, defaultValue);
}
public static int getSharedPreferenceInt(Context c, String preference, int defaultValue){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
return settings.getInt(preference, defaultValue);
}

public static long getSharedPreferenceLong(Context c, String preference, long defaultValue){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
return settings.getLong(preference, defaultValue);
}

public static void setSharedPreference(Context c, String preference, boolean prefValue){

SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(preference, prefValue);
editor.commit();
}

public static void setSharedPreference(Context c, String preference, Long prefValue){

SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putLong(preference, prefValue);
editor.commit();
}

public static void setSharedPreference(Context c, String preference, int prefValue){

SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(preference, prefValue);
editor.commit();
}
public static void setSharedPreference(Context c, String preference, String prefValue){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(preference, prefValue);
editor.commit();
}

}


Using it is pretty simple:



  1. To load the data, you just have to call:



    UserModel sharedData = new UserModel(getApplicationContext());



  2. To change or retrieve data just call it's associated field getter/setter:



    sharedData.getPassword();
    sharedData.setPassword("new password");



  3. Finally, if you change field(s), just don't forget to call saveIntoPreferences:



    sharedData.saveIntoPreferences();


Hope this help to solve your problem.



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...