make threads share one instance of GoogleAccessProtectedResource
authorEugene Crosser <Eugene.Crosser@ru.ibm.com>
Wed, 8 Feb 2012 14:26:13 +0000 (18:26 +0400)
committerEugene Crosser <Eugene.Crosser@ru.ibm.com>
Wed, 8 Feb 2012 14:26:13 +0000 (18:26 +0400)
src/org/average/whereami/APIBase.java [new file with mode: 0644]
src/org/average/whereami/LastLocation.java
src/org/average/whereami/Oracle.java
src/org/average/whereami/PhoneLog.java
src/org/average/whereami/WhereAmI.java

diff --git a/src/org/average/whereami/APIBase.java b/src/org/average/whereami/APIBase.java
new file mode 100644 (file)
index 0000000..e8dfbb1
--- /dev/null
@@ -0,0 +1,98 @@
+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;
+
+public class APIBase {
+
+       final String TAG = getClass().getName();
+
+       public PersistentStore store;
+       public Resources res;
+       public HttpTransport httpTransport =
+                       AndroidHttp.newCompatibleTransport();
+       public JsonFactory jsonFactory = new JacksonFactory();
+       public MyAccessProtectedResource accessProtectedResource;
+
+       public String authErrorMessage;
+
+       public APIBase(final Resources res, final PersistentStore store) {
+               this.res = res;
+               this.store = store;
+               AccessTokenResponse token = store.readTokens();
+               Log.v(TAG, "tokens - access: \"" + token.accessToken +
+                       "\", refresh: \"" + token.refreshToken +
+                       "\", client_id: \"" + ClientCredentials.CLIENT_ID +
+                       "\", client_secret: \"" +
+                               ClientCredentials.CLIENT_SECRET +
+                       "\"");
+               accessProtectedResource = new MyAccessProtectedResource(
+                       token.accessToken, httpTransport, jsonFactory,
+                       ClientCredentials.CLIENT_ID,
+                       ClientCredentials.CLIENT_SECRET,
+                       token.refreshToken);
+               authErrorMessage = res.getString(R.string.autherror);
+               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();
+               }
+       }
+
+       private 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.d("HttpTransport", record.getMessage());
+                       }
+               });
+       }
+}
index ba6722854bb7c1635ccebc956ee58ec00147a36d..99911c37afa8398714483de88b1dc4393287bc0c 100644 (file)
@@ -1,5 +1,6 @@
 package org.average.whereami;
 
+import org.average.whereami.APIBase;
 import org.average.whereami.PersistentStore;
 import org.average.whereami.SayWhen;
 
@@ -30,18 +31,19 @@ public final class LastLocation extends Oracle {
        private SayWhen sayWhen;
        private Latitude latitude;
 
-       public LastLocation(final Resources res, final PersistentStore store) {
-               super(res, store);
-               loc_names = res.getStringArray(R.array.loc_names);
-               loc_lats = res.getStringArray(R.array.loc_lats);
-               loc_lons = res.getStringArray(R.array.loc_lons);
-               loc_dists = res.getStringArray(R.array.loc_dists);
-               myname = res.getString(R.string.myname);
-               registered = res.getString(R.string.registered);
-               sayWhen = new SayWhen(res);
-               latitude = Latitude.builder(httpTransport, jsonFactory)
+       public LastLocation(APIBase base) {
+               super(base);
+               loc_names = base.res.getStringArray(R.array.loc_names);
+               loc_lats = base.res.getStringArray(R.array.loc_lats);
+               loc_lons = base.res.getStringArray(R.array.loc_lons);
+               loc_dists = base.res.getStringArray(R.array.loc_dists);
+               myname = base.res.getString(R.string.myname);
+               registered = base.res.getString(R.string.registered);
+               sayWhen = new SayWhen(base.res);
+               latitude = Latitude.builder(base.httpTransport,
+                                                       base.jsonFactory)
                        .setHttpRequestInitializer(
-                                       newMyAccessProtectedResource())
+                                               base.accessProtectedResource)
                        .setApplicationName("WhereAmI/1.0").build();
        }
 
@@ -63,7 +65,7 @@ public final class LastLocation extends Oracle {
                } catch (GoogleJsonResponseException e) {
                        Log.e(TAG, "GoogleJsonResponseException: " + e);
                        if (e.getResponse().getStatusCode() == 401) {
-                               return authErrorMessage;
+                               return base.authErrorMessage;
                        } else {
                                return e.getMessage();
                        }
index b3d992e410a437fe0e9852407aa7d830854696f5..848cf278b4cab94f28dc215f34519da971943947 100644 (file)
 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;
+import org.average.whereami.APIBase;
 
 abstract public class Oracle {
 
-       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 APIBase base;
 
-       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);
+       public Oracle(APIBase apibase) {
+               this.base = apibase;
        }
 
        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());
-                       }
-               });
-       }
 }
index b8f29d6eca99043325c657216794efbf8ea01335..2e2b30b7f7c075dd85f932bcbb04ad31492c2c93 100644 (file)
@@ -1,5 +1,6 @@
 package org.average.whereami;
 
+import org.average.whereami.APIBase;
 import org.average.whereami.PersistentStore;
 import org.average.whereami.SayWhen;
 
@@ -25,7 +26,6 @@ public final class PhoneLog extends Oracle {
 
        final String TAG = getClass().getName();
 
-       private PersistentStore store;
        private String calendar_name;
        private String peer_name;
        private String calendar_id;
@@ -42,26 +42,26 @@ public final class PhoneLog extends Oracle {
        private SayWhen sayWhen;
        private Calendar calendar;
 
-       public PhoneLog(final Resources res, final PersistentStore store) {
-               super(res, store);
-               this.store = store;
-               nocalendar = res.getString(R.string.nocalendar);
-               noevents = res.getString(R.string.noevents);
-               last_call = res.getString(R.string.last_call);
-               in_call = res.getString(R.string.in_call);
-               out_call = res.getString(R.string.out_call);
-               duration = res.getString(R.string.duration);
-               lessthan = res.getString(R.string.lessthan);
-               minutes1 = res.getString(R.string.minutes1);
-               minutes2to4 = res.getString(R.string.minutes2to4);
-               minutes5up = res.getString(R.string.minutes5up);
-               sayWhen = new SayWhen(res);
-               calendar_name = store.get("calendar_name");
-               peer_name = store.get("peer_name");
-               calendar_id = store.get("calendar_id");
-               calendar = Calendar.builder(httpTransport, jsonFactory)
+       public PhoneLog(APIBase base) {
+               super(base);
+               nocalendar = base.res.getString(R.string.nocalendar);
+               noevents = base.res.getString(R.string.noevents);
+               last_call = base.res.getString(R.string.last_call);
+               in_call = base.res.getString(R.string.in_call);
+               out_call = base.res.getString(R.string.out_call);
+               duration = base.res.getString(R.string.duration);
+               lessthan = base.res.getString(R.string.lessthan);
+               minutes1 = base.res.getString(R.string.minutes1);
+               minutes2to4 = base.res.getString(R.string.minutes2to4);
+               minutes5up = base.res.getString(R.string.minutes5up);
+               sayWhen = new SayWhen(base.res);
+               calendar_name = base.store.get("calendar_name");
+               peer_name = base.store.get("peer_name");
+               calendar_id = base.store.get("calendar_id");
+               calendar = Calendar.builder(base.httpTransport,
+                                                       base.jsonFactory)
                        .setHttpRequestInitializer(
-                                       newMyAccessProtectedResource())
+                                               base.accessProtectedResource)
                        .setApplicationName("WhereAmI/1.0").build();
        }
 
@@ -76,13 +76,13 @@ public final class PhoneLog extends Oracle {
                                return nocalendar + " \"" +
                                        calendar_name + "\"";
                        } else {
-                               store.put("calendar_id", calendar_id);
+                               base.store.put("calendar_id", calendar_id);
                                return scanEvents(calendar_id, peer_name);
                        }
                } catch (GoogleJsonResponseException e) {
                        Log.e(TAG, "GoogleJsonResponseException: " + e);
                        if (e.getResponse().getStatusCode() == 401) {
-                               return authErrorMessage;
+                               return base.authErrorMessage;
                        } else {
                                return e.getMessage();
                        }
index 9731e099c75d717d95e9d496f0114850b09b8e29..3a4b05594209dbc2ff527b687921c0826df9881f 100644 (file)
@@ -1,6 +1,7 @@
 package org.average.whereami;
 
 import org.average.whereami.WhereAmIprefs;
+import org.average.whereami.APIBase;
 import org.average.whereami.Oracle;
 import org.average.whereami.PhoneLog;
 import org.average.whereami.LastLocation;
@@ -221,11 +222,12 @@ public class WhereAmI extends Activity
         tvt = (TextView)findViewById(R.id.time);
         tvd = (TextView)findViewById(R.id.date);
         tvs = (TextView)findViewById(R.id.timestamp);
+       APIBase base = new APIBase(res, store);
        ut = new UpdateTarget[] {
             new UpdateTarget((TextView)findViewById(R.id.phonecall),
-                             new PhoneLog(res, store)),
+                             new PhoneLog(base)),
             new UpdateTarget((TextView)findViewById(R.id.location),
-                             new LastLocation(res, store))
+                             new LastLocation(base))
         };
     }