logging in LastLocation
[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(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 +
39                         "\"");
40                 GoogleAccessProtectedResource accessProtectedResource =
41                         new GoogleAccessProtectedResource(
42                                 token.accessToken,
43                                 transport, jsonFactory,
44                                 ClientCredentials.CLIENT_ID,
45                                 ClientCredentials.CLIENT_SECRET,
46                                 token.refreshToken)
47                                 {
48                                         @Override
49                                         public void onAccessToken(
50                                                         String accessToken) {
51                                                 Log.v(TAG, "Update access token to \"" + accessToken + "\"");
52                                                 store.updateAccessToken(
53                                                         accessToken);
54                                         }
55                                 }
56                                 ;
57                 Latitude.Builder lbldr = Latitude.builder(transport,
58                                                                 jsonFactory);
59                 lbldr.setHttpRequestInitializer(accessProtectedResource);
60                 lbldr.setApplicationName("WhereAmI/1.0");
61                 latitude = lbldr.build();
62         }
63
64         @Override
65         public final String getResult() {
66                 try {
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);
75                         ex.printStackTrace();
76                         return ex.getMessage();
77                 }
78         }
79
80         private String locationMessage(Location currentLocation) {
81                 Log.v(TAG, "entering locationMessage: " +
82                         currentLocation);
83                 // lat = currentLocation.getLatitude();
84                 // lon = currentLocation.getLongitude();
85                 // tsm = currentLocation.getTimestampMs();
86                 return "Current location: " + currentLocation;
87         }
88 }