mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 13:59:29 +00:00
The application can store login tokens
This commit is contained in:
parent
f7b80e8f6d
commit
554d597a0e
@ -1,5 +1,8 @@
|
||||
package org.communiquons.android.comunic.client;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import org.json.JSONObject;
|
||||
@ -20,7 +23,6 @@ import java.net.URL;
|
||||
*
|
||||
* Created by pierre on 10/31/17.
|
||||
*/
|
||||
//TODO Create APIResponse class
|
||||
abstract class APIRequestTask extends AsyncTask<APIRequestParameters, Void, APIResponse> {
|
||||
|
||||
/**
|
||||
@ -83,11 +85,9 @@ abstract class APIRequestTask extends AsyncTask<APIRequestParameters, Void, APIR
|
||||
|
||||
|
||||
//Send request parameters
|
||||
OutputStream out = conn.getOutputStream();
|
||||
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(out, "UTF-8");
|
||||
BufferedWriter writer = new BufferedWriter(outputStreamWriter);
|
||||
String parameters_encoded = parameters.get_parameters_encoded();
|
||||
writer.write(parameters_encoded);
|
||||
OutputStream out = new BufferedOutputStream(conn.getOutputStream());
|
||||
BufferedWriter writer = new BufferedWriter (new OutputStreamWriter(out, "UTF-8"));
|
||||
writer.write(parameters.get_parameters_encoded());
|
||||
writer.flush();
|
||||
writer.close();
|
||||
out.close();
|
||||
@ -95,7 +95,6 @@ abstract class APIRequestTask extends AsyncTask<APIRequestParameters, Void, APIR
|
||||
//Connect to the server
|
||||
conn.connect();
|
||||
|
||||
|
||||
//Get response code
|
||||
result.setResponse_code(conn.getResponseCode());
|
||||
|
||||
@ -116,10 +115,24 @@ abstract class APIRequestTask extends AsyncTask<APIRequestParameters, Void, APIR
|
||||
|
||||
// Reads an InputStream and converts it to a String.
|
||||
private String readIt(InputStream stream, int len) throws IOException {
|
||||
Reader reader = null;
|
||||
reader = new InputStreamReader(stream, "UTF-8");
|
||||
Reader reader = new InputStreamReader(stream, "UTF-8");
|
||||
char[] buffer = new char[len];
|
||||
reader.read(buffer);
|
||||
return new String(buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine it is possible or not to connect to the API now
|
||||
*
|
||||
* @param context The context used to perform the test
|
||||
* @return True if a network connection is available
|
||||
*/
|
||||
static boolean isAPIavailable(Context context){
|
||||
ConnectivityManager connMgr = (ConnectivityManager)
|
||||
context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
|
||||
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
|
||||
|
||||
return networkInfo != null && networkInfo.isConnected();
|
||||
}
|
||||
}
|
||||
|
@ -110,7 +110,7 @@ class Account {
|
||||
* @param toks The array containing the tokens
|
||||
* @return False in case of failure
|
||||
*/
|
||||
private boolean save_new_tokens(ArrayList<String> toks){
|
||||
boolean save_new_tokens(ArrayList<String> toks){
|
||||
|
||||
//Create tokens array
|
||||
JSONArray tokens = new JSONArray();
|
||||
|
@ -1,16 +1,20 @@
|
||||
package org.communiquons.android.comunic.client;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.ScrollView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class LoginActivity extends AppCompatActivity {
|
||||
|
||||
/**
|
||||
@ -26,14 +30,17 @@ public class LoginActivity extends AppCompatActivity {
|
||||
//Create utilities object
|
||||
utils = new Utilities(this);
|
||||
|
||||
//Check for connectivity
|
||||
if(!APIRequestTask.isAPIavailable(this)){
|
||||
Toast.makeText(this, R.string.err_no_internet_connection, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
//Make login submit button lives
|
||||
findViewById(R.id.login_submit).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
//Submit login form
|
||||
submitLogin();
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
@ -46,7 +53,7 @@ public class LoginActivity extends AppCompatActivity {
|
||||
void submitLogin(){
|
||||
|
||||
//Get the fields
|
||||
EditText login_mail = (EditText) findViewById(R.id.email_field);
|
||||
final EditText login_mail = (EditText) findViewById(R.id.email_field);
|
||||
EditText login_password = (EditText) findViewById(R.id.password_field);
|
||||
|
||||
|
||||
@ -66,11 +73,20 @@ public class LoginActivity extends AppCompatActivity {
|
||||
stop = 1;
|
||||
}
|
||||
|
||||
//Check internet connection
|
||||
if(!APIRequestTask.isAPIavailable(this)){
|
||||
show_form_error(getString(R.string.err_no_internet_connection));
|
||||
stop = 1;
|
||||
}
|
||||
|
||||
|
||||
//Stop if required
|
||||
if(stop != 0)
|
||||
return;
|
||||
|
||||
open_loading_state(true);
|
||||
show_form_error("");
|
||||
enter_loading_state(true);
|
||||
|
||||
|
||||
//Perform a request on the API to check user credentials and get login tokens
|
||||
APIRequestParameters params = new APIRequestParameters("user/connectUSER");
|
||||
@ -82,20 +98,73 @@ public class LoginActivity extends AppCompatActivity {
|
||||
@Override
|
||||
protected void onPostExecute(APIResponse result) {
|
||||
|
||||
open_loading_state(false);
|
||||
//Check for errors
|
||||
if(result == null) {
|
||||
|
||||
//Hide loading wheel
|
||||
enter_loading_state(false);
|
||||
|
||||
//Put the error on the login mail field
|
||||
show_form_error(getString(R.string.activity_login_err_invalid_credentials));
|
||||
}
|
||||
|
||||
else
|
||||
//Perform next actions
|
||||
handle_server_response(result);
|
||||
|
||||
}
|
||||
}.execute(params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle server responses that seems to accept a response
|
||||
*
|
||||
* @param response The server reponse
|
||||
*/
|
||||
void handle_server_response(APIResponse response){
|
||||
|
||||
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
|
||||
Account account = new Account(this);
|
||||
if(!account.save_new_tokens(tokens)) {
|
||||
show_err_server_response();
|
||||
return;
|
||||
}
|
||||
|
||||
} catch (JSONException e){
|
||||
e.printStackTrace();
|
||||
show_err_server_response();
|
||||
return;
|
||||
}
|
||||
|
||||
//Redirect to the main activity
|
||||
Intent redirect = new Intent(this, MainActivity.class);
|
||||
startActivity(redirect);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch between loading state and ready state for the login form
|
||||
*
|
||||
* @param show_progress Specify wether a progress bar should be shown or not
|
||||
*/
|
||||
void open_loading_state(boolean show_progress){
|
||||
void enter_loading_state(boolean show_progress){
|
||||
//Grab elements
|
||||
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
|
||||
ScrollView login_form = (ScrollView) findViewById(R.id.login_form);
|
||||
@ -103,4 +172,33 @@ public class LoginActivity extends AppCompatActivity {
|
||||
progressBar.setVisibility(show_progress ? View.VISIBLE : View.GONE);
|
||||
login_form.setVisibility(show_progress ? View.GONE : View.VISIBLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display an error message on the login screen
|
||||
*
|
||||
* @param message The message to display on the screen
|
||||
*/
|
||||
void show_form_error(String message){
|
||||
|
||||
//Retrieve error field
|
||||
TextView v_error = (TextView) findViewById(R.id.login_error_message);
|
||||
|
||||
//Check what to do
|
||||
boolean display = message.length() > 0;
|
||||
|
||||
//Make it visible (or hide it)
|
||||
v_error.setVisibility(display ? View.VISIBLE : View.GONE);
|
||||
|
||||
//Set the text
|
||||
v_error.setText(message);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Display an error message to say the server responded incorrectly to the request
|
||||
*/
|
||||
void show_err_server_response(){
|
||||
show_form_error(getString(R.string.activity_login_err_server_response));
|
||||
enter_loading_state(false);
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package org.communiquons.android.comunic.client;
|
||||
import android.content.Intent;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@ -16,6 +17,11 @@ public class MainActivity extends AppCompatActivity {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
//Check for connectivity
|
||||
if(!APIRequestTask.isAPIavailable(this)){
|
||||
Toast.makeText(this, R.string.err_no_internet_connection, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
//Initialize account object
|
||||
account = new Account(this);
|
||||
|
||||
|
@ -31,6 +31,16 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<!-- Error message -->
|
||||
<TextView
|
||||
android:id="@+id/login_error_message"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:textAlignment="center"
|
||||
android:textColor="#FF2222"
|
||||
tools:text="Error message"
|
||||
android:visibility="gone"/>
|
||||
|
||||
<!-- Email field -->
|
||||
<android.support.design.widget.TextInputLayout
|
||||
android:layout_width="match_parent"
|
||||
|
@ -6,4 +6,7 @@
|
||||
<string name="activity_login_submit_form">Login</string>
|
||||
<string name="activity_login_err_invalid_password">The password is invalid!</string>
|
||||
<string name="activity_login_err_invalid_email">The email specified is invalid !</string>
|
||||
<string name="activity_login_err_invalid_credentials">The connexion couldn\'t be etablished. Please check your credentials !</string>
|
||||
<string name="err_no_internet_connection">No Internet connexion !</string>
|
||||
<string name="activity_login_err_server_response">The server sent an unexcepted response…</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user