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
|
/**
|
76de4d29
JLo'w
Connexion et NFC ...
|
20
|
* Created by beaus 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 {
|
76de4d29
JLo'w
Connexion et NFC ...
|
40
|
switch (b.getInt("state")) {
|
edd209be
JLo'w
URL encode + pref...
|
41
42
43
|
case MainActivite.STATE_COMMANDE:
//TODO: XOR du cancer
//mParam.put("quantite", String.valueOf(getIntent().getIntExtra("quantite", -1)));
|
76de4d29
JLo'w
Connexion et NFC ...
|
44
45
|
mParam.put("montant", URLEncoder.encode(String.valueOf(b.getFloat("montant")), "UTF-8"));
mParam.put("jeton", URLEncoder.encode(b.getString("token"), "UTF-8"));
|
edd209be
JLo'w
URL encode + pref...
|
46
47
48
|
mAPI = "api/client/payer";
break;
case MainActivite.STATE_CREATION_COMPTE:
|
76de4d29
JLo'w
Connexion et NFC ...
|
49
50
|
mParam.put("solde", URLEncoder.encode(String.valueOf(b.getFloat("montant")), "UTF-8"));
mParam.put("jeton", URLEncoder.encode(b.getString("token"), "UTF-8"));
|
edd209be
JLo'w
URL encode + pref...
|
51
52
53
54
|
mAPI = "api/client/ajouter";
break;
case MainActivite.STATE_RECHARGEMENT:
System.out.println("bite");
|
76de4d29
JLo'w
Connexion et NFC ...
|
55
56
|
mParam.put("montant", URLEncoder.encode(String.valueOf(b.getFloat("montant")), "UTF-8"));
mParam.put("jeton", URLEncoder.encode(b.getString("token"), "UTF-8"));
|
edd209be
JLo'w
URL encode + pref...
|
57
58
59
|
mAPI = "api/client/recharger";
break;
case MainActivite.STATE_VIDANGE:
|
76de4d29
JLo'w
Connexion et NFC ...
|
60
|
mParam.put("jeton", URLEncoder.encode(b.getString("token"), "UTF-8"));
|
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());
|
520cecde
JLo'w
Un petit plus, un...
|
71
72
|
}
|
76de4d29
JLo'w
Connexion et NFC ...
|
73
|
return ret;
|
6feece85
JLo'w
ajout NFC
|
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
}
// 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
|
92
|
|
93f90e35
JLo'w
NFC et foreground
|
93
|
|
76de4d29
JLo'w
Connexion et NFC ...
|
94
|
public void handleIntent(Intent intent){
|
93f90e35
JLo'w
NFC et foreground
|
95
|
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
|
6feece85
JLo'w
ajout NFC
|
96
|
String id_carte = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID));
|
f775eb7e
JLo'w
Toast cancers + p...
|
97
|
mParam.put("idCarte", id_carte);
|
520cecde
JLo'w
Un petit plus, un...
|
98
|
clientAPI();
|
6feece85
JLo'w
ajout NFC
|
99
|
}
|
6feece85
JLo'w
ajout NFC
|
100
101
|
}
|
76de4d29
JLo'w
Connexion et NFC ...
|
102
|
|
520cecde
JLo'w
Un petit plus, un...
|
103
104
105
|
public void clientAPI() {
try {
|
76de4d29
JLo'w
Connexion et NFC ...
|
106
|
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
|
edd209be
JLo'w
URL encode + pref...
|
107
|
URL url = new URL(settings.getString("server_address", null) + mAPI);
|
520cecde
JLo'w
Un petit plus, un...
|
108
|
NetworkThread nt = new NetworkThread(url, mParam);
|
76de4d29
JLo'w
Connexion et NFC ...
|
109
|
nt.delegate = (MainActivite) getActivity();
|
520cecde
JLo'w
Un petit plus, un...
|
110
111
112
|
nt.execute();
}
catch (Throwable t){
|
76de4d29
JLo'w
Connexion et NFC ...
|
113
|
Toast.makeText(getActivity().getApplicationContext(), "WTF, le cancer est dans l'application!! " + t.toString(), Toast.LENGTH_LONG).show();
|
520cecde
JLo'w
Un petit plus, un...
|
114
115
|
}
}
|
c8059613
JLo'w
Re-fonte de l'app...
|
116
|
}
|