Blame view

PremiereActivite/app/src/main/java/com/example/app_10p5/NFCFragment.java 4.26 KB
c8059613   JLo'w   Re-fonte de l'app...
1
2
  package com.example.app_10p5;
  
76de4d29   JLo'w   Connexion et NFC ...
3
  import android.app.Fragment;
6feece85   JLo'w   ajout NFC
4
  import android.content.Intent;
edd209be   JLo'w   URL encode + pref...
5
  import android.content.SharedPreferences;
6feece85   JLo'w   ajout NFC
6
  import android.nfc.NfcAdapter;
c8059613   JLo'w   Re-fonte de l'app...
7
  import android.os.Bundle;
edd209be   JLo'w   URL encode + pref...
8
  import android.preference.PreferenceManager;
76de4d29   JLo'w   Connexion et NFC ...
9
10
11
12
  import android.support.design.widget.Snackbar;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
6feece85   JLo'w   ajout NFC
13
14
  import android.widget.Toast;
  
520cecde   JLo'w   Un petit plus, un...
15
  import java.net.URL;
edd209be   JLo'w   URL encode + pref...
16
  import java.net.URLEncoder;
520cecde   JLo'w   Un petit plus, un...
17
  import java.util.HashMap;
c8059613   JLo'w   Re-fonte de l'app...
18
19
  
  /**
fde33444   JLo'w   Update header ave...
20
   * Created by Jean-loup Beaussart on 05/05/2016.
c8059613   JLo'w   Re-fonte de l'app...
21
   */
76de4d29   JLo'w   Connexion et NFC ...
22
  public class NFCFragment extends Fragment {
520cecde   JLo'w   Un petit plus, un...
23
24
25
      private HashMap<String, String> mParam;
      private String mAPI;
  
c8059613   JLo'w   Re-fonte de l'app...
26
      @Override
76de4d29   JLo'w   Connexion et NFC ...
27
      public void onCreate(Bundle savedInstanceState){
c8059613   JLo'w   Re-fonte de l'app...
28
          super.onCreate(savedInstanceState);
76de4d29   JLo'w   Connexion et NFC ...
29
30
31
32
33
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View ret = inflater.inflate(R.layout.layout_carte, container, false);
6feece85   JLo'w   ajout NFC
34
  
d57b6aad   JLo'w   Oups paramètres n...
35
          mParam = new HashMap<String, String>();
520cecde   JLo'w   Un petit plus, un...
36
  
76de4d29   JLo'w   Connexion et NFC ...
37
38
          Bundle b = getArguments();
  
edd209be   JLo'w   URL encode + pref...
39
          try {
86ba2e51   JLo'w   XOR du cancer
40
              mParam.put("jeton", URLEncoder.encode(b.getString("token"), "UTF-8"));
76de4d29   JLo'w   Connexion et NFC ...
41
              switch (b.getInt("state")) {
edd209be   JLo'w   URL encode + pref...
42
                  case MainActivite.STATE_COMMANDE:
86ba2e51   JLo'w   XOR du cancer
43
44
45
46
47
48
49
                      if(b.getInt("quantite") != 0){
                          mParam.put("quantite", URLEncoder.encode(String.valueOf(b.getInt("quantite")), "UTF-8"));
                      }
                      else{
                          mParam.put("montant", URLEncoder.encode(String.valueOf(b.getFloat("montant")), "UTF-8"));
  
                      }
edd209be   JLo'w   URL encode + pref...
50
51
52
                      mAPI = "api/client/payer";
                      break;
                  case MainActivite.STATE_CREATION_COMPTE:
76de4d29   JLo'w   Connexion et NFC ...
53
                      mParam.put("solde", URLEncoder.encode(String.valueOf(b.getFloat("montant")), "UTF-8"));
edd209be   JLo'w   URL encode + pref...
54
55
56
                      mAPI = "api/client/ajouter";
                      break;
                  case MainActivite.STATE_RECHARGEMENT:
76de4d29   JLo'w   Connexion et NFC ...
57
                      mParam.put("montant", URLEncoder.encode(String.valueOf(b.getFloat("montant")), "UTF-8"));
edd209be   JLo'w   URL encode + pref...
58
59
60
                      mAPI = "api/client/recharger";
                      break;
                  case MainActivite.STATE_VIDANGE:
edd209be   JLo'w   URL encode + pref...
61
62
63
64
65
                      mAPI = "api/client/vidange";
                      break;
                  case MainActivite.STATE_CONNEXION:  //Impossible c'est pas géré ici
                  case MainActivite.STATE_RIEN:
                  default:
76de4d29   JLo'w   Connexion et NFC ...
66
                      Snackbar.make(getActivity().findViewById(R.id.coordinator), "WTF, le cancer est dans l'application!!", Snackbar.LENGTH_INDEFINITE).show();
edd209be   JLo'w   URL encode + pref...
67
68
69
70
              }
          }
          catch (Throwable t){
              System.out.println("Exception: " + t.toString());
86ba2e51   JLo'w   XOR du cancer
71
              Snackbar.make(getActivity().findViewById(R.id.coordinator), "WTF, le cancer est dans l'application!! " + t.toString(), Snackbar.LENGTH_INDEFINITE).show();
520cecde   JLo'w   Un petit plus, un...
72
73
          }
  
76de4d29   JLo'w   Connexion et NFC ...
74
          return ret;
6feece85   JLo'w   ajout NFC
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
      }
  
      // 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;
      }
  
93f90e35   JLo'w   NFC et foreground
93
  
93f90e35   JLo'w   NFC et foreground
94
  
76de4d29   JLo'w   Connexion et NFC ...
95
      public void handleIntent(Intent intent){
93f90e35   JLo'w   NFC et foreground
96
          if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
6feece85   JLo'w   ajout NFC
97
              String id_carte = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID));
f775eb7e   JLo'w   Toast cancers + p...
98
              mParam.put("idCarte", id_carte);
520cecde   JLo'w   Un petit plus, un...
99
              clientAPI();
6feece85   JLo'w   ajout NFC
100
          }
6feece85   JLo'w   ajout NFC
101
102
      }
  
76de4d29   JLo'w   Connexion et NFC ...
103
  
520cecde   JLo'w   Un petit plus, un...
104
105
106
  
      public void clientAPI() {
          try {
76de4d29   JLo'w   Connexion et NFC ...
107
              SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
edd209be   JLo'w   URL encode + pref...
108
              URL url = new URL(settings.getString("server_address", null) + mAPI);
520cecde   JLo'w   Un petit plus, un...
109
              NetworkThread nt = new NetworkThread(url, mParam);
76de4d29   JLo'w   Connexion et NFC ...
110
              nt.delegate = (MainActivite) getActivity();
520cecde   JLo'w   Un petit plus, un...
111
112
113
              nt.execute();
          }
          catch (Throwable t){
76de4d29   JLo'w   Connexion et NFC ...
114
              Toast.makeText(getActivity().getApplicationContext(), "WTF, le cancer est dans l'application!! " + t.toString(), Toast.LENGTH_LONG).show();
520cecde   JLo'w   Un petit plus, un...
115
116
          }
      }
c8059613   JLo'w   Re-fonte de l'app...
117
  }