From 6a4744c5765e743aa4057f5fcf5571e6ed8abafe Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Fri, 10 Feb 2012 00:46:00 +0400 Subject: [PATCH] make one retry on failure --- src/org/average/whereami/LastLocation.java | 19 ++++++++++------ src/org/average/whereami/Oracle.java | 3 ++- src/org/average/whereami/PhoneLog.java | 25 ++++++++++++++-------- src/org/average/whereami/Utterance.java | 10 +++++++++ src/org/average/whereami/WhereAmI.java | 15 ++++++++++++- 5 files changed, 54 insertions(+), 18 deletions(-) create mode 100644 src/org/average/whereami/Utterance.java diff --git a/src/org/average/whereami/LastLocation.java b/src/org/average/whereami/LastLocation.java index 99911c3..c579116 100644 --- a/src/org/average/whereami/LastLocation.java +++ b/src/org/average/whereami/LastLocation.java @@ -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()); } } diff --git a/src/org/average/whereami/Oracle.java b/src/org/average/whereami/Oracle.java index 848cf27..c138507 100644 --- a/src/org/average/whereami/Oracle.java +++ b/src/org/average/whereami/Oracle.java @@ -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(); } diff --git a/src/org/average/whereami/PhoneLog.java b/src/org/average/whereami/PhoneLog.java index 2e2b30b..cf904c6 100644 --- a/src/org/average/whereami/PhoneLog.java +++ b/src/org/average/whereami/PhoneLog.java @@ -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 index 0000000..1e713d9 --- /dev/null +++ b/src/org/average/whereami/Utterance.java @@ -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; + } +} diff --git a/src/org/average/whereami/WhereAmI.java b/src/org/average/whereami/WhereAmI.java index 3a4b055..e3f510e 100644 --- a/src/org/average/whereami/WhereAmI.java +++ b/src/org/average/whereami/WhereAmI.java @@ -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 -- 2.43.0