dbb735357eaff4a7086edb4e51f3236a4dbebc11
[WhereAmI.git] / src / org / average / whereami / PhoneLog.java
1 package org.average.whereami;
2
3 import org.average.whereami.CredentialStore;
4
5 //import java.lang.Long;
6
7 import com.google.api.client.extensions.android2.AndroidHttp;
8 import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
9 import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
10 import com.google.api.client.http.HttpResponseException;
11 import com.google.api.client.http.HttpTransport;
12 import com.google.api.client.http.javanet.NetHttpTransport;
13 import com.google.api.client.json.JsonFactory;
14 import com.google.api.client.json.jackson.JacksonFactory;
15 import com.google.api.client.util.DateTime;
16 import com.google.api.services.calendar.Calendar;
17 import com.google.api.services.calendar.CalendarRequest;
18 import com.google.api.services.calendar.model.CalendarList;
19 import com.google.api.services.calendar.model.CalendarListEntry;
20 import com.google.api.services.calendar.model.Event;
21 import com.google.api.services.calendar.model.Events;
22 import com.google.common.collect.Lists;
23
24 import android.content.Context;
25 import android.content.res.Resources;
26 import android.content.SharedPreferences;
27 import android.util.Log;
28
29 public final class PhoneLog extends Oracle {
30
31         final String TAG = getClass().getName();
32
33         private Calendar calendar;
34
35         public PhoneLog(final Resources res, final CredentialStore store) {
36                 HttpTransport transport = AndroidHttp.newCompatibleTransport();
37                 JsonFactory jsonFactory = new JacksonFactory();
38                 AccessTokenResponse token = store.read();
39                 Log.v(TAG, "tokens - access: \"" + token.accessToken +
40                         "\", refresh: \"" + token.refreshToken +
41                         "\", client_id: \"" + ClientCredentials.CLIENT_ID +
42                         "\", client_secret: \"" +
43                                         ClientCredentials.CLIENT_SECRET +
44                         "\"");
45                 GoogleAccessProtectedResource accessProtectedResource =
46                         new GoogleAccessProtectedResource(
47                                 token.accessToken,
48                                 transport, jsonFactory,
49                                 ClientCredentials.CLIENT_ID,
50                                 ClientCredentials.CLIENT_SECRET,
51                                 token.refreshToken)
52                                 {
53                                         @Override
54                                         public void onAccessToken(
55                                                         String accessToken) {
56                                                 Log.v(TAG, "Update access token to \"" + accessToken + "\"");
57                                                 store.updateAccessToken(
58                                                         accessToken);
59                                         }
60                                 }
61                                 ;
62                 Calendar.Builder cbldr = Calendar.builder(transport,
63                                                                 jsonFactory);
64                 cbldr.setHttpRequestInitializer(accessProtectedResource);
65                 cbldr.setApplicationName("WhereAmI/1.0");
66                 calendar = cbldr.build();
67         }
68
69         @Override
70         public final String getResult() {
71                 try {
72                         Log.v(TAG, "entering getResult");
73                         return TAG + " complete";
74                 } catch (Exception ex) {
75                         Log.v(TAG, "exception in getResult: " + ex);
76                         ex.printStackTrace();
77                         return ex.getMessage();
78                 }
79         }
80 }