Browse Source

SessionManager changes

Drawer small tweak
pull/24/head
Ezerous 8 years ago
parent
commit
ce5a95cc73
  1. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java
  2. 17
      app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java
  3. 58
      app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java
  4. 2
      build.gradle

4
app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java

@ -206,8 +206,8 @@ public class BaseActivity extends AppCompatActivity
else else
loginLogoutItem.withName(R.string.logout).withIcon(logoutIcon); //Swap login with logout loginLogoutItem.withName(R.string.logout).withIcon(logoutIcon); //Swap login with logout
ProfileDrawerItem p = new ProfileDrawerItem().withName(sessionManager.getUsername()); //TODO: set profile picture profileDrawerItem.withName(sessionManager.getUsername()); //TODO: set profile picture
accountHeader.updateProfile(p); accountHeader.updateProfile(profileDrawerItem);
drawer.updateItem(loginLogoutItem); drawer.updateItem(loginLogoutItem);
} }

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

@ -36,6 +36,8 @@ public class LoginActivity extends BaseActivity {
//Other variables //Other variables
private static final String TAG = "LoginActivity"; private static final String TAG = "LoginActivity";
private LoginTask loginTask;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -65,7 +67,8 @@ public class LoginActivity extends BaseActivity {
} }
//Login user //Login user
new LoginTask().execute(username, password); loginTask = new LoginTask();
loginTask.execute(username, password);
} }
}); });
@ -89,6 +92,9 @@ public class LoginActivity extends BaseActivity {
public void onBackPressed() { public void onBackPressed() {
// Disable going back to the MainActivity // Disable going back to the MainActivity
moveTaskToBack(true); moveTaskToBack(true);
// if(loginTask!=null && loginTask.getStatus() == AsyncTask.Status.RUNNING){ TODO
// loginTask.cancel(true);
// }
} }
private void onLoginFailed() { private void onLoginFailed() {
@ -192,6 +198,15 @@ public class LoginActivity extends BaseActivity {
loginContent.setVisibility(View.VISIBLE); loginContent.setVisibility(View.VISIBLE);
spinner.setVisibility(View.INVISIBLE); spinner.setVisibility(View.INVISIBLE);
} }
@Override
protected void onCancelled() {
super.onCancelled();
btnLogin.setEnabled(true); //Re-enable login button
loginContent.setVisibility(View.VISIBLE);
spinner.setVisibility(View.INVISIBLE);
}
} }
//---------------------------------------LOGIN ENDS------------------------------------------------- //---------------------------------------LOGIN ENDS-------------------------------------------------
} }

58
app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java

@ -3,7 +3,6 @@ package gr.thmmy.mthmmy.session;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.os.Environment; import android.os.Environment;
import android.util.Log;
import com.android.volley.VolleyError; import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader; import com.android.volley.toolbox.ImageLoader;
@ -19,11 +18,13 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InterruptedIOException;
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;
import gr.thmmy.mthmmy.utils.ImageController; import gr.thmmy.mthmmy.utils.ImageController;
import mthmmy.utils.Report;
import okhttp3.Cookie; import okhttp3.Cookie;
import okhttp3.FormBody; import okhttp3.FormBody;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
@ -51,8 +52,9 @@ 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 CONNECTION_ERROR = 4; public static final int CANCELLED = 4;
public static final int EXCEPTION = 5; public static final int CONNECTION_ERROR = 5;
public static final int EXCEPTION = 6;
//Login status codes //Login status codes
public static final int LOGGED_OUT = 0; public static final int LOGGED_OUT = 0;
@ -87,7 +89,7 @@ public class SessionManager
*/ */
public int login(String... strings) public int login(String... strings)
{ {
Log.i(TAG, "Logging in..."); Report.i(TAG, "Logging in...");
//Build the login request for each case //Build the login request for each case
Request request; Request request;
@ -123,7 +125,7 @@ public class SessionManager
Element logoutButton = document.getElementById("logoutbtn"); //Attempt to find logout button Element logoutButton = document.getElementById("logoutbtn"); //Attempt to find logout button
if (logoutButton != null) //If logout button exists, login was successful if (logoutButton != null) //If logout button exists, login was successful
{ {
Log.i(TAG, "Login successful!"); Report.i(TAG, "Login successful!");
setPersistentCookieSession(); //Store cookies setPersistentCookieSession(); //Store cookies
//Edit SharedPreferences, save session's data //Edit SharedPreferences, save session's data
@ -135,18 +137,18 @@ public class SessionManager
} }
else else
{ {
Log.i(TAG, "Login failed."); Report.i(TAG, "Login failed.");
//Investigate login failure //Investigate login failure
Elements error = document.select("b:contains(That username does not exist.)"); Elements error = document.select("b:contains(That username does not exist.)");
if (error.size() == 1) { //Wrong username if (error.size() == 1) { //Wrong username
Log.i(TAG, "Wrong Username"); Report.i(TAG, "Wrong Username");
return WRONG_USER; return WRONG_USER;
} }
error = document.select("body:contains(Password incorrect)"); error = document.select("body:contains(Password incorrect)");
if (error.size() == 1) { //Wrong password if (error.size() == 1) { //Wrong password
Log.i(TAG, "Wrong Password"); Report.i(TAG, "Wrong Password");
return WRONG_PASSWORD; return WRONG_PASSWORD;
} }
@ -156,35 +158,39 @@ public class SessionManager
} }
//Handle exception //Handle exception
} }
catch (InterruptedIOException e){
Report.i(TAG, "Login InterruptedIOException: "+ e.getMessage(), e); //users cancels LoginTask
return CANCELLED;
}
catch (IOException e) { catch (IOException e) {
Log.w(TAG, "Login IOException: "+ e.getMessage(), e); Report.w(TAG, "Login IOException: "+ e.getMessage(), e);
return CONNECTION_ERROR; return CONNECTION_ERROR;
} }
catch (Exception e) { catch (Exception e) {
Log.w(TAG, "Login Exception (other): "+ e.getMessage(), e); Report.w(TAG, "Login Exception (other): "+ e.getMessage(), e);
return EXCEPTION; return EXCEPTION;
} }
} }
/** /**
* 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. On failure, this can only return
* the codes {SUCCESS, FAILURE, CONNECTION_ERROR, EXCEPTION}. CONNECTION_ERROR and EXCEPTION * the code FAILURE. CANCELLED, CONNECTION_ERROR and EXCEPTION are simply considered a SUCCESS
* are simply considered a SUCCESS (e.g. no internet connection), at least until a more * (e.g. no internet connection), at least until a more thorough handling of different
* thorough handling of different exceptions is implemented (if considered mandatory). * exceptions is implemented (if considered mandatory).
* Always call it in a separate thread in a way that won't hinder performance (e.g. after * Always call it in a separate thread in a way that won't hinder performance (e.g. after
* fragments' data are retrieved). * fragments' data are retrieved).
*/ */
public void validateSession() public void validateSession()
{ {
Log.i(TAG, "Validating session..."); Report.i(TAG, "Validating session...");
//Check if user is currently logged in //Check if user is currently logged in
int status = sharedPrefs.getInt(LOGIN_STATUS,LOGGED_OUT); int status = sharedPrefs.getInt(LOGIN_STATUS,LOGGED_OUT);
if(status==LOGGED_IN) if(status==LOGGED_IN)
{ {
int loginResult = login(); int loginResult = login();
if(loginResult == SUCCESS || loginResult == CONNECTION_ERROR || loginResult == EXCEPTION) if(loginResult != FAILURE)
return; return;
} }
else if(status==AS_GUEST) else if(status==AS_GUEST)
@ -198,7 +204,7 @@ public class SessionManager
*/ */
public void guestLogin() public void guestLogin()
{ {
Log.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().putInt(LOGIN_STATUS, AS_GUEST).apply();
} }
@ -209,7 +215,7 @@ public class SessionManager
*/ */
public int logout() public int logout()
{ {
Log.i(TAG, "Logging out..."); Report.i(TAG, "Logging out...");
Request request = new Request.Builder() Request request = new Request.Builder()
.url(sharedPrefs.getString(LOGOUT_LINK,"LogoutLink")) .url(sharedPrefs.getString(LOGOUT_LINK,"LogoutLink"))
@ -223,17 +229,17 @@ public class SessionManager
Elements loginButton = document.select("[value=Login]"); //Attempt to find login button Elements loginButton = document.select("[value=Login]"); //Attempt to find login button
if (!loginButton.isEmpty()) //If login button exists, logout was successful if (!loginButton.isEmpty()) //If login button exists, logout was successful
{ {
Log.i("Logout", "Logout successful!"); Report.i(TAG, "Logout successful!");
return SUCCESS; return SUCCESS;
} else { } else {
Log.i(TAG, "Logout failed."); Report.i(TAG, "Logout failed.");
return FAILURE; return FAILURE;
} }
} catch (IOException e) { } catch (IOException e) {
Log.w(TAG, "Logout IOException: "+ e.getMessage(), e); Report.w(TAG, "Logout IOException: "+ e.getMessage(), e);
return CONNECTION_ERROR; return CONNECTION_ERROR;
} catch (Exception e) { } catch (Exception e) {
Log.w(TAG, "Logout Exception: "+ e.getMessage(), e); Report.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
@ -282,7 +288,7 @@ public class SessionManager
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().putInt(LOGIN_STATUS, LOGGED_OUT).apply(); //User logs out
Log.i(TAG,"Session data cleared."); Report.i(TAG,"Session data cleared.");
} }
private String extractUserName(Document doc) private String extractUserName(Document doc)
@ -313,7 +319,7 @@ public class SessionManager
File pictureFile = getOutputMediaFile(package_name, image_name); File pictureFile = getOutputMediaFile(package_name, image_name);
if (pictureFile == null) { if (pictureFile == null) {
Log.d(TAG, Report.d(TAG,
"Error creating media file, check storage permissions: ");// e.getMessage()); "Error creating media file, check storage permissions: ");// e.getMessage());
return; return;
} }
@ -324,9 +330,9 @@ public class SessionManager
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos); bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close(); fos.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage()); Report.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) { } catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage()); Report.d(TAG, "Error accessing file: " + e.getMessage());
} }
returnImage[0] = pictureFile; returnImage[0] = pictureFile;
} }

2
build.gradle

@ -6,7 +6,7 @@ buildscript {
maven { url "https://jitpack.io" } maven { url "https://jitpack.io" }
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:2.2.2' classpath 'com.android.tools.build:gradle:2.2.3'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files

Loading…
Cancel
Save