--- /dev/null
+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());
+ }
+ });
+ }
+}
package org.average.whereami;
+import org.average.whereami.APIBase;
import org.average.whereami.PersistentStore;
import org.average.whereami.SayWhen;
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();
}
} catch (GoogleJsonResponseException e) {
Log.e(TAG, "GoogleJsonResponseException: " + e);
if (e.getResponse().getStatusCode() == 401) {
- return authErrorMessage;
+ return base.authErrorMessage;
} else {
return e.getMessage();
}
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());
- }
- });
- }
}
package org.average.whereami;
+import org.average.whereami.APIBase;
import org.average.whereami.PersistentStore;
import org.average.whereami.SayWhen;
final String TAG = getClass().getName();
- private PersistentStore store;
private String calendar_name;
private String peer_name;
private String calendar_id;
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();
}
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();
}
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;
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))
};
}