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 5 import android.app.FragmentTransaction;
6 6 import android.content.Context;
7 7 import android.content.Intent;
  8 +import android.content.SharedPreferences;
8 9 import android.graphics.drawable.BitmapDrawable;
9 10 import android.graphics.drawable.ColorDrawable;
10 11 import android.os.Bundle;
  12 +import android.preference.PreferenceManager;
11 13 import android.support.v4.view.ViewPager;
  14 +import android.text.TextUtils;
12 15 import android.view.Gravity;
13 16 import android.view.LayoutInflater;
14 17 import android.view.Menu;
... ... @@ -27,6 +30,7 @@ import android.widget.Toast;
27 30 import org.json.JSONObject;
28 31  
29 32 import java.net.URL;
  33 +import java.net.URLEncoder;
30 34 import java.util.HashMap;
31 35  
32 36  
... ... @@ -41,6 +45,7 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
41 45 public static final int STATE_RECHARGEMENT = 2;
42 46 public static final int STATE_CREATION_COMPTE = 1;
43 47 public static final int STATE_CONNEXION = 5;
  48 +
44 49 public static final long EXPIRATION = 1000*60*10;
45 50  
46 51 private int mState;
... ... @@ -53,6 +58,7 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
53 58 protected void onCreate(Bundle savedInstanceState){
54 59 super.onCreate(savedInstanceState);
55 60 setContentView(R.layout.layout_main);
  61 + PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
56 62  
57 63 mState = STATE_RIEN;
58 64 mTimeToken = -1;
... ... @@ -86,13 +92,8 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
86 92 @Override
87 93 public boolean onOptionsItemSelected(MenuItem item) {
88 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 99 return super.onOptionsItemSelected(item);
... ... @@ -115,29 +116,39 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
115 116 }
116 117  
117 118 public void valideCreationCompte(View v){
118   - if((mToken != "") && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) {
  119 + if((TextUtils.getTrimmedLength(mToken) == 30) && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) {
119 120 EditText champMontant = (EditText) findViewById(R.id.creation_montant);
120 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 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 154 else{
... ... @@ -149,12 +160,14 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
149 160  
150 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 164 EditText champMontant = (EditText) findViewById(R.id.commande_prix);
154 165 EditText champQuantite = (EditText) findViewById(R.id.commande_quantite);
155 166 float montant = 0.0f;
156 167 int quantite = 0;
157 168  
  169 + //TODO: gérer le XOR de pute
  170 +
158 171 try{
159 172 montant = Float.parseFloat(champMontant.getText().toString());
160 173 quantite = Integer.parseInt(champQuantite.getText().toString());
... ... @@ -166,8 +179,8 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
166 179  
167 180 if ((montant > 0.0f) && (montant < 200.0f) && (quantite > 0) && (mDroit >= 1)) {
168 181 mState = STATE_COMMANDE;
169   - champMontant.setText("");
170   - champQuantite.setText("");
  182 + champMontant.setText(null);
  183 + champQuantite.setText(null);
171 184  
172 185 Intent intent = new Intent(this, CarteActivite.class);
173 186 intent.putExtra("token", mToken);
... ... @@ -189,29 +202,40 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
189 202  
190 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 207 EditText champMontant = (EditText) findViewById(R.id.rechargement_champ_montant);
194 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 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 241 else{
... ... @@ -229,32 +253,39 @@ public class MainActivite extends Activity implements ASyncResponse, main_tab_fr
229 253 String user = viewUser.getText().toString();
230 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 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 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 290 if((mDroit >= 2)){
260 291 mState = STATE_VIDANGE;
... ...