Now login data is saved to SharedPreferences

This commit is contained in:
Juan Miguel Boyero Corral 2016-08-24 11:34:33 +02:00
parent 7f618d11be
commit dd45acb04d
7 changed files with 303 additions and 101 deletions

View File

@ -64,4 +64,5 @@ dependencies {
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
compile 'com.google.zxing:core:3.2.1'
compile 'com.google.code.gson:gson:2.7'
}

View File

@ -0,0 +1,125 @@
package es.ugr.swad.swadroid.model;
import es.ugr.swad.swadroid.Constants;
/**
* Login data.
*
* @author Juan Miguel Boyero Corral <juanmi1982@gmail.com>
*/
public class LoginInfo {
/**
* User logged flag
*/
private boolean logged;
/**
* Logged user
*/
private User loggedUser;
/**
* Time of application's last login
*/
private long lastLoginTime;
/**
* Role of the logged User in the current selected course
*/
private int currentUserRole;
public LoginInfo() {
this.logged = false;
this.loggedUser = new User();
this.lastLoginTime = -1;
this.currentUserRole = -1;
}
public LoginInfo(boolean logged, User loggedUser, long lastLoginTime, int currentUserRole) {
this.logged = logged;
this.loggedUser = loggedUser;
this.lastLoginTime = lastLoginTime;
this.currentUserRole = currentUserRole;
}
/**
* Checks if user is already logged on SWAD
*
* @return User logged flag
*/
public boolean isLogged() {
return logged;
}
/**
* Sets user logged flag
*
* @param logged User logged flag
*/
public void setLogged(boolean logged) {
this.logged = logged;
}
/**
* Gets the user logged on SWAD
*/
public User getLoggedUser() {
return loggedUser;
}
/**
* Sets the user logged on SWAD
*/
public void setLoggedUser(User loggedUser) {
this.loggedUser = loggedUser;
}
/**
* Gets the last synchronization time
*
* @return The last synchronization time
*/
public long getLastLoginTime() {
return lastLoginTime;
}
/**
* Sets the last synchronization time
*
* @param lastLoginTime The last synchronization time
*/
public void setLastLoginTime(long lastLoginTime) {
this.lastLoginTime = lastLoginTime;
}
/**
* Gets the role of the logged user in the current selected course
*
* @return -1 if the user role has not been fixed,
* 0 if the user role is unknown
* 2 (STUDENT_TYPE_CODE) if the user is a student
* 3 (TEACHER_TYPE_CODE) if the user is a teacher
*/
public int getCurrentUserRole() {
return currentUserRole;
}
/**
* Sets user role in the current selected course
*
* @param userRole Role of the user: 0- unknown STUDENT_TYPE_CODE - student TEACHER_TYPE_CODE - teacher
*/
public void setCurrentUserRole(int userRole) {
if (userRole == 0 || userRole == Constants.TEACHER_TYPE_CODE || userRole == Constants.STUDENT_TYPE_CODE)
currentUserRole = userRole;
else
currentUserRole = -1;
}
@Override
public String toString() {
return "LoginInfo{" +
"logged=" + logged +
", loggedUser=" + loggedUser +
", lastLoginTime=" + lastLoginTime +
", currentUserRole=" + currentUserRole +
'}';
}
}

View File

@ -94,6 +94,10 @@ public class User extends Model {
PI_userRole
};
public User() {
super(-1);
}
/**
* Constructor.
*
@ -416,4 +420,18 @@ public class User extends Model {
}
}
@Override
public String toString() {
return "User{" +
"wsKey='" + wsKey + '\'' +
", userID='" + userID + '\'' +
", userNickname='" + userNickname + '\'' +
", userSurname1='" + userSurname1 + '\'' +
", userSurname2='" + userSurname2 + '\'' +
", userFirstname='" + userFirstname + '\'' +
", userPhoto='" + userPhoto + '\'' +
", userBirthday=" + userBirthday +
", userRole=" + userRole +
'}';
}
}

View File

@ -27,6 +27,7 @@ import org.ksoap2.serialization.SoapObject;
import es.ugr.swad.swadroid.Config;
import es.ugr.swad.swadroid.Constants;
import es.ugr.swad.swadroid.model.LoginInfo;
import es.ugr.swad.swadroid.model.User;
import es.ugr.swad.swadroid.modules.Module;
import es.ugr.swad.swadroid.preferences.Preferences;
@ -41,34 +42,30 @@ import es.ugr.swad.swadroid.webservices.SOAPClient;
* @author Antonio Aguilera Malagon <aguilerin@gmail.com>
*/
public class Login extends Module {
/**
* Time to force relogin
*/
public static final int RELOGIN_TIME = 86400000; //24h
/**
* User logged flag
*/
private static boolean logged;
/**
* Logged user
*/
private static User loggedUser;
/**
* Time of application's last login
*/
private static long lastLoginTime;
/**
* Role of the logged User in the current selected course
*/
private static int currentUserRole = -1;
/**
* Login tag name for Logcat
*/
private static final String TAG = Constants.APP_TAG + " Login";
/**
* Time to force relogin
*/
public static final int RELOGIN_TIME = 86400000; //24h
/**
* Login data
*/
private static LoginInfo loginInfo;
//Initialize login data
static {
loginInfo = Preferences.getLoginInfo();
if(loginInfo == null) {
loginInfo = new LoginInfo();
}
}
/* (non-Javadoc)
* @see android.app.Activity#onCreate()
*/
* @see android.app.Activity#onCreate()
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -104,12 +101,12 @@ public class Login extends Module {
throws Exception {
//If last login time > Global.RELOGIN_TIME, force login
if (System.currentTimeMillis() - Login.getLastLoginTime() > Login.RELOGIN_TIME) {
Login.setLogged(false);
if (System.currentTimeMillis() - loginInfo.getLastLoginTime() > Login.RELOGIN_TIME) {
loginInfo.setLogged(false);
}
//If the application isn't logged, force login
if (!Login.isLogged()) {
if (!loginInfo.isLogged()) {
String userID = Preferences.getUserID();
//If the user ID is a DNI
@ -148,11 +145,11 @@ public class Login extends Module {
Integer.parseInt(soap.getProperty("userRole").toString()) // userRole
);
Login.setLogged(true);
Login.setLoggedUser(user);
loginInfo.setLogged(true);
loginInfo.setLoggedUser(user);
//Update application last login time
Login.setLastLoginTime(System.currentTimeMillis());
loginInfo.setLastLoginTime(System.currentTimeMillis());
if(isDebuggable) {
Log.d(TAG, "id=" + user.getId());
@ -165,12 +162,15 @@ public class Login extends Module {
Log.d(TAG, "userPhoto=" + user.getUserPhoto());
Log.d(TAG, "userBirthday=" + ((user.getUserBirthday() != null) ? user.getUserBirthday().getTime(): "null"));
Log.d(TAG, "userRole=" + user.getUserRole());
Log.d(TAG, "isLogged=" + Login.isLogged());
Log.d(TAG, "lastLoginTime=" + Login.getLastLoginTime());
Log.d(TAG, "isLogged=" + loginInfo.isLogged());
Log.d(TAG, "lastLoginTime=" + loginInfo.getLastLoginTime());
}
}
}
//Save login data
setLoginInfo(loginInfo);
//Request finalized without errors
setResult(RESULT_OK);
}
@ -191,77 +191,97 @@ public class Login extends Module {
}
/**
* Checks if user is already logged on SWAD
*
* @return User logged flag
*/
public static boolean isLogged() {
return logged;
}
/**
* Gets the login data
*
* @return The login data
*/
public static LoginInfo getLoginInfo() {
return loginInfo;
}
/**
* Sets user logged flag
*
* @param logged User logged flag
*/
public static void setLogged(boolean logged) {
Login.logged = logged;
}
/**
* Sets the login data
*
* @param loginInfo The login data
*/
public static void setLoginInfo(LoginInfo loginInfo) {
Login.loginInfo = loginInfo;
Preferences.setLoginInfo(loginInfo);
/**
* Gets the user logged on SWAD
*/
public static User getLoggedUser() {
return loggedUser;
}
Log.i(TAG, "LoginInfo saved: " + loginInfo.toString());
}
/**
* Sets the user logged on SWAD
*/
public static void setLoggedUser(User loggedUser) {
Login.loggedUser = loggedUser;
}
/**
* Gets start time of application
*
* @return Start time of application
*/
public static long getLastLoginTime() {
return lastLoginTime;
}
/**
* Sets start time of application
*
* @param l Start time of application
*/
public static void setLastLoginTime(long l) {
Login.lastLoginTime = l;
}
/**
* Checks if user is already logged on SWAD
*
* @return User logged flag
*/
public static boolean isLogged() {
return loginInfo.isLogged();
}
/**
* Sets user role in the current selected course
*
* @param userRole Role of the user: 0- unknown STUDENT_TYPE_CODE - student TEACHER_TYPE_CODE - teacher
*/
public static void setCurrentUserRole(int userRole) {
if (userRole == 0 || userRole == Constants.TEACHER_TYPE_CODE || userRole == Constants.STUDENT_TYPE_CODE)
currentUserRole = userRole;
else
currentUserRole = -1;
}
/**
* Sets user logged flag
*
* @param logged User logged flag
*/
public static void setLogged(boolean logged) {
loginInfo.setLogged(logged);
}
/**
* Gets the role of the logged user in the current selected course
*
* @return -1 if the user role has not been fixed,
* 0 if the user role is unknown
* 2 (STUDENT_TYPE_CODE) if the user is a student
* 3 (TEACHER_TYPE_CODE) if the user is a teacher
*/
public static int getCurrentUserRole() {
return currentUserRole;
}
/**
* Gets the user logged on SWAD
*/
public static User getLoggedUser() {
return loginInfo.getLoggedUser();
}
/**
* Sets the user logged on SWAD
*/
public static void setLoggedUser(User loggedUser) {
loginInfo.setLoggedUser(loggedUser);
}
/**
* Gets the last synchronization time
*
* @return The last synchronization time
*/
public static long getLastLoginTime() {
return loginInfo.getLastLoginTime();
}
/**
* Sets the last synchronization time
*
* @param lastLoginTime The last synchronization time
*/
public static void setLastLoginTime(long lastLoginTime) {
loginInfo.setLastLoginTime(lastLoginTime);
}
/**
* Gets the role of the logged user in the current selected course
*
* @return -1 if the user role has not been fixed,
* 0 if the user role is unknown
* 2 (STUDENT_TYPE_CODE) if the user is a student
* 3 (TEACHER_TYPE_CODE) if the user is a teacher
*/
public static int getCurrentUserRole() {
return loginInfo.getCurrentUserRole();
}
/**
* Sets user role in the current selected course
*
* @param userRole Role of the user: 0- unknown STUDENT_TYPE_CODE - student TEACHER_TYPE_CODE - teacher
*/
public static void setCurrentUserRole(int userRole) {
loginInfo.setCurrentUserRole(userRole);
}
}

View File

@ -324,7 +324,7 @@ public class LoginActivity extends AppCompatActivity implements AdapterView.OnIt
switch (requestCode) {
case Constants.LOGIN_REQUEST_CODE:
mProgressScreen.hide();
Login.setLogged(true);
Login.getLoginInfo().setLogged(true);
setResult(RESULT_OK);
mFromPreference = false;
finish();

View File

@ -26,11 +26,14 @@ import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import com.google.gson.Gson;
import java.security.NoSuchAlgorithmException;
import es.ugr.swad.swadroid.Constants;
import es.ugr.swad.swadroid.analytics.SWADroidTracker;
import es.ugr.swad.swadroid.database.DataBaseHelper;
import es.ugr.swad.swadroid.model.LoginInfo;
import es.ugr.swad.swadroid.modules.login.Login;
import es.ugr.swad.swadroid.sync.SyncUtils;
import es.ugr.swad.swadroid.utils.Crypto;
@ -145,6 +148,10 @@ public class Preferences {
* Authors preference name
*/
public static final String AUTHORSPREF = "authorsPref";
/**
* Authors preference name
*/
public static final String LOGININFOPREF = "loginInfoPref";
/**
* Database Helper.
*/
@ -157,7 +164,6 @@ public class Preferences {
/**
* Gets application preferences
* @param ctx Application context
* @return Application preferences
*/
private static void getPreferences(Context ctx) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
@ -423,6 +429,37 @@ public class Preferences {
editor.commit();
}
/**
* Gets the login data
*
* @return The login data
*/
public static LoginInfo getLoginInfo() {
Gson gson = new Gson();
String json = prefs.getString(LOGININFOPREF, null);
return gson.fromJson(json, LoginInfo.class);
}
/**
* Sets the login data
*
* @param loginInfo The login data
*/
public static void setLoginInfo(LoginInfo loginInfo) {
Gson gson = new Gson();
String json = gson.toJson(loginInfo);
editor = editor.putString(LOGININFOPREF, json);
editor.commit();
}
/**
* Removes the login data
*/
public static void removeLoginInfo() {
editor = editor.remove(LOGININFOPREF);
editor.commit();
}
/**
* Upgrade password encryption
*
@ -446,7 +483,8 @@ public class Preferences {
}
public static void logoutClean(Context context, String key) {
Login.setLogged(false);
Login.getLoginInfo().setLogged(false);
removeLoginInfo();
Log.i(TAG, "Forced logout due to " + key + " change in preferences");
cleanDatabase();

View File

@ -45,6 +45,7 @@ import es.ugr.swad.swadroid.Constants;
import es.ugr.swad.swadroid.R;
import es.ugr.swad.swadroid.analytics.SWADroidTracker;
import es.ugr.swad.swadroid.gui.DialogFactory;
import es.ugr.swad.swadroid.model.LoginInfo;
import es.ugr.swad.swadroid.modules.login.Login;
import es.ugr.swad.swadroid.modules.login.LoginActivity;
import es.ugr.swad.swadroid.sync.SyncUtils;
@ -214,7 +215,6 @@ public class PreferencesActivity extends PreferenceActivity implements OnPrefere
Preferences.logoutClean(ctx, Preferences.LOGOUTPREF);
Preferences.setUserID("");
Preferences.setUserPassword("");
Login.setLogged(false);
startActivity(new Intent(getBaseContext(), LoginActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP).putExtra("fromPreference", true));
@ -339,7 +339,7 @@ public class PreferencesActivity extends PreferenceActivity implements OnPrefere
userPasswordPrefChanged = true;
syncPrefsChanged = true;
Preferences.setPreferencesChanged();
Login.setLogged(false);
Login.getLoginInfo().setLogged(false);
} else {
Toast.makeText(getApplicationContext(), R.string.pradoLoginToast,
Toast.LENGTH_LONG).show();