Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. public class SessionManager {
  2. // Shared Preferences
  3. SharedPreferences pref;
  4.  
  5. // Editor for Shared preferences
  6. SharedPreferences.Editor editor;
  7.  
  8. // Context
  9. Context _context;
  10.  
  11. // Shared pref mode
  12. int PRIVATE_MODE = 0;
  13.  
  14. // Sharedpref file name
  15. private static final String PREF_NAME = "SessionSharedPreference";
  16.  
  17. //if user logged in or not
  18. private static final String IS_LOGIN = "isLoggedIn";
  19.  
  20. // User name (make variable public to access from outside)
  21. public static final String KEY_NAME = "full_name";
  22.  
  23.  
  24. public static final String KEY_USERNAME = "username";
  25.  
  26. // email address
  27. public static final String KEY_EMAIL = "email";
  28.  
  29.  
  30. // user id
  31. public static final String KEY_USER_ID = "user_id";
  32.  
  33.  
  34. public static final String KEY_COMPANY_ID = "company";
  35.  
  36. // session token
  37. public static final String KEY_SESSION_TOKEN = "session_token";
  38.  
  39. // Constructor
  40. public SessionManager(Context context){
  41. this._context = context;
  42. pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
  43. editor = pref.edit();
  44. }
  45.  
  46. /**
  47. * Create login session
  48. * */
  49. public void createLoginSession(String name, String username, String email , int user_id ,int company_id, String session_token){
  50.  
  51. editor.putBoolean(IS_LOGIN, true);
  52.  
  53. editor.putString(KEY_NAME, name);
  54. editor.putString(KEY_USERNAME, username);
  55.  
  56. editor.putString(KEY_EMAIL, email);
  57.  
  58. editor.putInt(KEY_USER_ID, user_id);
  59. editor.putInt(KEY_COMPANY_ID, company_id);
  60.  
  61. editor.putString(KEY_SESSION_TOKEN, session_token);
  62.  
  63. editor.commit();
  64.  
  65.  
  66. }
  67.  
  68.  
  69. /**
  70. * Clear session details
  71. * */
  72. public void logoutUser(){
  73. // Clearing all data from Shared Preferences
  74. editor.clear();
  75. editor.commit();
  76. }
  77.  
  78.  
  79. public boolean isLoggedIn(){
  80. return pref.getBoolean(IS_LOGIN, false);
  81. }
  82.  
  83. public int getUserId(){
  84. return pref.getInt(KEY_USER_ID, 0);
  85. }
  86.  
  87. public int getCompanyId(){
  88. return pref.getInt(KEY_COMPANY_ID, 0);
  89. }
  90.  
  91. public String getUserEmail(){
  92. return pref.getString(KEY_EMAIL, null);
  93. }
  94.  
  95. public String getUsername(){
  96. return pref.getString(KEY_USERNAME, null);
  97. }
  98.  
  99. public String getUserFullname(){
  100. return pref.getString(KEY_NAME, null);
  101. }
  102.  
  103. public String getKeySessionToken(){
  104. return pref.getString(KEY_SESSION_TOKEN, null);
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement