ab4431c8957f36ed53073576475332b43e7422c6
[WhereAmI.git] / src / org / average / whereami / Oracle.java
1 package org.average.whereami;
2
3 import org.average.whereami.PersistentStore;
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 abstract public class Oracle {
18
19         final String TAG = getClass().getName();
20
21         public HttpTransport httpTransport;
22         public JsonFactory jsonFactory;
23
24         public GoogleAccessProtectedResource accessProtectedResource;
25
26         public String authErrorMessage;
27
28         public Oracle(final Resources res, final PersistentStore store) {
29                 authErrorMessage = res.getString(R.string.autherror);
30
31                 httpTransport = AndroidHttp.newCompatibleTransport();
32                 jsonFactory = new JacksonFactory();
33                 AccessTokenResponse token = store.readTokens();
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                 accessProtectedResource = new GoogleAccessProtectedResource(
41                         token.accessToken, httpTransport, jsonFactory,
42                         ClientCredentials.CLIENT_ID,
43                         ClientCredentials.CLIENT_SECRET,
44                         token.refreshToken)
45                         {
46                                 @Override
47                                 public void onAccessToken(
48                                                 String accessToken) {
49                                         Log.v(TAG, "Update access token to \""
50                                                 + accessToken + "\"");
51                                         store.updateAccessToken(
52                                                 accessToken);
53                                 }
54                         };
55         }
56
57         abstract public String getResult();
58 }