commit b64f63c26895fa753aa16af859abb8b88399ffc6
parent d8aedf04b9a6300f8ce284ce289edb01f8f20e42
Author: Bertrand BRUN <bertrand0brun@gmail.com>
Date: Wed, 13 Apr 2011 10:09:38 +0200
Nettoyage du code et ajout de l'interface avec Javascript
Diffstat:
4 files changed, 64 insertions(+), 123 deletions(-)
diff --git a/code/PtiClic/src/org/pticlic/FrontPage.java b/code/PtiClic/src/org/pticlic/FrontPage.java
@@ -1,5 +1,6 @@
package org.pticlic;
+import org.pticlic.js.JavaScriptInterface;
import org.pticlic.model.Constant;
import android.app.Activity;
@@ -19,8 +20,9 @@ public class FrontPage extends Activity {
webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
- webSettings.setJavaScriptEnabled(true);
+ webSettings.setJavaScriptEnabled(true);
+ webView.addJavascriptInterface(new JavaScriptInterface(this), "Pticlic");
webView.loadUrl(Constant.SERVER + Constant.SERVER_URL);
}
diff --git a/code/PtiClic/src/org/pticlic/exception/PtiClicException.java b/code/PtiClic/src/org/pticlic/exception/PtiClicException.java
@@ -1,58 +0,0 @@
-package org.pticlic.exception;
-
-import java.io.Serializable;
-
-import com.google.gson.Gson;
-
-public class PtiClicException extends Exception {
-
- private static final long serialVersionUID = 1L;
- private Error error;
-
- public static class Error implements Serializable {
-
- private static final long serialVersionUID = 1L;
- private int num;
- private String msg;
-
- public Error() {}
-
- public Error(int num, String msg) {
- this.num = num;
- this.msg = msg;
- }
-
- public int getNum() {
- return num;
- }
-
- public String getMsg() {
- return msg;
- }
- }
-
- public PtiClicException(Error error) {
- this.error = error;
- }
-
- public PtiClicException(int num, String msg) {
- this.error = new Error(num, msg);
- }
-
- public PtiClicException(String json) {
- Gson gson = new Gson();
- error = gson.fromJson(json, Error.class);
- }
-
- public String getMessage() {
- if (error == null) {
- return "Erreur inconnue !";
- } else {
- String s = error.getMsg();
- if (s == null) {
- return "Erreur inconnue !";
- }
- return "Erreur numero: " + error.getNum() + "\n" + error.getMsg();
- }
- }
-}
diff --git a/code/PtiClic/src/org/pticlic/js/JavaScriptInterface.java b/code/PtiClic/src/org/pticlic/js/JavaScriptInterface.java
@@ -0,0 +1,60 @@
+package org.pticlic.js;
+
+import org.pticlic.Preference;
+
+import android.app.ProgressDialog;
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
+
+public class JavaScriptInterface {
+ private Context mContext;
+ private ProgressDialog dialog;
+
+ /** Instantie l'interface et initialise le context */
+ public JavaScriptInterface(Context c) {
+ mContext = c;
+ }
+
+ /** Permet d'afficher la page des preferences qui est directement
+ * implemente sur le telephone
+ *
+ */
+ public void goToPreferences() {
+ mContext.startActivity(new Intent(mContext, Preference.class));
+ }
+
+ /** Permet de recupere une des preferences du systeme.
+ *
+ * @param pref La preference que l'on veux recupere
+ * @return La preference a recupere.
+ */
+ public String getPreference(String pref) {
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
+ return prefs.getString(pref, "");
+ }
+
+ /** Permet d'afficher une progressbar
+ * @param title Le titre a afficher par la ProgressBar
+ * @param message Le message a afficher par la progressBar
+ */
+ public void show(String title, String message) {
+ dialog = ProgressDialog.show(mContext, title, message);
+ }
+
+ /** Permet de retirer l'affichage de la boite de dialog
+ *
+ */
+ public void dismiss() {
+ if (dialog.isShowing())
+ dialog.dismiss();
+ }
+
+ /** Permet de quitter l'application
+ *
+ */
+ public void exit() {
+ System.exit(0);
+ }
+}
diff --git a/code/PtiClic/src/org/pticlic/model/Network.java b/code/PtiClic/src/org/pticlic/model/Network.java
@@ -17,38 +17,10 @@ import com.google.gson.Gson;
* soit le score qu'a réalisé un utilisateur.
* Elle permet aussi d'envoyer au serveur les parties realiser par l'utilisateur pour que le serveur
* puisse insérer la contribution de l'utilisateur, mais aussi pouvoir calculer le score de celui-ci.
+ *
*/
public class Network {
- public static class ScoreResponse {
- private int scoreTotal;
- private int[] scores;
- private String newGame;
- private boolean alreadyPlayed;
-
- public ScoreResponse() {}
-
- public int[] getScores() {
- return scores;
- }
-
- public int getScoreOfWord(int i) {
- return scores[i];
- }
-
- public int getScoreTotal() {
- return scoreTotal;
- }
-
- public String getNewGame() {
- return newGame;
- }
-
- public boolean getAlreadyPlayed() {
- return alreadyPlayed;
- }
- }
-
public static class Check implements Serializable {
private static final long serialVersionUID = 1L;
private boolean login_ok = false;
@@ -65,9 +37,6 @@ public class Network {
String newGameJson = null;
public enum Action {
- GET_GAMES(0),
- SEND_GAME(1),
- CREATE_GAME(2),
CHECK_LOGIN(3);
private final int value;
@@ -79,38 +48,6 @@ public class Network {
private String value() { return String.valueOf(value); }
}
- public enum Mode {
- SIMPLE_GAME("normal");
-
- private final String value;
-
- Mode(String value) {
- this.value = value;
- }
-
- private String value() { return value; }
- }
-
- private Mode mode;
- private String serverURL;
- private String id;
- private String passwd;
-
- /**
- * Constructeur
- *
- * @param serverURL Chaine de caractères représentant l'URL où se situe le serveur.
- * @param mode Le type de partie que l'on veut récupérer.
- * @param id L'indentifiant du joueur.
- * @param passwd Le mot de passe de l'utilisateur.
- */
- public Network(String serverURL, Mode mode, String id, String passwd) {
- this.mode = mode;
- this.serverURL = serverURL + "/server.php";
- this.id = id;
- this.passwd = passwd;
- }
-
/**
* Permet de savoir si l'application a access a internet ou non
*