commit 24200343cde7913320ac363481862db479ed65db Author: DAMBRINE Arthur Date: Sun Apr 12 13:40:48 2020 +0200 start new dev commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..603b140 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..4d8359d --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +My Application OC \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..681f41a --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,116 @@ + + + + + + + +
+ + + + xmlns:android + + ^$ + + + +
+
+ + + + xmlns:.* + + ^$ + + + BY_NAME + +
+
+ + + + .*:id + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + .*:name + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + name + + ^$ + + + +
+
+ + + + style + + ^$ + + + +
+
+ + + + .* + + ^$ + + + BY_NAME + +
+
+ + + + .* + + http://schemas.android.com/apk/res/android + + + ANDROID_ATTRIBUTE_ORDER + +
+
+ + + + .* + + .* + + + BY_NAME + +
+
+
+
+
+
\ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..5cd135a --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,20 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..37a7509 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..c95c00b --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,29 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 29 + buildToolsVersion "29.0.2" + defaultConfig { + applicationId "com.speculos.myapplicationoc" + minSdkVersion 23 + targetSdkVersion 29 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation 'androidx.appcompat:appcompat:1.0.2' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'androidx.test:runner:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' + implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: []) +} diff --git a/app/libs/gson-2.8.6-javadoc.jar b/app/libs/gson-2.8.6-javadoc.jar new file mode 100644 index 0000000..e3cce52 Binary files /dev/null and b/app/libs/gson-2.8.6-javadoc.jar differ diff --git a/app/libs/gson-2.8.6.jar b/app/libs/gson-2.8.6.jar new file mode 100644 index 0000000..4765c4a Binary files /dev/null and b/app/libs/gson-2.8.6.jar differ diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/androidTest/java/com/speculos/myapplicationoc/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/speculos/myapplicationoc/ExampleInstrumentedTest.java new file mode 100644 index 0000000..d4e5a83 --- /dev/null +++ b/app/src/androidTest/java/com/speculos/myapplicationoc/ExampleInstrumentedTest.java @@ -0,0 +1,27 @@ +package com.speculos.myapplicationoc; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + + assertEquals("com.speculos.myapplicationoc", appContext.getPackageName()); + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..440dc11 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/speculos/myapplicationoc/APICommandes.java b/app/src/main/java/com/speculos/myapplicationoc/APICommandes.java new file mode 100644 index 0000000..37715f7 --- /dev/null +++ b/app/src/main/java/com/speculos/myapplicationoc/APICommandes.java @@ -0,0 +1,130 @@ +package com.speculos.myapplicationoc; + +import android.os.StrictMode; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; + +public class APICommandes { + + private static HttpURLConnection con; + private static String response; + private static int SDK_INT = android.os.Build.VERSION.SDK_INT; + + + /* + A propos de l'implementation JWT : + + Using the token + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestProperty("Authorization", "Bearer " + accessToken); + */ + + + static String getMethod(String url) throws IOException { + + if (SDK_INT > 8) /*Eviter l'erreur android qui empêche call API dans le main thread*/ + { + StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() + .permitAll().build(); + StrictMode.setThreadPolicy(policy); + //your codes here + } + + try { + + URL myurl = new URL(url); + con = (HttpURLConnection) myurl.openConnection(); + + con.setRequestMethod("GET"); + + StringBuilder content; + + try (BufferedReader in = new BufferedReader( + new InputStreamReader(con.getInputStream()))) { + + String line; + content = new StringBuilder(); + + while ((line = in.readLine()) != null) { + + content.append(line); + content.append(System.lineSeparator()); + } + } + + response = content.toString(); + + } finally { + + con.disconnect(); + } + + return response; + } + + + public static String postMethod(String url, String urlParameters) throws IOException { + /* + Exemple de paramètres pour faire un POST : + + String urlParameters = "{\"name\": \"user4\", \"solde\": 22, \"email\": \"user4@mail.fr\"}"; + */ + + if (SDK_INT > 8) /*Eviter l'erreur android qui empêche call API dans le main thread*/ + { + StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() + .permitAll().build(); + StrictMode.setThreadPolicy(policy); + //your codes here + } + + byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); + + try { + + URL myurl = new URL(url); + con = (HttpURLConnection) myurl.openConnection(); + + con.setDoOutput(true); + con.setRequestMethod("POST"); + con.setRequestProperty("User-Agent", "Java client"); + con.setRequestProperty("Content-Type", "application/json"); + + try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) { + + wr.write(postData); + } + + StringBuilder content; + + try (BufferedReader br = new BufferedReader( + new InputStreamReader(con.getInputStream()))) { + + String line; + content = new StringBuilder(); + + while ((line = br.readLine()) != null) { + content.append(line); + content.append(System.lineSeparator()); + } + } + + //System.out.println(content.toString()); + response = content.toString(); + + } finally { + + con.disconnect(); + } + + return response; + } + +} + diff --git a/app/src/main/java/com/speculos/myapplicationoc/JsonTraductor.java b/app/src/main/java/com/speculos/myapplicationoc/JsonTraductor.java new file mode 100644 index 0000000..336ed23 --- /dev/null +++ b/app/src/main/java/com/speculos/myapplicationoc/JsonTraductor.java @@ -0,0 +1,76 @@ +package com.speculos.myapplicationoc; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import java.util.ArrayList; +import java.util.List; + + +public class JsonTraductor { + + private String json; + + private List listJson = new ArrayList(); + + + + public JsonTraductor(){ + // Json de test + // {"id":1,"nfcId":"azerty","client":{"id":1,"name":"Omer"},"state":1} + // "[{\"id\":1,\"nfcId\":\"azerty\",\"client\":{\"id\":1,\"name\":\"Omer\"},\"state\":1},{\"id\":2,\"nfcId\":\"oui\",\"client\":null,\"state\":0}]"; + json = "{\"id\":1,\"nfcId\":\"azerty\",\"client\":{\"id\":1,\"name\":\"Omer\"},\"state\":1}"; + } + + JsonTraductor(String jsonEntry){ + json = jsonEntry; + buildList(); + } + + private void buildList(){ + // Parse le json et tiens à jour une liste d'objets json + listJson.clear(); + + if(jsonIsArray(json)){ + + JsonArray entries = (JsonArray) new JsonParser().parse(json); + //String id = ((JsonObject)entries.get(0)).get("id").toString(); + //System.out.println(id); + + for(int i = 0; i= listJson.size()){ + // pour ne pas depasser l'index final + return null; + + } else { + return listJson.get(jsonIndex).get(jsonId).toString(); + } + + } + +} diff --git a/app/src/main/java/com/speculos/myapplicationoc/MainActivity.java b/app/src/main/java/com/speculos/myapplicationoc/MainActivity.java new file mode 100644 index 0000000..165aed6 --- /dev/null +++ b/app/src/main/java/com/speculos/myapplicationoc/MainActivity.java @@ -0,0 +1,346 @@ +package com.speculos.myapplicationoc; + +import androidx.appcompat.app.AppCompatActivity; + +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.Button; + +import android.nfc.NdefRecord; +import android.nfc.Tag; +import android.nfc.tech.MifareClassic; +import android.nfc.tech.MifareUltralight; + +import android.app.PendingIntent; +import android.content.IntentFilter; +import android.nfc.NdefMessage; +import android.nfc.NfcAdapter; +import android.os.Parcelable; +import android.widget.TextView; +import android.widget.Toast; + +import java.io.UnsupportedEncodingException; + +public class MainActivity extends AppCompatActivity { + + private NfcAdapter nfcAdapter; + private static boolean modeEdition = false; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + // Ping à mettre dans un thread + ThreadPing threadping = new ThreadPing(); + threadping.start(); + + threadping.addListener(new PingListener() { + @Override + public void ping(Boolean isReachable) { + TextView indicateur_etat_connexion = findViewById(R.id.indicateur_etat_connexion); + TextView indicateur_nfc_actif = findViewById(R.id.indicateur_nfc_actif); + + if(isReachable){ + Log.d("PING"," is reachable."); + indicateur_etat_connexion.setText("OK"); + } else { + Log.d("PING"," NOT reachable."); + indicateur_etat_connexion.setText("NOT OK"); + } + + if(nfcAdapter.isEnabled()){ + Log.d("NFC"," is enabled."); + indicateur_nfc_actif.setText("OK"); + } else { + Log.d("NFC"," NOT enabled."); + indicateur_nfc_actif.setText("NOT OK"); + } + } + }); + + /* On recupere le botton du layout pour le traiter */ + Button myButton = findViewById(R.id.submit_button); + + myButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + + TextView title = findViewById(R.id.textViewTitle); + Button myButton = findViewById(R.id.submit_button); + + + if(modeEdition){ + modeEdition = false; + title.setText("NFC Reader"); + myButton.setText("Aller au mode Edition"); + } else { + modeEdition = true; + title.setText("Mode Edition"); + myButton.setText("Retour mode NFC Reader"); + } + } + }); + + nfcAdapter = NfcAdapter.getDefaultAdapter(this); + + if (nfcAdapter == null) { + Toast.makeText(this, "No NFC", Toast.LENGTH_SHORT).show(); + finish(); + return; + } else { + + // Verif NFC Actif + TextView indicateur_nfc_actif = findViewById(R.id.indicateur_nfc_actif); + + if(nfcAdapter.isEnabled()){ + indicateur_nfc_actif.setText("OK"); + } else { + indicateur_nfc_actif.setText("NOT OK"); + } + } + } + + private String toHex(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + for (int i = bytes.length - 1; i >= 0; --i) { + int b = bytes[i] & 0xff; + if (b < 0x10) + sb.append('0'); + sb.append(Integer.toHexString(b)); + if (i > 0) { + sb.append(" "); + } + } + return sb.toString(); + } + + private String toReversedHex(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < bytes.length; ++i) { + if (i > 0) { + sb.append(" "); + } + int b = bytes[i] & 0xff; + if (b < 0x10) + sb.append('0'); + sb.append(Integer.toHexString(b)); + } + return sb.toString(); + } + + private long toDec(byte[] bytes) { + long result = 0; + long factor = 1; + for (int i = 0; i < bytes.length; ++i) { + long value = bytes[i] & 0xffl; + result += value * factor; + factor *= 256l; + } + return result; + } + + private long toReversedDec(byte[] bytes) { + long result = 0; + long factor = 1; + for (int i = bytes.length - 1; i >= 0; --i) { + long value = bytes[i] & 0xffl; + result += value * factor; + factor *= 256l; + } + return result; + } + + private String dumpTagData(Tag tag) { + StringBuilder sb = new StringBuilder(); + byte[] id = tag.getId(); + sb.append("ID (hex): ").append(toHex(id)).append('\n'); + sb.append("ID (reversed hex): ").append(toReversedHex(id)).append('\n'); + sb.append("ID (dec): ").append(toDec(id)).append('\n'); + sb.append("ID (reversed dec): ").append(toReversedDec(id)).append('\n'); + + String prefix = "android.nfc.tech."; + sb.append("Technologies: "); + for (String tech : tag.getTechList()) { + sb.append(tech.substring(prefix.length())); + sb.append(", "); + } + + sb.delete(sb.length() - 2, sb.length()); + + for (String tech : tag.getTechList()) { + if (tech.equals(MifareClassic.class.getName())) { + sb.append('\n'); + String type = "Unknown"; + + try { + MifareClassic mifareTag = MifareClassic.get(tag); + + switch (mifareTag.getType()) { + case MifareClassic.TYPE_CLASSIC: + type = "Classic"; + break; + case MifareClassic.TYPE_PLUS: + type = "Plus"; + break; + case MifareClassic.TYPE_PRO: + type = "Pro"; + break; + } + sb.append("Mifare Classic type: "); + sb.append(type); + sb.append('\n'); + + sb.append("Mifare size: "); + sb.append(mifareTag.getSize() + " bytes"); + sb.append('\n'); + + sb.append("Mifare sectors: "); + sb.append(mifareTag.getSectorCount()); + sb.append('\n'); + + sb.append("Mifare blocks: "); + sb.append(mifareTag.getBlockCount()); + } catch (Exception e) { + sb.append("Mifare classic error: " + e.getMessage()); + } + } + + if (tech.equals(MifareUltralight.class.getName())) { + sb.append('\n'); + MifareUltralight mifareUlTag = MifareUltralight.get(tag); + String type = "Unknown"; + switch (mifareUlTag.getType()) { + case MifareUltralight.TYPE_ULTRALIGHT: + type = "Ultralight"; + break; + case MifareUltralight.TYPE_ULTRALIGHT_C: + type = "Ultralight C"; + break; + } + sb.append("Mifare Ultralight type: "); + sb.append(type); + } + } + + return sb.toString(); + } + + private String readFromIntent(Intent intent) { + Log.d("buildTagViews", intent.toString()); + String action = intent.getAction(); + Log.d("buildTagViews", action); + if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) + || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) + || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) { + // Log.d("buildTagViews","MSG? "+intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)); + Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); + NdefMessage[] msgs = null; + + if (rawMsgs != null) { + msgs = new NdefMessage[rawMsgs.length]; + + for (int i = 0; i < rawMsgs.length; i++) { + msgs[i] = (NdefMessage) rawMsgs[i]; + } + + } else { + byte[] empty = new byte[0]; + byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID); + Tag tag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); + byte[] payload = dumpTagData(tag).getBytes(); + NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, id, payload); + NdefMessage msg = new NdefMessage(new NdefRecord[] {record}); + msgs = new NdefMessage[] {msg}; + } + + return buildTagViews(msgs); + } + return "No"; + } + + private String buildTagViews(NdefMessage[] msgs) { + if (msgs == null || msgs.length == 0) { + Log.d("buildTagViews", "buildTagViews failed"); + return "Error msg"; + } + + String text = ""; + //String tagId = new String(msgs[0].getRecords()[0].toString()); + byte[] payload = msgs[0].getRecords()[0].getPayload(); + String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; // Get the Text Encoding + int languageCodeLength = payload[0] & 0063; // Get the Language Code, e.g. "en" + // String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII"); + + try { + // Get the Text + text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding); + } catch (UnsupportedEncodingException e) { + Log.e("UnsupportedEncoding", e.toString()); + } + Log.d("buildTagViews",text); + return text; + } + + @Override + protected void onResume() { + super.onResume(); + + Intent intent = new Intent(this, MainActivity.class).addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); + + PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); + IntentFilter[] filtres = new IntentFilter[]{}; + + nfcAdapter.enableForegroundDispatch(this, pendingIntent, filtres, null); + } + + @Override + protected void onPause() { + super.onPause(); + if (nfcAdapter != null) { + nfcAdapter.disableForegroundDispatch(this); + } + } + + @Override + protected void onNewIntent(Intent intent) { + //String action = intent.getAction(); + //Toast.makeText(this, action, Toast.LENGTH_SHORT).show(); + + + String NFC_info = readFromIntent(intent); + + String lines[] = NFC_info.split("\\r?\\n"); + + Log.d("StringToParse",lines[0]); + + int left = lines[0].indexOf(":"); /* correspond à (hex): 80 5e 2b 22 3d 8f 04 */ + String sub = lines[0].substring(left+1); + sub = sub.trim(); + + Log.d("StringToParse",sub); + + + Toast.makeText(this, "Tag NFC détecté", Toast.LENGTH_SHORT).show(); + + super.onNewIntent(intent); + + + if(modeEdition){ + + Intent myIntent = new Intent(MainActivity.this, TagDetectedActivityAdmin.class); + myIntent.putExtra("hexa_id",sub); + startActivity(myIntent); + + } else { + + Intent myIntent = new Intent(MainActivity.this, TagDetectedActivity.class); + myIntent.putExtra("hexa_id",sub); + startActivity(myIntent); + } + + } + +} diff --git a/app/src/main/java/com/speculos/myapplicationoc/MyApplication.java b/app/src/main/java/com/speculos/myapplicationoc/MyApplication.java new file mode 100644 index 0000000..98aef17 --- /dev/null +++ b/app/src/main/java/com/speculos/myapplicationoc/MyApplication.java @@ -0,0 +1,13 @@ +package com.speculos.myapplicationoc; + +import android.app.Application; + +public class MyApplication extends Application { + + private String serverIP = "art-dev"; + + String getServerIP(){ + return serverIP; + } + +} diff --git a/app/src/main/java/com/speculos/myapplicationoc/PaymentActivity.java b/app/src/main/java/com/speculos/myapplicationoc/PaymentActivity.java new file mode 100644 index 0000000..8d2b10e --- /dev/null +++ b/app/src/main/java/com/speculos/myapplicationoc/PaymentActivity.java @@ -0,0 +1,133 @@ +package com.speculos.myapplicationoc; + +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.view.inputmethod.InputMethodManager; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; + +import java.io.IOException; + +public class PaymentActivity extends AppCompatActivity { + + static String hexa_id, title, soldeString; + static float solde; + + // Temporaire + String srvURL = "http://192.168.1.21"; + static String nfcId = "azerty"; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_payment); + + hexa_id = getIntent().getStringExtra("hexa_id"); + + if(hexa_id == null){ + hexa_id = "-";//Assign default string + } + + title = getIntent().getStringExtra("title"); + + TextView title_payment = findViewById(R.id.title_payment); + + title_payment.setText("Error"); + + if(isPayment()){ + title_payment.setText("Payer"); + soldeString = getIntent().getStringExtra("solde"); + solde = Float.parseFloat(soldeString); + } else { + title_payment.setText("Recharger"); + solde = 0; + } + + + /*Toast.makeText(this, hexa_id, Toast.LENGTH_SHORT).show();*/ + + EditText edit_number = (EditText) findViewById(R.id.edit_number); + + edit_number.requestFocus(); + + InputMethodManager imm = (InputMethodManager)getSystemService(this.INPUT_METHOD_SERVICE); + imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY); + + Button button_valid = findViewById(R.id.button_valid); + button_valid.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + // Call api payment + String transaction_label = ((EditText)(findViewById(R.id.edit_number))).getText().toString(); + float transaction_value = Float.parseFloat(transaction_label); + Boolean valider_transaction = false; + + if (transaction_label.contains(".")){ + // Valeur float avec virgule + + if(transaction_label.split("[.]")[1].length() > 2){ + + Log.d("PAYMENT-debug","Error too long"); + Toast.makeText(PaymentActivity.this, "Trop long!", Toast.LENGTH_LONG).show(); + + } else { + Log.d("PAYMENT-debug",""+transaction_value); + valider_transaction = true; + } + + } else { + // Valeur float sans virgule + Log.d("PAYMENT-debug",""+transaction_value); + valider_transaction = true; + } + + if(valider_transaction){ + if(isPayment()){ + if(solde < transaction_value){ + Toast.makeText(PaymentActivity.this, "Pas assez d'argent!", Toast.LENGTH_LONG).show(); + } else { + // Call API Paiement + try { + String response = APICommandes.postMethod(srvURL + "/api/payment","{\"nfcId\": \"" + nfcId + "\", \"price\": " + transaction_value + "}"); + Log.d("PAYMENT-debug",response); + } catch (IOException e) { + Toast.makeText(PaymentActivity.this, "Pas assez d'argent!", Toast.LENGTH_LONG).show(); + e.printStackTrace(); + } + } + + + } else { + //Call API Rechargement + try { + String response = APICommandes.postMethod(srvURL + "/api/load","{\"nfcId\": \"" + nfcId + "\", \"price\": " + transaction_value + "}"); + Log.d("PAYMENT-debug",response); + } catch (IOException e) { + Toast.makeText(PaymentActivity.this, "Erreur rechargement!", Toast.LENGTH_LONG).show(); + e.printStackTrace(); + } + + } + } + + + + } + }); + + } + + private static Boolean isPayment(){ + if(title.equals("Payer")){ + return true; + } else { + return false; + } + } +} diff --git a/app/src/main/java/com/speculos/myapplicationoc/PingListener.java b/app/src/main/java/com/speculos/myapplicationoc/PingListener.java new file mode 100644 index 0000000..6034140 --- /dev/null +++ b/app/src/main/java/com/speculos/myapplicationoc/PingListener.java @@ -0,0 +1,7 @@ +package com.speculos.myapplicationoc; + + + +public interface PingListener { + public void ping(Boolean isReachable); +} diff --git a/app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivity.java b/app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivity.java new file mode 100644 index 0000000..53bddf4 --- /dev/null +++ b/app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivity.java @@ -0,0 +1,120 @@ +package com.speculos.myapplicationoc; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import androidx.annotation.Nullable; + +import java.io.IOException; + +public class TagDetectedActivity extends Activity { + + String hexa_id, mail; + float solde = 0; + + // Temporaire + String srvURL = "http://192.168.1.21"; + String nfcId = "azerty"; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.activity_tag_detected); + + hexa_id = getIntent().getStringExtra("hexa_id"); + + if(hexa_id == null){ + hexa_id = "-";//Assign default string + } + + TextView value_NFCID = findViewById(R.id.value_NFCID); + value_NFCID.setText(hexa_id); + + /* Initialisation des info client*/ + // Call API info client à partir du bracelet + String stringAPI = ""; + + try { + stringAPI = APICommandes.getMethod(srvURL + "/api/bracelets.json?nfcId="+ nfcId); + Log.d("API-debug",stringAPI); + } catch (IOException e) { + e.printStackTrace(); + } + + String json = stringAPI; + JsonTraductor maListeJson = new JsonTraductor(json); + maListeJson = new JsonTraductor(maListeJson.getJsonData("client", 0)); + mail = maListeJson.getJsonData("email", 0); + mail = mail.substring(1,mail.length()-1); + + if(mail == null){ + mail = "-";//Assign default string + } + + TextView value_mail = findViewById(R.id.value_mail); + value_mail.setText(mail); + + solde = Float.parseFloat(maListeJson.getJsonData("solde", 0)); + TextView value_solde = findViewById(R.id.value_solde); + value_solde.setText(solde+" €"); + + + + + Button button_payer = findViewById(R.id.button_enregistrer); + + button_payer.setOnClickListener( new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent myIntent = new Intent(TagDetectedActivity.this, PaymentActivity.class); + myIntent.putExtra("hexa_id",hexa_id); + myIntent.putExtra("title","Payer"); + myIntent.putExtra("solde", ""+solde); + startActivity(myIntent); + + } + }); + + Button button_recharger = findViewById(R.id.button_retour); + + button_recharger.setOnClickListener( new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent myIntent = new Intent(TagDetectedActivity.this, PaymentActivity.class); + myIntent.putExtra("hexa_id",hexa_id); + myIntent.putExtra("title","Recharger"); + startActivity(myIntent); + + } + }); + } + + @Override + protected void onResume() { + super.onResume(); + + // code à la fermeture de myIntent + String stringAPI2 = ""; + + try { + stringAPI2 = APICommandes.getMethod(srvURL + "/api/bracelets.json?nfcId="+ nfcId); + Log.d("API-debug",stringAPI2); + } catch (IOException e) { + e.printStackTrace(); + } + + String json = stringAPI2; + JsonTraductor maListeJson = new JsonTraductor(json); + maListeJson = new JsonTraductor(maListeJson.getJsonData("client", 0)); + + solde = Float.parseFloat(maListeJson.getJsonData("solde", 0)); + TextView value_solde = findViewById(R.id.value_solde); + value_solde.setText(solde+" €"); + } +} diff --git a/app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivityAdmin.java b/app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivityAdmin.java new file mode 100644 index 0000000..f47513c --- /dev/null +++ b/app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivityAdmin.java @@ -0,0 +1,66 @@ +package com.speculos.myapplicationoc; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.Nullable; + +import java.io.IOException; + +public class TagDetectedActivityAdmin extends Activity { + + String hexa_id, mail; + + // Temporaire + String srvURL = "http://192.168.1.21"; + String nfcId = "azerty"; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.activity_tag_detected_admin); + + hexa_id = getIntent().getStringExtra("hexa_id"); + + if (hexa_id == null) { + hexa_id = "-";//Assign default string + } + + TextView value_NFCID = findViewById(R.id.value_NFCID); + value_NFCID.setText(hexa_id); + + + Button button_enregistrer = findViewById(R.id.button_enregistrer); + + button_enregistrer.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + + // Vérifications et Call API créer le compte + EditText value_mail = findViewById(R.id.value_mail); + mail = value_mail.getText().toString(); + Toast.makeText(TagDetectedActivityAdmin.this, mail, Toast.LENGTH_LONG).show(); + + } + }); + + Button button_retour = findViewById(R.id.button_retour); + + button_retour.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + finish(); + } + }); + } + +} + diff --git a/app/src/main/java/com/speculos/myapplicationoc/ThreadPing.java b/app/src/main/java/com/speculos/myapplicationoc/ThreadPing.java new file mode 100644 index 0000000..4ec17de --- /dev/null +++ b/app/src/main/java/com/speculos/myapplicationoc/ThreadPing.java @@ -0,0 +1,59 @@ +package com.speculos.myapplicationoc; + +import java.net.InetAddress; + + +public class ThreadPing extends Thread { + + public static Boolean isReachable; + + // Temporaire + private String ipA = "192.168.1.21"; + + + +// private String ipA = + + private int delay = 3000; // temps entre chaque ping 3sec + + private PingListener listen = null; + + public void addListener(PingListener listen){ + this.listen = listen; + } + + public void run() { + + while(true) { + try { + String ipAddress = ipA; + InetAddress inet = InetAddress.getByName(ipAddress); + //System.out.println("Sending Ping Request to " + ipAddress); + if (inet.isReachable(4000)) { + + // Si PING OK declenche l'event ping(isReachable) + + isReachable = true; + if(listen != null) listen.ping(isReachable); + + } else { + + // Si PING NOT OK declenche l'event ping(isReachable) + + 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/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..1f6bb29 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..0d025f9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/drawable/logo_network.png b/app/src/main/res/drawable/logo_network.png new file mode 100644 index 0000000..cc6a01d Binary files /dev/null and b/app/src/main/res/drawable/logo_network.png differ diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..29b01dd --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,132 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +