Removed useless threads.

This commit is contained in:
Pierre HUBERT 2018-09-01 08:42:32 +02:00
parent d5cda41fb6
commit a90ee9d5f5
9 changed files with 244 additions and 267 deletions

View File

@ -0,0 +1,29 @@
package org.communiquons.android.comunic.client.data.enums;
/**
* Login result state
*
* @author Pierre HUBERT
*/
public enum LoginResult {
/**
* Login succeeded
*/
SUCCESS,
/**
* Too many login attempts
*/
TOO_MANY_ATTEMPTS,
/**
* Invalid credentials
*/
INVALID_CREDENTIALS,
/**
* Server error
*/
SERVER_ERROR
}

View File

@ -4,18 +4,20 @@ import android.content.Context;
import android.util.Log; import android.util.Log;
import org.communiquons.android.comunic.client.data.enums.CreateAccountResult; import org.communiquons.android.comunic.client.data.enums.CreateAccountResult;
import org.communiquons.android.comunic.client.data.enums.LoginResult;
import org.communiquons.android.comunic.client.data.models.APIRequest; import org.communiquons.android.comunic.client.data.models.APIRequest;
import org.communiquons.android.comunic.client.data.models.APIResponse; import org.communiquons.android.comunic.client.data.models.APIResponse;
import org.communiquons.android.comunic.client.data.models.NewAccount; import org.communiquons.android.comunic.client.data.models.NewAccount;
import org.communiquons.android.comunic.client.data.utils.Utilities; import org.communiquons.android.comunic.client.data.utils.Utilities;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Objects; import java.util.Objects;
/** /**
* Comunic account class * Comunic account helper class
* *
* This class stores the account tokens * This class stores the account tokens
* *
@ -23,7 +25,7 @@ import java.util.Objects;
* Created by pierre on 10/29/17. * Created by pierre on 10/29/17.
*/ */
public class AccountHelper { public class AccountHelper extends BaseHelper {
/** /**
* Utilities object * Utilities object
@ -41,9 +43,9 @@ public class AccountHelper {
private String tokFilename = "login_tokens.json"; private String tokFilename = "login_tokens.json";
/** /**
* Application context * The name of the userID file
*/ */
private Context mContext; private static final String USER_ID_FILENAME = "user_id.txt";
/** /**
* Account class constructor * Account class constructor
@ -51,27 +53,72 @@ public class AccountHelper {
* @param context Context of the application * @param context Context of the application
*/ */
public AccountHelper(Context context){ public AccountHelper(Context context){
mContext = context; super(context);
utils = new Utilities(context); utils = new Utilities(context);
//Initialize tokens array //Initialize tokens array
tokens = new ArrayList<>(); tokens = new ArrayList<>();
} }
/**
* Intend to sign in user
*
* @param email Email address of the user
* @param password The password of the user
* @return The result of the operation
*/
public LoginResult sign_in(String email, String password){
APIRequest request = new APIRequest(getContext(), "account/login");
request.setTryContinueOnError(true);
request.addString("userMail", email);
request.addString("userPassword", password);
try {
APIResponse response = new APIRequestHelper().exec(request);
//Check for login errors
if(response.getResponse_code() != 200){
return response.getResponse_code() == 429 ?
LoginResult.TOO_MANY_ATTEMPTS :
LoginResult.INVALID_CREDENTIALS;
}
//Get login tokens
JSONObject tokensObj = response.getJSONObject().getJSONObject("tokens");
ArrayList<String> tokens = new ArrayList<>();
tokens.add(tokensObj.getString("token1"));
tokens.add(tokensObj.getString("token2"));
//Try to save new tokens
if(!save_new_tokens(tokens))
return LoginResult.SERVER_ERROR;
//Get user ID
if(fetch_current_user_id() < 1)
return LoginResult.SERVER_ERROR;
//Success
return LoginResult.SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return LoginResult.SERVER_ERROR;
}
}
/** /**
* Determine whether user is signed in or not * Determine whether user is signed in or not
* *
* @return True if signed in * @return True if signed in
*/ */
public boolean signed_in(){ public boolean signed_in() {
//Check if tokens are already loaded //Check if tokens are already loaded
if(tokens.size() < 1){ return tokens.size() >= 1 || load_tokens();
if(!load_tokens())
return false;
}
return true;
} }
/** /**
@ -82,6 +129,103 @@ public class AccountHelper {
return remove_login_tokens(); return remove_login_tokens();
} }
/**
* Fetch on the server the current user ID
*
* @return Current user ID / -1 in case of failure
*/
private int fetch_current_user_id(){
APIRequest request = new APIRequest(getContext(), "user/getCurrentUserID");
try {
APIResponse response = new APIRequestHelper().exec(request);
if(response.getResponse_code() != 200)
return -1;
int userID = response.getJSONObject().getInt("userID");
return save_new_user_id(userID) ? userID : -1;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
/**
* Get the current user ID
*
* @return The user ID or -1 in case of error
*/
public int get_current_user_id(){
//Get file content
String userIDString = utils.file_get_content(USER_ID_FILENAME);
//Convert into an int
try {
int userID = Integer.decode(userIDString);
//Return user ID
return userID > 0 ? userID : -1;
} catch (NumberFormatException e){
e.printStackTrace();
//This is a failure
return -1;
}
}
/**
* Save new user ID
*
* @param id The new ID to save
* @return True in case of success / false else
*/
private boolean save_new_user_id(int id){
//Save new file content
return utils.file_put_contents(USER_ID_FILENAME, ""+id);
}
/**
* Create a new account
*
* @param newAccount Information about the new account to create
* @return TRUE for a success / FALSE else
*/
public CreateAccountResult createAccount(NewAccount newAccount) {
APIRequest request = new APIRequest(getContext(), "account/create");
request.setTryContinueOnError(true);
request.addString("firstName", newAccount.getFirstName());
request.addString("lastName", newAccount.getLastName());
request.addString("emailAddress", newAccount.getEmail());
request.addString("password", newAccount.getPassword());
//Perform the request
try {
APIResponse response = new APIRequestHelper().exec(request);
switch (response.getResponse_code()) {
case 200:
return CreateAccountResult.SUCCESS;
case 409:
return CreateAccountResult.ERROR_EXISTING_EMAIL;
case 429:
return CreateAccountResult.ERROR_TOO_MANY_REQUESTS;
default:
return CreateAccountResult.ERROR;
}
} catch (Exception e) {
e.printStackTrace();
return CreateAccountResult.ERROR;
}
}
/** /**
* Try to load tokens in tokens array * Try to load tokens in tokens array
* *
@ -145,7 +289,7 @@ public class AccountHelper {
* @param toks The array containing the tokens * @param toks The array containing the tokens
* @return False in case of failure * @return False in case of failure
*/ */
public boolean save_new_tokens(ArrayList<String> toks){ private boolean save_new_tokens(ArrayList<String> toks){
//Create tokens array //Create tokens array
JSONArray tokens = new JSONArray(); JSONArray tokens = new JSONArray();
@ -183,43 +327,5 @@ public class AccountHelper {
return true; return true;
} }
/**
* Create a new account
*
* @param newAccount Information about the new account to create
* @return TRUE for a success / FALSE else
*/
public CreateAccountResult createAccount(NewAccount newAccount) {
APIRequest request = new APIRequest(mContext, "account/create");
request.setTryContinueOnError(true);
request.addString("firstName", newAccount.getFirstName());
request.addString("lastName", newAccount.getLastName());
request.addString("emailAddress", newAccount.getEmail());
request.addString("password", newAccount.getPassword());
//Perform the request
try {
APIResponse response = new APIRequestHelper().exec(request);
switch (response.getResponse_code()) {
case 200:
return CreateAccountResult.SUCCESS;
case 409:
return CreateAccountResult.ERROR_EXISTING_EMAIL;
case 429:
return CreateAccountResult.ERROR_TOO_MANY_REQUESTS;
default:
return CreateAccountResult.ERROR;
}
} catch (Exception e) {
e.printStackTrace();
return CreateAccountResult.ERROR;
}
}
} }

View File

@ -184,7 +184,7 @@ public class ConversationsListHelper {
for(Integer id : users.keySet()){ for(Integer id : users.keySet()){
//Do not display current user name //Do not display current user name
if(id == new AccountUtils(mContext).get_current_user_id()) if(id == AccountUtils.getID(mContext))
continue; continue;
if(users.get(id) != null){ if(users.get(id) != null){

View File

@ -2,10 +2,7 @@ package org.communiquons.android.comunic.client.data.utils;
import android.content.Context; import android.content.Context;
import org.communiquons.android.comunic.client.data.models.APIRequest; import org.communiquons.android.comunic.client.data.helpers.AccountHelper;
import org.communiquons.android.comunic.client.data.asynctasks.APIRequestTask;
import org.communiquons.android.comunic.client.data.models.APIResponse;
import org.json.JSONObject;
/** /**
* Account utilities functions * Account utilities functions
@ -16,125 +13,6 @@ import org.json.JSONObject;
public class AccountUtils { public class AccountUtils {
/**
* Execution context
*/
private Context mContext;
/**
* Utilities object
*/
private Utilities utils;
/**
* The name of the userID file
*/
private static final String USER_ID_FILENAME = "user_id.txt";
/**
* The constructor of the object
*
* @param context The constructor of the application
*/
public AccountUtils(Context context){
//Save context
mContext = context;
//Create utilities object
utils = new Utilities(context);
}
/**
* This interface has to be implemented and passed as an argument of refresh_current_user_id()
*/
public interface onceRefreshedUserID{
/**
* Callback function
*
* @param success True in case of success / false else
*/
void callback(boolean success);
}
/**
* Refresh current user ID
*
* @param callback What to do once userID is refreshed
*/
public void refresh_current_user_id(final onceRefreshedUserID callback){
//Perform an API request
APIRequest params = new APIRequest(mContext, "user/getCurrentUserID");
new APIRequestTask(){
@Override
protected void onPostExecute(APIResponse result) {
//Remove old user ID
save_new_user_id(-1);
JSONObject response = result.getJSONObject();
//Check for errors
if(response == null)
callback.callback(false);
//Try to extract and save user ID
try {
assert response != null;
int userID = response.getInt("userID");
callback.callback(
save_new_user_id(userID)); //The success of the operation depends of the
//ability to save it too.
} catch (Exception e) {
e.printStackTrace();
}
}
}.execute(params);
}
/**
* Save new user ID
*
* @param id The new ID to save
* @return True in case of success / false else
*/
private boolean save_new_user_id(int id){
//Save new file content
return utils.file_put_contents(USER_ID_FILENAME, ""+id);
}
/**
* Get the current user ID
*
* @return The user ID or -1 in case of error
*/
public int get_current_user_id(){
//Get file content
String userIDstring = utils.file_get_content(USER_ID_FILENAME);
//Convert into an int
try {
int userIDid = Integer.decode(userIDstring);
//Return user ID
return userIDid > 0 ? userIDid : -1;
} catch (NumberFormatException e){
e.printStackTrace();
//This is a failure
return -1;
}
}
/** /**
* Get the current user ID quickly * Get the current user ID quickly
* *
@ -142,7 +20,7 @@ public class AccountUtils {
* @return The ID of the current user or -1 in case of failure * @return The ID of the current user or -1 in case of failure
*/ */
public static int getID(Context context){ public static int getID(Context context){
return new AccountUtils(context).get_current_user_id(); return new AccountHelper(context).get_current_user_id();
} }
} }

View File

@ -3,7 +3,6 @@ package org.communiquons.android.comunic.client.ui.activities;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.view.View; import android.view.View;
import android.widget.EditText; import android.widget.EditText;
@ -14,17 +13,11 @@ import android.widget.Toast;
import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.R;
import org.communiquons.android.comunic.client.data.asynctasks.SafeAsyncTask; import org.communiquons.android.comunic.client.data.asynctasks.SafeAsyncTask;
import org.communiquons.android.comunic.client.data.enums.LoginResult;
import org.communiquons.android.comunic.client.data.helpers.APIRequestHelper; import org.communiquons.android.comunic.client.data.helpers.APIRequestHelper;
import org.communiquons.android.comunic.client.data.helpers.AccountHelper; import org.communiquons.android.comunic.client.data.helpers.AccountHelper;
import org.communiquons.android.comunic.client.data.models.APIRequest;
import org.communiquons.android.comunic.client.data.models.APIResponse;
import org.communiquons.android.comunic.client.data.utils.AccountUtils;
import org.communiquons.android.comunic.client.data.utils.Utilities; import org.communiquons.android.comunic.client.data.utils.Utilities;
import org.communiquons.android.comunic.client.ui.asynctasks.APIRequestTask; import org.communiquons.android.comunic.client.ui.asynctasks.LoginTask;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
/** /**
* Login activity of the application * Login activity of the application
@ -34,15 +27,10 @@ import java.util.ArrayList;
public class LoginActivity extends AppCompatActivity { public class LoginActivity extends AppCompatActivity {
/**
* Account utilities object
*/
private AccountUtils aUtils;
/** /**
* API request task (to perform login) * API request task (to perform login)
*/ */
private APIRequestTask mApiRequestTask; private LoginTask mLoginTask;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -51,9 +39,6 @@ public class LoginActivity extends AppCompatActivity {
assert getSupportActionBar() != null; assert getSupportActionBar() != null;
getSupportActionBar().hide(); getSupportActionBar().hide();
//Create account utilities object
aUtils = new AccountUtils(this);
//Check for connectivity //Check for connectivity
if(!APIRequestHelper.isAPIavailable(this)){ if(!APIRequestHelper.isAPIavailable(this)){
Toast.makeText(this, R.string.err_no_internet_connection, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.err_no_internet_connection, Toast.LENGTH_SHORT).show();
@ -100,8 +85,8 @@ public class LoginActivity extends AppCompatActivity {
* Cancel any running task * Cancel any running task
*/ */
private void undoRunningTasks(){ private void undoRunningTasks(){
if(mApiRequestTask != null) if(mLoginTask != null)
mApiRequestTask.setOnPostExecuteListener(null); mLoginTask.setOnPostExecuteListener(null);
} }
/** /**
@ -144,23 +129,18 @@ public class LoginActivity extends AppCompatActivity {
show_form_error(""); show_form_error("");
enter_loading_state(true); enter_loading_state(true);
//Perform a request on the API to check user credentials and get login tokens
APIRequest params = new APIRequest(this, "user/connectUSER");
params.setTryContinueOnError(true);
params.addString("userMail", ""+login_mail.getText());
params.addString("userPassword", ""+login_password.getText());
//Create Request //Create Request
undoRunningTasks(); undoRunningTasks();
mApiRequestTask = new APIRequestTask(this); mLoginTask = new LoginTask(this);
mApiRequestTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<APIResponse>() { mLoginTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<LoginResult>() {
@Override @Override
public void OnPostExecute(APIResponse apiResponse) { public void OnPostExecute(LoginResult loginResult) {
handle_server_response(apiResponse); handle_server_response(loginResult);
} }
}); });
mApiRequestTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params); mLoginTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
""+login_mail.getText(), ""+login_password.getText());
} }
@ -170,71 +150,30 @@ public class LoginActivity extends AppCompatActivity {
* *
* @param response The server response * @param response The server response
*/ */
void handle_server_response(@Nullable APIResponse response){ void handle_server_response(LoginResult response){
if(response == null){
show_err_server_response();
return;
}
if(response.getResponse_code() != 200){
enter_loading_state(false); enter_loading_state(false);
if(response.getResponse_code() == 429) switch (response){
show_form_error(getString(R.string.activity_login_too_many_request));
else
show_form_error(getString(R.string.activity_login_err_invalid_credentials));
return; case SUCCESS:
}
JSONObject data = response.getJSONObject();
//Check for decoding response errors
if(data == null) {
show_err_server_response();
return;
}
try {
//Search for tokens
JSONObject tokensObj = data.getJSONObject("tokens");
//Extract tokens
ArrayList<String> tokens = new ArrayList<>();
tokens.add(tokensObj.getString("token1"));
tokens.add(tokensObj.getString("token2"));
//Save tokens
AccountHelper accountHelper = new AccountHelper(this);
if(!accountHelper.save_new_tokens(tokens)) {
show_err_server_response();
return;
}
} catch (JSONException e){
e.printStackTrace();
show_err_server_response();
return;
}
//Refresh current user ID
aUtils.refresh_current_user_id(new AccountUtils.onceRefreshedUserID(){
@Override
public void callback(boolean success) {
//Check if it is a success or a failure
if(!success){
show_err_server_response();
}
else {
//Redirect to the main activity
openMainActivity(); openMainActivity();
} break;
case TOO_MANY_ATTEMPTS:
show_form_error(getString(R.string.activity_login_too_many_request));
break;
case INVALID_CREDENTIALS:
show_form_error(getString(R.string.activity_login_err_invalid_credentials));
break;
case SERVER_ERROR:
default:
show_err_server_response();
break;
} }
});
} }

View File

@ -286,7 +286,7 @@ public class MainActivity extends AppCompatActivity implements
//User personal page //User personal page
else if(id == R.id.action_personal_page){ else if(id == R.id.action_personal_page){
openUserPage(new AccountUtils(this).get_current_user_id()); openUserPage(new AccountHelper(this).get_current_user_id());
} }
//Latest posts //Latest posts

View File

@ -0,0 +1,25 @@
package org.communiquons.android.comunic.client.ui.asynctasks;
import android.content.Context;
import org.communiquons.android.comunic.client.data.asynctasks.SafeAsyncTask;
import org.communiquons.android.comunic.client.data.enums.LoginResult;
import org.communiquons.android.comunic.client.data.helpers.AccountHelper;
/**
* User login task
*
* @author Pierre HUBERT
*/
public class LoginTask extends SafeAsyncTask<String, Void, LoginResult> {
public LoginTask(Context context) {
super(context);
}
@Override
protected LoginResult doInBackground(String... strings) {
return new AccountHelper(getContext()).sign_in(strings[0], strings[1]);
}
}

View File

@ -260,7 +260,7 @@ public class ConversationFragment extends Fragment
mAppBar = view.findViewById(R.id.appbar); mAppBar = view.findViewById(R.id.appbar);
//Need user ID //Need user ID
userID = new AccountUtils(getActivity()).get_current_user_id(); userID = AccountUtils.getID(getActivity());
//Initialize toolbar //Initialize toolbar
mAppBar.addBackButton(new View.OnClickListener() { mAppBar.addBackButton(new View.OnClickListener() {

View File

@ -246,7 +246,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
for(int userID : conv.getMembers()){ for(int userID : conv.getMembers()){
//Do not display current user name //Do not display current user name
if(userID == new AccountUtils(getActivity()).get_current_user_id()) if(userID == AccountUtils.getID(getActivity()))
continue; continue;
if(usersInfo.containsKey(userID)){ if(usersInfo.containsKey(userID)){