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 | package com.example.app_10p5; | 1 | package com.example.app_10p5; |
2 | 2 | ||
3 | -import android.os.Bundle; | ||
4 | import android.app.Fragment; | 3 | import android.app.Fragment; |
4 | +import android.os.Bundle; | ||
5 | import android.view.LayoutInflater; | 5 | import android.view.LayoutInflater; |
6 | import android.view.View; | 6 | import android.view.View; |
7 | import android.view.ViewGroup; | 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 | @Override | 18 | @Override |
15 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | 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 | \ No newline at end of file | 23 | \ No newline at end of file |
24 | +} |
PremiereActivite/app/src/main/java/com/example/app_10p5/MainActivite.java
1 | package com.example.app_10p5; | 1 | package com.example.app_10p5; |
2 | 2 | ||
3 | import android.app.Activity; | 3 | import android.app.Activity; |
4 | +import android.app.Fragment; | ||
4 | import android.app.FragmentManager; | 5 | import android.app.FragmentManager; |
5 | import android.app.FragmentTransaction; | 6 | import android.app.FragmentTransaction; |
6 | -import android.content.Context; | 7 | +import android.app.PendingIntent; |
7 | import android.content.Intent; | 8 | import android.content.Intent; |
8 | import android.content.SharedPreferences; | 9 | import android.content.SharedPreferences; |
9 | -import android.graphics.drawable.BitmapDrawable; | ||
10 | import android.graphics.drawable.ColorDrawable; | 10 | import android.graphics.drawable.ColorDrawable; |
11 | +import android.nfc.NfcAdapter; | ||
11 | import android.os.Bundle; | 12 | import android.os.Bundle; |
12 | import android.preference.PreferenceManager; | 13 | import android.preference.PreferenceManager; |
14 | +import android.support.design.widget.Snackbar; | ||
13 | import android.support.v4.view.ViewPager; | 15 | import android.support.v4.view.ViewPager; |
14 | import android.text.TextUtils; | 16 | import android.text.TextUtils; |
15 | -import android.view.Gravity; | ||
16 | -import android.view.LayoutInflater; | ||
17 | import android.view.Menu; | 17 | import android.view.Menu; |
18 | import android.view.MenuInflater; | 18 | import android.view.MenuInflater; |
19 | import android.view.MenuItem; | 19 | import android.view.MenuItem; |
20 | import android.view.View; | 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 | import android.widget.Toast; | 22 | import android.widget.Toast; |
29 | 23 | ||
30 | import org.json.JSONObject; | 24 | import org.json.JSONObject; |
@@ -54,6 +48,8 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr | @@ -54,6 +48,8 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr | ||
54 | private long mTimeToken; | 48 | private long mTimeToken; |
55 | private String mUser; | 49 | private String mUser; |
56 | 50 | ||
51 | + private NfcAdapter mNfcAdapter; | ||
52 | + | ||
57 | @Override | 53 | @Override |
58 | protected void onCreate(Bundle savedInstanceState){ | 54 | protected void onCreate(Bundle savedInstanceState){ |
59 | super.onCreate(savedInstanceState); | 55 | super.onCreate(savedInstanceState); |
@@ -67,11 +63,12 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr | @@ -67,11 +63,12 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr | ||
67 | mToken = null; | 63 | mToken = null; |
68 | 64 | ||
69 | getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimary))); | 65 | getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimary))); |
66 | + mNfcAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext()); | ||
70 | 67 | ||
71 | if(savedInstanceState == null){ | 68 | if(savedInstanceState == null){ |
72 | FragmentManager fragmentManager = getFragmentManager(); | 69 | FragmentManager fragmentManager = getFragmentManager(); |
73 | FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); | 70 | FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); |
74 | - main_tab_frag fragment = new main_tab_frag(); | 71 | + ConnectionFragment fragment = new ConnectionFragment(); |
75 | fragmentTransaction.add(R.id.fragment_container, fragment); | 72 | fragmentTransaction.add(R.id.fragment_container, fragment); |
76 | fragmentTransaction.commit(); | 73 | fragmentTransaction.commit(); |
77 | } | 74 | } |
@@ -120,6 +117,39 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr | @@ -120,6 +117,39 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr | ||
120 | super.onSaveInstanceState(savedInstanceState); | 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 | public void valideCreationCompte(View v){ | 153 | public void valideCreationCompte(View v){ |
124 | if((TextUtils.getTrimmedLength(mToken) == 30) && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) { | 154 | if((TextUtils.getTrimmedLength(mToken) == 30) && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) { |
125 | EditText champMontant = (EditText) findViewById(R.id.creation_montant); | 155 | EditText champMontant = (EditText) findViewById(R.id.creation_montant); |
@@ -138,11 +168,16 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr | @@ -138,11 +168,16 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr | ||
138 | mState = STATE_CREATION_COMPTE; | 168 | mState = STATE_CREATION_COMPTE; |
139 | champMontant.setText(null); | 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 | else{ | 182 | else{ |
148 | Toast.makeText(this, "Valeur incorrecte.", Toast.LENGTH_LONG).show(); | 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,12 +220,16 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr | ||
185 | champMontant.setText(null); | 220 | champMontant.setText(null); |
186 | champQuantite.setText(null); | 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 | else{ | 234 | else{ |
196 | Toast.makeText(this, "Valeur incorrecte ou droit insuffisant.", Toast.LENGTH_LONG).show(); | 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,11 +260,15 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr | ||
221 | mState = STATE_RECHARGEMENT; | 260 | mState = STATE_RECHARGEMENT; |
222 | champMontant.setText(null); | 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 | else{ | 273 | else{ |
231 | Toast.makeText(this, "Valeur incorrecte.", Toast.LENGTH_LONG).show(); | 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,10 +331,14 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr | ||
288 | 331 | ||
289 | if((mDroit >= 2)){ | 332 | if((mDroit >= 2)){ |
290 | mState = STATE_VIDANGE; | 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 | else{ | 343 | else{ |
297 | Toast.makeText(this, "Droit insuffisant.", Toast.LENGTH_LONG).show(); | 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,55 +350,52 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr | ||
303 | } | 350 | } |
304 | 351 | ||
305 | @Override | 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 | /* Retour du network thread */ | 363 | /* Retour du network thread */ |
340 | @Override | 364 | @Override |
341 | public void processFinish(JSONObject output) { | 365 | public void processFinish(JSONObject output) { |
366 | + | ||
342 | if(output.length() != 0){ | 367 | if(output.length() != 0){ |
343 | try{ | 368 | try{ |
344 | if(output.get("status").toString().equals("ok")){ | 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 | else{ | 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 | catch(Throwable t){ | 401 | catch(Throwable t){ |
@@ -361,6 +405,9 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr | @@ -361,6 +405,9 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr | ||
361 | else{ | 405 | else{ |
362 | Toast.makeText(this, "Impossible de se connecter au serveur", Toast.LENGTH_LONG).show(); | 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 | public String getToken(){ | 413 | public String getToken(){ |
@@ -376,6 +423,7 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr | @@ -376,6 +423,7 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr | ||
376 | mDroit = 0; | 423 | mDroit = 0; |
377 | mUser = null; | 424 | mUser = null; |
378 | mTimeToken = -1; | 425 | mTimeToken = -1; |
426 | + mState = STATE_RIEN; | ||
379 | 427 | ||
380 | Toast.makeText(this, "Veuillez vous reconnecter.", Toast.LENGTH_SHORT).show(); | 428 | Toast.makeText(this, "Veuillez vous reconnecter.", Toast.LENGTH_SHORT).show(); |
381 | final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); | 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 | package com.example.app_10p5; | 1 | package com.example.app_10p5; |
2 | 2 | ||
3 | -import android.app.Activity; | ||
4 | -import android.app.PendingIntent; | 3 | +import android.app.Fragment; |
5 | import android.content.Intent; | 4 | import android.content.Intent; |
6 | import android.content.SharedPreferences; | 5 | import android.content.SharedPreferences; |
7 | -import android.graphics.drawable.ColorDrawable; | ||
8 | import android.nfc.NfcAdapter; | 6 | import android.nfc.NfcAdapter; |
9 | -import android.nfc.Tag; | ||
10 | -import android.nfc.tech.MifareClassic; | ||
11 | import android.os.Bundle; | 7 | import android.os.Bundle; |
12 | import android.preference.PreferenceManager; | 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 | import android.widget.Toast; | 13 | import android.widget.Toast; |
14 | 14 | ||
15 | -import org.json.JSONObject; | ||
16 | - | ||
17 | import java.net.URL; | 15 | import java.net.URL; |
18 | import java.net.URLEncoder; | 16 | import java.net.URLEncoder; |
19 | import java.util.HashMap; | 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 | private HashMap<String, String> mParam; | 23 | private HashMap<String, String> mParam; |
28 | private String mAPI; | 24 | private String mAPI; |
29 | 25 | ||
30 | @Override | 26 | @Override |
31 | - protected void onCreate(Bundle savedInstanceState) { | 27 | + public void onCreate(Bundle savedInstanceState){ |
32 | super.onCreate(savedInstanceState); | 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 | mParam = new HashMap<String, String>(); | 35 | mParam = new HashMap<String, String>(); |
37 | 36 | ||
37 | + Bundle b = getArguments(); | ||
38 | + | ||
38 | try { | 39 | try { |
39 | - switch (getIntent().getIntExtra("state", MainActivite.STATE_RIEN)) { | 40 | + switch (b.getInt("state")) { |
40 | case MainActivite.STATE_COMMANDE: | 41 | case MainActivite.STATE_COMMANDE: |
41 | //TODO: XOR du cancer | 42 | //TODO: XOR du cancer |
42 | //mParam.put("quantite", String.valueOf(getIntent().getIntExtra("quantite", -1))); | 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 | mAPI = "api/client/payer"; | 46 | mAPI = "api/client/payer"; |
46 | break; | 47 | break; |
47 | case MainActivite.STATE_CREATION_COMPTE: | 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 | mAPI = "api/client/ajouter"; | 51 | mAPI = "api/client/ajouter"; |
51 | break; | 52 | break; |
52 | case MainActivite.STATE_RECHARGEMENT: | 53 | case MainActivite.STATE_RECHARGEMENT: |
53 | System.out.println("bite"); | 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 | mAPI = "api/client/recharger"; | 57 | mAPI = "api/client/recharger"; |
57 | break; | 58 | break; |
58 | case MainActivite.STATE_VIDANGE: | 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 | mAPI = "api/client/vidange"; | 61 | mAPI = "api/client/vidange"; |
61 | break; | 62 | break; |
62 | case MainActivite.STATE_CONNEXION: //Impossible c'est pas géré ici | 63 | case MainActivite.STATE_CONNEXION: //Impossible c'est pas géré ici |
63 | case MainActivite.STATE_RIEN: | 64 | case MainActivite.STATE_RIEN: |
64 | default: | 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 | catch (Throwable t){ | 69 | catch (Throwable t){ |
71 | System.out.println("Exception: " + t.toString()); | 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 | // Convertit l'array de byte en chaîne hexadécimale (si le byte = 0x63, str = "63"). | 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,58 +89,28 @@ public class CarteActivite extends Activity implements ASyncResponse { | ||
105 | return out; | 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 | if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { | 95 | if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { |
121 | String id_carte = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)); | 96 | String id_carte = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)); |
122 | mParam.put("idCarte", id_carte); | 97 | mParam.put("idCarte", id_carte); |
123 | clientAPI(); | 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 | public void clientAPI() { | 104 | public void clientAPI() { |
139 | try { | 105 | try { |
140 | - SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); | 106 | + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); |
141 | URL url = new URL(settings.getString("server_address", null) + mAPI); | 107 | URL url = new URL(settings.getString("server_address", null) + mAPI); |
142 | NetworkThread nt = new NetworkThread(url, mParam); | 108 | NetworkThread nt = new NetworkThread(url, mParam); |
143 | - nt.delegate = this; | 109 | + nt.delegate = (MainActivite) getActivity(); |
144 | nt.execute(); | 110 | nt.execute(); |
145 | } | 111 | } |
146 | catch (Throwable t){ | 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
@@ -31,9 +31,6 @@ public class PagerAdapter extends FragmentStatePagerAdapter { | @@ -31,9 +31,6 @@ public class PagerAdapter extends FragmentStatePagerAdapter { | ||
31 | case 3: | 31 | case 3: |
32 | TabFragment4 tab4 = new TabFragment4(); | 32 | TabFragment4 tab4 = new TabFragment4(); |
33 | return tab4; | 33 | return tab4; |
34 | - case 4: | ||
35 | - TabFragment5 tab5 = new TabFragment5(); | ||
36 | - return tab5; | ||
37 | default: | 34 | default: |
38 | return null; | 35 | return null; |
39 | } | 36 | } |
PremiereActivite/app/src/main/java/com/example/app_10p5/TabFragment1.java
@@ -2,9 +2,11 @@ package com.example.app_10p5; | @@ -2,9 +2,11 @@ package com.example.app_10p5; | ||
2 | 2 | ||
3 | import android.os.Bundle; | 3 | import android.os.Bundle; |
4 | import android.app.Fragment; | 4 | import android.app.Fragment; |
5 | +import android.text.InputFilter; | ||
5 | import android.view.LayoutInflater; | 6 | import android.view.LayoutInflater; |
6 | import android.view.View; | 7 | import android.view.View; |
7 | import android.view.ViewGroup; | 8 | import android.view.ViewGroup; |
9 | +import android.widget.EditText; | ||
8 | 10 | ||
9 | /** | 11 | /** |
10 | * Created by beaus on 24/04/2016. | 12 | * Created by beaus on 24/04/2016. |
@@ -13,6 +15,10 @@ import android.view.ViewGroup; | @@ -13,6 +15,10 @@ import android.view.ViewGroup; | ||
13 | public class TabFragment1 extends Fragment { | 15 | public class TabFragment1 extends Fragment { |
14 | @Override | 16 | @Override |
15 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | 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 | \ No newline at end of file | 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,11 +2,9 @@ package com.example.app_10p5; | ||
2 | 2 | ||
3 | import android.os.Bundle; | 3 | import android.os.Bundle; |
4 | import android.app.Fragment; | 4 | import android.app.Fragment; |
5 | -import android.text.InputFilter; | ||
6 | import android.view.LayoutInflater; | 5 | import android.view.LayoutInflater; |
7 | import android.view.View; | 6 | import android.view.View; |
8 | import android.view.ViewGroup; | 7 | import android.view.ViewGroup; |
9 | -import android.widget.EditText; | ||
10 | 8 | ||
11 | /** | 9 | /** |
12 | * Created by beaus on 24/04/2016. | 10 | * Created by beaus on 24/04/2016. |
@@ -16,11 +14,6 @@ public class TabFragment2 extends Fragment { | @@ -16,11 +14,6 @@ public class TabFragment2 extends Fragment { | ||
16 | 14 | ||
17 | @Override | 15 | @Override |
18 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | 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,7 +32,6 @@ public class main_tab_frag extends Fragment { | ||
32 | 32 | ||
33 | TabLayout tabLayout = (TabLayout) ret.findViewById(R.id.tab_layout); | 33 | TabLayout tabLayout = (TabLayout) ret.findViewById(R.id.tab_layout); |
34 | tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); | 34 | tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); |
35 | - tabLayout.addTab(tabLayout.newTab().setText("Connexion")); | ||
36 | tabLayout.addTab(tabLayout.newTab().setText("Commande")); | 35 | tabLayout.addTab(tabLayout.newTab().setText("Commande")); |
37 | tabLayout.addTab(tabLayout.newTab().setText("Rechargement")); | 36 | tabLayout.addTab(tabLayout.newTab().setText("Rechargement")); |
38 | tabLayout.addTab(tabLayout.newTab().setText("Création")); | 37 | tabLayout.addTab(tabLayout.newTab().setText("Création")); |
PremiereActivite/app/src/main/res/layout/layout_main.xml
1 | <?xml version="1.0" encoding="utf-8"?> | 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 | <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | 6 | <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" |
3 | android:id="@+id/fragment_container" | 7 | android:id="@+id/fragment_container" |
4 | android:layout_width="match_parent" | 8 | android:layout_width="match_parent" |
5 | android:layout_height="match_parent" | 9 | android:layout_height="match_parent" |
6 | android:background="?android:attr/detailsElementBackground" /> | 10 | android:background="?android:attr/detailsElementBackground" /> |
11 | +</android.support.design.widget.CoordinatorLayout> |
PremiereActivite/app/src/main/res/layout/tab_fragment_1.xml
@@ -4,6 +4,6 @@ | @@ -4,6 +4,6 @@ | ||
4 | android:layout_height="match_parent" | 4 | android:layout_height="match_parent" |
5 | android:orientation="vertical"> | 5 | android:orientation="vertical"> |
6 | 6 | ||
7 | - <include layout="@layout/layout_connection"></include> | 7 | + <include layout="@layout/layout_commande"></include> |
8 | 8 | ||
9 | </RelativeLayout> | 9 | </RelativeLayout> |
10 | \ No newline at end of file | 10 | \ No newline at end of file |
PremiereActivite/app/src/main/res/layout/tab_fragment_2.xml
@@ -4,6 +4,6 @@ | @@ -4,6 +4,6 @@ | ||
4 | android:layout_height="match_parent" | 4 | android:layout_height="match_parent" |
5 | android:orientation="vertical"> | 5 | android:orientation="vertical"> |
6 | 6 | ||
7 | - <include layout="@layout/layout_commande"></include> | 7 | + <include layout="@layout/layout_rechargement"></include> |
8 | 8 | ||
9 | </RelativeLayout> | 9 | </RelativeLayout> |
10 | \ No newline at end of file | 10 | \ No newline at end of file |
PremiereActivite/app/src/main/res/layout/tab_fragment_3.xml
@@ -4,6 +4,6 @@ | @@ -4,6 +4,6 @@ | ||
4 | android:layout_height="match_parent" | 4 | android:layout_height="match_parent" |
5 | android:orientation="vertical"> | 5 | android:orientation="vertical"> |
6 | 6 | ||
7 | - <include layout="@layout/layout_rechargement"></include> | 7 | + <include layout="@layout/layout_creation"></include> |
8 | 8 | ||
9 | </RelativeLayout> | 9 | </RelativeLayout> |
10 | \ No newline at end of file | 10 | \ No newline at end of file |
PremiereActivite/app/src/main/res/layout/tab_fragment_4.xml
@@ -4,6 +4,6 @@ | @@ -4,6 +4,6 @@ | ||
4 | android:layout_height="match_parent" | 4 | android:layout_height="match_parent" |
5 | android:orientation="vertical"> | 5 | android:orientation="vertical"> |
6 | 6 | ||
7 | - <include layout="@layout/layout_creation"></include> | 7 | + <include layout="@layout/layout_vidange"></include> |
8 | 8 | ||
9 | </RelativeLayout> | 9 | </RelativeLayout> |
10 | \ No newline at end of file | 10 | \ No newline at end of file |
PremiereActivite/app/src/main/res/layout/tab_fragment_5.xml deleted
@@ -1,9 +0,0 @@ | @@ -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 | \ No newline at end of file | 0 | \ No newline at end of file |