diff --git a/app/src/main/java/org/communiquons/android/comunic/client/APIRequestTask.java b/app/src/main/java/org/communiquons/android/comunic/client/APIRequestTask.java index c2cbf54..39d1b57 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/APIRequestTask.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/APIRequestTask.java @@ -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 { /** @@ -83,11 +85,9 @@ abstract class APIRequestTask extends AsyncTask toks){ + 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/LoginActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java index 5a274e8..091afdf 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 @@ -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 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); + } } 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 1ed3336..1eb8096 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 @@ -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); diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index 0f2d98d..e14a9c9 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -31,6 +31,16 @@ android:layout_height="wrap_content" android:orientation="vertical"> + + + Login The password is invalid! The email specified is invalid ! + The connexion couldn\'t be etablished. Please check your credentials ! + No Internet connexion ! + The server sent an unexcepted response…