]> www.average.org Git - WhereAmI.git/blobdiff - src/org/average/whereami/LastLocation.java
WIP on Latitude API
[WhereAmI.git] / src / org / average / whereami / LastLocation.java
diff --git a/src/org/average/whereami/LastLocation.java b/src/org/average/whereami/LastLocation.java
new file mode 100644 (file)
index 0000000..52b9098
--- /dev/null
@@ -0,0 +1,74 @@
+package org.average.whereami;
+
+import org.average.whereami.CredentialStore;
+//import org.average.whereami.WhereAmIAccessResource;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+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.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.util.Log;
+
+public final class LastLocation extends Oracle {
+
+       final String TAG = getClass().getName();
+
+       private Latitude latitude;
+
+       public LastLocation(CredentialStore store) {
+               HttpTransport transport = new NetHttpTransport();
+               JsonFactory jsonFactory = new JacksonFactory();
+               AccessTokenResponse token = store.read();
+               GoogleAccessProtectedResource accessProtectedResource =
+                       new GoogleAccessProtectedResource(
+                               token.accessToken,
+                               transport, jsonFactory,
+                               ClientCredentials.CLIENT_ID,
+                               ClientCredentials.CLIENT_SECRET,
+                               token.refreshToken)
+                               //{
+                               //@Override
+                               //public void onAccessToken(String accessToken) {
+                               //      store.updateAccessToken(accessToken);
+                               //  }
+                               //}
+                               ;
+               Latitude.Builder lbldr = Latitude.builder(transport,
+                                                               jsonFactory);
+               lbldr.setHttpRequestInitializer(accessProtectedResource);
+               lbldr.setApplicationName("WhereAmI/1.0");
+               latitude = lbldr.build();
+       }
+
+       @Override
+       public final String getResult() {
+               try {
+                       Latitude.CurrentLocation.Get request =
+                               latitude.currentLocation().get();
+                       request.setGranularity("best");
+                       Location currentLocation = request.execute();
+                       return locationMessage(currentLocation);
+                } catch (Exception ex) {
+                       return ex.getMessage();
+                }
+       }
+
+       private String locationMessage(Location currentLocation) {
+               // lat = currentLocation.getLatitude();
+               // lon = currentLocation.getLongitude();
+               // tsm = currentLocation.getTimestampMs();
+               return "Current location: " + currentLocation;
+       }
+}