mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 13:59:29 +00:00
Work progress on signed in user requests
This commit is contained in:
parent
5584840145
commit
6089b20b9a
@ -37,7 +37,7 @@
|
||||
<ConfirmationsSetting value="0" id="Add" />
|
||||
<ConfirmationsSetting value="0" id="Remove" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||
</component>
|
||||
<component name="ProjectType">
|
||||
|
@ -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());
|
||||
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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<APIRequestParameters, Void, APIResponse> {
|
||||
|
||||
/**
|
||||
* 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<APIRequestParameters, Voi
|
||||
|
||||
try {
|
||||
//Perform the API request
|
||||
addLoginTokens(params[0]);
|
||||
return downloadUrl(params[0]);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@ -142,4 +145,28 @@ public abstract class APIRequestTask extends AsyncTask<APIRequestParameters, Voi
|
||||
|
||||
return networkInfo != null && networkInfo.isConnected();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the login tokens to an API request object
|
||||
*/
|
||||
private void addLoginTokens(APIRequestParameters params){
|
||||
|
||||
//Create account object
|
||||
Account account = new Account(params.getContext());
|
||||
|
||||
//Check if user is signed in or not
|
||||
if(!account.signed_in())
|
||||
return; //Do nothing
|
||||
|
||||
//Get login tokens
|
||||
ArrayList<String> 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));
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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<String> 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<String> toks){
|
||||
public boolean save_new_tokens(ArrayList<String> toks){
|
||||
|
||||
//Create tokens array
|
||||
JSONArray tokens = new JSONArray();
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user