Browse Source

Further login/logout improvements & bug fixes

pull/24/head
Ezerous 8 years ago
parent
commit
e3a3de6702
  1. 14
      app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java
  2. 15
      app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java
  3. 42
      app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java

14
app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java

@ -12,6 +12,7 @@ import android.widget.Toast;
import gr.thmmy.mthmmy.R; 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.EXCEPTION;
import static gr.thmmy.mthmmy.session.SessionManager.FAILURE; import static gr.thmmy.mthmmy.session.SessionManager.FAILURE;
import static gr.thmmy.mthmmy.session.SessionManager.SUCCESS; import static gr.thmmy.mthmmy.session.SessionManager.SUCCESS;
@ -154,18 +155,19 @@ public class LoginActivity extends BaseActivity {
break; break;
case WRONG_PASSWORD: case WRONG_PASSWORD:
Toast.makeText(getApplicationContext(), Toast.makeText(getApplicationContext(),
"Wrong password!", Toast.LENGTH_LONG) "Wrong password!", Toast.LENGTH_LONG).show();
.show();
break; break;
case FAILURE: case FAILURE:
Toast.makeText(getApplicationContext(), Toast.makeText(getApplicationContext(),
"Login failed...", Toast.LENGTH_LONG) "Login failed...", Toast.LENGTH_LONG).show();
.show(); break;
case CONNECTION_ERROR:
Toast.makeText(getApplicationContext(),
"Connection Error", Toast.LENGTH_LONG).show();
break; break;
case EXCEPTION: case EXCEPTION:
Toast.makeText(getApplicationContext(), Toast.makeText(getApplicationContext(),
"Login failed...", Toast.LENGTH_LONG) "Error", Toast.LENGTH_LONG).show();
.show();
break; break;
} }

15
app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java

@ -164,6 +164,10 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF
//-------------------------------FragmentPagerAdapter END------------------------------------------- //-------------------------------FragmentPagerAdapter END-------------------------------------------
//-------------------------------------------LOGOUT------------------------------------------------- //-------------------------------------------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<Void, Void, Integer> { //Attempt logout private class LogoutTask extends AsyncTask<Void, Void, Integer> { //Attempt logout
ProgressDialog progressDialog; ProgressDialog progressDialog;
@ -171,7 +175,8 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF
return sessionManager.logout(); 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, progressDialog = new ProgressDialog(MainActivity.this,
R.style.AppTheme); R.style.AppTheme);
progressDialog.setIndeterminate(true); progressDialog.setIndeterminate(true);
@ -179,16 +184,12 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF
progressDialog.show(); progressDialog.show();
} }
protected void onPostExecute(Integer result) { //Handle attempt result protected void onPostExecute(Integer result)
progressDialog.dismiss(); //Hide progress dialog
if (result == LOGGED_OUT) //Successful logout
{ {
Toast.makeText(getBaseContext(), "Logged out successfully!", Toast.LENGTH_LONG).show(); Toast.makeText(getBaseContext(), "Logged out successfully!", Toast.LENGTH_LONG).show();
sessionManager.guestLogin(); //Fall to guest login sessionManager.guestLogin(); //Fall to guest login
hideLogout(); hideLogout();
} progressDialog.dismiss();
else //Logout failed
hideLogin();
} }
} }
//-----------------------------------------LOGOUT END----------------------------------------------- //-----------------------------------------LOGOUT END-----------------------------------------------

42
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.nodes.Element;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -25,7 +26,7 @@ import okhttp3.Response;
/** /**
This class handles all session related operations (e.g. login, logout) 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 public class SessionManager
{ {
@ -42,7 +43,8 @@ public class SessionManager
public static final int FAILURE = 1; //Generic Error public static final int FAILURE = 1; //Generic Error
public static final int WRONG_USER = 2; public static final int WRONG_USER = 2;
public static final int WRONG_PASSWORD = 3; 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 //Login status codes
public static final int LOGGED_OUT = 0; public static final int LOGGED_OUT = 0;
@ -145,8 +147,13 @@ public class SessionManager
return FAILURE; return FAILURE;
} }
//Handle exception //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; return EXCEPTION;
} }
} }
@ -154,9 +161,11 @@ 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. This can only return * 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 * the codes {SUCCESS, FAILURE, CONNECTION_ERROR, EXCEPTION}. CONNECTION_ERROR and EXCEPTION
* connection), at least until a more thorough handling of different exceptions is implemented. * are simply considered a SUCCESS (e.g. no internet connection), at least until a more
* Always call it in a separate thread. * 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() public void validateSession()
{ {
@ -167,7 +176,7 @@ public class SessionManager
if(status==LOGGED_IN) if(status==LOGGED_IN)
{ {
int loginResult = login(); int loginResult = login();
if(loginResult == SUCCESS || loginResult == EXCEPTION) if(loginResult == SUCCESS || loginResult == CONNECTION_ERROR || loginResult == EXCEPTION)
return; return;
} }
else if(status==AS_GUEST) else if(status==AS_GUEST)
@ -208,19 +217,19 @@ public class SessionManager
{ {
Log.i("Logout", "Logout successful!"); Log.i("Logout", "Logout successful!");
return SUCCESS; return SUCCESS;
} } else {
else
{
Log.i(TAG, "Logout failed."); Log.i(TAG, "Logout failed.");
return FAILURE; return FAILURE;
} }
} catch (IOException e) {
Log.w(TAG, "Logout IOException: "+ e.getMessage(), e);
return CONNECTION_ERROR;
} catch (Exception e) { } catch (Exception e) {
Log.w(TAG, "Logout Exception: "+ e.getMessage(), e); Log.w(TAG, "Logout Exception: "+ e.getMessage(), e);
return EXCEPTION; return EXCEPTION;
} finally { } finally {
//All data should always be cleared from device regardless the result of logout //All data should always be cleared from device regardless the result of logout
clearSessionData(); clearSessionData();
Log.i(TAG,"Session data cleared.");
} }
} }
//--------------------------------------AUTH ENDS----------------------------------------------- //--------------------------------------AUTH ENDS-----------------------------------------------
@ -230,10 +239,6 @@ public class SessionManager
return sharedPrefs.getString(USERNAME, "Username"); return sharedPrefs.getString(USERNAME, "Username");
} }
public String getLogoutLink() {
return sharedPrefs.getString(LOGOUT_LINK, "LogoutLink");
}
public int getLogStatus() { public int getLogStatus() {
return sharedPrefs.getInt(LOGIN_STATUS, LOGGED_OUT); return sharedPrefs.getInt(LOGIN_STATUS, LOGGED_OUT);
} }
@ -266,8 +271,9 @@ 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(); //User becomes guest sharedPrefs.edit().putString(USERNAME, guestName).apply();
sharedPrefs.edit().putInt(LOGIN_STATUS, LOGGED_OUT).apply(); sharedPrefs.edit().putInt(LOGIN_STATUS, LOGGED_OUT).apply(); //User logs out
Log.i(TAG,"Session data cleared.");
} }
private String extractUserName(Document doc) private String extractUserName(Document doc)

Loading…
Cancel
Save