]> www.average.org Git - WhereAmI.git/blob - src/org/average/whereami/LastLocation.java
WIP on Latitude API
[WhereAmI.git] / src / org / average / whereami / LastLocation.java
1 package org.average.whereami;
2
3 import org.average.whereami.CredentialStore;
4 //import org.average.whereami.WhereAmIAccessResource;
5
6 import java.text.DateFormat;
7 import java.text.SimpleDateFormat;
8 import java.util.Date;
9
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;
19
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 import android.util.Log;
23
24 public final class LastLocation extends Oracle {
25
26         final String TAG = getClass().getName();
27
28         private Latitude latitude;
29
30         public LastLocation(CredentialStore store) {
31                 HttpTransport transport = new NetHttpTransport();
32                 JsonFactory jsonFactory = new JacksonFactory();
33                 AccessTokenResponse token = store.read();
34                 GoogleAccessProtectedResource accessProtectedResource =
35                         new GoogleAccessProtectedResource(
36                                 token.accessToken,
37                                 transport, jsonFactory,
38                                 ClientCredentials.CLIENT_ID,
39                                 ClientCredentials.CLIENT_SECRET,
40                                 token.refreshToken)
41                                 //{
42                                 //@Override
43                                 //public void onAccessToken(String accessToken) {
44                                 //      store.updateAccessToken(accessToken);
45                                 //  }
46                                 //}
47                                 ;
48                 Latitude.Builder lbldr = Latitude.builder(transport,
49                                                                 jsonFactory);
50                 lbldr.setHttpRequestInitializer(accessProtectedResource);
51                 lbldr.setApplicationName("WhereAmI/1.0");
52                 latitude = lbldr.build();
53         }
54
55         @Override
56         public final String getResult() {
57                 try {
58                         Latitude.CurrentLocation.Get request =
59                                 latitude.currentLocation().get();
60                         request.setGranularity("best");
61                         Location currentLocation = request.execute();
62                         return locationMessage(currentLocation);
63                 } catch (Exception ex) {
64                         return ex.getMessage();
65                 }
66         }
67
68         private String locationMessage(Location currentLocation) {
69                 // lat = currentLocation.getLatitude();
70                 // lon = currentLocation.getLongitude();
71                 // tsm = currentLocation.getTimestampMs();
72                 return "Current location: " + currentLocation;
73         }
74 }