diff --git a/code/PtiClic/src/org/pticlic/FrontPage.java b/code/PtiClic/src/org/pticlic/FrontPage.java index 5485779..5cf39ff 100644 --- 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 index faae23f..6f82732 100644 --- 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 index 72d3626..6dc6ced 100644 --- a/code/PtiClic/src/org/pticlic/model/Network.java +++ b/code/PtiClic/src/org/pticlic/model/Network.java @@ -5,6 +5,9 @@ import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; +import android.content.Context; +import android.net.ConnectivityManager; + import com.google.gson.Gson; import com.google.gson.stream.JsonReader; @@ -54,6 +57,15 @@ public class Network { this.passwd = passwd; } + 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; + } + /** * 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. @@ -171,12 +183,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;