make accessProtectedResource a method
[WhereAmI.git] / src / org / average / whereami / LastLocation.java
index 98b7cf09fd84a9d22e75d2c458fd14471e3b695e..47fd4b388029a7aba3c026a0ccb4600f174b09fd 100644 (file)
@@ -1,23 +1,18 @@
 package org.average.whereami;
 
-import org.average.whereami.CredentialStore;
+import org.average.whereami.PersistentStore;
+import org.average.whereami.SayWhen;
 
 import java.lang.Math;
+import java.io.IOException;
 
-import com.google.api.client.extensions.android2.AndroidHttp;
-import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
-import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
+import com.google.api.client.googleapis.json.GoogleJsonResponseException;
 import com.google.api.client.http.HttpResponseException;
-import com.google.api.client.http.HttpTransport;
-import com.google.api.client.http.javanet.NetHttpTransport;
-import com.google.api.client.json.JsonFactory;
-import com.google.api.client.json.jackson.JacksonFactory;
 import com.google.api.services.latitude.Latitude;
 import com.google.api.services.latitude.LatitudeRequest;
 import com.google.api.services.latitude.model.Location;
 
 import android.content.Context;
-import android.content.SharedPreferences;
 import android.content.res.Resources;
 import android.text.format.Time;
 import android.util.Log;
@@ -26,68 +21,30 @@ 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 CredentialStore store) {
+       public LastLocation(final Resources res, final PersistentStore store) {
+               super(res, store);
                loc_names = res.getStringArray(R.array.loc_names);
                loc_lats = res.getStringArray(R.array.loc_lats);
                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);
-
-               HttpTransport transport = AndroidHttp.newCompatibleTransport();
-               JsonFactory jsonFactory = new JacksonFactory();
-               AccessTokenResponse token = store.read();
-               Log.v(TAG, "tokens - access: \"" + token.accessToken +
-                       "\", refresh: \"" + token.refreshToken +
-                       "\", client_id: \"" + ClientCredentials.CLIENT_ID +
-                       "\", client_secret: \"" +
-                                       ClientCredentials.CLIENT_SECRET +
-                       "\"");
-               GoogleAccessProtectedResource accessProtectedResource =
-                       new GoogleAccessProtectedResource(
-                               token.accessToken,
-                               transport, jsonFactory,
-                               ClientCredentials.CLIENT_ID,
-                               ClientCredentials.CLIENT_SECRET,
-                               token.refreshToken)
-                               {
-                                       @Override
-                                       public void onAccessToken(
-                                                       String accessToken) {
-                                               Log.v(TAG, "Update access token to \"" + accessToken + "\"");
-                                               store.updateAccessToken(
-                                                       accessToken);
-                                       }
-                               }
-                               ;
-               Latitude.Builder lbldr = Latitude.builder(transport,
-                                                               jsonFactory);
-               lbldr.setHttpRequestInitializer(accessProtectedResource);
-               lbldr.setApplicationName("WhereAmI/1.0");
-               latitude = lbldr.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 =
@@ -101,10 +58,19 @@ public final class LastLocation extends Oracle {
                        Long tsm = Long.parseLong(
                                        cloc.getTimestampMs().toString());
                        return locationMessage(lat, lon, tsm);
-                } catch (Exception ex) {
-                       Log.v(TAG, "exception in getResult: " + ex);
-                       ex.printStackTrace();
-                       return ex.getMessage();
+               } catch (GoogleJsonResponseException e) {
+                       Log.e(TAG, "GoogleJsonResponseException: " + e);
+                       if (e.getResponse().getStatusCode() == 401) {
+                               return authErrorMessage;
+                       } else {
+                               return e.getMessage();
+                       }
+                } catch (HttpResponseException e) {
+                       Log.e(TAG, "HttpResponseException: " + e);
+                       return e.getMessage();
+               } catch (IOException e) {
+                       e.printStackTrace();
+                       return e.getMessage();
                 }
        }
 
@@ -125,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) {