Commit e274d65a1200ab31cab99a60ba66e781f46d6570

Authored by JLo'w
1 parent edd209be

Meilleur vérification des champs + urlencode

PremiereActivite/app/src/main/java/com/example/app_10p5/MainActivite.java
@@ -5,10 +5,13 @@ import android.app.FragmentManager; @@ -5,10 +5,13 @@ import android.app.FragmentManager;
5 import android.app.FragmentTransaction; 5 import android.app.FragmentTransaction;
6 import android.content.Context; 6 import android.content.Context;
7 import android.content.Intent; 7 import android.content.Intent;
  8 +import android.content.SharedPreferences;
8 import android.graphics.drawable.BitmapDrawable; 9 import android.graphics.drawable.BitmapDrawable;
9 import android.graphics.drawable.ColorDrawable; 10 import android.graphics.drawable.ColorDrawable;
10 import android.os.Bundle; 11 import android.os.Bundle;
  12 +import android.preference.PreferenceManager;
11 import android.support.v4.view.ViewPager; 13 import android.support.v4.view.ViewPager;
  14 +import android.text.TextUtils;
12 import android.view.Gravity; 15 import android.view.Gravity;
13 import android.view.LayoutInflater; 16 import android.view.LayoutInflater;
14 import android.view.Menu; 17 import android.view.Menu;
@@ -27,6 +30,7 @@ import android.widget.Toast; @@ -27,6 +30,7 @@ import android.widget.Toast;
27 import org.json.JSONObject; 30 import org.json.JSONObject;
28 31
29 import java.net.URL; 32 import java.net.URL;
  33 +import java.net.URLEncoder;
30 import java.util.HashMap; 34 import java.util.HashMap;
31 35
32 36
@@ -41,6 +45,7 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr @@ -41,6 +45,7 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
41 public static final int STATE_RECHARGEMENT = 2; 45 public static final int STATE_RECHARGEMENT = 2;
42 public static final int STATE_CREATION_COMPTE = 1; 46 public static final int STATE_CREATION_COMPTE = 1;
43 public static final int STATE_CONNEXION = 5; 47 public static final int STATE_CONNEXION = 5;
  48 +
44 public static final long EXPIRATION = 1000*60*10; 49 public static final long EXPIRATION = 1000*60*10;
45 50
46 private int mState; 51 private int mState;
@@ -53,6 +58,7 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr @@ -53,6 +58,7 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
53 protected void onCreate(Bundle savedInstanceState){ 58 protected void onCreate(Bundle savedInstanceState){
54 super.onCreate(savedInstanceState); 59 super.onCreate(savedInstanceState);
55 setContentView(R.layout.layout_main); 60 setContentView(R.layout.layout_main);
  61 + PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
56 62
57 mState = STATE_RIEN; 63 mState = STATE_RIEN;
58 mTimeToken = -1; 64 mTimeToken = -1;
@@ -86,13 +92,8 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr @@ -86,13 +92,8 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
86 @Override 92 @Override
87 public boolean onOptionsItemSelected(MenuItem item) { 93 public boolean onOptionsItemSelected(MenuItem item) {
88 // Handle item selection 94 // Handle item selection
89 - if(item.getItemId() == R.id.menu_2_choice_1){  
90 - CarteActivite.HOST = "https://10p5.clubinfo.frogeye.fr/";  
91 - item.setChecked(true);  
92 - }  
93 - else if(item.getItemId() == R.id.menu_2_choice_2){  
94 - CarteActivite.HOST = "http://pcbar.insecure.deule.net/";  
95 - item.setChecked(true); 95 + if(item.getItemId() == R.id.action_settings){
  96 + getFragmentManager().beginTransaction().replace(R.id.fragment_container, new SettingsFragment()).addToBackStack("settings").commit();
96 } 97 }
97 98
98 return super.onOptionsItemSelected(item); 99 return super.onOptionsItemSelected(item);
@@ -115,29 +116,39 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr @@ -115,29 +116,39 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
115 } 116 }
116 117
117 public void valideCreationCompte(View v){ 118 public void valideCreationCompte(View v){
118 - if((mToken != "") && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) { 119 + if((TextUtils.getTrimmedLength(mToken) == 30) && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) {
119 EditText champMontant = (EditText) findViewById(R.id.creation_montant); 120 EditText champMontant = (EditText) findViewById(R.id.creation_montant);
120 float montant = 0.0f; 121 float montant = 0.0f;
121 122
122 - try{  
123 - montant = Float.parseFloat(champMontant.getText().toString());  
124 - }  
125 - catch (Throwable t){  
126 - Toast.makeText(this, "Remplir le champ montant avec un nombre: " + t.toString(), Toast.LENGTH_LONG).show();  
127 - }  
128 -  
129 - if((montant > 0.0f) && (montant < 200.0f) && (mDroit >= 1)){  
130 - mState = STATE_CREATION_COMPTE;  
131 - champMontant.setText(""); 123 + if(!TextUtils.isEmpty(champMontant.getText().toString())){
  124 + try{
  125 + montant = Float.parseFloat(champMontant.getText().toString());
  126 + }
  127 + catch (Throwable t){
  128 + Toast.makeText(this, "Remplir le champ montant avec un nombre: " + t.toString(), Toast.LENGTH_LONG).show();
  129 + }
132 130
133 - Intent intent = new Intent(this, CarteActivite.class);  
134 - intent.putExtra("token", mToken);  
135 - intent.putExtra("state", mState);  
136 - intent.putExtra("montant", montant);  
137 - startActivityForResult(intent, mState); 131 + if(mDroit >= 1){
  132 + if((montant > 0.0f) && (montant < 200.0f)){
  133 + mState = STATE_CREATION_COMPTE;
  134 + champMontant.setText(null);
  135 +
  136 + Intent intent = new Intent(this, CarteActivite.class);
  137 + intent.putExtra("token", mToken);
  138 + intent.putExtra("state", mState);
  139 + intent.putExtra("montant", montant);
  140 + startActivityForResult(intent, mState);
  141 + }
  142 + else{
  143 + Toast.makeText(this, "Valeur incorrecte.", Toast.LENGTH_LONG).show();
  144 + }
  145 + }
  146 + else{
  147 + Toast.makeText(this, "Droit insuffisant.", Toast.LENGTH_LONG).show();
  148 + }
138 } 149 }
139 else{ 150 else{
140 - Toast.makeText(this, "Valeur incorrecte ou droit insuffisant.", Toast.LENGTH_LONG).show(); 151 + champMontant.setError("Montant requis.");
141 } 152 }
142 } 153 }
143 else{ 154 else{
@@ -149,12 +160,14 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr @@ -149,12 +160,14 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
149 160
150 public void valideCommande(View v) 161 public void valideCommande(View v)
151 { 162 {
152 - if((mToken != "") && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) { 163 + if((TextUtils.getTrimmedLength(mToken) == 30) && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) {
153 EditText champMontant = (EditText) findViewById(R.id.commande_prix); 164 EditText champMontant = (EditText) findViewById(R.id.commande_prix);
154 EditText champQuantite = (EditText) findViewById(R.id.commande_quantite); 165 EditText champQuantite = (EditText) findViewById(R.id.commande_quantite);
155 float montant = 0.0f; 166 float montant = 0.0f;
156 int quantite = 0; 167 int quantite = 0;
157 168
  169 + //TODO: gérer le XOR de pute
  170 +
158 try{ 171 try{
159 montant = Float.parseFloat(champMontant.getText().toString()); 172 montant = Float.parseFloat(champMontant.getText().toString());
160 quantite = Integer.parseInt(champQuantite.getText().toString()); 173 quantite = Integer.parseInt(champQuantite.getText().toString());
@@ -166,8 +179,8 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr @@ -166,8 +179,8 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
166 179
167 if ((montant > 0.0f) && (montant < 200.0f) && (quantite > 0) && (mDroit >= 1)) { 180 if ((montant > 0.0f) && (montant < 200.0f) && (quantite > 0) && (mDroit >= 1)) {
168 mState = STATE_COMMANDE; 181 mState = STATE_COMMANDE;
169 - champMontant.setText("");  
170 - champQuantite.setText(""); 182 + champMontant.setText(null);
  183 + champQuantite.setText(null);
171 184
172 Intent intent = new Intent(this, CarteActivite.class); 185 Intent intent = new Intent(this, CarteActivite.class);
173 intent.putExtra("token", mToken); 186 intent.putExtra("token", mToken);
@@ -189,29 +202,40 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr @@ -189,29 +202,40 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
189 202
190 public void valideRechargement(View v) 203 public void valideRechargement(View v)
191 { 204 {
192 - if((mToken != "") && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) { 205 + System.out.println(TextUtils.getTrimmedLength(mToken));
  206 + if((TextUtils.getTrimmedLength(mToken) == 30) && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) {
193 EditText champMontant = (EditText) findViewById(R.id.rechargement_champ_montant); 207 EditText champMontant = (EditText) findViewById(R.id.rechargement_champ_montant);
194 float montant = 0.0f; 208 float montant = 0.0f;
195 209
196 - try{  
197 - montant = Float.parseFloat(champMontant.getText().toString());  
198 - }  
199 - catch (Throwable t){  
200 - Toast.makeText(this, "Remplir le champ montant avec un nombre: " + t.toString(), Toast.LENGTH_LONG).show();  
201 - }  
202 -  
203 - if((montant > 0.0f) && (montant < 200.0f) && (mDroit >= 2)){  
204 - mState = STATE_RECHARGEMENT;  
205 - champMontant.setText(""); 210 + if(!TextUtils.isEmpty(champMontant.getText().toString())){
  211 + try{
  212 + montant = Float.parseFloat(champMontant.getText().toString());
  213 + }
  214 + catch (Throwable t){
  215 + Toast.makeText(this, "Remplir le champ montant avec un nombre: " + t.toString(), Toast.LENGTH_LONG).show();
  216 + }
206 217
207 - Intent intent = new Intent(this, CarteActivite.class);  
208 - intent.putExtra("token", mToken);  
209 - intent.putExtra("state", mState);  
210 - intent.putExtra("montant", montant);  
211 - startActivityForResult(intent, mState); 218 + if(mDroit >= 2){
  219 + if((montant > 0.0f) && (montant < 200.0f)){
  220 + mState = STATE_RECHARGEMENT;
  221 + champMontant.setText(null);
  222 +
  223 + Intent intent = new Intent(this, CarteActivite.class);
  224 + intent.putExtra("token", mToken);
  225 + intent.putExtra("state", mState);
  226 + intent.putExtra("montant", montant);
  227 + startActivityForResult(intent, mState);
  228 + }
  229 + else{
  230 + Toast.makeText(this, "Valeur incorrecte.", Toast.LENGTH_LONG).show();
  231 + }
  232 + }
  233 + else{
  234 + Toast.makeText(this, "Droit insuffisant.", Toast.LENGTH_LONG).show();
  235 + }
212 } 236 }
213 else{ 237 else{
214 - Toast.makeText(this, "Valeur incorrecte ou droit insuffisant.", Toast.LENGTH_LONG).show(); 238 + champMontant.setError("Montant requis.");
215 } 239 }
216 } 240 }
217 else{ 241 else{
@@ -229,32 +253,39 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr @@ -229,32 +253,39 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
229 String user = viewUser.getText().toString(); 253 String user = viewUser.getText().toString();
230 String password = viewPsw.getText().toString(); 254 String password = viewPsw.getText().toString();
231 255
232 - if ((user != "") && (password != "")) {  
233 - mState = STATE_CONNEXION;  
234 -  
235 - viewUser.setText("");  
236 - viewPsw.setText("");  
237 -  
238 - try{  
239 - URL url = new URL(CarteActivite.HOST + "api/utilisateur/connexion");  
240 - HashMap<String, String> param = new HashMap<String, String>();  
241 - param.put("login", user);  
242 - param.put("mdp", password);  
243 - NetworkThread nt = new NetworkThread(url, param);  
244 - nt.delegate = this;  
245 - nt.execute(); 256 + if (!TextUtils.isEmpty(user)) {
  257 + if(!TextUtils.isEmpty(password)){
  258 + mState = STATE_CONNEXION;
  259 +
  260 + viewUser.setText(null);
  261 + viewPsw.setText(null);
  262 +
  263 + try{
  264 + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  265 + URL url = new URL(settings.getString("server_address", null) + "api/utilisateur/connexion");
  266 + HashMap<String, String> param = new HashMap<String, String>();
  267 + param.put("login", URLEncoder.encode(user, "UTF-8"));
  268 + param.put("mdp", URLEncoder.encode(password, "UTF-8"));
  269 + NetworkThread nt = new NetworkThread(url, param);
  270 + nt.delegate = this;
  271 + nt.execute();
  272 + }
  273 + catch (Throwable t) {
  274 + Toast.makeText(this, "Erreur: " + t.toString(), Toast.LENGTH_LONG).show();
  275 + System.out.println("Exception: " + t.toString());
  276 + }
246 } 277 }
247 - catch (Throwable t) {  
248 - System.out.println("Exception: " + t.toString()); 278 + else {
  279 + viewPsw.setError("Mot de passe requis.");
249 } 280 }
250 } 281 }
251 else{ 282 else{
252 - Toast.makeText(this, "Veuillez remplir les champs.", Toast.LENGTH_LONG).show(); 283 + viewUser.setError("Utilisateur requis.");
253 } 284 }
254 } 285 }
255 286
256 public void valideVidange(View v){ 287 public void valideVidange(View v){
257 - if((mToken != "") && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) { 288 + if((TextUtils.getTrimmedLength(mToken) == 30) && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) {
258 289
259 if((mDroit >= 2)){ 290 if((mDroit >= 2)){
260 mState = STATE_VIDANGE; 291 mState = STATE_VIDANGE;