From: Eugene Crosser Date: Fri, 3 Feb 2012 13:07:07 +0000 (+0400) Subject: produce phonelog message X-Git-Url: http://average.org/gitweb/?a=commitdiff_plain;h=5a86e601c703aee386f13ea2cf9122fbde7cfcb0;p=WhereAmI.git produce phonelog message --- diff --git a/res/values/strings.xml b/res/values/strings.xml index 5957bba..83a28e1 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -8,6 +8,7 @@ Последнее обновление Неудачная попытка Нет календаря с именем + Нет записей за последнюю неделю Необходимо авторизоваться (через меню) Женя отметка diff --git a/src/org/average/whereami/PhoneLog.java b/src/org/average/whereami/PhoneLog.java index 2665c34..dde2a1a 100644 --- a/src/org/average/whereami/PhoneLog.java +++ b/src/org/average/whereami/PhoneLog.java @@ -17,6 +17,7 @@ import com.google.common.collect.Lists; import android.content.Context; import android.content.res.Resources; +import android.text.format.Time; import android.util.Log; public final class PhoneLog extends Oracle { @@ -30,11 +31,13 @@ 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"); @@ -50,13 +53,13 @@ public final class PhoneLog extends Oracle { 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); @@ -105,44 +108,71 @@ public final class PhoneLog extends Oracle { 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"); } }