<string name="failure">Неудача</string>
<string name="lasttry">Последнее обновление</string>
<string name="failtry">Неудачная попытка</string>
+ <string name="autherror">Необходимо авторизоваться (через меню)</string>
<string name="myname">Женя</string>
<string name="timestamp">отметка</string>
<string name="longago">больше суток назад</string>
import org.average.whereami.CredentialStore;
import java.lang.Math;
+import java.io.IOException;
-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.googleapis.json.GoogleJsonResponseException;
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 com.google.api.services.latitude.Latitude;
import com.google.api.services.latitude.LatitudeRequest;
import com.google.api.services.latitude.model.Location;
import android.content.Context;
-import android.content.SharedPreferences;
import android.content.res.Resources;
import android.text.format.Time;
import android.util.Log;
private String hours5up;
public LastLocation(final Resources res, final CredentialStore 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);
hours2to4 = res.getString(R.string.hours2to4);
hours5up = res.getString(R.string.hours5up);
- HttpTransport transport = AndroidHttp.newCompatibleTransport();
- JsonFactory jsonFactory = new JacksonFactory();
- AccessTokenResponse token = store.read();
- Log.v(TAG, "tokens - access: \"" + token.accessToken +
- "\", refresh: \"" + token.refreshToken +
- "\", client_id: \"" + ClientCredentials.CLIENT_ID +
- "\", client_secret: \"" +
- ClientCredentials.CLIENT_SECRET +
- "\"");
- GoogleAccessProtectedResource accessProtectedResource =
- new GoogleAccessProtectedResource(
- token.accessToken,
- transport, jsonFactory,
- ClientCredentials.CLIENT_ID,
- ClientCredentials.CLIENT_SECRET,
- token.refreshToken)
- {
- @Override
- public void onAccessToken(
- String accessToken) {
- Log.v(TAG, "Update access token to \"" + accessToken + "\"");
- store.updateAccessToken(
- accessToken);
- }
- }
- ;
- Latitude.Builder lbldr = Latitude.builder(transport,
- jsonFactory);
- lbldr.setHttpRequestInitializer(accessProtectedResource);
- lbldr.setApplicationName("WhereAmI/1.0");
- latitude = lbldr.build();
+ latitude = Latitude.builder(httpTransport, jsonFactory)
+ .setHttpRequestInitializer(accessProtectedResource)
+ .setApplicationName("WhereAmI/1.0").build();
}
@Override
Long tsm = Long.parseLong(
cloc.getTimestampMs().toString());
return locationMessage(lat, lon, tsm);
- } catch (Exception ex) {
- Log.v(TAG, "exception in getResult: " + ex);
- ex.printStackTrace();
- return ex.getMessage();
+ } catch (GoogleJsonResponseException e) {
+ Log.v(TAG, "GoogleJsonResponseException: " + e);
+ if (e.getResponse().getStatusCode() == 401) {
+ return authErrorMessage;
+ } else {
+ return e.getMessage();
+ }
+ } catch (HttpResponseException e) {
+ Log.v(TAG, "HttpResponseException: " + e);
+ return e.getMessage();
+ } catch (IOException e) {
+ e.printStackTrace();
+ return e.getMessage();
}
}
package org.average.whereami;
-abstract public class Oracle {
- abstract String getResult();
+import org.average.whereami.CredentialStore;
+
+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 Oracle {
+
+ final String TAG = getClass().getName();
+
+ public HttpTransport httpTransport = AndroidHttp.newCompatibleTransport();
+ public JsonFactory jsonFactory = new JacksonFactory();
+
+ public GoogleAccessProtectedResource accessProtectedResource;
+
+ public String authErrorMessage;
+
+ public Oracle(final Resources res, final CredentialStore store) {
+ authErrorMessage = res.getString(R.string.autherror);
+
+ AccessTokenResponse token = store.read();
+ Log.v(TAG, "tokens - access: \"" + token.accessToken +
+ "\", refresh: \"" + token.refreshToken +
+ "\", client_id: \"" + ClientCredentials.CLIENT_ID +
+ "\", client_secret: \"" +
+ ClientCredentials.CLIENT_SECRET +
+ "\"");
+ accessProtectedResource = new GoogleAccessProtectedResource(
+ token.accessToken, httpTransport, jsonFactory,
+ ClientCredentials.CLIENT_ID,
+ ClientCredentials.CLIENT_SECRET,
+ token.refreshToken)
+ {
+ @Override
+ public void onAccessToken(
+ String accessToken) {
+ Log.v(TAG, "Update access token to \""
+ + accessToken + "\"");
+ store.updateAccessToken(
+ accessToken);
+ }
+ };
+ }
+
+ public String getResult() {
+ return "<need to be overriden>";
+ }
}
import org.average.whereami.CredentialStore;
-//import java.lang.Long;
+import java.io.IOException;
-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.googleapis.json.GoogleJsonResponseException;
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 com.google.api.client.util.DateTime;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.CalendarRequest;
import android.content.Context;
import android.content.res.Resources;
-import android.content.SharedPreferences;
import android.util.Log;
public final class PhoneLog extends Oracle {
private Calendar calendar;
+ private String calendar_name = "Call Log";
+ private String calendar_id = null;
+ private String peer_name = "Alla Cherkashina";
+
public PhoneLog(final Resources res, final CredentialStore store) {
- HttpTransport transport = AndroidHttp.newCompatibleTransport();
- JsonFactory jsonFactory = new JacksonFactory();
- AccessTokenResponse token = store.read();
- Log.v(TAG, "tokens - access: \"" + token.accessToken +
- "\", refresh: \"" + token.refreshToken +
- "\", client_id: \"" + ClientCredentials.CLIENT_ID +
- "\", client_secret: \"" +
- ClientCredentials.CLIENT_SECRET +
- "\"");
- GoogleAccessProtectedResource accessProtectedResource =
- new GoogleAccessProtectedResource(
- token.accessToken,
- transport, jsonFactory,
- ClientCredentials.CLIENT_ID,
- ClientCredentials.CLIENT_SECRET,
- token.refreshToken)
- {
- @Override
- public void onAccessToken(
- String accessToken) {
- Log.v(TAG, "Update access token to \"" + accessToken + "\"");
- store.updateAccessToken(
- accessToken);
- }
- }
- ;
- Calendar.Builder cbldr = Calendar.builder(transport,
- jsonFactory);
- cbldr.setHttpRequestInitializer(accessProtectedResource);
- cbldr.setApplicationName("WhereAmI/1.0");
- calendar = cbldr.build();
+ super(res, store);
+
+ calendar = Calendar.builder(httpTransport, jsonFactory)
+ .setHttpRequestInitializer(accessProtectedResource)
+ .setApplicationName("WhereAmI/1.0").build();
}
@Override
public final String getResult() {
try {
Log.v(TAG, "entering getResult");
- return TAG + " complete";
- } catch (Exception ex) {
- Log.v(TAG, "exception in getResult: " + ex);
- ex.printStackTrace();
- return ex.getMessage();
- }
+ if (calendar_id == null) {
+ calendar_id = scanCalendars(calendar_name);
+ }
+ return scanEvents(calendar_id, peer_name);
+ } catch (GoogleJsonResponseException e) {
+ Log.v(TAG, "GoogleJsonResponseException: " + e);
+ if (e.getResponse().getStatusCode() == 401) {
+ return authErrorMessage;
+ } else {
+ return e.getMessage();
+ }
+ } catch (HttpResponseException e) {
+ Log.v(TAG, "HttpResponseException: " + e);
+ return e.getMessage();
+ } catch (IOException e) {
+ e.printStackTrace();
+ return e.getMessage();
+ }
+ }
+
+ private String scanCalendars(String calendar_name)
+ throws java.io.IOException {
+ String res = "";
+ Log.v(TAG, "Entering scanCalendars for \"" +
+ calendar_name + "\"");
+
+ CalendarList cl = calendar.calendarList().list().execute();
+ while (true) {
+ String pt = cl.getNextPageToken();
+ Log.v(TAG, "got cl with pt=" + pt);
+ for (CalendarListEntry le : cl.getItems()) {
+ Log.v(TAG, "entry " + le.getId() + ":\"" +
+ le.getSummary() + "\"");
+ if (le.getSummary().equals(calendar_name)) {
+ res = le.getId();
+ //break; // get all in the log
+ }
+ }
+ if (pt != null && pt != "") {
+ cl = calendar.calendarList().list()
+ .setPageToken(pt).execute();
+ } else {
+ break;
+ }
+ }
+
+ return res;
+ }
+
+ private String scanEvents(String calendar_id, String peer_name)
+ throws java.io.IOException {
+ Integer count = 0;
+ String res = "No records found";
+ Log.v(TAG, "Entering scanEvents for \"" +
+ peer_name + "\" in \"" + calendar_id + "\"");
+
+ Events events = calendar.events().list(calendar_id)
+ //.setOrderBy("starttime")
+ //.setSortOrder("descending") // this does not exist
+ .execute();
+ while (true) {
+ String pt = events.getNextPageToken();
+ Log.v(TAG, "got events with pt=" + pt);
+ for (Event event : events.getItems()) {
+ count++;
+ Log.v(TAG, "event " +
+ event.getStart() + " \"" +
+ event.getSummary() + "\"");
+ if ((event.getSummary().startsWith("Called ") ||
+ event.getSummary().startsWith("Call from ")) &&
+ event.getSummary().contains(peer_name)) {
+ res = makeMessage(event);
+ }
+ }
+ if (count < 100 && pt != null && pt != "") {
+ events = calendar.events().list(calendar_id)
+ .setPageToken(pt).execute();
+ } else {
+ break;
+ }
+ }
+
+ return res;
+ }
+
+ private String makeMessage(Event event) {
+ Log.v(TAG, "Chosen event: " + event.getStatus() +
+ " start " + event.getStart() +
+ " end " + event.getEnd());
+ return event.getId();
}
}