Blame view

PremiereActivite/app/src/main/java/com/example/app_10p5/CarteActivite.java 7.49 KB
c8059613   JLo'w   Re-fonte de l'app...
1
2
3
  package com.example.app_10p5;
  
  import android.app.Activity;
6feece85   JLo'w   ajout NFC
4
5
  import android.app.PendingIntent;
  import android.content.Intent;
6feece85   JLo'w   ajout NFC
6
7
8
  import android.nfc.NfcAdapter;
  import android.nfc.Tag;
  import android.nfc.tech.MifareClassic;
c8059613   JLo'w   Re-fonte de l'app...
9
  import android.os.Bundle;
6feece85   JLo'w   ajout NFC
10
11
  import android.widget.Toast;
  
520cecde   JLo'w   Un petit plus, un...
12
13
  import org.json.JSONObject;
  
520cecde   JLo'w   Un petit plus, un...
14
15
  import java.net.URL;
  import java.util.HashMap;
c8059613   JLo'w   Re-fonte de l'app...
16
17
18
19
  
  /**
   * Created by beaus on 24/04/2016.
   */
520cecde   JLo'w   Un petit plus, un...
20
  public class CarteActivite extends Activity implements ASyncResponse {
6feece85   JLo'w   ajout NFC
21
22
  
      private NfcAdapter mNfcAdapter;
520cecde   JLo'w   Un petit plus, un...
23
24
25
26
      private HashMap<String, String> mParam;
      private String mAPI;
  
      public static final String HOST = "https://10p5.clubinfo.frogeye.fr/";
6feece85   JLo'w   ajout NFC
27
  
c8059613   JLo'w   Re-fonte de l'app...
28
29
30
31
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.layout_carte);
6feece85   JLo'w   ajout NFC
32
  
d57b6aad   JLo'w   Oups paramètres n...
33
          mParam = new HashMap<String, String>();
520cecde   JLo'w   Un petit plus, un...
34
  
d57b6aad   JLo'w   Oups paramètres n...
35
          switch (getIntent().getIntExtra("state", MainActivite.STATE_RIEN)) {
520cecde   JLo'w   Un petit plus, un...
36
              case MainActivite.STATE_COMMANDE:
f775eb7e   JLo'w   Toast cancers + p...
37
38
                  //TODO: XOR du cancer
                  //mParam.put("quantite", String.valueOf(getIntent().getIntExtra("quantite", -1)));
520cecde   JLo'w   Un petit plus, un...
39
                  mParam.put("montant", String.valueOf(getIntent().getFloatExtra("montant", -1)));
f775eb7e   JLo'w   Toast cancers + p...
40
                  mParam.put("jeton", getIntent().getStringExtra("token"));
520cecde   JLo'w   Un petit plus, un...
41
42
43
                  mAPI = "api/client/payer";
                  break;
              case MainActivite.STATE_CREATION_COMPTE:
f775eb7e   JLo'w   Toast cancers + p...
44
45
                  mParam.put("solde", String.valueOf(getIntent().getFloatExtra("montant", -1)));
                  mParam.put("jeton", getIntent().getStringExtra("token"));
520cecde   JLo'w   Un petit plus, un...
46
47
48
                  mAPI = "api/client/ajouter";
                  break;
              case MainActivite.STATE_RECHARGEMENT:
f775eb7e   JLo'w   Toast cancers + p...
49
50
                  mParam.put("montant", String.valueOf(getIntent().getFloatExtra("montant", -1)));
                  mParam.put("jeton", getIntent().getStringExtra("token"));
520cecde   JLo'w   Un petit plus, un...
51
52
53
                  mAPI = "api/client/recharger";
                  break;
              case MainActivite.STATE_VIDANGE:
8d573e63   JLo'w   Mise en place de ...
54
                  mParam.put("jeton", getIntent().getStringExtra("token"));
520cecde   JLo'w   Un petit plus, un...
55
56
                  mAPI = "api/client/vidange";
                  break;
f775eb7e   JLo'w   Toast cancers + p...
57
              case MainActivite.STATE_CONNEXION:  //Impossible c'est pas géré ici
520cecde   JLo'w   Un petit plus, un...
58
59
60
61
62
63
64
              case MainActivite.STATE_RIEN:
              default:
                  Toast.makeText(this, "WTF, le cancer est dans l'application!!", Toast.LENGTH_LONG).show();
                  finish();
                  return;
          }
  
6feece85   JLo'w   ajout NFC
65
66
          mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  
93f90e35   JLo'w   NFC et foreground
67
68
69
70
71
          if (mNfcAdapter == null) {
              // Stop here, we definitely need NFC
              Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
              finish();
              return;
6feece85   JLo'w   ajout NFC
72
          }
6feece85   JLo'w   ajout NFC
73
  
93f90e35   JLo'w   NFC et foreground
74
          handleIntent(getIntent());
6feece85   JLo'w   ajout NFC
75
76
      }
  
93f90e35   JLo'w   NFC et foreground
77
78
      @Override
      protected void onResume() {
6feece85   JLo'w   ajout NFC
79
          super.onResume();
93f90e35   JLo'w   NFC et foreground
80
          setupForegroundDispatch(this, mNfcAdapter);
6feece85   JLo'w   ajout NFC
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
      }
  
      // Convertit l'array de byte en chaîne hexadécimale (si le byte = 0x63, str = "63").
      private String ByteArrayToHexString(byte [] inarray) {
          int i, j, in;
          String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
          String out= "";
          for(j = 0 ; j < inarray.length ; ++j)
          {
              in = (int) inarray[j] & 0xff;
              i = (in >> 4) & 0x0f;
              out += hex[i];
              i = in & 0x0f;
              out += hex[i];
          }
          return out;
      }
  
      @Override
93f90e35   JLo'w   NFC et foreground
100
101
102
103
      protected void onNewIntent(Intent intent) {
          handleIntent(intent);
      }
  
93f90e35   JLo'w   NFC et foreground
104
105
106
107
108
109
110
111
      @Override
      protected void onPause() {
          stopForegroundDispatch(this, mNfcAdapter);
          super.onPause();
      }
  
      private void handleIntent(Intent intent){
          if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
6feece85   JLo'w   ajout NFC
112
              String id_carte = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID));
f775eb7e   JLo'w   Toast cancers + p...
113
              mParam.put("idCarte", id_carte);
6feece85   JLo'w   ajout NFC
114
115
116
117
118
119
120
121
122
123
  
              //Lecture des données
              Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
              MifareClassic mfc = MifareClassic.get(tag);
              byte[] data;
              String prenom = null, nom = null, login = null;
  
              if (mfc != null) {
                  try {
                      mfc.connect();
6feece85   JLo'w   ajout NFC
124
125
126
127
  
                      //Clé A
                      byte[] cleA = new byte[]{(byte) 0xa0, (byte) 0xa1, (byte) 0xa2,
                              (byte) 0xa3, (byte) 0xa4, (byte) 0xa5};
93f90e35   JLo'w   NFC et foreground
128
  
6feece85   JLo'w   ajout NFC
129
130
                      //On veut juste lire le secteur 12
                      boolean estConnecte = mfc.authenticateSectorWithKeyA(12, cleA);
93f90e35   JLo'w   NFC et foreground
131
  
6feece85   JLo'w   ajout NFC
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
                      if (estConnecte) {
                          //Il y a 4 blocs dans le secteur 12 -> INE, numéro étudiant, prénom, nom
                          //On ne veut que prénom et nom, donc on passe les deux premiers
                          for (int i = 2, bIndex = mfc.sectorToBlock(12) + i; i < 4; i++, bIndex++) {
                              //sectorToBlock : Renvoie l'indice (global, parmis tous les blocs de
                              // tous les secteurs) du premier bloc du secteur 12
                              //Lit les données du bloc
                              data = mfc.readBlock(bIndex);
  
                              //Convertit les bytes en String
                              String dataStr = new String(data);
                              if (i == 2) //Prénom
                                  prenom = dataStr;
                              else if (i == 3) //Nom
                                  nom = dataStr;
6feece85   JLo'w   ajout NFC
147
148
                          }
                      } else {
f775eb7e   JLo'w   Toast cancers + p...
149
                          Toast.makeText(this, "Impossible de lire le secteur 12", Toast.LENGTH_LONG).show();
6feece85   JLo'w   ajout NFC
150
151
                      }
                      mfc.close();
f775eb7e   JLo'w   Toast cancers + p...
152
                  } catch (Throwable t) {
bb05919b   JLo'w   import inutile et...
153
                      Toast.makeText(this, "WTF, le cancer est dans l'application!! Autodéstruction dans 5 secondes !!!!!!!!!" + t.toString(), Toast.LENGTH_LONG).show();
6feece85   JLo'w   ajout NFC
154
155
156
157
158
159
                  }
  
                  //Concaténation des données récupérées en login
                  login = prenom;
                  login.concat(".");
                  login.concat(nom);
6feece85   JLo'w   ajout NFC
160
              } else {
f775eb7e   JLo'w   Toast cancers + p...
161
                  Toast.makeText(this, "Pas de Mifare Classic", Toast.LENGTH_SHORT).show();
6feece85   JLo'w   ajout NFC
162
163
              }
  
6feece85   JLo'w   ajout NFC
164
              //Éxécution de la fonction
520cecde   JLo'w   Un petit plus, un...
165
166
              clientAPI();
              }
6feece85   JLo'w   ajout NFC
167
          }
c8059613   JLo'w   Re-fonte de l'app...
168
  
93f90e35   JLo'w   NFC et foreground
169
170
171
172
173
      public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter){
          final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
          intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
          final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
          adapter.enableForegroundDispatch(activity, pendingIntent, null, null);
6feece85   JLo'w   ajout NFC
174
175
      }
  
93f90e35   JLo'w   NFC et foreground
176
177
      public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
          adapter.disableForegroundDispatch(activity);
6feece85   JLo'w   ajout NFC
178
      }
520cecde   JLo'w   Un petit plus, un...
179
180
181
182
183
  
      public void clientAPI() {
          try {
              URL url = new URL(HOST + mAPI);
              NetworkThread nt = new NetworkThread(url, mParam);
6230c012   JLo'w   Correction sur le...
184
              nt.delegate = this;
520cecde   JLo'w   Un petit plus, un...
185
186
187
              nt.execute();
          }
          catch (Throwable t){
8ff199b1   JLo'w   Ajout info except...
188
              Toast.makeText(this, "WTF, le cancer est dans l'application!! " + t.toString(), Toast.LENGTH_LONG).show();
520cecde   JLo'w   Un petit plus, un...
189
190
191
192
193
194
195
196
197
              finish();
              return;
          }
      }
  
  
      /* Retour du NetworkThread */
      @Override
      public void processFinish(JSONObject output) {
4ef24041   JLo'w   Retour du JSONObj...
198
199
200
          Intent intent = new Intent(this, CarteActivite.class);
          intent.putExtra("json", output.toString());
          setResult(0, intent);
f775eb7e   JLo'w   Toast cancers + p...
201
          finish();
520cecde   JLo'w   Un petit plus, un...
202
      }
c8059613   JLo'w   Re-fonte de l'app...
203
  }