1 package org.average.whereami;
3 import org.average.whereami.CredentialStore;
4 //import org.average.whereami.WhereAmIAccessResource;
6 import java.text.DateFormat;
7 import java.text.SimpleDateFormat;
10 import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
11 import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
12 import com.google.api.client.http.HttpTransport;
13 import com.google.api.client.http.javanet.NetHttpTransport;
14 import com.google.api.client.json.JsonFactory;
15 import com.google.api.client.json.jackson.JacksonFactory;
16 import com.google.api.services.latitude.Latitude;
17 import com.google.api.services.latitude.LatitudeRequest;
18 import com.google.api.services.latitude.model.Location;
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 import android.util.Log;
24 public final class LastLocation extends Oracle {
26 final String TAG = getClass().getName();
28 private Latitude latitude;
30 public LastLocation(final CredentialStore store) {
31 HttpTransport transport = new NetHttpTransport();
32 JsonFactory jsonFactory = new JacksonFactory();
33 AccessTokenResponse token = store.read();
34 Log.v(TAG, "tokens - access: \"" + token.accessToken +
35 "\", refresh: \"" + token.refreshToken +
36 "\", client_id: \"" + ClientCredentials.CLIENT_ID +
37 "\", client_secret: \"" +
38 ClientCredentials.CLIENT_SECRET +
40 GoogleAccessProtectedResource accessProtectedResource =
41 new GoogleAccessProtectedResource(
43 transport, jsonFactory,
44 ClientCredentials.CLIENT_ID,
45 ClientCredentials.CLIENT_SECRET,
49 public void onAccessToken(
51 Log.v(TAG, "Update access token to \"" + accessToken + "\"");
52 store.updateAccessToken(
57 Latitude.Builder lbldr = Latitude.builder(transport,
59 lbldr.setHttpRequestInitializer(accessProtectedResource);
60 lbldr.setApplicationName("WhereAmI/1.0");
61 latitude = lbldr.build();
65 public final String getResult() {
67 Log.v(TAG, "entering getResult");
68 Latitude.CurrentLocation.Get request =
69 latitude.currentLocation().get();
70 request.setGranularity("best");
71 Location currentLocation = request.execute();
72 return locationMessage(currentLocation);
73 } catch (Exception ex) {
74 Log.v(TAG, "exception in getResult: " + ex);
76 return ex.getMessage();
80 private String locationMessage(Location currentLocation) {
81 Log.v(TAG, "entering locationMessage: " +
83 // lat = currentLocation.getLatitude();
84 // lon = currentLocation.getLongitude();
85 // tsm = currentLocation.getTimestampMs();
86 return "Current location: " + currentLocation;