enable HTTP logging
[WhereAmI.git] / src / org / average / whereami / Oracle.java
index 4043451bce61cc35d10764bd7f3bfa501efae3e9..b3d992e410a437fe0e9852407aa7d830854696f5 100644 (file)
@@ -1,5 +1,100 @@
 package org.average.whereami;
 
+import org.average.whereami.PersistentStore;
+
+import java.io.IOException;
+import com.google.api.client.http.HttpTransport;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
+
+import com.google.api.client.extensions.android2.AndroidHttp;
+import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
+import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
+import com.google.api.client.http.HttpResponseException;
+import com.google.api.client.http.HttpTransport;
+import com.google.api.client.http.javanet.NetHttpTransport;
+import com.google.api.client.json.JsonFactory;
+import com.google.api.client.json.jackson.JacksonFactory;
+
+import android.content.res.Resources;
+import android.util.Log;
+
 abstract public class Oracle {
-       abstract String getResult();
+
+       final String TAG = getClass().getName();
+
+       static public PersistentStore store;
+       static public HttpTransport httpTransport =
+                       AndroidHttp.newCompatibleTransport();
+       static public JsonFactory jsonFactory = new JacksonFactory();
+       static public String authErrorMessage;
+
+       public Oracle(final Resources res, final PersistentStore store) {
+               authErrorMessage = res.getString(R.string.autherror);
+               this.store = store;
+               enableLogging();
+       }
+
+       private class MyAccessProtectedResource
+               extends GoogleAccessProtectedResource {
+
+               public MyAccessProtectedResource(
+                               String accessToken,
+                               HttpTransport transport,
+                               JsonFactory jsonFactory,
+                               String clientId,
+                               String clientSecret,
+                               String refreshToken) {
+                       super(accessToken, transport, jsonFactory,
+                               clientId, clientSecret, refreshToken);
+               }
+
+               @Override
+               public void onAccessToken(String accessToken) {
+                       Log.v(TAG, "Update access token to \""
+                               + accessToken + "\"");
+                       store.updateAccessToken( accessToken);
+               }
+
+               @Override
+               protected boolean executeRefreshToken() throws IOException {
+                       Log.v(TAG, "executeRefreshToken was called");
+                       return super.executeRefreshToken();
+               }
+       }
+
+       public MyAccessProtectedResource
+                               newMyAccessProtectedResource() {
+               AccessTokenResponse token = store.readTokens();
+               Log.v(TAG, "tokens - access: \"" + token.accessToken +
+                       "\", refresh: \"" + token.refreshToken +
+                       "\", client_id: \"" + ClientCredentials.CLIENT_ID +
+                       "\", client_secret: \"" +
+                               ClientCredentials.CLIENT_SECRET +
+                       "\"");
+               return new MyAccessProtectedResource(
+                       token.accessToken, httpTransport, jsonFactory,
+                       ClientCredentials.CLIENT_ID,
+                       ClientCredentials.CLIENT_SECRET,
+                       token.refreshToken);
+       }
+
+       abstract public String getResult();
+
+       public static void enableLogging() {
+               Logger logger = Logger.getLogger(HttpTransport.class.getName());
+               logger.setLevel(Level.CONFIG);
+               logger.addHandler(new Handler() {
+                       @Override
+                       public void close() throws SecurityException {}
+                       @Override
+                       public void flush() {}
+                       @Override
+                       public void publish(LogRecord record) {
+                               Log.v("HttpTransport", record.getMessage());
+                       }
+               });
+       }
 }