Blame view

PremiereActivite/app/src/main/java/com/example/app_10p5/CarteActivite.java 5.98 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;
edd209be   JLo'w   URL encode + pref...
6
  import android.content.SharedPreferences;
2681581a   JLo'w   menu pour changer...
7
  import android.graphics.drawable.ColorDrawable;
6feece85   JLo'w   ajout NFC
8
9
10
  import android.nfc.NfcAdapter;
  import android.nfc.Tag;
  import android.nfc.tech.MifareClassic;
c8059613   JLo'w   Re-fonte de l'app...
11
  import android.os.Bundle;
edd209be   JLo'w   URL encode + pref...
12
  import android.preference.PreferenceManager;
6feece85   JLo'w   ajout NFC
13
14
  import android.widget.Toast;
  
520cecde   JLo'w   Un petit plus, un...
15
16
  import org.json.JSONObject;
  
520cecde   JLo'w   Un petit plus, un...
17
  import java.net.URL;
edd209be   JLo'w   URL encode + pref...
18
  import java.net.URLEncoder;
520cecde   JLo'w   Un petit plus, un...
19
  import java.util.HashMap;
c8059613   JLo'w   Re-fonte de l'app...
20
21
22
23
  
  /**
   * Created by beaus on 24/04/2016.
   */
520cecde   JLo'w   Un petit plus, un...
24
  public class CarteActivite extends Activity implements ASyncResponse {
6feece85   JLo'w   ajout NFC
25
26
  
      private NfcAdapter mNfcAdapter;
520cecde   JLo'w   Un petit plus, un...
27
28
29
      private HashMap<String, String> mParam;
      private String mAPI;
  
c8059613   JLo'w   Re-fonte de l'app...
30
31
32
33
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.layout_carte);
2681581a   JLo'w   menu pour changer...
34
          getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimary)));
6feece85   JLo'w   ajout NFC
35
  
d57b6aad   JLo'w   Oups paramètres n...
36
          mParam = new HashMap<String, String>();
520cecde   JLo'w   Un petit plus, un...
37
  
edd209be   JLo'w   URL encode + pref...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
          try {
              switch (getIntent().getIntExtra("state", MainActivite.STATE_RIEN)) {
                  case MainActivite.STATE_COMMANDE:
                      //TODO: XOR du cancer
                      //mParam.put("quantite", String.valueOf(getIntent().getIntExtra("quantite", -1)));
                      mParam.put("montant", URLEncoder.encode(String.valueOf(getIntent().getFloatExtra("montant", -1)), "UTF-8"));
                      mParam.put("jeton", URLEncoder.encode(getIntent().getStringExtra("token"), "UTF-8"));
                      mAPI = "api/client/payer";
                      break;
                  case MainActivite.STATE_CREATION_COMPTE:
                      mParam.put("solde", URLEncoder.encode(String.valueOf(getIntent().getFloatExtra("montant", -1)), "UTF-8"));
                      mParam.put("jeton", URLEncoder.encode(getIntent().getStringExtra("token"), "UTF-8"));
                      mAPI = "api/client/ajouter";
                      break;
                  case MainActivite.STATE_RECHARGEMENT:
                      System.out.println("bite");
                      mParam.put("montant", URLEncoder.encode(String.valueOf(getIntent().getFloatExtra("montant", -1)), "UTF-8"));
                      mParam.put("jeton", URLEncoder.encode(getIntent().getStringExtra("token"), "UTF-8"));
                      mAPI = "api/client/recharger";
                      break;
                  case MainActivite.STATE_VIDANGE:
                      mParam.put("jeton", URLEncoder.encode(getIntent().getStringExtra("token"), "UTF-8"));
                      mAPI = "api/client/vidange";
                      break;
                  case MainActivite.STATE_CONNEXION:  //Impossible c'est pas géré ici
                  case MainActivite.STATE_RIEN:
                  default:
                      Toast.makeText(this, "WTF, le cancer est dans l'application!!", Toast.LENGTH_LONG).show();
                      finish();
                      return;
              }
          }
          catch (Throwable t){
              System.out.println("Exception: " + t.toString());
520cecde   JLo'w   Un petit plus, un...
72
73
          }
  
6feece85   JLo'w   ajout NFC
74
75
          mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  
93f90e35   JLo'w   NFC et foreground
76
77
78
79
80
          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
81
          }
6feece85   JLo'w   ajout NFC
82
  
93f90e35   JLo'w   NFC et foreground
83
          handleIntent(getIntent());
6feece85   JLo'w   ajout NFC
84
85
      }
  
93f90e35   JLo'w   NFC et foreground
86
87
      @Override
      protected void onResume() {
6feece85   JLo'w   ajout NFC
88
          super.onResume();
93f90e35   JLo'w   NFC et foreground
89
          setupForegroundDispatch(this, mNfcAdapter);
6feece85   JLo'w   ajout NFC
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
      }
  
      // 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
109
110
111
112
      protected void onNewIntent(Intent intent) {
          handleIntent(intent);
      }
  
93f90e35   JLo'w   NFC et foreground
113
114
115
116
117
118
119
120
      @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
121
              String id_carte = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID));
f775eb7e   JLo'w   Toast cancers + p...
122
              mParam.put("idCarte", id_carte);
520cecde   JLo'w   Un petit plus, un...
123
124
              clientAPI();
              }
6feece85   JLo'w   ajout NFC
125
          }
c8059613   JLo'w   Re-fonte de l'app...
126
  
93f90e35   JLo'w   NFC et foreground
127
128
129
130
131
      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
132
133
      }
  
93f90e35   JLo'w   NFC et foreground
134
135
      public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
          adapter.disableForegroundDispatch(activity);
6feece85   JLo'w   ajout NFC
136
      }
520cecde   JLo'w   Un petit plus, un...
137
138
139
  
      public void clientAPI() {
          try {
edd209be   JLo'w   URL encode + pref...
140
141
              SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
              URL url = new URL(settings.getString("server_address", null) + mAPI);
520cecde   JLo'w   Un petit plus, un...
142
              NetworkThread nt = new NetworkThread(url, mParam);
6230c012   JLo'w   Correction sur le...
143
              nt.delegate = this;
520cecde   JLo'w   Un petit plus, un...
144
145
146
              nt.execute();
          }
          catch (Throwable t){
8ff199b1   JLo'w   Ajout info except...
147
              Toast.makeText(this, "WTF, le cancer est dans l'application!! " + t.toString(), Toast.LENGTH_LONG).show();
520cecde   JLo'w   Un petit plus, un...
148
149
150
151
152
153
154
155
156
              finish();
              return;
          }
      }
  
  
      /* Retour du NetworkThread */
      @Override
      public void processFinish(JSONObject output) {
4ef24041   JLo'w   Retour du JSONObj...
157
158
159
          Intent intent = new Intent(this, CarteActivite.class);
          intent.putExtra("json", output.toString());
          setResult(0, intent);
f775eb7e   JLo'w   Toast cancers + p...
160
          finish();
520cecde   JLo'w   Un petit plus, un...
161
      }
c8059613   JLo'w   Re-fonte de l'app...
162
  }