7ba16a5ec2dfde72e118e4b339783a6979895760
[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         public PersistentStore store;
24
25
26         public String authErrorMessage;
27
28         public Oracle(final Resources res, final PersistentStore store) {
29                 authErrorMessage = res.getString(R.string.autherror);
30                 this.store = store;
31
32                 httpTransport = AndroidHttp.newCompatibleTransport();
33                 jsonFactory = new JacksonFactory();
34 }
35
36         public GoogleAccessProtectedResource accessProtectedResource() {
37                 AccessTokenResponse token = store.readTokens();
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                 return new GoogleAccessProtectedResource(
45                         token.accessToken, httpTransport, jsonFactory,
46                         ClientCredentials.CLIENT_ID,
47                         ClientCredentials.CLIENT_SECRET,
48                         token.refreshToken)
49                         {
50                                 @Override
51                                 public void onAccessToken(
52                                                 String accessToken) {
53                                         Log.v(TAG, "Update access token to \""
54                                                 + accessToken + "\"");
55                                         store.updateAccessToken(
56                                                 accessToken);
57                                 }
58                         };
59         }
60
61         abstract public String getResult();
62 }