import android.content.Context;
import android.content.res.Resources;
+import android.text.format.Time;
import android.util.Log;
public final class PhoneLog extends Oracle {
private String peer_name;
private String calendar_id;
private String nocalendar;
+ private String noevents;
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);
calendar_name = store.get("calendar_name");
peer_name = store.get("peer_name");
calendar_id = store.get("calendar_id");
Log.v(TAG, "entering getResult");
if (calendar_id == null || calendar_id == "") {
calendar_id = scanCalendars(calendar_name);
- store.put("calendar_id", calendar_id);
}
if (calendar_id == null || calendar_id == "") {
- return scanEvents(calendar_id, peer_name);
- } else {
return nocalendar + " \"" +
calendar_name + "\"";
+ } else {
+ store.put("calendar_id", calendar_id);
+ return scanEvents(calendar_id, peer_name);
}
} catch (GoogleJsonResponseException e) {
Log.v(TAG, "GoogleJsonResponseException: " + e);
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 + "\"");
+ Integer count = 0;
+ Time ago = new Time();
+ ago.set(System.currentTimeMillis() - 604800000L);
+ Long latestStartTime = 0L;
+ Long latestStopTime = 0L;
+ Boolean latestWasOutgoing = false;
+ Boolean wasFound = false;
Events events = calendar.events().list(calendar_id)
- //.setOrderBy("starttime")
- //.setSortOrder("descending") // this does not exist
+ .setTimeMin(ago.format3339(false))
+ .setQ(peer_name)
.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);
+ String evSummary = event.getSummary();
+ Long evStart = event.getStart()
+ .getDateTime().getValue();
+ Long evStop = event.getEnd()
+ .getDateTime().getValue();
+ Log.v(TAG, "event " + evStart +
+ " - " + evStop +
+ " : \"" + evSummary + "\"");
+ if ((evSummary.startsWith("Called ") ||
+ evSummary.startsWith("Call from ")) &&
+ evStart > latestStartTime) {
+ latestStartTime = evStart;
+ latestStopTime = evStop;
+ latestWasOutgoing = evSummary
+ .startsWith("Called ");
+ wasFound = true;
}
}
if (count < 100 && pt != null && pt != "") {
events = calendar.events().list(calendar_id)
+ .setTimeMin(ago.format3339(false))
+ .setQ(peer_name)
.setPageToken(pt).execute();
} else {
break;
}
}
- return res;
+ if (wasFound) {
+ return makeMessage(latestStartTime, latestStopTime,
+ latestWasOutgoing);
+ } else {
+ return noevents;
+ }
}
- private String makeMessage(Event event) {
- Log.v(TAG, "Chosen event: " + event.getStatus() +
- " start " + event.getStart() +
- " end " + event.getEnd());
- return event.getId();
+ private String makeMessage(Long latestStartTime, Long latestStopTime,
+ Boolean latestWasOutgoing) {
+ Time stime = new Time();
+ stime.set(latestStartTime);
+ Long durms = latestStopTime - latestStartTime;
+ Log.v(TAG, "Chosen event: start " + stime +
+ " for " + durms + " msec");
+ Long nowms = System.currentTimeMillis();
+ return "" + (latestWasOutgoing?"Out ":"In ") +
+ (durms/3600000) + " min " +
+ stime.format("%H:%M");
}
}