Commit 76de4d29ee24dff5c70fe1545e06b594f06b2406
1 parent
efd095eb
Connexion et NFC sur fragment séparé+snack d'info
Showing
13 changed files
with
174 additions
and
176 deletions
Show diff stats
PremiereActivite/app/src/main/java/com/example/app_10p5/TabFragment5.java renamed to PremiereActivite/app/src/main/java/com/example/app_10p5/ConnectionFragment.java
1 | 1 | package com.example.app_10p5; |
2 | 2 | |
3 | -import android.os.Bundle; | |
4 | 3 | import android.app.Fragment; |
4 | +import android.os.Bundle; | |
5 | 5 | import android.view.LayoutInflater; |
6 | 6 | import android.view.View; |
7 | 7 | import android.view.ViewGroup; |
8 | 8 | |
9 | 9 | /** |
10 | - * Created by beaus on 24/04/2016. | |
10 | + * Created by beaus on 05/05/2016. | |
11 | 11 | */ |
12 | +public class ConnectionFragment extends Fragment { | |
13 | + @Override | |
14 | + public void onCreate(Bundle savedInstanceState){ | |
15 | + super.onCreate(savedInstanceState); | |
16 | + } | |
12 | 17 | |
13 | -public class TabFragment5 extends Fragment { | |
14 | 18 | @Override |
15 | 19 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
16 | - return inflater.inflate(R.layout.tab_fragment_5, container, false); | |
20 | + View ret = inflater.inflate(R.layout.layout_connection, container, false); | |
21 | + return ret; | |
17 | 22 | } |
18 | -} | |
19 | 23 | \ No newline at end of file |
24 | +} | ... | ... |
PremiereActivite/app/src/main/java/com/example/app_10p5/MainActivite.java
1 | 1 | package com.example.app_10p5; |
2 | 2 | |
3 | 3 | import android.app.Activity; |
4 | +import android.app.Fragment; | |
4 | 5 | import android.app.FragmentManager; |
5 | 6 | import android.app.FragmentTransaction; |
6 | -import android.content.Context; | |
7 | +import android.app.PendingIntent; | |
7 | 8 | import android.content.Intent; |
8 | 9 | import android.content.SharedPreferences; |
9 | -import android.graphics.drawable.BitmapDrawable; | |
10 | 10 | import android.graphics.drawable.ColorDrawable; |
11 | +import android.nfc.NfcAdapter; | |
11 | 12 | import android.os.Bundle; |
12 | 13 | import android.preference.PreferenceManager; |
14 | +import android.support.design.widget.Snackbar; | |
13 | 15 | import android.support.v4.view.ViewPager; |
14 | 16 | import android.text.TextUtils; |
15 | -import android.view.Gravity; | |
16 | -import android.view.LayoutInflater; | |
17 | 17 | import android.view.Menu; |
18 | 18 | import android.view.MenuInflater; |
19 | 19 | import android.view.MenuItem; |
20 | 20 | import android.view.View; |
21 | -import android.view.ViewGroup; | |
22 | -import android.widget.Button; | |
23 | -import android.widget.EditText; | |
24 | -import android.widget.FrameLayout; | |
25 | -import android.widget.LinearLayout; | |
26 | -import android.widget.PopupMenu; | |
27 | -import android.widget.PopupWindow; | |
21 | +import android.widget.EditText;; | |
28 | 22 | import android.widget.Toast; |
29 | 23 | |
30 | 24 | import org.json.JSONObject; |
... | ... | @@ -54,6 +48,8 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr |
54 | 48 | private long mTimeToken; |
55 | 49 | private String mUser; |
56 | 50 | |
51 | + private NfcAdapter mNfcAdapter; | |
52 | + | |
57 | 53 | @Override |
58 | 54 | protected void onCreate(Bundle savedInstanceState){ |
59 | 55 | super.onCreate(savedInstanceState); |
... | ... | @@ -67,11 +63,12 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr |
67 | 63 | mToken = null; |
68 | 64 | |
69 | 65 | getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimary))); |
66 | + mNfcAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext()); | |
70 | 67 | |
71 | 68 | if(savedInstanceState == null){ |
72 | 69 | FragmentManager fragmentManager = getFragmentManager(); |
73 | 70 | FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); |
74 | - main_tab_frag fragment = new main_tab_frag(); | |
71 | + ConnectionFragment fragment = new ConnectionFragment(); | |
75 | 72 | fragmentTransaction.add(R.id.fragment_container, fragment); |
76 | 73 | fragmentTransaction.commit(); |
77 | 74 | } |
... | ... | @@ -120,6 +117,39 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr |
120 | 117 | super.onSaveInstanceState(savedInstanceState); |
121 | 118 | } |
122 | 119 | |
120 | + @Override | |
121 | + public void onPause() { | |
122 | + stopForegroundDispatch(this, mNfcAdapter); | |
123 | + super.onPause(); | |
124 | + } | |
125 | + | |
126 | + @Override | |
127 | + public void onResume() { | |
128 | + super.onResume(); | |
129 | + setupForegroundDispatch(this, mNfcAdapter); | |
130 | + } | |
131 | + | |
132 | + public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter){ | |
133 | + if(adapter != null){ | |
134 | + final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass()); | |
135 | + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); | |
136 | + final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0); | |
137 | + adapter.enableForegroundDispatch(activity, pendingIntent, null, null); | |
138 | + } | |
139 | + else{ | |
140 | + Toast.makeText(activity, "Impossible d'initialiser le NFC", Toast.LENGTH_SHORT).show(); | |
141 | + } | |
142 | + } | |
143 | + | |
144 | + public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) { | |
145 | + if(adapter != null){ | |
146 | + adapter.disableForegroundDispatch(activity); | |
147 | + } | |
148 | + else{ | |
149 | + Toast.makeText(activity, "Impossible d'initialiser le NFC", Toast.LENGTH_SHORT).show(); | |
150 | + } | |
151 | + } | |
152 | + | |
123 | 153 | public void valideCreationCompte(View v){ |
124 | 154 | if((TextUtils.getTrimmedLength(mToken) == 30) && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) { |
125 | 155 | EditText champMontant = (EditText) findViewById(R.id.creation_montant); |
... | ... | @@ -138,11 +168,16 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr |
138 | 168 | mState = STATE_CREATION_COMPTE; |
139 | 169 | champMontant.setText(null); |
140 | 170 | |
141 | - Intent intent = new Intent(this, CarteActivite.class); | |
142 | - intent.putExtra("token", mToken); | |
143 | - intent.putExtra("state", mState); | |
144 | - intent.putExtra("montant", montant); | |
145 | - startActivityForResult(intent, mState); | |
171 | + Bundle b = new Bundle(); | |
172 | + b.putString("token", mToken); | |
173 | + b.putInt("state", mState); | |
174 | + b.putFloat("montant", montant); | |
175 | + | |
176 | + | |
177 | + NFCFragment nfc = new NFCFragment(); | |
178 | + nfc.setArguments(b); | |
179 | + | |
180 | + getFragmentManager().beginTransaction().replace(R.id.fragment_container, nfc).addToBackStack(null).commit(); | |
146 | 181 | } |
147 | 182 | else{ |
148 | 183 | Toast.makeText(this, "Valeur incorrecte.", Toast.LENGTH_LONG).show(); |
... | ... | @@ -185,12 +220,16 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr |
185 | 220 | champMontant.setText(null); |
186 | 221 | champQuantite.setText(null); |
187 | 222 | |
188 | - Intent intent = new Intent(this, CarteActivite.class); | |
189 | - intent.putExtra("token", mToken); | |
190 | - intent.putExtra("state", mState); | |
191 | - intent.putExtra("montant", montant); | |
192 | - intent.putExtra("quantite", quantite); | |
193 | - startActivityForResult(intent, mState); | |
223 | + Bundle b = new Bundle(); | |
224 | + b.putString("token", mToken); | |
225 | + b.putInt("state", mState); | |
226 | + b.putFloat("montant", montant); | |
227 | + b.putInt("quantite", quantite); | |
228 | + | |
229 | + NFCFragment nfc = new NFCFragment(); | |
230 | + nfc.setArguments(b); | |
231 | + | |
232 | + getFragmentManager().beginTransaction().replace(R.id.fragment_container, nfc).addToBackStack(null).commit(); | |
194 | 233 | } |
195 | 234 | else{ |
196 | 235 | Toast.makeText(this, "Valeur incorrecte ou droit insuffisant.", Toast.LENGTH_LONG).show(); |
... | ... | @@ -221,11 +260,15 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr |
221 | 260 | mState = STATE_RECHARGEMENT; |
222 | 261 | champMontant.setText(null); |
223 | 262 | |
224 | - Intent intent = new Intent(this, CarteActivite.class); | |
225 | - intent.putExtra("token", mToken); | |
226 | - intent.putExtra("state", mState); | |
227 | - intent.putExtra("montant", montant); | |
228 | - startActivityForResult(intent, mState); | |
263 | + Bundle b = new Bundle(); | |
264 | + b.putString("token", mToken); | |
265 | + b.putInt("state", mState); | |
266 | + b.putFloat("montant", montant); | |
267 | + | |
268 | + NFCFragment nfc = new NFCFragment(); | |
269 | + nfc.setArguments(b); | |
270 | + | |
271 | + getFragmentManager().beginTransaction().replace(R.id.fragment_container, nfc).addToBackStack(null).commit(); | |
229 | 272 | } |
230 | 273 | else{ |
231 | 274 | Toast.makeText(this, "Valeur incorrecte.", Toast.LENGTH_LONG).show(); |
... | ... | @@ -288,10 +331,14 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr |
288 | 331 | |
289 | 332 | if((mDroit >= 2)){ |
290 | 333 | mState = STATE_VIDANGE; |
291 | - Intent intent = new Intent(this, CarteActivite.class); | |
292 | - intent.putExtra("token", mToken); | |
293 | - intent.putExtra("state", mState); | |
294 | - startActivityForResult(intent, mState); | |
334 | + Bundle b = new Bundle(); | |
335 | + b.putString("token", mToken); | |
336 | + b.putInt("state", mState); | |
337 | + | |
338 | + NFCFragment nfc = new NFCFragment(); | |
339 | + nfc.setArguments(b); | |
340 | + | |
341 | + getFragmentManager().beginTransaction().replace(R.id.fragment_container, nfc).addToBackStack(null).commit(); | |
295 | 342 | } |
296 | 343 | else{ |
297 | 344 | Toast.makeText(this, "Droit insuffisant.", Toast.LENGTH_LONG).show(); |
... | ... | @@ -303,55 +350,52 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr |
303 | 350 | } |
304 | 351 | |
305 | 352 | @Override |
306 | - protected void onActivityResult(int requestCode, int resultCode, Intent data){ | |
307 | - //TODO: faire des choses avec ca | |
308 | - | |
309 | - mState = STATE_RIEN; | |
310 | - | |
311 | - try{ | |
312 | - JSONObject json = new JSONObject(data.getStringExtra("json")); | |
313 | - Toast.makeText(this, "Status: " + json.getString("status"), Toast.LENGTH_SHORT).show(); | |
353 | + protected void onNewIntent(Intent intent){ | |
354 | + super.onNewIntent(intent); | |
355 | + Fragment frag = getFragmentManager().findFragmentById(R.id.fragment_container); | |
314 | 356 | |
315 | - } | |
316 | - catch (Throwable t){ | |
317 | - Toast.makeText(this, "Retour", Toast.LENGTH_SHORT).show(); | |
318 | - } | |
319 | - | |
320 | - | |
321 | - switch (requestCode) { | |
322 | - case STATE_COMMANDE: | |
323 | - break; | |
324 | - case STATE_CONNEXION: | |
325 | - break; | |
326 | - case STATE_CREATION_COMPTE: | |
327 | - break; | |
328 | - case STATE_RECHARGEMENT: | |
329 | - break; | |
330 | - case STATE_VIDANGE: | |
331 | - break; | |
332 | - case STATE_RIEN: | |
333 | - default: | |
334 | - Toast.makeText(this, "WTF, le cancer est dans l'application!!", Toast.LENGTH_LONG).show(); | |
335 | - break; | |
357 | + if(mState != STATE_RIEN && frag instanceof NFCFragment){ | |
358 | + NFCFragment nfc = (NFCFragment) frag; | |
359 | + nfc.handleIntent(intent); | |
336 | 360 | } |
337 | 361 | } |
338 | 362 | |
339 | 363 | /* Retour du network thread */ |
340 | 364 | @Override |
341 | 365 | public void processFinish(JSONObject output) { |
366 | + | |
342 | 367 | if(output.length() != 0){ |
343 | 368 | try{ |
344 | 369 | if(output.get("status").toString().equals("ok")){ |
345 | - mToken = output.get("jeton").toString(); | |
346 | - mTimeToken = System.currentTimeMillis(); | |
347 | - mDroit = output.getInt("droit"); | |
348 | - mUser = output.get("login").toString(); | |
349 | - Toast.makeText(this, "Bonjour " + mUser + " vous êtes bien connecté pour " + EXPIRATION / (1000 * 60) + " minutes.", Toast.LENGTH_LONG).show(); | |
350 | - final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); | |
351 | - viewPager.setCurrentItem(1); | |
370 | + switch (mState){ | |
371 | + case STATE_COMMANDE: | |
372 | + Snackbar.make(findViewById(R.id.coordinator), "Client débité de " + output.get("montant") + "€. " + output.get("soldeAncien") + "€ -> " + output.getString("soldeNouveau") + "€", Snackbar.LENGTH_INDEFINITE).show(); | |
373 | + break; | |
374 | + case STATE_CONNEXION: | |
375 | + mToken = output.get("jeton").toString(); | |
376 | + mTimeToken = System.currentTimeMillis(); | |
377 | + mDroit = output.getInt("droit"); | |
378 | + mUser = output.get("login").toString(); | |
379 | + Snackbar.make(findViewById(R.id.coordinator), "Bonjour " + mUser + " vous êtes connecté pour " + EXPIRATION / (1000 * 60) + " minutes.", Snackbar.LENGTH_SHORT).show(); | |
380 | + getFragmentManager().beginTransaction().replace(R.id.fragment_container, new main_tab_frag()).commit(); | |
381 | + break; | |
382 | + case STATE_CREATION_COMPTE: | |
383 | + Snackbar.make(findViewById(R.id.coordinator), "Client créé avec un solde de " + output.get("soldeNouveau") + "€", Snackbar.LENGTH_INDEFINITE).show(); | |
384 | + break; | |
385 | + case STATE_RECHARGEMENT: | |
386 | + Snackbar.make(findViewById(R.id.coordinator), "Client rechargé: " + output.get("soldeAncien") + "€ ->" + output.get("soldeNouveau") + "€", Snackbar.LENGTH_INDEFINITE).show(); | |
387 | + break; | |
388 | + case STATE_VIDANGE: | |
389 | + Snackbar.make(findViewById(R.id.coordinator), "Client vidé: " + output.get("soldeAncien") + "€ -> 0€", Snackbar.LENGTH_INDEFINITE).show(); | |
390 | + break; | |
391 | + case STATE_RIEN: | |
392 | + default: | |
393 | + Toast.makeText(this, "WTF, le cancer est dans l'application!!", Toast.LENGTH_LONG).show(); | |
394 | + break; | |
395 | + } | |
352 | 396 | } |
353 | 397 | else{ |
354 | - Toast.makeText(this, "Erreur dans la requête: " + output.get("status"), Toast.LENGTH_LONG).show(); | |
398 | + Toast.makeText(this, "Erreur: " + output.get("status"), Toast.LENGTH_LONG).show(); | |
355 | 399 | } |
356 | 400 | } |
357 | 401 | catch(Throwable t){ |
... | ... | @@ -361,6 +405,9 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr |
361 | 405 | else{ |
362 | 406 | Toast.makeText(this, "Impossible de se connecter au serveur", Toast.LENGTH_LONG).show(); |
363 | 407 | } |
408 | + | |
409 | + mState = STATE_RIEN; | |
410 | + getFragmentManager().popBackStack(); | |
364 | 411 | } |
365 | 412 | |
366 | 413 | public String getToken(){ |
... | ... | @@ -376,6 +423,7 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr |
376 | 423 | mDroit = 0; |
377 | 424 | mUser = null; |
378 | 425 | mTimeToken = -1; |
426 | + mState = STATE_RIEN; | |
379 | 427 | |
380 | 428 | Toast.makeText(this, "Veuillez vous reconnecter.", Toast.LENGTH_SHORT).show(); |
381 | 429 | final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); | ... | ... |
PremiereActivite/app/src/main/java/com/example/app_10p5/CarteActivite.java renamed to PremiereActivite/app/src/main/java/com/example/app_10p5/NFCFragment.java
1 | 1 | package com.example.app_10p5; |
2 | 2 | |
3 | -import android.app.Activity; | |
4 | -import android.app.PendingIntent; | |
3 | +import android.app.Fragment; | |
5 | 4 | import android.content.Intent; |
6 | 5 | import android.content.SharedPreferences; |
7 | -import android.graphics.drawable.ColorDrawable; | |
8 | 6 | import android.nfc.NfcAdapter; |
9 | -import android.nfc.Tag; | |
10 | -import android.nfc.tech.MifareClassic; | |
11 | 7 | import android.os.Bundle; |
12 | 8 | import android.preference.PreferenceManager; |
9 | +import android.support.design.widget.Snackbar; | |
10 | +import android.view.LayoutInflater; | |
11 | +import android.view.View; | |
12 | +import android.view.ViewGroup; | |
13 | 13 | import android.widget.Toast; |
14 | 14 | |
15 | -import org.json.JSONObject; | |
16 | - | |
17 | 15 | import java.net.URL; |
18 | 16 | import java.net.URLEncoder; |
19 | 17 | import java.util.HashMap; |
20 | 18 | |
21 | 19 | /** |
22 | - * Created by beaus on 24/04/2016. | |
20 | + * Created by beaus on 05/05/2016. | |
23 | 21 | */ |
24 | -public class CarteActivite extends Activity implements ASyncResponse { | |
25 | - | |
26 | - private NfcAdapter mNfcAdapter; | |
22 | +public class NFCFragment extends Fragment { | |
27 | 23 | private HashMap<String, String> mParam; |
28 | 24 | private String mAPI; |
29 | 25 | |
30 | 26 | @Override |
31 | - protected void onCreate(Bundle savedInstanceState) { | |
27 | + public void onCreate(Bundle savedInstanceState){ | |
32 | 28 | super.onCreate(savedInstanceState); |
33 | - setContentView(R.layout.layout_carte); | |
34 | - getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimary))); | |
29 | + } | |
30 | + | |
31 | + @Override | |
32 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
33 | + View ret = inflater.inflate(R.layout.layout_carte, container, false); | |
35 | 34 | |
36 | 35 | mParam = new HashMap<String, String>(); |
37 | 36 | |
37 | + Bundle b = getArguments(); | |
38 | + | |
38 | 39 | try { |
39 | - switch (getIntent().getIntExtra("state", MainActivite.STATE_RIEN)) { | |
40 | + switch (b.getInt("state")) { | |
40 | 41 | case MainActivite.STATE_COMMANDE: |
41 | 42 | //TODO: XOR du cancer |
42 | 43 | //mParam.put("quantite", String.valueOf(getIntent().getIntExtra("quantite", -1))); |
43 | - mParam.put("montant", URLEncoder.encode(String.valueOf(getIntent().getFloatExtra("montant", -1)), "UTF-8")); | |
44 | - mParam.put("jeton", URLEncoder.encode(getIntent().getStringExtra("token"), "UTF-8")); | |
44 | + mParam.put("montant", URLEncoder.encode(String.valueOf(b.getFloat("montant")), "UTF-8")); | |
45 | + mParam.put("jeton", URLEncoder.encode(b.getString("token"), "UTF-8")); | |
45 | 46 | mAPI = "api/client/payer"; |
46 | 47 | break; |
47 | 48 | case MainActivite.STATE_CREATION_COMPTE: |
48 | - mParam.put("solde", URLEncoder.encode(String.valueOf(getIntent().getFloatExtra("montant", -1)), "UTF-8")); | |
49 | - mParam.put("jeton", URLEncoder.encode(getIntent().getStringExtra("token"), "UTF-8")); | |
49 | + mParam.put("solde", URLEncoder.encode(String.valueOf(b.getFloat("montant")), "UTF-8")); | |
50 | + mParam.put("jeton", URLEncoder.encode(b.getString("token"), "UTF-8")); | |
50 | 51 | mAPI = "api/client/ajouter"; |
51 | 52 | break; |
52 | 53 | case MainActivite.STATE_RECHARGEMENT: |
53 | 54 | System.out.println("bite"); |
54 | - mParam.put("montant", URLEncoder.encode(String.valueOf(getIntent().getFloatExtra("montant", -1)), "UTF-8")); | |
55 | - mParam.put("jeton", URLEncoder.encode(getIntent().getStringExtra("token"), "UTF-8")); | |
55 | + mParam.put("montant", URLEncoder.encode(String.valueOf(b.getFloat("montant")), "UTF-8")); | |
56 | + mParam.put("jeton", URLEncoder.encode(b.getString("token"), "UTF-8")); | |
56 | 57 | mAPI = "api/client/recharger"; |
57 | 58 | break; |
58 | 59 | case MainActivite.STATE_VIDANGE: |
59 | - mParam.put("jeton", URLEncoder.encode(getIntent().getStringExtra("token"), "UTF-8")); | |
60 | + mParam.put("jeton", URLEncoder.encode(b.getString("token"), "UTF-8")); | |
60 | 61 | mAPI = "api/client/vidange"; |
61 | 62 | break; |
62 | 63 | case MainActivite.STATE_CONNEXION: //Impossible c'est pas géré ici |
63 | 64 | case MainActivite.STATE_RIEN: |
64 | 65 | default: |
65 | - Toast.makeText(this, "WTF, le cancer est dans l'application!!", Toast.LENGTH_LONG).show(); | |
66 | - finish(); | |
67 | - return; | |
66 | + Snackbar.make(getActivity().findViewById(R.id.coordinator), "WTF, le cancer est dans l'application!!", Snackbar.LENGTH_INDEFINITE).show(); | |
68 | 67 | } |
69 | 68 | } |
70 | 69 | catch (Throwable t){ |
71 | 70 | System.out.println("Exception: " + t.toString()); |
72 | 71 | } |
73 | 72 | |
74 | - mNfcAdapter = NfcAdapter.getDefaultAdapter(this); | |
75 | - | |
76 | - if (mNfcAdapter == null) { | |
77 | - // Stop here, we definitely need NFC | |
78 | - Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show(); | |
79 | - finish(); | |
80 | - return; | |
81 | - } | |
82 | - | |
83 | - handleIntent(getIntent()); | |
84 | - } | |
85 | - | |
86 | - @Override | |
87 | - protected void onResume() { | |
88 | - super.onResume(); | |
89 | - setupForegroundDispatch(this, mNfcAdapter); | |
73 | + return ret; | |
90 | 74 | } |
91 | 75 | |
92 | 76 | // Convertit l'array de byte en chaîne hexadécimale (si le byte = 0x63, str = "63"). |
... | ... | @@ -105,58 +89,28 @@ public class CarteActivite extends Activity implements ASyncResponse { |
105 | 89 | return out; |
106 | 90 | } |
107 | 91 | |
108 | - @Override | |
109 | - protected void onNewIntent(Intent intent) { | |
110 | - handleIntent(intent); | |
111 | - } | |
112 | 92 | |
113 | - @Override | |
114 | - protected void onPause() { | |
115 | - stopForegroundDispatch(this, mNfcAdapter); | |
116 | - super.onPause(); | |
117 | - } | |
118 | 93 | |
119 | - private void handleIntent(Intent intent){ | |
94 | + public void handleIntent(Intent intent){ | |
120 | 95 | if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { |
121 | 96 | String id_carte = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)); |
122 | 97 | mParam.put("idCarte", id_carte); |
123 | 98 | clientAPI(); |
124 | - } | |
125 | 99 | } |
126 | - | |
127 | - public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter){ | |
128 | - final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass()); | |
129 | - intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); | |
130 | - final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0); | |
131 | - adapter.enableForegroundDispatch(activity, pendingIntent, null, null); | |
132 | 100 | } |
133 | 101 | |
134 | - public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) { | |
135 | - adapter.disableForegroundDispatch(activity); | |
136 | - } | |
102 | + | |
137 | 103 | |
138 | 104 | public void clientAPI() { |
139 | 105 | try { |
140 | - SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); | |
106 | + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); | |
141 | 107 | URL url = new URL(settings.getString("server_address", null) + mAPI); |
142 | 108 | NetworkThread nt = new NetworkThread(url, mParam); |
143 | - nt.delegate = this; | |
109 | + nt.delegate = (MainActivite) getActivity(); | |
144 | 110 | nt.execute(); |
145 | 111 | } |
146 | 112 | catch (Throwable t){ |
147 | - Toast.makeText(this, "WTF, le cancer est dans l'application!! " + t.toString(), Toast.LENGTH_LONG).show(); | |
148 | - finish(); | |
149 | - return; | |
113 | + Toast.makeText(getActivity().getApplicationContext(), "WTF, le cancer est dans l'application!! " + t.toString(), Toast.LENGTH_LONG).show(); | |
150 | 114 | } |
151 | 115 | } |
152 | - | |
153 | - | |
154 | - /* Retour du NetworkThread */ | |
155 | - @Override | |
156 | - public void processFinish(JSONObject output) { | |
157 | - Intent intent = new Intent(this, CarteActivite.class); | |
158 | - intent.putExtra("json", output.toString()); | |
159 | - setResult(0, intent); | |
160 | - finish(); | |
161 | - } | |
162 | 116 | } | ... | ... |
PremiereActivite/app/src/main/java/com/example/app_10p5/PagerAdapter.java
PremiereActivite/app/src/main/java/com/example/app_10p5/TabFragment1.java
... | ... | @@ -2,9 +2,11 @@ package com.example.app_10p5; |
2 | 2 | |
3 | 3 | import android.os.Bundle; |
4 | 4 | import android.app.Fragment; |
5 | +import android.text.InputFilter; | |
5 | 6 | import android.view.LayoutInflater; |
6 | 7 | import android.view.View; |
7 | 8 | import android.view.ViewGroup; |
9 | +import android.widget.EditText; | |
8 | 10 | |
9 | 11 | /** |
10 | 12 | * Created by beaus on 24/04/2016. |
... | ... | @@ -13,6 +15,10 @@ import android.view.ViewGroup; |
13 | 15 | public class TabFragment1 extends Fragment { |
14 | 16 | @Override |
15 | 17 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
16 | - return inflater.inflate(R.layout.tab_fragment_1, container, false); | |
18 | + View v = inflater.inflate(R.layout.tab_fragment_1, container, false); | |
19 | + EditText et = (EditText) v.findViewById(R.id.commande_prix); | |
20 | + et.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(3, 2)}); | |
21 | + | |
22 | + return v; | |
17 | 23 | } |
18 | 24 | } |
19 | 25 | \ No newline at end of file | ... | ... |
PremiereActivite/app/src/main/java/com/example/app_10p5/TabFragment2.java
... | ... | @@ -2,11 +2,9 @@ package com.example.app_10p5; |
2 | 2 | |
3 | 3 | import android.os.Bundle; |
4 | 4 | import android.app.Fragment; |
5 | -import android.text.InputFilter; | |
6 | 5 | import android.view.LayoutInflater; |
7 | 6 | import android.view.View; |
8 | 7 | import android.view.ViewGroup; |
9 | -import android.widget.EditText; | |
10 | 8 | |
11 | 9 | /** |
12 | 10 | * Created by beaus on 24/04/2016. |
... | ... | @@ -16,11 +14,6 @@ public class TabFragment2 extends Fragment { |
16 | 14 | |
17 | 15 | @Override |
18 | 16 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
19 | - View v = inflater.inflate(R.layout.tab_fragment_2, container, false);; | |
20 | - | |
21 | - EditText et = (EditText) v.findViewById(R.id.commande_prix); | |
22 | - et.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(3, 2)}); | |
23 | - | |
24 | - return v; | |
17 | + return inflater.inflate(R.layout.tab_fragment_2, container, false); | |
25 | 18 | } |
26 | 19 | } | ... | ... |
PremiereActivite/app/src/main/java/com/example/app_10p5/main_tab_frag.java
... | ... | @@ -32,7 +32,6 @@ public class main_tab_frag extends Fragment { |
32 | 32 | |
33 | 33 | TabLayout tabLayout = (TabLayout) ret.findViewById(R.id.tab_layout); |
34 | 34 | tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); |
35 | - tabLayout.addTab(tabLayout.newTab().setText("Connexion")); | |
36 | 35 | tabLayout.addTab(tabLayout.newTab().setText("Commande")); |
37 | 36 | tabLayout.addTab(tabLayout.newTab().setText("Rechargement")); |
38 | 37 | tabLayout.addTab(tabLayout.newTab().setText("Création")); | ... | ... |
PremiereActivite/app/src/main/res/layout/layout_main.xml
1 | 1 | <?xml version="1.0" encoding="utf-8"?> |
2 | +<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
3 | + android:layout_width="match_parent" | |
4 | + android:layout_height="match_parent" | |
5 | + android:id="@+id/coordinator"> | |
2 | 6 | <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" |
3 | 7 | android:id="@+id/fragment_container" |
4 | 8 | android:layout_width="match_parent" |
5 | 9 | android:layout_height="match_parent" |
6 | 10 | android:background="?android:attr/detailsElementBackground" /> |
11 | +</android.support.design.widget.CoordinatorLayout> | ... | ... |
PremiereActivite/app/src/main/res/layout/tab_fragment_1.xml
PremiereActivite/app/src/main/res/layout/tab_fragment_2.xml
PremiereActivite/app/src/main/res/layout/tab_fragment_3.xml
PremiereActivite/app/src/main/res/layout/tab_fragment_4.xml
PremiereActivite/app/src/main/res/layout/tab_fragment_5.xml deleted
... | ... | @@ -1,9 +0,0 @@ |
1 | -<?xml version="1.0" encoding="utf-8"?> | |
2 | -<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
3 | - android:layout_width="match_parent" | |
4 | - android:layout_height="match_parent" | |
5 | - android:orientation="vertical"> | |
6 | - | |
7 | - <include layout="@layout/layout_vidange"></include> | |
8 | - | |
9 | -</RelativeLayout> | |
10 | 0 | \ No newline at end of file |