produce phonelog message
authorEugene Crosser <Eugene.Crosser@ru.ibm.com>
Fri, 3 Feb 2012 13:07:07 +0000 (17:07 +0400)
committerEugene Crosser <Eugene.Crosser@ru.ibm.com>
Fri, 3 Feb 2012 13:07:07 +0000 (17:07 +0400)
res/values/strings.xml
src/org/average/whereami/PhoneLog.java

index 5957bbaeb0e8d64a3a5880a48ee7f800922b2b68..83a28e140aab5834df42809414e95e4506034f36 100644 (file)
@@ -8,6 +8,7 @@
     <string name="lasttry">Последнее обновление</string>
     <string name="failtry">Неудачная попытка</string>
     <string name="nocalendar">Нет календаря с именем</string>
+    <string name="noevents">Нет записей за последнюю неделю</string>
     <string name="autherror">Необходимо авторизоваться (через меню)</string>
     <string name="myname">Женя</string>
     <string name="timestamp">отметка</string>
index 2665c340449914d3de40ae4352b12a562d2253d2..dde2a1a582a5f02458661533995d4863e135e24f 100644 (file)
@@ -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");
        }
 }