]> average.org Git - WhereAmI.git/commitdiff
make one retry on failure
authorEugene Crosser <crosser@average.org>
Thu, 9 Feb 2012 20:46:00 +0000 (00:46 +0400)
committerEugene Crosser <crosser@average.org>
Thu, 9 Feb 2012 20:46:00 +0000 (00:46 +0400)
src/org/average/whereami/LastLocation.java
src/org/average/whereami/Oracle.java
src/org/average/whereami/PhoneLog.java
src/org/average/whereami/Utterance.java [new file with mode: 0644]
src/org/average/whereami/WhereAmI.java

index 99911c37afa8398714483de88b1dc4393287bc0c..c5791160298ab42ece4bf5f4950b100561938a6c 100644 (file)
@@ -1,6 +1,8 @@
 package org.average.whereami;
 
 import org.average.whereami.APIBase;
+import org.average.whereami.Oracle;
+import org.average.whereami.Utterance;
 import org.average.whereami.PersistentStore;
 import org.average.whereami.SayWhen;
 
@@ -48,7 +50,7 @@ public final class LastLocation extends Oracle {
        }
 
        @Override
-       public final String getResult() {
+       public final Utterance getResult() {
                try {
                        Log.v(TAG, "entering getResult");
                        Latitude.CurrentLocation.Get request =
@@ -61,20 +63,23 @@ public final class LastLocation extends Oracle {
                                        cloc.getLongitude().toString());
                        Long tsm = Long.parseLong(
                                        cloc.getTimestampMs().toString());
-                       return locationMessage(lat, lon, tsm);
+                       return new Utterance(true,
+                                       locationMessage(lat, lon, tsm));
                } catch (GoogleJsonResponseException e) {
                        Log.e(TAG, "GoogleJsonResponseException: " + e);
                        if (e.getResponse().getStatusCode() == 401) {
-                               return base.authErrorMessage;
+                               return new Utterance(false,
+                                       base.authErrorMessage + "\n" +
+                                       e.getMessage());
                        } else {
-                               return e.getMessage();
+                               return new Utterance(false, e.getMessage());
                        }
                 } catch (HttpResponseException e) {
                        Log.e(TAG, "HttpResponseException: " + e);
-                       return e.getMessage();
+                       return new Utterance(false, e.getMessage());
                } catch (IOException e) {
-                       e.printStackTrace();
-                       return e.getMessage();
+                       Log.e(TAG, "IOException: " + e);
+                       return new Utterance(false, e.getMessage());
                 }
        }
 
index 848cf278b4cab94f28dc215f34519da971943947..c1385074dd0540c3f6c18a0dc70d76e6c3becb9e 100644 (file)
@@ -1,6 +1,7 @@
 package org.average.whereami;
 
 import org.average.whereami.APIBase;
+import org.average.whereami.Utterance;
 
 abstract public class Oracle {
 
@@ -10,5 +11,5 @@ abstract public class Oracle {
                this.base = apibase;
        }
 
-       abstract public String getResult();
+       abstract public Utterance getResult();
 }
index 2e2b30b7f7c075dd85f932bcbb04ad31492c2c93..cf904c67a5038e5e515a2fbdf23bdb515bc07538 100644 (file)
@@ -1,6 +1,8 @@
 package org.average.whereami;
 
 import org.average.whereami.APIBase;
+import org.average.whereami.Oracle;
+import org.average.whereami.Utterance;
 import org.average.whereami.PersistentStore;
 import org.average.whereami.SayWhen;
 
@@ -66,32 +68,37 @@ public final class PhoneLog extends Oracle {
        }
 
        @Override
-       public final String getResult() {
+       public final Utterance getResult() {
                try {
                        Log.v(TAG, "entering getResult");
                        if (calendar_id == null || calendar_id == "") {
                                calendar_id = scanCalendars(calendar_name);
                        }
                        if (calendar_id == null || calendar_id == "") {
-                               return nocalendar + " \"" +
-                                       calendar_name + "\"";
+                               return new Utterance(true,
+                                       nocalendar + " \"" +
+                                       calendar_name + "\"");
                        } else {
                                base.store.put("calendar_id", calendar_id);
-                               return scanEvents(calendar_id, peer_name);
+                               return new Utterance(true,
+                                       scanEvents(calendar_id, peer_name));
                        }
                } catch (GoogleJsonResponseException e) {
                        Log.e(TAG, "GoogleJsonResponseException: " + e);
                        if (e.getResponse().getStatusCode() == 401) {
-                               return base.authErrorMessage;
+                               return new Utterance(false,
+                                       base.authErrorMessage + "\n" +
+                                       e.getMessage());
                        } else {
-                               return e.getMessage();
+                               return new Utterance(false,
+                                       e.getMessage());
                        }
                } catch (HttpResponseException e) {
                        Log.e(TAG, "HttpResponseException: " + e);
-                       return e.getMessage();
+                       return new Utterance(false, e.getMessage());
                } catch (IOException e) {
-                       e.printStackTrace();
-                       return e.getMessage();
+                       Log.e(TAG, "IOException: " + e);
+                       return new Utterance(false, e.getMessage());
                }
        }
 
diff --git a/src/org/average/whereami/Utterance.java b/src/org/average/whereami/Utterance.java
new file mode 100644 (file)
index 0000000..1e713d9
--- /dev/null
@@ -0,0 +1,10 @@
+package org.average.whereami;
+
+public class Utterance {
+       public Boolean success;
+       public String message;
+       public Utterance(Boolean success, String message) {
+               this.success = success;
+               this.message = message;
+       }
+}
index 3a4b05594209dbc2ff527b687921c0826df9881f..e3f510ea4a6ff641d4823abb7e319327f970ea20 100644 (file)
@@ -6,12 +6,15 @@ import org.average.whereami.Oracle;
 import org.average.whereami.PhoneLog;
 import org.average.whereami.LastLocation;
 
+import java.util.Random;
+
 import android.app.Activity;
 import android.net.wifi.WifiManager;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.AsyncTask;
 import android.os.Handler;
+import android.os.SystemClock;
 import android.net.ConnectivityManager;
 import android.net.NetworkInfo;
 import android.content.BroadcastReceiver;
@@ -41,6 +44,7 @@ public class WhereAmI extends Activity
     private Integer runningtasks = 0;
     private SharedPreferences prefs;
     private PersistentStore store;
+    private Random random = new Random();
 
     private class UpdateTarget {
         private TextView tv;
@@ -51,7 +55,16 @@ public class WhereAmI extends Activity
             @Override
             protected String doInBackground(Void... params) {
                 Log.v(TAG, "BgUpdate " + updater + " starting");
-                return updater.getResult();
+               Utterance result = updater.getResult();
+                if (!result.success) {
+                    SystemClock.sleep(1000 + random.nextInt(1000));
+                    result = updater.getResult();
+                }
+                if (!result.success) {
+                    Log.e(TAG, "After second attempt still " +
+                               result.message);
+                }
+                return result.message;
             }
         
             @Override