make accessProtectedResource a method
[WhereAmI.git] / src / org / average / whereami / LastLocation.java
index b02a4b1b4f9765e1e4f6871544e6b5c44c7ba092..47fd4b388029a7aba3c026a0ccb4600f174b09fd 100644 (file)
@@ -1,6 +1,7 @@
 package org.average.whereami;
 
 import org.average.whereami.PersistentStore;
+import org.average.whereami.SayWhen;
 
 import java.lang.Math;
 import java.io.IOException;
@@ -20,19 +21,13 @@ public final class LastLocation extends Oracle {
 
        final String TAG = getClass().getName();
 
-       private Latitude latitude;
-
        private String[] loc_names;
        private String[] loc_lats;
        private String[] loc_lons;
        private String[] loc_dists;
        private String myname;
-       private String timestamp;
-       private String longago;
-       private String lessthanhour;
-       private String onehour;
-       private String hours2to4;
-       private String hours5up;
+       private String registered;
+       private SayWhen sayWhen;
 
        public LastLocation(final Resources res, final PersistentStore store) {
                super(res, store);
@@ -41,20 +36,15 @@ public final class LastLocation extends Oracle {
                loc_lons = res.getStringArray(R.array.loc_lons);
                loc_dists = res.getStringArray(R.array.loc_dists);
                myname = res.getString(R.string.myname);
-               timestamp = res.getString(R.string.timestamp);
-               longago = res.getString(R.string.longago);
-               lessthanhour = res.getString(R.string.lessthanhour);
-               onehour = res.getString(R.string.onehour);
-               hours2to4 = res.getString(R.string.hours2to4);
-               hours5up = res.getString(R.string.hours5up);
-
-               latitude = Latitude.builder(httpTransport, jsonFactory)
-                       .setHttpRequestInitializer(accessProtectedResource)
-                       .setApplicationName("WhereAmI/1.0").build();
+               registered = res.getString(R.string.registered);
+               sayWhen = new SayWhen(res);
        }
 
        @Override
        public final String getResult() {
+               Latitude latitude = Latitude.builder(httpTransport, jsonFactory)
+                       .setHttpRequestInitializer(accessProtectedResource())
+                       .setApplicationName("WhereAmI/1.0").build();
                try {
                        Log.v(TAG, "entering getResult");
                        Latitude.CurrentLocation.Get request =
@@ -69,14 +59,14 @@ public final class LastLocation extends Oracle {
                                        cloc.getTimestampMs().toString());
                        return locationMessage(lat, lon, tsm);
                } catch (GoogleJsonResponseException e) {
-                       Log.v(TAG, "GoogleJsonResponseException: " + e);
+                       Log.e(TAG, "GoogleJsonResponseException: " + e);
                        if (e.getResponse().getStatusCode() == 401) {
                                return authErrorMessage;
                        } else {
                                return e.getMessage();
                        }
                 } catch (HttpResponseException e) {
-                       Log.v(TAG, "HttpResponseException: " + e);
+                       Log.e(TAG, "HttpResponseException: " + e);
                        return e.getMessage();
                } catch (IOException e) {
                        e.printStackTrace();
@@ -101,20 +91,8 @@ public final class LastLocation extends Oracle {
                                break;
                        }
                }
-               Long ago = (System.currentTimeMillis() - tsm) / 3600000;
-               if (ago < 1) {
-                       agomsg = lessthanhour;
-               } else if (ago < 2) {
-                       agomsg = onehour;
-               } else if (ago < 5) {
-                       agomsg = "" + ago + " " + hours2to4;
-               } else if (ago < 24) {
-                       agomsg = "" + ago + " " + hours5up;
-               } else {
-                       agomsg = longago;
-               }
-               return myname + " " + locmsg + ", " + timestamp +
-                       " " + agomsg;
+               return myname + " " + locmsg + ", " + registered +
+                       " " + sayWhen.say(tsm);
        }
 
        private Double toRad(Double deg) {