www

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

commit 08e0b1cd7e7872e6cb73f36979bbd18bc708d760
parent 0773fb9f25b417108ae256dd91b698367bf64b02
Author: Yoann Bonavero <yoann.b87@voila.fr>
Date:   Thu,  3 Feb 2011 12:40:29 +0100

Merge branch 'master' of https://github.com/jsmaniac/2011-m1s2-ter

Diffstat:
Mcode/PtiClic/AndroidManifest.xml | 2++
Mcode/PtiClic/src/org/pticlic/FrontPage.java | 41+++++++++++++++++++++++++++++++++++------
Mcode/PtiClic/src/org/pticlic/games/BaseGame.java | 19++++++++++++-------
Mcode/PtiClic/src/org/pticlic/model/Network.java | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
4 files changed, 108 insertions(+), 15 deletions(-)

diff --git a/code/PtiClic/AndroidManifest.xml b/code/PtiClic/AndroidManifest.xml @@ -19,4 +19,6 @@ <uses-permission android:name="android.permission.INTERNET"></uses-permission> + +<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> </manifest> diff --git a/code/PtiClic/src/org/pticlic/FrontPage.java b/code/PtiClic/src/org/pticlic/FrontPage.java @@ -1,8 +1,11 @@ package org.pticlic; import org.pticlic.games.BaseGame; +import org.pticlic.model.Network; import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; @@ -19,17 +22,23 @@ public class FrontPage extends Activity implements OnClickListener{ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.frontpage); - + // Écoute des clics sur les différents boutons ((ImageView)findViewById(R.id.prefs)).setOnClickListener(this); ((ImageView)findViewById(R.id.play)).setOnClickListener(this); ((ImageView)findViewById(R.id.infoButton)).setOnClickListener(this); + } - + @Override protected void onStart() { super.onStart(); - + + if (Network.isConnected(this)) + System.out.println("Connecter"); + else + System.out.println("Non Connecter"); + // On récupère le nom du joueur des préférences. SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); String loginPref = sp.getString("login", "joueur"); @@ -44,14 +53,34 @@ 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) : startActivity(new Intent(this, BaseGame.class)); break; + case (R.id.play) : checkNetworkConnection(BaseGame.class); break; case (R.id.infoButton) : startActivity(new Intent(this, Information.class)); break; } } - + + @SuppressWarnings("rawtypes") + private void checkNetworkConnection(Class c) { + if (Network.isConnected(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("Problème de connexion au serveur. Vérifiez que vous êtes connecté au réseau.") + .setCancelable(false) + .setNegativeButton("Ok", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.cancel(); + } + }); + AlertDialog alert = builder.create(); + alert.show(); + } + } + @Override public void onBackPressed() { System.exit(0); } - + } diff --git a/code/PtiClic/src/org/pticlic/games/BaseGame.java b/code/PtiClic/src/org/pticlic/games/BaseGame.java @@ -11,6 +11,8 @@ import org.pticlic.model.Relation; import android.R.anim; import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; @@ -41,12 +43,14 @@ import android.widget.TextView; * proposer celle qui lui semble le mieux approprier. * */ + public class BaseGame extends Activity implements OnClickListener, AnimationListener { private int currentWord = 0; private TextView currentWordTextView; private int nbWord = 0; private DownloadedGame game; private Match match; + private Network network; /** Called when the activity is first created. */ @Override @@ -62,6 +66,7 @@ public class BaseGame extends Activity implements OnClickListener, AnimationList // On initialise la classe permettant la communication avec le serveur. Network network = new Network(serverURL, Mode.SIMPLE_GAME, id, passwd); + game = network.getGames(1); int nbrel = game.getNbRelation(); nbWord = game.getNbWord(); @@ -70,14 +75,14 @@ public class BaseGame extends Activity implements OnClickListener, AnimationList match = new Match(); match.setGame(game); - Relation r = Relation.getInstance(); - // Boutons des relations ImageView r1 = ((ImageView)findViewById(R.id.relation1)); ImageView r2 = ((ImageView)findViewById(R.id.relation2)); ImageView r3 = ((ImageView)findViewById(R.id.relation3)); ImageView r4 = ((ImageView)findViewById(R.id.relation4)); - + + Relation r = Relation.getInstance(); + // TODO : Pour l'instant la poubelle ne fait rien. Il faudra certainement la ranger dans un categorie dans GamePlayed pour calculer le score. ImageView trash = ((ImageView)findViewById(R.id.trash)); trash.setOnClickListener(this); @@ -122,7 +127,7 @@ public class BaseGame extends Activity implements OnClickListener, AnimationList //On recupere le centre de mainWord pour l'animation de translation. TextView mainWord = (TextView)findViewById(R.id.mainWord); currentWordTextView = (TextView)findViewById(R.id.currentWord); - + // On defini un ensemble d'animation AnimationSet set = new AnimationSet(true); set.setDuration(1000); @@ -189,18 +194,18 @@ public class BaseGame extends Activity implements OnClickListener, AnimationList @Override public void onAnimationEnd(Animation animation) { - + } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub - + } @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub - + } } \ No newline at end of file diff --git a/code/PtiClic/src/org/pticlic/model/Network.java b/code/PtiClic/src/org/pticlic/model/Network.java @@ -1,10 +1,18 @@ package org.pticlic.model; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; +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; @@ -55,6 +63,55 @@ public class Network { } /** + * Permet de savoir si l'application a access a internet ou non + * + * @param context l'activite permettant de tester l'access a internet + * @return <code>true</code> si on a access a internet <code>false</code> sinon + */ + public static boolean isConnected(Context context) { + ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); + if (cm != null && (cm.getActiveNetworkInfo() == null + || !cm.getActiveNetworkInfo().isConnected())) { + return false; + } + return true; + } + + /** + * Permet de verifier que la combinaison login/mdp est correct + * + * @param context l'activite permettant de tester l'access a internet + * @param id l'identifiant de l'utilisateur + * @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) { + 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; + } + + /** * Cette méthode permet de récupérer du serveur un certain nombre de parties. * @param nbGames Le nombre de parties que l'on veut récupérer. * @return @@ -171,12 +228,12 @@ public class Network { for (Integer i : game.getTrash()) { connection.addRequestProperty("trash[]", i.toString()); } - + Gson gson = new Gson(); JsonReader reader = new JsonReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); score = gson.fromJson(reader, TotalScore.class); - + } catch (IOException e) { return score;