Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SessionManager {
- // Shared Preferences
- SharedPreferences pref;
- // Editor for Shared preferences
- SharedPreferences.Editor editor;
- // Context
- Context _context;
- // Shared pref mode
- int PRIVATE_MODE = 0;
- // Sharedpref file name
- private static final String PREF_NAME = "SessionSharedPreference";
- //if user logged in or not
- private static final String IS_LOGIN = "isLoggedIn";
- // User name (make variable public to access from outside)
- public static final String KEY_NAME = "full_name";
- public static final String KEY_USERNAME = "username";
- // email address
- public static final String KEY_EMAIL = "email";
- // user id
- public static final String KEY_USER_ID = "user_id";
- public static final String KEY_COMPANY_ID = "company";
- // session token
- public static final String KEY_SESSION_TOKEN = "session_token";
- // Constructor
- public SessionManager(Context context){
- this._context = context;
- pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
- editor = pref.edit();
- }
- /**
- * Create login session
- * */
- public void createLoginSession(String name, String username, String email , int user_id ,int company_id, String session_token){
- editor.putBoolean(IS_LOGIN, true);
- editor.putString(KEY_NAME, name);
- editor.putString(KEY_USERNAME, username);
- editor.putString(KEY_EMAIL, email);
- editor.putInt(KEY_USER_ID, user_id);
- editor.putInt(KEY_COMPANY_ID, company_id);
- editor.putString(KEY_SESSION_TOKEN, session_token);
- editor.commit();
- }
- /**
- * Clear session details
- * */
- public void logoutUser(){
- // Clearing all data from Shared Preferences
- editor.clear();
- editor.commit();
- }
- public boolean isLoggedIn(){
- return pref.getBoolean(IS_LOGIN, false);
- }
- public int getUserId(){
- return pref.getInt(KEY_USER_ID, 0);
- }
- public int getCompanyId(){
- return pref.getInt(KEY_COMPANY_ID, 0);
- }
- public String getUserEmail(){
- return pref.getString(KEY_EMAIL, null);
- }
- public String getUsername(){
- return pref.getString(KEY_USERNAME, null);
- }
- public String getUserFullname(){
- return pref.getString(KEY_NAME, null);
- }
- public String getKeySessionToken(){
- return pref.getString(KEY_SESSION_TOKEN, null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement