Commit 6ff8ec9bde3b88385d23d62df7b9ac4befb87924

Authored by JLo'w
1 parent f5fd15ef

Système d'héritage gérer NFC plusieurs fragments

Donc connexion possible avec carte directement
PremiereActivite/app/src/main/java/com/example/app_10p5/ConnectionFragment.java
1 package com.example.app_10p5; 1 package com.example.app_10p5;
2 2
3 -import android.app.Fragment; 3 +import android.content.Intent;
  4 +import android.content.SharedPreferences;
  5 +import android.nfc.NfcAdapter;
4 import android.os.Bundle; 6 import android.os.Bundle;
  7 +import android.preference.PreferenceManager;
5 import android.view.LayoutInflater; 8 import android.view.LayoutInflater;
6 import android.view.View; 9 import android.view.View;
7 import android.view.ViewGroup; 10 import android.view.ViewGroup;
  11 +import android.widget.Toast;
  12 +
  13 +import java.net.URL;
  14 +import java.util.HashMap;
8 15
9 /** 16 /**
10 * Created by Jean-loup Beaussart on 05/05/2016. 17 * Created by Jean-loup Beaussart on 05/05/2016.
11 */ 18 */
12 -public class ConnectionFragment extends Fragment {  
13 - @Override  
14 - public void onCreate(Bundle savedInstanceState){  
15 - super.onCreate(savedInstanceState);  
16 - } 19 +public class ConnectionFragment extends NFC {
17 20
18 @Override 21 @Override
19 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 22 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
20 View ret = inflater.inflate(R.layout.layout_connection, container, false); 23 View ret = inflater.inflate(R.layout.layout_connection, container, false);
21 return ret; 24 return ret;
22 } 25 }
  26 +
  27 + @Override
  28 + public void handleIntent(Intent intent) {
  29 + try{
  30 + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
  31 + URL url = new URL(settings.getString("server_address", null) + "api/utilisateur/connexion");
  32 + HashMap<String, String> param = new HashMap<String, String>();
  33 + String id_carte = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID));
  34 + param.put("idCarte", id_carte);
  35 + NetworkThread nt = new NetworkThread(url, param);
  36 + nt.delegate = (MainActivite) getActivity();
  37 + nt.execute();
  38 + }
  39 + catch (Throwable t) {
  40 + Toast.makeText(getActivity(), "Erreur: " + t.toString(), Toast.LENGTH_LONG).show();
  41 + System.out.println("Exception: " + t.toString());
  42 + }
  43 + }
23 } 44 }
PremiereActivite/app/src/main/java/com/example/app_10p5/MainActivite.java
@@ -391,9 +391,16 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr @@ -391,9 +391,16 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
391 super.onNewIntent(intent); 391 super.onNewIntent(intent);
392 Fragment frag = getFragmentManager().findFragmentById(R.id.fragment_container); 392 Fragment frag = getFragmentManager().findFragmentById(R.id.fragment_container);
393 393
394 - if(mState != STATE_RIEN && frag instanceof NFCFragment){  
395 - NFCFragment nfc = (NFCFragment) frag;  
396 - nfc.handleIntent(intent); 394 + if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())){
  395 + if(mState != STATE_RIEN && frag instanceof NFCFragment){
  396 + NFCFragment nfc = (NFCFragment) frag;
  397 + nfc.handleIntent(intent);
  398 + }
  399 + else if(mState == STATE_RIEN && frag instanceof ConnectionFragment){
  400 + mState = STATE_CONNEXION;
  401 + ConnectionFragment co = (ConnectionFragment) frag;
  402 + co.handleIntent(intent);
  403 + }
397 } 404 }
398 } 405 }
399 406
PremiereActivite/app/src/main/java/com/example/app_10p5/NFC.java 0 → 100644
@@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
  1 +package com.example.app_10p5;
  2 +
  3 +import android.app.Fragment;
  4 +import android.content.Intent;
  5 +
  6 +/**
  7 + * Created by Jean-loup Beaussart on 07/05/2016.
  8 + */
  9 +public abstract class NFC extends Fragment {
  10 +
  11 + // Convertit l'array de byte en chaîne hexadécimale (si le byte = 0x63, str = "63").
  12 + public String ByteArrayToHexString(byte [] inarray) {
  13 + int i, j, in;
  14 + String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
  15 + String out= "";
  16 + for(j = 0 ; j < inarray.length ; ++j)
  17 + {
  18 + in = (int) inarray[j] & 0xff;
  19 + i = (in >> 4) & 0x0f;
  20 + out += hex[i];
  21 + i = in & 0x0f;
  22 + out += hex[i];
  23 + }
  24 + return out;
  25 + }
  26 +
  27 +
  28 + public abstract void handleIntent(Intent intent);
  29 +}
PremiereActivite/app/src/main/java/com/example/app_10p5/NFCFragment.java
1 package com.example.app_10p5; 1 package com.example.app_10p5;
2 2
3 -import android.app.Fragment;  
4 import android.content.Intent; 3 import android.content.Intent;
5 import android.content.SharedPreferences; 4 import android.content.SharedPreferences;
6 import android.nfc.NfcAdapter; 5 import android.nfc.NfcAdapter;
@@ -19,16 +18,11 @@ import java.util.HashMap; @@ -19,16 +18,11 @@ import java.util.HashMap;
19 /** 18 /**
20 * Created by Jean-loup Beaussart on 05/05/2016. 19 * Created by Jean-loup Beaussart on 05/05/2016.
21 */ 20 */
22 -public class NFCFragment extends Fragment { 21 +public class NFCFragment extends NFC {
23 private HashMap<String, String> mParam; 22 private HashMap<String, String> mParam;
24 private String mAPI; 23 private String mAPI;
25 24
26 @Override 25 @Override
27 - public void onCreate(Bundle savedInstanceState){  
28 - super.onCreate(savedInstanceState);  
29 - }  
30 -  
31 - @Override  
32 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 26 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
33 View ret = inflater.inflate(R.layout.layout_carte, container, false); 27 View ret = inflater.inflate(R.layout.layout_carte, container, false);
34 28
@@ -76,34 +70,14 @@ public class NFCFragment extends Fragment { @@ -76,34 +70,14 @@ public class NFCFragment extends Fragment {
76 return ret; 70 return ret;
77 } 71 }
78 72
79 - // Convertit l'array de byte en chaîne hexadécimale (si le byte = 0x63, str = "63").  
80 - private String ByteArrayToHexString(byte [] inarray) {  
81 - int i, j, in;  
82 - String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};  
83 - String out= "";  
84 - for(j = 0 ; j < inarray.length ; ++j)  
85 - {  
86 - in = (int) inarray[j] & 0xff;  
87 - i = (in >> 4) & 0x0f;  
88 - out += hex[i];  
89 - i = in & 0x0f;  
90 - out += hex[i];  
91 - }  
92 - return out;  
93 - }  
94 -  
95 -  
96 - 73 + @Override
97 public void handleIntent(Intent intent){ 74 public void handleIntent(Intent intent){
98 - if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {  
99 - String id_carte = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID));  
100 - mParam.put("idCarte", id_carte);  
101 - clientAPI();  
102 - } 75 + String id_carte = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID));
  76 + mParam.put("idCarte", id_carte);
  77 + clientAPI();
103 } 78 }
104 79
105 80
106 -  
107 public void clientAPI() { 81 public void clientAPI() {
108 try { 82 try {
109 SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); 83 SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());