diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java index 39256f21..e4c3ed1b 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java @@ -12,6 +12,7 @@ import android.widget.Toast; import gr.thmmy.mthmmy.R; +import static gr.thmmy.mthmmy.session.SessionManager.CONNECTION_ERROR; import static gr.thmmy.mthmmy.session.SessionManager.EXCEPTION; import static gr.thmmy.mthmmy.session.SessionManager.FAILURE; import static gr.thmmy.mthmmy.session.SessionManager.SUCCESS; @@ -154,18 +155,19 @@ public class LoginActivity extends BaseActivity { break; case WRONG_PASSWORD: Toast.makeText(getApplicationContext(), - "Wrong password!", Toast.LENGTH_LONG) - .show(); + "Wrong password!", Toast.LENGTH_LONG).show(); break; case FAILURE: Toast.makeText(getApplicationContext(), - "Login failed...", Toast.LENGTH_LONG) - .show(); + "Login failed...", Toast.LENGTH_LONG).show(); + break; + case CONNECTION_ERROR: + Toast.makeText(getApplicationContext(), + "Connection Error", Toast.LENGTH_LONG).show(); break; case EXCEPTION: Toast.makeText(getApplicationContext(), - "Login failed...", Toast.LENGTH_LONG) - .show(); + "Error", Toast.LENGTH_LONG).show(); break; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java index 916e81e8..602074c2 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java @@ -164,6 +164,10 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF //-------------------------------FragmentPagerAdapter END------------------------------------------- //-------------------------------------------LOGOUT------------------------------------------------- +/** + * Result toast will always display a success, because when user chooses logout all data are + * cleared regardless of the actual outcome + */ private class LogoutTask extends AsyncTask { //Attempt logout ProgressDialog progressDialog; @@ -171,7 +175,8 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF return sessionManager.logout(); } - protected void onPreExecute() { //Show a progress dialog until done + protected void onPreExecute() + { //Show a progress dialog until done progressDialog = new ProgressDialog(MainActivity.this, R.style.AppTheme); progressDialog.setIndeterminate(true); @@ -179,16 +184,12 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF progressDialog.show(); } - protected void onPostExecute(Integer result) { //Handle attempt result - progressDialog.dismiss(); //Hide progress dialog - if (result == LOGGED_OUT) //Successful logout - { - Toast.makeText(getBaseContext(), "Logged out successfully!", Toast.LENGTH_LONG).show(); - sessionManager.guestLogin(); //Fall to guest login - hideLogout(); - } - else //Logout failed - hideLogin(); + protected void onPostExecute(Integer result) + { + Toast.makeText(getBaseContext(), "Logged out successfully!", Toast.LENGTH_LONG).show(); + sessionManager.guestLogin(); //Fall to guest login + hideLogout(); + progressDialog.dismiss(); } } //-----------------------------------------LOGOUT END----------------------------------------------- diff --git a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java index d608c68f..a351e1b0 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java +++ b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java @@ -11,6 +11,7 @@ import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; +import java.io.IOException; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -25,7 +26,7 @@ import okhttp3.Response; /** This class handles all session related operations (e.g. login, logout) - and stores data to SharedPreferences (session informarion and cookies). + and stores data to SharedPreferences (session information and cookies). */ public class SessionManager { @@ -42,7 +43,8 @@ public class SessionManager public static final int FAILURE = 1; //Generic Error public static final int WRONG_USER = 2; public static final int WRONG_PASSWORD = 3; - public static final int EXCEPTION = 4; + public static final int CONNECTION_ERROR = 4; + public static final int EXCEPTION = 5; //Login status codes public static final int LOGGED_OUT = 0; @@ -145,8 +147,13 @@ public class SessionManager return FAILURE; } //Handle exception - } catch (Exception e) { - Log.w(TAG, "Login Exception: "+ e.getMessage(), e); + } + catch (IOException e) { + Log.w(TAG, "Login IOException: "+ e.getMessage(), e); + return CONNECTION_ERROR; + } + catch (Exception e) { + Log.w(TAG, "Login Exception (other): "+ e.getMessage(), e); return EXCEPTION; } } @@ -154,9 +161,11 @@ public class SessionManager /** * 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. This can only return - * the codes {SUCCESS, FAILURE, EXCEPTION}. EXCEPTION is considered a SUCCESS (e.g. no internet - * connection), at least until a more thorough handling of different exceptions is implemented. - * Always call it in a separate thread. + * the codes {SUCCESS, FAILURE, CONNECTION_ERROR, EXCEPTION}. CONNECTION_ERROR and EXCEPTION + * are simply considered a SUCCESS (e.g. no internet connection), at least until a more + * thorough handling of different exceptions is implemented (if considered mandatory). + * Always call it in a separate thread in a way that won't hinder performance (e.g. after + * fragments' data are retrieved). */ public void validateSession() { @@ -167,7 +176,7 @@ public class SessionManager if(status==LOGGED_IN) { int loginResult = login(); - if(loginResult == SUCCESS || loginResult == EXCEPTION) + if(loginResult == SUCCESS || loginResult == CONNECTION_ERROR || loginResult == EXCEPTION) return; } else if(status==AS_GUEST) @@ -208,19 +217,19 @@ public class SessionManager { Log.i("Logout", "Logout successful!"); return SUCCESS; - } - else - { + } else { Log.i(TAG, "Logout failed."); return FAILURE; } + } catch (IOException e) { + Log.w(TAG, "Logout IOException: "+ e.getMessage(), e); + return CONNECTION_ERROR; } catch (Exception e) { Log.w(TAG, "Logout Exception: "+ e.getMessage(), e); return EXCEPTION; } finally { //All data should always be cleared from device regardless the result of logout clearSessionData(); - Log.i(TAG,"Session data cleared."); } } //--------------------------------------AUTH ENDS----------------------------------------------- @@ -230,10 +239,6 @@ public class SessionManager return sharedPrefs.getString(USERNAME, "Username"); } - public String getLogoutLink() { - return sharedPrefs.getString(LOGOUT_LINK, "LogoutLink"); - } - public int getLogStatus() { return sharedPrefs.getInt(LOGIN_STATUS, LOGGED_OUT); } @@ -266,8 +271,9 @@ public class SessionManager { cookieJar.clear(); sharedPrefs.edit().clear().apply(); //Clear session data - sharedPrefs.edit().putString(USERNAME, guestName).apply(); //User becomes guest - sharedPrefs.edit().putInt(LOGIN_STATUS, LOGGED_OUT).apply(); + sharedPrefs.edit().putString(USERNAME, guestName).apply(); + sharedPrefs.edit().putInt(LOGIN_STATUS, LOGGED_OUT).apply(); //User logs out + Log.i(TAG,"Session data cleared."); } private String extractUserName(Document doc)