add README
[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         static public PersistentStore store;
24         static public HttpTransport httpTransport =
25                         AndroidHttp.newCompatibleTransport();
26         static public JsonFactory jsonFactory = new JacksonFactory();
27         static public String authErrorMessage;
28
29         public Oracle(final Resources res, final PersistentStore store) {
30                 authErrorMessage = res.getString(R.string.autherror);
31                 this.store = store;
32         }
33
34         private class MyAccessProtectedResource
35                 extends GoogleAccessProtectedResource {
36
37                 public MyAccessProtectedResource(
38                                 String accessToken,
39                                 HttpTransport transport,
40                                 JsonFactory jsonFactory,
41                                 String clientId,
42                                 String clientSecret,
43                                 String refreshToken) {
44                         super(accessToken, transport, jsonFactory,
45                                 clientId, clientSecret, refreshToken);
46                 }
47
48                 @Override
49                 public void onAccessToken(String accessToken) {
50                         Log.v(TAG, "Update access token to \""
51                                 + accessToken + "\"");
52                         store.updateAccessToken( accessToken);
53                 }
54
55                 @Override
56                 protected boolean executeRefreshToken() throws IOException {
57                         Log.v(TAG, "executeRefreshToken was called");
58                         return super.executeRefreshToken();
59                 }
60         }
61
62         public MyAccessProtectedResource
63                                 newMyAccessProtectedResource() {
64                 AccessTokenResponse token = store.readTokens();
65                 Log.v(TAG, "tokens - access: \"" + token.accessToken +
66                         "\", refresh: \"" + token.refreshToken +
67                         "\", client_id: \"" + ClientCredentials.CLIENT_ID +
68                         "\", client_secret: \"" +
69                                 ClientCredentials.CLIENT_SECRET +
70                         "\"");
71                 return new MyAccessProtectedResource(
72                         token.accessToken, httpTransport, jsonFactory,
73                         ClientCredentials.CLIENT_ID,
74                         ClientCredentials.CLIENT_SECRET,
75                         token.refreshToken);
76         }
77
78         abstract public String getResult();
79 }