From 6089b20b9a951fef1eb9ac0e4ee4f74fda23e878 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 5 Nov 2017 17:23:28 +0100 Subject: [PATCH] Work progress on signed in user requests --- .idea/misc.xml | 2 +- .../android/comunic/client/LoginActivity.java | 4 +- .../android/comunic/client/MainActivity.java | 19 ++++ .../client/api/APIRequestParameters.java | 25 +++++- .../comunic/client/api/APIRequestTask.java | 33 ++++++- .../comunic/client/{ => data}/Account.java | 31 +++++-- .../client/data/UsersInfo/GetUsersInfos.java | 86 +++++++++++++++++++ .../data/UsersInfo/UsersInfosDbHelper.java | 43 ++++++++-- .../comunic/client/{ => data}/Utilities.java | 8 +- 9 files changed, 225 insertions(+), 26 deletions(-) rename app/src/main/java/org/communiquons/android/comunic/client/{ => data}/Account.java (82%) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersInfos.java rename app/src/main/java/org/communiquons/android/comunic/client/{ => data}/Utilities.java (93%) diff --git a/.idea/misc.xml b/.idea/misc.xml index fbb6828..5d19981 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + 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 f28af0a..6debca9 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 @@ -10,6 +10,8 @@ import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; +import org.communiquons.android.comunic.client.data.Account; +import org.communiquons.android.comunic.client.data.Utilities; import org.json.JSONException; import org.json.JSONObject; @@ -97,7 +99,7 @@ public class LoginActivity extends AppCompatActivity { //Perform a request on the API to check user credentials and get login tokens - APIRequestParameters params = new APIRequestParameters("user/connectUSER"); + APIRequestParameters params = new APIRequestParameters(this, "user/connectUSER"); params.addParameter("userMail", ""+login_mail.getText()); params.addParameter("userPassword", ""+login_password.getText()); 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 7a89f6b..4380b36 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 @@ -10,6 +10,10 @@ import android.view.MenuItem; import android.widget.Toast; import org.communiquons.android.comunic.client.api.APIRequestTask; +import org.communiquons.android.comunic.client.data.Account; +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; /** @@ -36,6 +40,21 @@ public class MainActivity extends AppCompatActivity { //Initialize account object account = new Account(this); + + //DEVELOPMENT : Try to get information about a user over the network + GetUsersInfos uInfos = new GetUsersInfos(this, null); + + //Get infos... about me! + int uID = 1; + uInfos.get(uID, new GetUsersInfos.getUserInfosCallback() { + @Override + public void callback(UserInfo info) { + if(info == null) + Toast.makeText(MainActivity.this, "Failure !", Toast.LENGTH_SHORT).show(); + else + Toast.makeText(MainActivity.this, "Success !", Toast.LENGTH_SHORT).show(); + } + }); } @Override diff --git a/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequestParameters.java b/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequestParameters.java index cf870aa..8c7767c 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequestParameters.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequestParameters.java @@ -1,5 +1,7 @@ package org.communiquons.android.comunic.client.api; +import android.content.Context; + import org.communiquons.android.comunic.client.api.APIPostData; import java.util.ArrayList; @@ -13,6 +15,11 @@ import java.util.ArrayList; public class APIRequestParameters { + /** + * The context of the request + */ + private Context context; + /** * Parameters of the request */ @@ -26,13 +33,18 @@ public class APIRequestParameters { /** * The class constructor * + * @param context The context of the request * @param uri The request URI on the server */ - public APIRequestParameters(String uri){ + public APIRequestParameters(Context context, String uri){ + + //Save the context + this.context = context; + //Save request URI request_uri = uri; - //Intializate parametres array + //Intialize parameters array parameters = new ArrayList<>(); } @@ -76,4 +88,13 @@ public class APIRequestParameters { return result; } + + /** + * Get the context of the request + * + * @return The context of the request + */ + public Context getContext() { + return context; + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequestTask.java b/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequestTask.java index 1620fc2..5fdcfeb 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequestTask.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequestTask.java @@ -4,9 +4,10 @@ import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.AsyncTask; +import android.util.Log; +import org.communiquons.android.comunic.client.data.Account; import org.communiquons.android.comunic.client.BuildConfig; -import org.json.JSONObject; import java.io.BufferedOutputStream; import java.io.BufferedWriter; @@ -18,6 +19,7 @@ import java.io.OutputStreamWriter; import java.io.Reader; import java.net.HttpURLConnection; import java.net.URL; +import java.util.ArrayList; /** * Perform an API request on the server @@ -33,11 +35,11 @@ import java.net.URL; public abstract class APIRequestTask extends AsyncTask { /** - * Background task + * API request in a Background task * * Warning: This method support only one request per object * - * @param params Parametres required to perform the API request + * @param params Parameters required to perform the API request * @return JSONObject The result of the request */ @Override @@ -45,6 +47,7 @@ public abstract class APIRequestTask extends AsyncTask tokens = account.getLoginTokens(); + + if(tokens.size() < 2) + return; //Not enough tokens + + //Add them to the request + params.addParameter("userToken1", tokens.get(0)); + params.addParameter("userToken2", tokens.get(1)); + + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/Account.java b/app/src/main/java/org/communiquons/android/comunic/client/data/Account.java similarity index 82% rename from app/src/main/java/org/communiquons/android/comunic/client/Account.java rename to app/src/main/java/org/communiquons/android/comunic/client/data/Account.java index 09451c1..ce677e5 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/Account.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/Account.java @@ -1,4 +1,4 @@ -package org.communiquons.android.comunic.client; +package org.communiquons.android.comunic.client.data; import android.content.Context; import android.util.Log; @@ -18,7 +18,7 @@ import java.util.Objects; * Created by pierre on 10/29/17. */ -class Account { +public class Account { /** * Utilities object @@ -45,7 +45,7 @@ class Account { * * @param context Context of the application */ - Account(Context context){ + public Account(Context context){ mContext = context; utils = new Utilities(context); @@ -58,7 +58,7 @@ class Account { * * @return True if signed in */ - boolean signed_in(){ + public boolean signed_in(){ //Check if tokens are already loaded if(tokens.size() < 1){ @@ -73,7 +73,7 @@ class Account { * Sign out user * @return True in case of success / false else */ - boolean sign_out(){ + public boolean sign_out(){ return remove_login_tokens(); } @@ -115,13 +115,32 @@ class Account { return true; } + /** + * Get and return login tokens + * + * @return The list of tokens / null if user isn't signed in + */ + public ArrayList getLoginTokens(){ + + //Check if tokens are already loaded or not + if(tokens.size()< 1) + load_tokens(); + + //Check if there is still no tokens + if(tokens.size() < 1) + return null; //Nothing to be done + + //Return tokens list + return tokens; + } + /** * Save new tokens in tokens file * * @param toks The array containing the tokens * @return False in case of failure */ - boolean save_new_tokens(ArrayList toks){ + public boolean save_new_tokens(ArrayList toks){ //Create tokens array JSONArray tokens = new JSONArray(); 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 new file mode 100644 index 0000000..42b193f --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersInfos.java @@ -0,0 +1,86 @@ +package org.communiquons.android.comunic.client.data.UsersInfo; + +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.DatabaseHelper; + +/** + * This class handles informations requests about user informations + * + * @author Pierre HUBERT + * Created by pierre on 11/5/17. + */ + +public class GetUsersInfos { + + /** + * Database helper + */ + private DatabaseHelper dbHelper = null; + + /** + * Operations context + */ + private Context context; + + /** + * Public constructor + * + * @param context The context of the application + * @param dbHelper Database helper object + */ + public GetUsersInfos(Context context, DatabaseHelper dbHelper){ + + //Save context + this.context = context; + + //Save database helper object + this.dbHelper = dbHelper; + + } + + /** + * This interface must be implemented to perform an API request + */ + public interface getUserInfosCallback{ + + /** + * Callback function called when we got informations about user + * + * @param info Information about the user + */ + void callback(UserInfo info); + + } + + /** + * Get and return the informations about a user + * + * @param id The ID of the user to get informations from + */ + public void get(int id, getUserInfosCallback callback){ + + //Perform a request on the API server + + //Setup the request + APIRequestParameters requestParameters = new APIRequestParameters(context, "user/getInfos"); + requestParameters.addParameter("userID", ""+id); + + //Do it. + new APIRequestTask(){ + + @Override + protected void onPostExecute(APIResponse result) { + + + + } + + }.execute(requestParameters); + + } + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersInfosDbHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersInfosDbHelper.java index 3a5d455..d3edefe 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersInfosDbHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersInfosDbHelper.java @@ -8,7 +8,7 @@ import org.communiquons.android.comunic.client.data.DatabaseContract.UsersInfoSc import org.communiquons.android.comunic.client.data.DatabaseHelper; /** - * Users informations helpers + * Users information helpers * * Makes the interface between the UsersInfo object and the SQLite object * @@ -152,16 +152,17 @@ public class UsersInfosDbHelper { c.moveToFirst(); //Extract the information and record them - result.setId( - c.getInt( - c.getColumnIndexOrThrow( - UsersInfoSchema.COLUMN_NAME_USER_ID - ) - ) - ); + result.setId(c.getInt(c.getColumnIndexOrThrow( + UsersInfoSchema.COLUMN_NAME_USER_ID))); + result.setFirstName(c.getString(c.getColumnIndexOrThrow( + UsersInfoSchema.COLUMN_NAME_USER_FIRSTNAME))); - //TODO : finish extraction + result.setLastName(c.getString(c.getColumnIndexOrThrow( + UsersInfoSchema.COLUMN_NAME_USER_LASTNAME))); + + result.setAccountImageURL(c.getString(c.getColumnIndexOrThrow( + UsersInfoSchema.COLUMN_NAME_USER_ACCOUNT_IMAGE))); } //Close the cursor @@ -172,4 +173,28 @@ public class UsersInfosDbHelper { return result; } + + /** + * Remove a user from the database (if he is present) + * + * @param userID The ID of the user to delete + * @return False if nothing was deleted + */ + boolean delete(int userID){ + + //Get write access to the database + SQLiteDatabase db = dbHelper.getWritableDatabase(); + + //Prepare the request + String condition = UsersInfoSchema.COLUMN_NAME_USER_ID + " = ?"; + String[] conditionArgs = {""+userID}; + + //Perform the request + int result = db.delete(UsersInfoSchema.TABLE_NAME, condition, conditionArgs); + + //Close database + db.close(); + + return result > 0; + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/Utilities.java b/app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java similarity index 93% rename from app/src/main/java/org/communiquons/android/comunic/client/Utilities.java rename to app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java index b20222e..9102950 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/Utilities.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java @@ -1,4 +1,4 @@ -package org.communiquons.android.comunic.client; +package org.communiquons.android.comunic.client.data; import android.content.Context; import android.text.TextUtils; @@ -15,7 +15,7 @@ import java.io.FileOutputStream; * Created by pierre on 10/29/17. */ -class Utilities { +public class Utilities { /** * Application context @@ -27,7 +27,7 @@ class Utilities { * * @param context Context of the application */ - Utilities(Context context){ + public Utilities(Context context){ mContext = context; } @@ -101,7 +101,7 @@ class Utilities { * @param mail The E-Mail address to check * @return True if the mail is valid / false else */ - boolean isValidMail(CharSequence mail){ + public boolean isValidMail(CharSequence mail){ return !TextUtils.isEmpty(mail) && Patterns.EMAIL_ADDRESS.matcher(mail).matches(); } }