Session Manager
Use this class to store and delete sessions.It is also used to check if the user is already logged in or session has already expired.The infomation set during login if true are stored as session using a private key.
Android supports the usage of the _SharedPreferences _class for persisting key-value pairs (preferences)of primitive data types in the Android file system.
ThePreferenceManager
class provides methods to get access to preferences stored in a certain file. The following code shows how to access preferences from a certain file
public class SessionManager {
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "LoginPref";
// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
// Key of Sessions(make variable public to access from outside)
public static final String KEY_EMAIL = "email";
public static final String KEY_PASSWORD = "password";
public static final String KEY_ROLE = "role";
public static final String KEY_NAME = "name";
public static final String KEY_TOKEN = "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 email, String password,String role, String token){
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
// Storing name in pref
editor.putString(KEY_NAME, name);
// Storing email in pref
editor.putString(KEY_EMAIL, email);
// Storing password in pref
editor.putString(KEY_PASSWORD, password);
// Storing role in pref
editor.putString(KEY_ROLE, role);
// Storing token in pref
editor.putString(KEY_TOKEN, token);
// commit changes
editor.commit();
}
/**
* Check login method wil check user login status
* If false it will redirect user to login page
* Else won't do anything
* */
public void checkLogin(){
// Check login status
if(!this.isLoggedIn()){
// user is not logged in redirect him to Login Activity
Intent i = new Intent(_context, LoginActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
}
/**
* Get stored session data
* */
public HashMap<String, String> getUserDetails(){
HashMap<String, String> user = new HashMap<String, String>();
// user name
user.put(KEY_NAME, pref.getString(KEY_NAME, null));
// user email id
user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
// user password
user.put(KEY_PASSWORD, pref.getString(KEY_PASSWORD, null));
// user role
user.put(KEY_ROLE, pref.getString(KEY_ROLE, null));
// session token
user.put(KEY_TOKEN, pref.getString(KEY_TOKEN, null));
// return user
return user;
}
/**
* Clear session details
* */
public void logoutUser(){
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, LoginActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
/**
* Quick check for login
* **/
// Get Login State
public boolean isLoggedIn(){
return pref.getBoolean(IS_LOGIN, false);
}
}