make accessProtectedResource a method
[WhereAmI.git] / src / org / average / whereami / LastLocation.java
index bd25283111bb1c2e48bfd70401229045d45b0f25..47fd4b388029a7aba3c026a0ccb4600f174b09fd 100644 (file)
 package org.average.whereami;
 
-import org.average.whereami.CredentialStore;
-//import org.average.whereami.WhereAmIAccessResource;
+import org.average.whereami.PersistentStore;
+import org.average.whereami.SayWhen;
 
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.util.Date;
+import java.lang.Math;
+import java.io.IOException;
 
-import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
-import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
-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.client.googleapis.json.GoogleJsonResponseException;
+import com.google.api.client.http.HttpResponseException;
 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 CredentialStore store) {
-               HttpTransport transport = new NetHttpTransport();
-               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();
+       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);
+               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 =
                                latitude.currentLocation().get();
                        request.setGranularity("best");
-                       Location currentLocation = request.execute();
-                       return locationMessage(currentLocation);
-                } catch (Exception ex) {
-                       Log.v(TAG, "exception in getResult: " + ex);
-                       ex.printStackTrace();
-                       return ex.getMessage();
+                       Location cloc = request.execute();
+                       Double lat = Double.parseDouble(
+                                       cloc.getLatitude().toString());
+                       Double lon = Double.parseDouble(
+                                       cloc.getLongitude().toString());
+                       Long tsm = Long.parseLong(
+                                       cloc.getTimestampMs().toString());
+                       return locationMessage(lat, lon, tsm);
+               } 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();
                 }
        }
 
-       private String locationMessage(Location currentLocation) {
-               Log.v(TAG, "entering locationMessage: " +
-                       currentLocation);
-               // lat = currentLocation.getLatitude();
-               // lon = currentLocation.getLongitude();
-               // tsm = currentLocation.getTimestampMs();
-               return "Current location: " + currentLocation;
+       private String locationMessage(Double lat, Double lon, Long tsm) {
+               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;
+                       }
+               }
+               return myname + " " + locmsg + ", " + registered +
+                       " " + sayWhen.say(tsm);
+       }
+
+       private Double toRad(Double deg) {
+               return deg * 3.14159265 / 180;
+       }
+
+       private Double haversine(Double lat1, Double lon1,
+                               Double lat2, Double lon2) {
+               Double dlat = toRad(lat2 - lat1);
+               Double dlon = toRad(lon2 - lon1);
+               Double a = Math.sin(dlat / 2) * Math.sin(dlat / 2) +
+                       Math.sin(dlon / 2) * Math.sin(dlon / 2) *
+                       Math.cos(toRad(lat1)) * Math.cos(toRad(lat2));
+               Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
+               return 6371 * c;
        }
 }