Android Session


import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.preference.PreferenceManager;
public class ApplicationSession {

   private static String TAG = "[ApplicationSession] : ";
   private static SharedPreferences preferences = null;
   private static Context context;

   public class FieldType {
      public static final String STRING = "String";
      public static final String INTEGER = "Integer";
      public static final String LONG = "Long";
      public static final String FLOAT = "Float";
      public static final String BOOLEAN = "Boolean";
   }

   public static void setApplicationContext(Context context) {
      try {
         ApplicationSession.context = context;
         preferences = PreferenceManager.getDefaultSharedPreferences(context);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public static Context getApplicationContext(){
      return ApplicationSession.context;
   }

   public static void setToPreference(String fieldName, Object fieldValue) {

      try {
         Editor editor = preferences.edit();
         if (fieldValue instanceof String) {
            editor.putString(fieldName, ((String)fieldValue).toString());
         }else if (fieldValue instanceof Integer) {
            editor.putInt(fieldName, ((Integer)fieldValue).intValue());
         }else if (fieldValue instanceof Long) {
            editor.putLong(fieldName, ((Long)fieldValue).longValue());
         }else if (fieldValue instanceof Float) {
            editor.putFloat(fieldName, ((Float)fieldValue).floatValue());
         }else if (fieldValue instanceof Boolean) {
            editor.putBoolean(fieldName, ((Boolean)fieldValue).booleanValue());
         }
         editor.commit();
      } catch (Exception e) {
         e.printStackTrace();
      }

   }

   public static Object getFromPreference(String fieldName, String fieldType) {
      Object fieldValue = null;
      try {
         if (fieldType.equals(FieldType.STRING)) {
            fieldValue = preferences.getString(fieldName, null);
         }else if (fieldType.equals(FieldType.INTEGER)) {
            fieldValue = preferences.getInt(fieldName, 0);
         }else if (fieldType.equals(FieldType.LONG)) {
            fieldValue = preferences.getLong(fieldName, 0);
         }else if (fieldType.equals(FieldType.FLOAT)) {
            fieldValue = preferences.getFloat(fieldName, 0);
         }else if (fieldType.equals(FieldType.BOOLEAN)) {
            fieldValue = preferences.getBoolean(fieldName, false);
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
      return fieldValue;
   }

   public static void clearPreference(String fieldName) {
      try {
         Editor editor = preferences.edit();
         editor.remove(fieldName);
         editor.commit();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   
   public static void clearAllPreference(){
      try {
         Editor editor = preferences.edit();
         editor.clear();
         editor.commit();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Application Activity


ApplicationSession.setApplicationContext(context);

Constants.isAppLaunched = false;
Log.i(TAG, "onCreate(), Constants.isAppLaunched : " + Constants.isAppLaunched);

isLoginDone = (Boolean) ApplicationSession.getFromPreference(Constants.Preferences.PREF_LOGIN_DONE, ApplicationSession.FieldType.BOOLEAN);
Log.i(TAG, "onCreate(), isLoginDone : " + isLoginDone);

loginType = (String) ApplicationSession.getFromPreference(Constants.Preferences.PREF_LOGIN_TYPE, ApplicationSession.FieldType.STRING);
Log.i(TAG, "onCreate(), loginType : " + loginType);


Constant file

public class Preferences {
    public static final String PREF_LOGIN_DONE = "login_done";
    public static final String PREF_FULL_NAME = "fullname";

    // google login & facebook    public static final String PREF_LOGIN_TYPE = "login_type";

    public static final String PREF_GOOGLE_ID = "google_id";
    public static final String PREF_GOOGLE_TOKEN = "google_token";
    public static final String PREF_PROFILE_PIC_PATH = "profile_pic_path";
    public static final String PREF_USER_FULL_NAME = "fullname";
    public static final String PREF_USER_LOCATION = "user_location";
    public static final String PREF_USER_EMAIL = "user_email";

    public static final String PREF_ERROR_LOG_MAIL_ENABLED = "error_log_mail_enabled";
    public static final String PREF_VERSION = "version";

    public static final String PREF_GWT_TOKEN = "gwt_token";

}

Comments

Popular posts from this blog

retrofil gjstatus lanuage

form object

Login Preference in android create class