diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4f4bc9a..d504afa 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -25,6 +25,7 @@ + \ No newline at end of file diff --git a/app/src/main/java/com/speculos/myapplicationoc/ClientAlreadyExistConfirmation.java b/app/src/main/java/com/speculos/myapplicationoc/ClientAlreadyExistConfirmation.java new file mode 100644 index 0000000..592d54c --- /dev/null +++ b/app/src/main/java/com/speculos/myapplicationoc/ClientAlreadyExistConfirmation.java @@ -0,0 +1,179 @@ +package com.speculos.myapplicationoc; + +import android.app.Activity; +import android.os.Bundle; +import android.os.StrictMode; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.Nullable; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + +public class ClientAlreadyExistConfirmation extends Activity { + + String hexa_id, mail, srvURL; + float solde, idClient; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Tweak pour contourner l'erreur android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork + StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); + StrictMode.setThreadPolicy(policy); + + // recuperation de MyApplication contenant les variables globales + MyApplication myApplication = (MyApplication) getApplication(); + srvURL = myApplication.getServerURL(); + + setContentView(R.layout.activity_client_exist_confirmation); + + hexa_id = getIntent().getStringExtra("hexa_id"); + + if (hexa_id == null) { + // TODO: afficher une erreur manque hexa_id + Toast.makeText(ClientAlreadyExistConfirmation.this, "Hexa_id non trouvé", Toast.LENGTH_LONG).show(); + finish(); + } + + mail = getIntent().getStringExtra("mail"); + + /* Initialisation des info client*/ + // Call API info client à partir du bracelet + String stringAPI = ""; + + try { + + OkHttpClient client = new OkHttpClient().newBuilder() + .connectTimeout(2000, TimeUnit.MILLISECONDS) + .callTimeout(2000, TimeUnit.MILLISECONDS) + .build(); + Request request = new Request.Builder() + .url(srvURL + "/api/clients.json?email=" + mail) + .method("GET", null) + .addHeader("Content-Type", "application/json") + .build(); + Response response = client.newCall(request).execute(); + + String in = response.body().string(); + in = in.substring(1, in.length() - 1); // on retire le [] autour de notre json premier et dernier char + Log.d("API-debug-in", in); + + JSONObject jsonObject = null; + + try { + jsonObject = new JSONObject(in); + Log.d("API-debug", "json: "+ jsonObject.toString()); + // jsonObject = new JSONObject(jsonObject.get("client").toString()); // recup client embed dans l'objet parent + } catch (JSONException e) { + Log.d("API-debug", e.getMessage()); + } + + + String mail = null; + mail = jsonObject.getString("email"); + solde = Float.parseFloat(jsonObject.getString("solde")); + + idClient = Float.parseFloat(jsonObject.getString("id")); + + TextView value_mail = findViewById(R.id.value_mail); + value_mail.setText(mail); + + JSONArray jsonBracelets = new JSONArray(jsonObject.get("bracelets").toString()); + Log.d("API-debug", "NBBRACELETS: " + jsonBracelets.length()); + + int nbBracelets = jsonBracelets.length(); + if (nbBracelets > 0) { + TextView textNbBracelets = findViewById(R.id.textNbBracelets); + + // 1 bracelet + textNbBracelets.setText("avec " + nbBracelets + " bracelet"); + // plusieurs bracelets + if (nbBracelets > 1) + textNbBracelets.setText("avec " + nbBracelets + " bracelets"); + + } else { + TextView textNbBracelets = findViewById(R.id.textNbBracelets); + textNbBracelets.setText("sans bracelet"); + } + + + } catch (IOException | JSONException e) { + e.printStackTrace(); + Log.d("API-debug-exception", e.getMessage()); + Toast.makeText(ClientAlreadyExistConfirmation.this, e.getMessage(), Toast.LENGTH_LONG).show(); + finish(); + } + + TextView value_solde = findViewById(R.id.value_solde); + value_solde.setText(solde + " €"); + + // Bouton de confirmation d'enregistrement + Button button_enregistrer = findViewById(R.id.button_enregistrer); + + button_enregistrer.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + // Requête ajout du bracelet au compte + + // On peut maintenant link le bracelet grace à l'hexa_id et l'idClient + try { + + OkHttpClient client = new OkHttpClient().newBuilder() + .build(); + MediaType mediaType = MediaType.parse("application/json"); + RequestBody body = RequestBody.create(mediaType, "{\"nfcId\": \"" + hexa_id + "\",\"client\": \"/api/clients/" + idClient + "\",\"state\": 0}"); + Request request = new Request.Builder() + .url(srvURL + "/api/bracelets") + .method("POST", body) + .addHeader("Content-Type", "application/json") + .build(); + Response responseHttp = client.newCall(request).execute(); + + if (responseHttp.code() == 400) { + try { + JSONObject jsonReader = null; + jsonReader = new JSONObject(responseHttp.body().string()); + Log.d("JSON-MSG", "violations: " + jsonReader.getJSONArray("violations").getString(0)); + Toast.makeText(ClientAlreadyExistConfirmation.this, "violations: " + jsonReader.getJSONArray("violations").getString(0), Toast.LENGTH_LONG).show(); + } catch (JSONException e) { + e.printStackTrace(); + } + } + + } catch (IOException e) { + e.printStackTrace(); + } + + finish(); + } + }); + + // Bouton de retour + Button button_retour = findViewById(R.id.button_non); + + button_retour.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + finish(); + + } + }); + + } +} diff --git a/app/src/main/java/com/speculos/myapplicationoc/MyApplication.java b/app/src/main/java/com/speculos/myapplicationoc/MyApplication.java index 404b5f6..900c8f9 100644 --- a/app/src/main/java/com/speculos/myapplicationoc/MyApplication.java +++ b/app/src/main/java/com/speculos/myapplicationoc/MyApplication.java @@ -1,12 +1,12 @@ -package com.speculos.myapplicationoc; - -import android.app.Application; - -public class MyApplication extends Application { - - private String serverURL = "http://art-dev:8080"; - - public String getServerURL(){ - return serverURL; - } -} +package com.speculos.myapplicationoc; + +import android.app.Application; + +public class MyApplication extends Application { + + private String serverURL = "http://192.168.1.72:8080"; + + public String getServerURL(){ + return serverURL; + } +} diff --git a/app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivityAdmin.java b/app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivityAdmin.java index 3517490..65ca1d5 100644 --- a/app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivityAdmin.java +++ b/app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivityAdmin.java @@ -1,6 +1,7 @@ package com.speculos.myapplicationoc; import android.app.Activity; +import android.content.Intent; import android.os.Bundle; import android.os.StrictMode; import android.util.Log; @@ -181,7 +182,12 @@ public class TagDetectedActivityAdmin extends Activity { } else { // Le mail est déjà present en base - Toast.makeText(TagDetectedActivityAdmin.this, "Le mail est déjà présent en base. Souhaitez vous ajouter ce bracelet au compte: " + mail, Toast.LENGTH_LONG).show(); + // TODO: rediriger vers une vue de confirmation + // Toast.makeText(TagDetectedActivityAdmin.this, "Le mail est déjà présent en base. Souhaitez vous ajouter ce bracelet au compte: " + mail, Toast.LENGTH_LONG).show(); + Intent myIntent = new Intent(TagDetectedActivityAdmin.this, ClientAlreadyExistConfirmation.class); + myIntent.putExtra("hexa_id", hexa_id); + myIntent.putExtra("mail",mail); + startActivity(myIntent); } @@ -202,6 +208,5 @@ public class TagDetectedActivityAdmin extends Activity { } }); } - } diff --git a/app/src/main/java/com/speculos/myapplicationoc/ThreadPing.java b/app/src/main/java/com/speculos/myapplicationoc/ThreadPing.java index e42b6d1..62e757d 100644 --- a/app/src/main/java/com/speculos/myapplicationoc/ThreadPing.java +++ b/app/src/main/java/com/speculos/myapplicationoc/ThreadPing.java @@ -1,77 +1,77 @@ -package com.speculos.myapplicationoc; - -import java.io.IOException; - -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; - - -public class ThreadPing extends Thread { - - /* Ce ThreadPing n'est pas un "ping" à proprement parler - * on réalise un call GET sur http://domain.dev/api - * pour savoir si le serveur répond - * */ - - public static Boolean isReachable; - private String url; - - // Constructeur - public ThreadPing(String url) { - this.url = url; - } - - private int delay = 3000; // temps entre chaque ping 5sec - - private PingListener listen = null; - - public void addListener(PingListener listen) { - this.listen = listen; - } - - public void run() { - - while (true) { - try { - String apiResponse = ""; - try { - OkHttpClient client = new OkHttpClient().newBuilder() - .build(); - Request request = new Request.Builder() - .url(url + "/api") - .method("GET", null) - .build(); - Response response = client.newCall(request).execute(); - apiResponse = response.body().toString(); - - } catch (IOException e) { - e.printStackTrace(); - } - - if (apiResponse != "") { - - // Si PING OK declenche l'event ping(isReachable) - isReachable = true; - if (listen != null) listen.ping(isReachable); - - } else { - - isReachable = false; - if (listen != null) listen.ping(isReachable); - - } - } catch (Exception e) { - System.out.println("Exception:" + e.getMessage()); - - } - - try { - sleep(delay); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - } -} +package com.speculos.myapplicationoc; + +import java.io.IOException; + +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + + +public class ThreadPing extends Thread { + + /* Ce ThreadPing n'est pas un "ping" à proprement parler + * on réalise un call GET sur http://domain.dev/api + * pour savoir si le serveur répond + * */ + + public static Boolean isReachable; + private String url; + + // Constructeur + public ThreadPing(String url) { + this.url = url; + } + + private int delay = 3000; // temps entre chaque ping 5sec + + private PingListener listen = null; + + public void addListener(PingListener listen) { + this.listen = listen; + } + + public void run() { + + while (true) { + try { + String apiResponse = ""; + try { + OkHttpClient client = new OkHttpClient().newBuilder() + .build(); + Request request = new Request.Builder() + .url(url + "/api") + .method("GET", null) + .build(); + Response response = client.newCall(request).execute(); + apiResponse = response.body().toString(); + + } catch (IOException e) { + e.printStackTrace(); + } + + if (apiResponse != "") { + + // Si PING OK declenche l'event ping(isReachable) + isReachable = true; + if (listen != null) listen.ping(isReachable); + + } else { + + isReachable = false; + if (listen != null) listen.ping(isReachable); + + } + } catch (Exception e) { + System.out.println("Exception:" + e.getMessage()); + + } + + try { + sleep(delay); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + } +} diff --git a/app/src/main/res/layout/activity_client_exist_confirmation.xml b/app/src/main/res/layout/activity_client_exist_confirmation.xml new file mode 100644 index 0000000..83fcaf3 --- /dev/null +++ b/app/src/main/res/layout/activity_client_exist_confirmation.xml @@ -0,0 +1,131 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +