prototype for Calendar
[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.SharedPreferences;
26 import android.util.Log;
27
28 public final class PhoneLog extends Oracle {
29
30         final String TAG = getClass().getName();
31
32         private Calendar calendar;
33
34         public PhoneLog(final CredentialStore store) {
35                 HttpTransport transport = AndroidHttp.newCompatibleTransport();
36                 JsonFactory jsonFactory = new JacksonFactory();
37                 AccessTokenResponse token = store.read();
38                 Log.v(TAG, "tokens - access: \"" + token.accessToken +
39                         "\", refresh: \"" + token.refreshToken +
40                         "\", client_id: \"" + ClientCredentials.CLIENT_ID +
41                         "\", client_secret: \"" +
42                                         ClientCredentials.CLIENT_SECRET +
43                         "\"");
44                 GoogleAccessProtectedResource accessProtectedResource =
45                         new GoogleAccessProtectedResource(
46                                 token.accessToken,
47                                 transport, jsonFactory,
48                                 ClientCredentials.CLIENT_ID,
49                                 ClientCredentials.CLIENT_SECRET,
50                                 token.refreshToken)
51                                 {
52                                         @Override
53                                         public void onAccessToken(
54                                                         String accessToken) {
55                                                 Log.v(TAG, "Update access token to \"" + accessToken + "\"");
56                                                 store.updateAccessToken(
57                                                         accessToken);
58                                         }
59                                 }
60                                 ;
61                 Calendar.Builder cbldr = Calendar.builder(transport,
62                                                                 jsonFactory);
63                 cbldr.setHttpRequestInitializer(accessProtectedResource);
64                 cbldr.setApplicationName("WhereAmI/1.0");
65                 calendar = cbldr.build();
66         }
67
68         @Override
69         public final String getResult() {
70                 try {
71                         Log.v(TAG, "entering getResult");
72                         return TAG + " complete";
73                 } catch (Exception ex) {
74                         Log.v(TAG, "exception in getResult: " + ex);
75                         ex.printStackTrace();
76                         return ex.getMessage();
77                 }
78         }
79 }