From daa2308626c3455d7703d14f57f17e2d9472c484 Mon Sep 17 00:00:00 2001 From: Pierre Date: Mon, 6 Nov 2017 19:02:29 +0100 Subject: [PATCH] Created account package --- .../android/comunic/client/LoginActivity.java | 29 +++- .../android/comunic/client/MainActivity.java | 15 +- .../client/data/Account/AccountUtils.java | 140 ++++++++++++++++++ .../client/data/UsersInfo/GetUsersInfos.java | 39 +++-- 4 files changed, 204 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/Account/AccountUtils.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java index 4760a80..1f0ceca 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java @@ -11,6 +11,7 @@ import android.widget.TextView; import android.widget.Toast; import org.communiquons.android.comunic.client.data.Account.Account; +import org.communiquons.android.comunic.client.data.Account.AccountUtils; import org.communiquons.android.comunic.client.data.Utilities; import org.json.JSONException; import org.json.JSONObject; @@ -32,6 +33,11 @@ public class LoginActivity extends AppCompatActivity { */ private Utilities utils; + /** + * Account utilities object + */ + private AccountUtils aUtils; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -40,6 +46,9 @@ public class LoginActivity extends AppCompatActivity { //Create utilities object utils = new Utilities(this); + //Create account utilities object + aUtils = new AccountUtils(this); + //Check for connectivity if(!APIRequestTask.isAPIavailable(this)){ Toast.makeText(this, R.string.err_no_internet_connection, Toast.LENGTH_SHORT).show(); @@ -163,9 +172,23 @@ public class LoginActivity extends AppCompatActivity { return; } - //Redirect to the main activity - Intent redirect = new Intent(this, MainActivity.class); - startActivity(redirect); + //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 + Intent redirect = new Intent(LoginActivity.this, MainActivity.class); + startActivity(redirect); + } + + } + }); } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java index 6e558cc..cd374d0 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java @@ -11,6 +11,7 @@ import android.widget.Toast; import org.communiquons.android.comunic.client.api.APIRequestTask; import org.communiquons.android.comunic.client.data.Account.Account; +import org.communiquons.android.comunic.client.data.Account.AccountUtils; import org.communiquons.android.comunic.client.data.DatabaseHelper; import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersInfos; import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; @@ -24,9 +25,14 @@ import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; public class MainActivity extends AppCompatActivity { /** - * Acount object + * Account object */ - Account account; + private Account account; + + /** + * Account utils object + */ + private AccountUtils aUtils; @Override protected void onCreate(Bundle savedInstanceState) { @@ -38,14 +44,15 @@ public class MainActivity extends AppCompatActivity { Toast.makeText(this, R.string.err_no_internet_connection, Toast.LENGTH_SHORT).show(); } - //Initialize account object + //Initialize account objects account = new Account(this); + aUtils = new AccountUtils(this); //DEVELOPMENT : Try to get information about a user over the network GetUsersInfos uInfos = new GetUsersInfos(this, new DatabaseHelper(this)); //Get infos... about me! :) - final int uID = 1; + final int uID = aUtils.get_current_user_id(); uInfos.get(uID, new GetUsersInfos.getUserInfosCallback() { @Override public void callback(UserInfo info) { diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/Account/AccountUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/Account/AccountUtils.java new file mode 100644 index 0000000..7a1f3d0 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/Account/AccountUtils.java @@ -0,0 +1,140 @@ +package org.communiquons.android.comunic.client.data.Account; + +import android.content.Context; + +import org.communiquons.android.comunic.client.api.APIRequestParameters; +import org.communiquons.android.comunic.client.api.APIRequestTask; +import org.communiquons.android.comunic.client.api.APIResponse; +import org.communiquons.android.comunic.client.data.Utilities; +import org.json.JSONException; +import org.json.JSONObject; + +/** + * Account utilities functions + * + * @author Pierre HUBERT + * Created by pierre on 11/5/17. + */ + +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 + APIRequestParameters params = new APIRequestParameters(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; + } + } + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersInfos.java b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersInfos.java index 719c288..345a203 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersInfos.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersInfos.java @@ -66,13 +66,19 @@ public class GetUsersInfos { */ public void get(int id, getUserInfosCallback callback){ + //Check if the ID is positive, error else + if(id < 1){ + callback.callback(null); //This is an error + } + //Check if the user is already present in the database or not if(!udbHelper.exists(id)) //Perform a request on the server - getOnServer(id, callback); + getOnServer(id, callback); //Else we can retrieve user informations from the local database - callback.callback(udbHelper.get(id)); + else + callback.callback(udbHelper.get(id)); } @@ -98,20 +104,29 @@ public class GetUsersInfos { UserInfo userInfos = null; try { + if(result != null) { - //Try to extract user informations - JSONObject userObject = result.getJSONObject().getJSONObject(""+id); + //Try to extract user informations + JSONObject userObjectContainer = result.getJSONObject(); + + if (userObjectContainer != null) { + + //Extract user object + JSONObject userObject = userObjectContainer.getJSONObject("" + id); + + //Continue only if we could extract required informations + if (userObject != null) { + //Parse user informations + userInfos = parse_user_json(userObject); + } + + //Save user information in the local database in case of success + if (userInfos != null) + udbHelper.insertOrUpdate(userInfos); + } - //Continue only if we could extract required informations - if (userObject != null) { - //Parse user informations - userInfos = parse_user_json(userObject); } - //Save user information in the local database in case of success - if(userInfos != null) - udbHelper.insertOrUpdate(userInfos); - } catch (JSONException e){ e.printStackTrace(); }