www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 861dd810e181e0e4e54773d6462aaec38ef47722
parent 08d97add297bd63ba3c85c8b600b2815109fb36b
Author: Bertrand BRUN <bertrand0brun@gmail.com>
Date:   Sat, 19 Feb 2011 12:12:16 +0100

Modification de l'action login_check dans le serveur, ainsi qu'ajout de la prise en compte du couple login/mdp lors du lancement du jeu

Diffstat:
Mcode/PtiClic/src/org/pticlic/FrontPage.java | 29+++++++++++++++++++++++++++--
Mcode/PtiClic/src/org/pticlic/Preference.java | 4+---
Mcode/PtiClic/src/org/pticlic/model/Network.java | 78+++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
Mcode/serveur/php/pticlic.php | 4++--
4 files changed, 81 insertions(+), 34 deletions(-)

diff --git a/code/PtiClic/src/org/pticlic/FrontPage.java b/code/PtiClic/src/org/pticlic/FrontPage.java @@ -8,6 +8,7 @@ import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; +import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.View; @@ -53,15 +54,39 @@ public class FrontPage extends Activity implements OnClickListener{ public void onClick(View v) { switch (v.getId()) { case (R.id.prefs) : startActivity(new Intent(this, Preference.class)); break; - case (R.id.play) : checkNetworkConnection(BaseGame.class); break; + case (R.id.play) : checkAllIsOk(BaseGame.class); break; case (R.id.infoButton) : startActivity(new Intent(this, Information.class)); break; } } @SuppressWarnings("rawtypes") - private void checkNetworkConnection(Class c) { + private void checkAllIsOk(Class c) { if (Network.isConnected(this)) { + if (Network.isLoginCorrect(this)) { startActivity(new Intent(this, c)); + } else { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(getString(R.string.app_name)) + .setIcon(android.R.drawable.ic_dialog_alert) + .setMessage("Le couple login/mdp n'est pas bon. Veuillez les modifier ou vous inscrire sur le site") + .setCancelable(false) + .setNeutralButton("Inscription", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + // TODO : Essayer de trouver comment mettre l'url qui est dans les preferences. + Uri uri = Uri.parse("http://dumbs.fr/~bbrun/pticlic/signup.php"); + startActivity(new Intent(Intent.ACTION_VIEW, uri)); + } + }) + .setPositiveButton("Preferences", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + startActivity(new Intent(getApplicationContext(), Preference.class)); + } + }); + AlertDialog alert = builder.create(); + alert.show(); + } } else { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(getString(R.string.app_name)) diff --git a/code/PtiClic/src/org/pticlic/Preference.java b/code/PtiClic/src/org/pticlic/Preference.java @@ -28,9 +28,7 @@ public class Preference extends PreferenceActivity implements OnSharedPreference public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (key.equals("passwd")) { if (Network.isConnected(this)) { - String id = sharedPreferences.getString("login", ""); - String passwd = sharedPreferences.getString("passwd", ""); - if (Network.isLoginCorrect(this, id, passwd)) { + if (Network.isLoginCorrect(this)) { Toast.makeText(this, "Couple login/mdp valide.", Toast.LENGTH_LONG).show(); diff --git a/code/PtiClic/src/org/pticlic/model/Network.java b/code/PtiClic/src/org/pticlic/model/Network.java @@ -5,13 +5,17 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.Serializable; import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; import java.net.URL; import org.pticlic.exception.PtiClicException; import android.content.Context; +import android.content.SharedPreferences; import android.net.ConnectivityManager; +import android.preference.PreferenceManager; import com.google.gson.Gson; import com.google.gson.stream.JsonReader; @@ -27,11 +31,26 @@ import com.google.gson.stream.JsonReader; */ public class Network { + public static class Check implements Serializable { + private static final long serialVersionUID = 1L; + private boolean login_ok = false; + + public boolean isLogin_ok() { + return login_ok; + } + + public void setLogin_ok(boolean login_ok) { + this.login_ok = login_ok; + } + } + String newGameJson = null; public enum Action { GET_GAMES(0), - SEND_GAME(1); + SEND_GAME(1), + CREATE_GAME(2), + CHECK_LOGIN(3); private final int value; @@ -97,33 +116,38 @@ public class Network { * @param passwd le mot de passe de l'utilisateur * @return <code>true</code> si la combinaison login/mdp est correct <code>false</code> sinon */ - public static boolean isLoginCorrect(Context context, String id, String passwd) { + public static boolean isLoginCorrect(Context context) { - //TODO : A decommenter le jour ou ce sera implemente cote serveur -// SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); -// String serverURL = sp.getString(Constant.SERVER_URL, "http://dumbs.fr/~bbrun/pticlic.json"); -// -// URL url; -// boolean res = false; -// try { -// url = new URL(serverURL); -// URLConnection connection = url.openConnection(); -// connection.addRequestProperty("action", "verifyAccess"); -// connection.addRequestProperty("user", id); -// connection.addRequestProperty("passwd", passwd); -// -// InputStream in = connection.getInputStream(); -// BufferedReader buf = new BufferedReader(new InputStreamReader(in)); -// res = Boolean.getBoolean(buf.readLine()); -// -// } catch (MalformedURLException e) { -// return false; -// } catch (IOException e) { -// return false; -// } -// -// return res; - return true; + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); + String serverURL = sp.getString(Constant.SERVER_URL, Constant.SERVER); + String id = sp.getString(Constant.USER_ID, "joueur"); + String passwd = sp.getString(Constant.USER_PASSWD, ""); + + URL url = null; + Gson gson = null; + BufferedReader reader = null; + String json = null; + boolean res = false; + try { + String urlS = serverURL+"/pticlic.php?" + + "action=" + Action.CHECK_LOGIN.value() + + "&user=" + id + + "&passwd=" + passwd; + + url = new URL(urlS); + gson = new Gson(); + reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8")); + json = reader.readLine(); + + Check check = gson.fromJson(json, Check.class); + res = check.isLogin_ok(); + } catch (MalformedURLException e) { + return false; + } catch (IOException e) { + return false; + } + + return res; } /** diff --git a/code/serveur/php/pticlic.php b/code/serveur/php/pticlic.php @@ -61,10 +61,10 @@ $user = SQLite3::escapeString($_GET['user']); $hash_passwd = md5($_GET['passwd']); $login_is_ok = ($hash_passwd == $db->querySingle("SELECT hash_passwd FROM user WHERE login='$user';")); -if ($action != "check_login" && (!$login_is_ok)) { +if ($action != 3 && (!$login_is_ok)) { mDie(3,"Utilisateur non enregistré ou mauvais mot de passe"); } -if ($action = "check_login") { +if ($action = 3) { if ($login_is_ok) { echo '{"login_ok":true}'; } else {