|
@ -1,6 +1,7 @@ |
|
|
package gr.thmmy.mthmmy.session; |
|
|
package gr.thmmy.mthmmy.session; |
|
|
|
|
|
|
|
|
import android.content.SharedPreferences; |
|
|
import android.content.SharedPreferences; |
|
|
|
|
|
import android.support.annotation.Nullable; |
|
|
|
|
|
|
|
|
import com.franmontiel.persistentcookiejar.PersistentCookieJar; |
|
|
import com.franmontiel.persistentcookiejar.PersistentCookieJar; |
|
|
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; |
|
|
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; |
|
@ -49,11 +50,6 @@ public class SessionManager |
|
|
public static final int CONNECTION_ERROR = 5; |
|
|
public static final int CONNECTION_ERROR = 5; |
|
|
public static final int EXCEPTION = 6; |
|
|
public static final int EXCEPTION = 6; |
|
|
|
|
|
|
|
|
//Login status codes
|
|
|
|
|
|
public static final int LOGGED_OUT = 0; |
|
|
|
|
|
public static final int LOGGED_IN = 1; //Logged in (as a normal user)
|
|
|
|
|
|
public static final int AS_GUEST = 2; //User chose to continue as guest
|
|
|
|
|
|
|
|
|
|
|
|
// Client & Cookies
|
|
|
// Client & Cookies
|
|
|
private OkHttpClient client; |
|
|
private OkHttpClient client; |
|
|
private PersistentCookieJar cookieJar; |
|
|
private PersistentCookieJar cookieJar; |
|
@ -65,7 +61,8 @@ public class SessionManager |
|
|
public static final String AVATAR_LINK = "AvatarLink"; |
|
|
public static final String AVATAR_LINK = "AvatarLink"; |
|
|
public static final String HAS_AVATAR = "HasAvatar"; |
|
|
public static final String HAS_AVATAR = "HasAvatar"; |
|
|
public static final String LOGOUT_LINK = "LogoutLink"; |
|
|
public static final String LOGOUT_LINK = "LogoutLink"; |
|
|
public static final String LOGIN_STATUS = "IsLoggedIn"; |
|
|
public static final String LOGGED_IN = "LoggedIn"; |
|
|
|
|
|
public static final String LOGIN_SCREEN_AS_DEFAULT = "LoginScreenAsDefault"; |
|
|
|
|
|
|
|
|
//Constructor
|
|
|
//Constructor
|
|
|
public SessionManager(OkHttpClient client, PersistentCookieJar cookieJar, |
|
|
public SessionManager(OkHttpClient client, PersistentCookieJar cookieJar, |
|
@ -124,7 +121,8 @@ public class SessionManager |
|
|
setPersistentCookieSession(); //Store cookies
|
|
|
setPersistentCookieSession(); //Store cookies
|
|
|
|
|
|
|
|
|
//Edit SharedPreferences, save session's data
|
|
|
//Edit SharedPreferences, save session's data
|
|
|
sharedPrefs.edit().putInt(LOGIN_STATUS, LOGGED_IN).apply(); |
|
|
sharedPrefs.edit().putBoolean(LOGGED_IN, true).apply(); |
|
|
|
|
|
sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, false).apply(); |
|
|
sharedPrefs.edit().putString(USERNAME, extractUserName(document)).apply(); |
|
|
sharedPrefs.edit().putString(USERNAME, extractUserName(document)).apply(); |
|
|
String avatar = extractAvatarLink(document); |
|
|
String avatar = extractAvatarLink(document); |
|
|
if (avatar!=null) |
|
|
if (avatar!=null) |
|
@ -178,7 +176,7 @@ public class SessionManager |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* A function that checks the validity of the current saved session (if it exists). |
|
|
* A function that checks the validity of the current saved session (if it exists). |
|
|
* If LOGIN_STATUS is true, it will call login() with cookies. On failure, this can only return |
|
|
* If isLoggedIn() is true, it will call login() with cookies. On failure, this can only return |
|
|
* the code FAILURE. CANCELLED, CONNECTION_ERROR and EXCEPTION are simply considered a SUCCESS |
|
|
* the code FAILURE. CANCELLED, CONNECTION_ERROR and EXCEPTION are simply considered a SUCCESS |
|
|
* (e.g. no internet connection), at least until a more thorough handling of different |
|
|
* (e.g. no internet connection), at least until a more thorough handling of different |
|
|
* exceptions is implemented (if considered mandatory). |
|
|
* exceptions is implemented (if considered mandatory). |
|
@ -189,17 +187,16 @@ public class SessionManager |
|
|
{ |
|
|
{ |
|
|
Report.i(TAG, "Validating session..."); |
|
|
Report.i(TAG, "Validating session..."); |
|
|
|
|
|
|
|
|
//Check if user is currently logged in
|
|
|
if(isLoggedIn()) |
|
|
int status = sharedPrefs.getInt(LOGIN_STATUS,LOGGED_OUT); |
|
|
|
|
|
if(status==LOGGED_IN) |
|
|
|
|
|
{ |
|
|
{ |
|
|
int loginResult = login(); |
|
|
int loginResult = login(); |
|
|
if(loginResult != FAILURE) |
|
|
if(loginResult != FAILURE) |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
else if(status==AS_GUEST) |
|
|
else if(isLoginScreenDefault()) |
|
|
return; |
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, true).apply(); |
|
|
clearSessionData(); |
|
|
clearSessionData(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -210,7 +207,7 @@ public class SessionManager |
|
|
{ |
|
|
{ |
|
|
Report.i("TAG", "Continuing as a guest, as chosen by the user."); |
|
|
Report.i("TAG", "Continuing as a guest, as chosen by the user."); |
|
|
clearSessionData(); |
|
|
clearSessionData(); |
|
|
sharedPrefs.edit().putInt(LOGIN_STATUS, AS_GUEST).apply(); |
|
|
sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, false).apply(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -266,8 +263,12 @@ public class SessionManager |
|
|
return sharedPrefs.getBoolean(HAS_AVATAR, false); |
|
|
return sharedPrefs.getBoolean(HAS_AVATAR, false); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public int getLogStatus() { |
|
|
public boolean isLoggedIn() { |
|
|
return sharedPrefs.getInt(LOGIN_STATUS, LOGGED_OUT); |
|
|
return sharedPrefs.getBoolean(LOGGED_IN, false); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public boolean isLoginScreenDefault() { |
|
|
|
|
|
return sharedPrefs.getBoolean(LOGIN_SCREEN_AS_DEFAULT, true); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//--------------------------------------GETTERS END---------------------------------------------
|
|
|
//--------------------------------------GETTERS END---------------------------------------------
|
|
@ -299,10 +300,11 @@ public class SessionManager |
|
|
cookieJar.clear(); |
|
|
cookieJar.clear(); |
|
|
sharedPrefs.edit().clear().apply(); //Clear session data
|
|
|
sharedPrefs.edit().clear().apply(); //Clear session data
|
|
|
sharedPrefs.edit().putString(USERNAME, guestName).apply(); |
|
|
sharedPrefs.edit().putString(USERNAME, guestName).apply(); |
|
|
sharedPrefs.edit().putInt(LOGIN_STATUS, LOGGED_OUT).apply(); //User logs out
|
|
|
sharedPrefs.edit().putBoolean(LOGGED_IN, false).apply(); //User logs out
|
|
|
Report.i(TAG,"Session data cleared."); |
|
|
Report.i(TAG,"Session data cleared."); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
private String extractUserName(Document doc) |
|
|
private String extractUserName(Document doc) |
|
|
{ |
|
|
{ |
|
|
if (doc != null) { |
|
|
if (doc != null) { |
|
@ -321,6 +323,7 @@ public class SessionManager |
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
private String extractAvatarLink(Document doc) |
|
|
private String extractAvatarLink(Document doc) |
|
|
{ |
|
|
{ |
|
|
if (doc != null) { |
|
|
if (doc != null) { |
|
|