Created account package

This commit is contained in:
Pierre 2017-11-06 19:02:29 +01:00
parent a5c5c525db
commit daa2308626
4 changed files with 204 additions and 19 deletions

View File

@ -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;
}
//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(this, MainActivity.class);
Intent redirect = new Intent(LoginActivity.this, MainActivity.class);
startActivity(redirect);
}
}
});
}

View File

@ -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) {

View File

@ -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;
}
}
}

View File

@ -66,12 +66,18 @@ 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);
//Else we can retrieve user informations from the local database
else
callback.callback(udbHelper.get(id));
}
@ -98,9 +104,15 @@ public class GetUsersInfos {
UserInfo userInfos = null;
try {
if(result != null) {
//Try to extract user informations
JSONObject userObject = result.getJSONObject().getJSONObject(""+id);
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) {
@ -109,8 +121,11 @@ public class GetUsersInfos {
}
//Save user information in the local database in case of success
if(userInfos != null)
if (userInfos != null)
udbHelper.insertOrUpdate(userInfos);
}
}
} catch (JSONException e){
e.printStackTrace();