Blame view

PremiereActivite/app/src/main/java/com/example/app_10p5/MainActivite.java 11.8 KB
c8059613   JLo'w   Re-fonte de l'app...
1
2
  package com.example.app_10p5;
  
c8059613   JLo'w   Re-fonte de l'app...
3
  import android.content.Intent;
8d573e63   JLo'w   Mise en place de ...
4
  import android.opengl.ETC1;
c8059613   JLo'w   Re-fonte de l'app...
5
  import android.os.Bundle;
c8059613   JLo'w   Re-fonte de l'app...
6
7
  import android.support.design.widget.TabLayout;
  import android.support.v4.app.FragmentActivity;
c8059613   JLo'w   Re-fonte de l'app...
8
  import android.support.v4.view.ViewPager;
8d573e63   JLo'w   Mise en place de ...
9
  import android.text.InputFilter;
c8059613   JLo'w   Re-fonte de l'app...
10
  import android.view.View;
520cecde   JLo'w   Un petit plus, un...
11
  import android.widget.EditText;
93f90e35   JLo'w   NFC et foreground
12
13
  import android.widget.Toast;
  
c1f92981   JLo'w   Implémentation de...
14
15
16
17
18
  import org.json.JSONObject;
  
  import java.net.URL;
  import java.util.HashMap;
  
c8059613   JLo'w   Re-fonte de l'app...
19
20
21
22
  
  /**
   * Created by beaus on 24/04/2016.
   */
c1f92981   JLo'w   Implémentation de...
23
  public class MainActivite extends FragmentActivity implements ASyncResponse {
c8059613   JLo'w   Re-fonte de l'app...
24
  
520cecde   JLo'w   Un petit plus, un...
25
26
27
28
29
30
31
32
33
34
      public static final int STATE_RIEN = 0;
      public static final int STATE_COMMANDE = 3;
      public static final int STATE_VIDANGE = 4;
      public static final int STATE_RECHARGEMENT = 2;
      public static final int STATE_CREATION_COMPTE = 1;
      public static final int STATE_CONNEXION = 5;
      public static final long EXPIRATION = 1000*60*10;
  
      private int mState;
      private String mToken;
8cd8911a   JLo'w   Connexion enfin f...
35
      private int mDroit;
520cecde   JLo'w   Un petit plus, un...
36
      private long mTimeToken;
8cd8911a   JLo'w   Connexion enfin f...
37
      private String mUser;
520cecde   JLo'w   Un petit plus, un...
38
  
c8059613   JLo'w   Re-fonte de l'app...
39
40
41
42
      @Override
      protected void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
          setContentView(R.layout.layout_main);
520cecde   JLo'w   Un petit plus, un...
43
44
45
46
47
  
          mState = STATE_RIEN;
          mTimeToken = -1;
          mToken = "";
  
c8059613   JLo'w   Re-fonte de l'app...
48
49
          TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
          tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
520cecde   JLo'w   Un petit plus, un...
50
          tabLayout.addTab(tabLayout.newTab().setText("Connexion"));
c8059613   JLo'w   Re-fonte de l'app...
51
52
53
54
          tabLayout.addTab(tabLayout.newTab().setText("Commande"));
          tabLayout.addTab(tabLayout.newTab().setText("Rechargement"));
          tabLayout.addTab(tabLayout.newTab().setText("Création"));
          tabLayout.addTab(tabLayout.newTab().setText("Vidange"));
c8059613   JLo'w   Re-fonte de l'app...
55
56
57
          tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
  
          final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
ecd8a267   JLo'w   Petites correctio...
58
          final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
c8059613   JLo'w   Re-fonte de l'app...
59
60
61
62
63
          viewPager.setAdapter(adapter);
          viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
          tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
              @Override
              public void onTabSelected(TabLayout.Tab tab) {
520cecde   JLo'w   Un petit plus, un...
64
65
66
67
68
69
                  if(mToken != "" && System.currentTimeMillis() - mTimeToken < EXPIRATION){
                      viewPager.setCurrentItem(tab.getPosition());
                  }
                  else{
                      viewPager.setCurrentItem(tab.getPosition());    //Empeche un bug graphique
                      viewPager.setCurrentItem(0);
ecd8a267   JLo'w   Petites correctio...
70
                      Toast.makeText(MainActivite.this, "Veuillez vous connecter.", Toast.LENGTH_LONG).show();
520cecde   JLo'w   Un petit plus, un...
71
                  }
c8059613   JLo'w   Re-fonte de l'app...
72
73
74
75
76
77
78
79
80
81
82
83
84
85
              }
  
              @Override
              public void onTabUnselected(TabLayout.Tab tab) {
  
              }
  
              @Override
              public void onTabReselected(TabLayout.Tab tab) {
  
              }
          });
      }
  
c66109e1   JLo'w   Ajout du code des...
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
      @Override
      public void onSaveInstanceState(Bundle savedInstanceState){
          savedInstanceState.putString("token", mToken);
          savedInstanceState.putInt("state", mState);
          savedInstanceState.putString("user", mUser);
          savedInstanceState.putInt("droit", mDroit);
          savedInstanceState.putLong("timeToken", mTimeToken);
  
          super.onSaveInstanceState(savedInstanceState);
      }
  
      @Override
      public void onRestoreInstanceState(Bundle savedInstanceState) {
          mTimeToken = savedInstanceState.getLong("timeToken");
          mToken = savedInstanceState.getString("user");
          mState = savedInstanceState.getInt("state");
          mUser = savedInstanceState.getString("user");
          mDroit = savedInstanceState.getInt("droit");
  
          super.onRestoreInstanceState(savedInstanceState);
      }
  
      public void valideCreationCompte(View v){
          if((mToken != "") && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) {
              EditText champMontant = (EditText) findViewById(R.id.creation_montant);
              float montant = 0.0f;
  
              try{
                  montant = Float.parseFloat(champMontant.getText().toString());
              }
              catch (Throwable t){
                  Toast.makeText(this, "Remplir le champ montant avec un nombre: " + t.toString(), Toast.LENGTH_LONG).show();
              }
  
              if((montant > 0.0f) && (montant < 200.0f) && (mDroit >= 1)){
                  mState = STATE_CREATION_COMPTE;
                  Intent intent = new Intent(this, CarteActivite.class);
                  intent.putExtra("token", mToken);
                  intent.putExtra("state", mState);
                  intent.putExtra("montant", montant);
                  startActivityForResult(intent, mState);
              }
              else{
                  Toast.makeText(this, "Valeur incorrecte ou droit insuffisant.", Toast.LENGTH_LONG).show();
              }
          }
          else{
              Toast.makeText(this, "Veuillez vous reconnecter.", Toast.LENGTH_LONG).show();
              final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
              viewPager.setCurrentItem(0);
          }
      }
  
c8059613   JLo'w   Re-fonte de l'app...
139
140
      public void valideCommande(View v)
      {
520cecde   JLo'w   Un petit plus, un...
141
142
143
          if((mToken != "") && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) {
              EditText champMontant = (EditText) findViewById(R.id.commande_prix);
              EditText champQuantite = (EditText) findViewById(R.id.commande_quantite);
c66109e1   JLo'w   Ajout du code des...
144
145
146
147
148
149
150
151
152
153
154
              float montant = 0.0f;
              int quantite = 0;
  
              try{
                  montant = Float.parseFloat(champMontant.getText().toString());
                  quantite = Integer.parseInt(champQuantite.getText().toString());
              }
              catch (Throwable t)
              {
                  Toast.makeText(this, "Remplir les champs avec des nombres: " + t.toString(), Toast.LENGTH_LONG).show();
              }
520cecde   JLo'w   Un petit plus, un...
155
  
c66109e1   JLo'w   Ajout du code des...
156
              if ((montant > 0.0f) && (montant < 200.0f) && (quantite > 0) && (mDroit >= 1)) {
520cecde   JLo'w   Un petit plus, un...
157
158
159
160
161
162
163
164
                  mState = STATE_COMMANDE;
                  Intent intent = new Intent(this, CarteActivite.class);
                  intent.putExtra("token", mToken);
                  intent.putExtra("state", mState);
                  intent.putExtra("montant", montant);
                  intent.putExtra("quantite", quantite);
                  startActivityForResult(intent, mState);
              }
c66109e1   JLo'w   Ajout du code des...
165
166
167
              else{
                  Toast.makeText(this, "Valeur incorrecte ou droit insuffisant.", Toast.LENGTH_LONG).show();
              }
520cecde   JLo'w   Un petit plus, un...
168
169
170
171
172
173
174
175
176
177
          }
          else{
              Toast.makeText(this, "Veuillez vous reconnecter.", Toast.LENGTH_LONG).show();
              final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
              viewPager.setCurrentItem(0);
          }
      }
  
      public void valideRechargement(View v)
      {
96cf88d2   JLo'w   Réception de JSON...
178
          if((mToken != "") && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) {
c66109e1   JLo'w   Ajout du code des...
179
180
181
182
183
184
185
186
187
              EditText champMontant = (EditText) findViewById(R.id.rechargement_champ_montant);
              float montant = 0.0f;
  
              try{
                  montant = Float.parseFloat(champMontant.getText().toString());
              }
              catch (Throwable t){
                  Toast.makeText(this, "Remplir le champ montant avec un nombre: " + t.toString(), Toast.LENGTH_LONG).show();
              }
520cecde   JLo'w   Un petit plus, un...
188
  
c66109e1   JLo'w   Ajout du code des...
189
190
191
192
193
194
195
196
197
198
199
              if((montant > 0.0f) && (montant < 200.0f) && (mDroit >= 2)){
                  mState = STATE_RECHARGEMENT;
                  Intent intent = new Intent(this, CarteActivite.class);
                  intent.putExtra("token", mToken);
                  intent.putExtra("state", mState);
                  intent.putExtra("montant", montant);
                  startActivityForResult(intent, mState);
              }
              else{
                  Toast.makeText(this, "Valeur incorrecte ou droit insuffisant.", Toast.LENGTH_LONG).show();
              }
520cecde   JLo'w   Un petit plus, un...
200
201
202
203
204
205
206
207
208
209
210
211
212
          }
          else{
              Toast.makeText(this, "Veuillez vous reconnecter.", Toast.LENGTH_LONG).show();
              final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
              viewPager.setCurrentItem(0);
          }
      }
  
      public void valideConnection(View v)
      {
          EditText viewUser = (EditText) findViewById(R.id.connection_username);
          EditText viewPsw = (EditText) findViewById(R.id.connection_password);
  
c1f92981   JLo'w   Implémentation de...
213
214
215
          String user = viewUser.getText().toString();
          String password = viewPsw.getText().toString();
  
520cecde   JLo'w   Un petit plus, un...
216
217
          if ((user != "") && (password != "")) {
              mState = STATE_CONNEXION;
c1f92981   JLo'w   Implémentation de...
218
219
  
              try{
c1f92981   JLo'w   Implémentation de...
220
                  URL url = new URL(CarteActivite.HOST + "api/utilisateur/connexion");
c1f92981   JLo'w   Implémentation de...
221
222
223
224
225
226
227
228
229
230
                  HashMap<String, String> param = new HashMap<String, String>();
                  param.put("login", user);
                  param.put("mdp", password);
                  NetworkThread nt = new NetworkThread(url, param);
                  nt.delegate = this;
                  nt.execute();
              }
              catch (Throwable t) {
                  //TODO: gérer les exceptions du cancer de la connexion
              }
520cecde   JLo'w   Un petit plus, un...
231
          }
c66109e1   JLo'w   Ajout du code des...
232
233
234
          else{
              Toast.makeText(this, "Veuillez remplir les champs.", Toast.LENGTH_LONG).show();
          }
520cecde   JLo'w   Un petit plus, un...
235
      }
8d573e63   JLo'w   Mise en place de ...
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
  
      public void valideVidange(View v){
          if((mToken != "") && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) {
  
              if((mDroit >= 2)){
                  mState = STATE_VIDANGE;
                  Intent intent = new Intent(this, CarteActivite.class);
                  intent.putExtra("token", mToken);
                  intent.putExtra("state", mState);
                  startActivityForResult(intent, mState);
              }
              else{
                  Toast.makeText(this, "Droit insuffisant.", Toast.LENGTH_LONG).show();
              }
          }
          else{
              Toast.makeText(this, "Veuillez vous reconnecter.", Toast.LENGTH_LONG).show();
              final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
              viewPager.setCurrentItem(0);
          }
      }
520cecde   JLo'w   Un petit plus, un...
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data){
          //TODO: faire des choses avec ca
          switch (requestCode) {
              case STATE_COMMANDE:
                  break;
              case STATE_CONNEXION:
                  break;
              case STATE_CREATION_COMPTE:
                  break;
              case STATE_RECHARGEMENT:
                  break;
              case STATE_VIDANGE:
                  break;
              case STATE_RIEN:
              default:
                  Toast.makeText(this, "WTF, le cancer est dans l'application!!", Toast.LENGTH_LONG).show();
                  break;
          }
c8059613   JLo'w   Re-fonte de l'app...
277
      }
93f90e35   JLo'w   NFC et foreground
278
  
c1f92981   JLo'w   Implémentation de...
279
280
281
      /* Retour du network thread */
      @Override
      public void processFinish(JSONObject output) {
96cf88d2   JLo'w   Réception de JSON...
282
283
          if(output.length() != 0){
              try{
8cd8911a   JLo'w   Connexion enfin f...
284
285
                  if(output.get("status").toString().equals("ok")){
                      mToken = output.get("jeton").toString();
96cf88d2   JLo'w   Réception de JSON...
286
                      mTimeToken = System.currentTimeMillis();
8cd8911a   JLo'w   Connexion enfin f...
287
288
289
                      mDroit = output.getInt("droit");
                      mUser = output.get("login").toString();
                      Toast.makeText(this, "Bonjour " + mUser + " vous êtes bien connecté pour " + EXPIRATION / (1000 * 60) + " minutes.", Toast.LENGTH_LONG).show();
ecd8a267   JLo'w   Petites correctio...
290
291
                      EditText coUser = (EditText) findViewById(R.id.connection_password);
                      coUser.setText("");
c66109e1   JLo'w   Ajout du code des...
292
293
                      final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
                      viewPager.setCurrentItem(1);
96cf88d2   JLo'w   Réception de JSON...
294
295
296
297
298
299
                  }
                  else{
                      Toast.makeText(this, "Erreur dans la requête: " + output.get("status"), Toast.LENGTH_LONG).show();
                  }
              }
              catch(Throwable t){
8cd8911a   JLo'w   Connexion enfin f...
300
                  Toast.makeText(this, "WTF, le cancer est dans l'application!!" + t.toString(), Toast.LENGTH_LONG).show();
96cf88d2   JLo'w   Réception de JSON...
301
302
303
304
305
              }
          }
          else{
              Toast.makeText(this, "Impossible de se connecter au serveur", Toast.LENGTH_LONG).show();
          }
c1f92981   JLo'w   Implémentation de...
306
      }
c8059613   JLo'w   Re-fonte de l'app...
307
  }