Blame view

PremiereActivite/app/src/main/java/com/example/app_10p5/MainActivite.java 6.89 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;
c8059613   JLo'w   Re-fonte de l'app...
4
  import android.os.Bundle;
96cf88d2   JLo'w   Réception de JSON...
5
  import android.os.SystemClock;
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
9
  import android.support.v4.view.ViewPager;
  import android.view.View;
520cecde   JLo'w   Un petit plus, un...
10
  import android.widget.EditText;
93f90e35   JLo'w   NFC et foreground
11
12
  import android.widget.Toast;
  
c1f92981   JLo'w   Implémentation de...
13
14
15
16
17
  import org.json.JSONObject;
  
  import java.net.URL;
  import java.util.HashMap;
  
c8059613   JLo'w   Re-fonte de l'app...
18
19
20
21
  
  /**
   * Created by beaus on 24/04/2016.
   */
c1f92981   JLo'w   Implémentation de...
22
  public class MainActivite extends FragmentActivity implements ASyncResponse {
c8059613   JLo'w   Re-fonte de l'app...
23
  
520cecde   JLo'w   Un petit plus, un...
24
25
26
27
28
29
30
31
32
33
34
35
      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;
      private long mTimeToken;
  
c8059613   JLo'w   Re-fonte de l'app...
36
37
38
39
      @Override
      protected void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
          setContentView(R.layout.layout_main);
520cecde   JLo'w   Un petit plus, un...
40
41
42
43
44
  
          mState = STATE_RIEN;
          mTimeToken = -1;
          mToken = "";
  
c8059613   JLo'w   Re-fonte de l'app...
45
46
          TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
          tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
520cecde   JLo'w   Un petit plus, un...
47
          tabLayout.addTab(tabLayout.newTab().setText("Connexion"));
c8059613   JLo'w   Re-fonte de l'app...
48
49
50
51
          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...
52
53
54
55
56
57
58
59
60
61
          tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
  
          final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
          final PagerAdapter adapter = new PagerAdapter
                  (getSupportFragmentManager(), tabLayout.getTabCount());
          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...
62
63
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);
                      Toast.makeText(MainActivite.this, "Veuillez vous connecter.", Toast.LENGTH_LONG).show();
                  }
c8059613   JLo'w   Re-fonte de l'app...
70
71
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) {
  
              }
          });
      }
  
      public void valideCommande(View v)
      {
520cecde   JLo'w   Un petit plus, un...
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
          if((mToken != "") && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) {
              EditText champMontant = (EditText) findViewById(R.id.commande_prix);
              EditText champQuantite = (EditText) findViewById(R.id.commande_quantite);
              float montant = Float.parseFloat(champMontant.getText().toString());
              int quantite = Integer.parseInt(champQuantite.getText().toString());
  
              if ((montant > 0.0) && (montant < 200.0)) {
                  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);
              }
          }
          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...
111
          if((mToken != "") && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) {
520cecde   JLo'w   Un petit plus, un...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  
          }
          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...
126
127
128
129
          String user = viewUser.getText().toString();
          String password = viewPsw.getText().toString();
  
          System.out.println(user + " " + password);
520cecde   JLo'w   Un petit plus, un...
130
131
132
  
          if ((user != "") && (password != "")) {
              mState = STATE_CONNEXION;
c1f92981   JLo'w   Implémentation de...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  
              try{
                  System.out.println("try");
                  URL url = new URL(CarteActivite.HOST + "api/utilisateur/connexion");
                  System.out.println("url");
                  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...
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
          }
      }
  
      @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...
170
      }
93f90e35   JLo'w   NFC et foreground
171
  
c1f92981   JLo'w   Implémentation de...
172
173
174
      /* Retour du network thread */
      @Override
      public void processFinish(JSONObject output) {
96cf88d2   JLo'w   Réception de JSON...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
          if(output.length() != 0){
              try{
                  if(output.get("status").toString() == "ok"){
                      mToken = output.get("token").toString();
                      mTimeToken = System.currentTimeMillis();
                  }
                  else{
                      Toast.makeText(this, "Erreur dans la requête: " + output.get("status"), Toast.LENGTH_LONG).show();
                  }
              }
              catch(Throwable t){
                  Toast.makeText(this, "WTF, le cancer est dans l'application!!", Toast.LENGTH_LONG).show();
              }
          }
          else{
              Toast.makeText(this, "Impossible de se connecter au serveur", Toast.LENGTH_LONG).show();
          }
c1f92981   JLo'w   Implémentation de...
192
      }
c8059613   JLo'w   Re-fonte de l'app...
193
  }