make accessProtectedResource a method
[WhereAmI.git] / src / org / average / whereami / LastLocation.java
index 914a615217ddec08903f1f33dbc390bf5d8f8fd4..47fd4b388029a7aba3c026a0ccb4600f174b09fd 100644 (file)
@@ -1,78 +1,50 @@
 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;
 
 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 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_lons = res.getStringArray(R.array.loc_dists);
-
-               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();
+               loc_dists = res.getStringArray(R.array.loc_dists);
+               myname = res.getString(R.string.myname);
+               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 =
@@ -86,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();
                 }
        }
 
@@ -97,19 +78,21 @@ public final class LastLocation extends Oracle {
                Log.v(TAG, "entering locationMessage(" + lat +
                                "," + lon + "," + tsm + ")");
                String locmsg = "<UNSET>";
+               String agomsg = "<UNSET>";
                for (int i = 0; i < loc_names.length; i++) {
                        Double dist = haversine(lat, lon,
                                        Double.parseDouble(loc_lats[i]),
                                        Double.parseDouble(loc_lons[i]));
                        Log.v(TAG, "Dist from " + loc_names[i] +
                                " is " + dist);
-                       //if ((loc_dists[i] == "0") ||
-                       //    (dist < Double.parseDouble(loc_dists[i]))) {
-                       //      locmsg = loc_names[i];
-                       //      break;
-                       //}
+                       if ((loc_dists[i] == "0") ||
+                           (dist < Double.parseDouble(loc_dists[i]))) {
+                               locmsg = loc_names[i];
+                               break;
+                       }
                }
-               return locmsg;
+               return myname + " " + locmsg + ", " + registered +
+                       " " + sayWhen.say(tsm);
        }
 
        private Double toRad(Double deg) {