subclass MyAccessProtectedResource
[WhereAmI.git] / src / org / average / whereami / Oracle.java
1 package org.average.whereami;
2
3 import org.average.whereami.PersistentStore;
4
5 import java.io.IOException;
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
16 import android.content.res.Resources;
17 import android.util.Log;
18
19 abstract public class Oracle {
20
21         final String TAG = getClass().getName();
22
23         public HttpTransport httpTransport;
24         public JsonFactory jsonFactory;
25         public PersistentStore store;
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         private class MyAccessProtectedResource
37                 extends GoogleAccessProtectedResource {
38
39         // Why on earth contructors are not inherited?!?!
40                 public MyAccessProtectedResource(
41                                 String accessToken,
42                                 HttpTransport transport,
43                                 JsonFactory jsonFactory,
44                                 String clientId,
45                                 String clientSecret,
46                                 String refreshToken) {
47                         super(accessToken, transport, jsonFactory,
48                                 clientId, clientSecret, refreshToken);
49                 }
50
51                 @Override
52                 public void onAccessToken(String accessToken) {
53                         Log.v(TAG, "Update access token to \""
54                                 + accessToken + "\"");
55                         store.updateAccessToken( accessToken);
56                 }
57
58                 @Override
59                 protected boolean executeRefreshToken() throws IOException {
60                         Log.v(TAG, "executeRefreshToken was called");
61                         return super.executeRefreshToken();
62                 }
63         }
64
65         public MyAccessProtectedResource accessProtectedResource() {
66                 AccessTokenResponse token = store.readTokens();
67                 Log.v(TAG, "tokens - access: \"" + token.accessToken +
68                         "\", refresh: \"" + token.refreshToken +
69                         "\", client_id: \"" + ClientCredentials.CLIENT_ID +
70                         "\", client_secret: \"" +
71                                         ClientCredentials.CLIENT_SECRET +
72                         "\"");
73                 return new MyAccessProtectedResource(
74                         token.accessToken, httpTransport, jsonFactory,
75                         ClientCredentials.CLIENT_ID,
76                         ClientCredentials.CLIENT_SECRET,
77                         token.refreshToken);
78         }
79
80         abstract public String getResult();
81 }