Calendar API WIP
[WhereAmI.git] / src / org / average / whereami / Oracle.java
1 package org.average.whereami;
2
3 import org.average.whereami.CredentialStore;
4
5 import com.google.api.client.extensions.android2.AndroidHttp;
6 import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
7 import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
8 import com.google.api.client.http.HttpResponseException;
9 import com.google.api.client.http.HttpTransport;
10 import com.google.api.client.http.javanet.NetHttpTransport;
11 import com.google.api.client.json.JsonFactory;
12 import com.google.api.client.json.jackson.JacksonFactory;
13
14 import android.content.res.Resources;
15 import android.util.Log;
16
17 public class Oracle {
18
19         final String TAG = getClass().getName();
20
21         public HttpTransport httpTransport = AndroidHttp.newCompatibleTransport();
22         public JsonFactory jsonFactory = new JacksonFactory();
23
24         public GoogleAccessProtectedResource accessProtectedResource;
25
26         public String authErrorMessage;
27
28         public Oracle(final Resources res, final CredentialStore store) {
29                 authErrorMessage = res.getString(R.string.autherror);
30
31                 AccessTokenResponse token = store.read();
32                 Log.v(TAG, "tokens - access: \"" + token.accessToken +
33                         "\", refresh: \"" + token.refreshToken +
34                         "\", client_id: \"" + ClientCredentials.CLIENT_ID +
35                         "\", client_secret: \"" +
36                                         ClientCredentials.CLIENT_SECRET +
37                         "\"");
38                 accessProtectedResource = new GoogleAccessProtectedResource(
39                         token.accessToken, httpTransport, jsonFactory,
40                         ClientCredentials.CLIENT_ID,
41                         ClientCredentials.CLIENT_SECRET,
42                         token.refreshToken)
43                         {
44                                 @Override
45                                 public void onAccessToken(
46                                                 String accessToken) {
47                                         Log.v(TAG, "Update access token to \""
48                                                 + accessToken + "\"");
49                                         store.updateAccessToken(
50                                                 accessToken);
51                                 }
52                         };
53         }
54
55         public String getResult() {
56                 return "<need to be overriden>";
57         }
58 }