change Log verbosity
[WhereAmI.git] / src / org / average / whereami / PhoneLog.java
1 package org.average.whereami;
2
3 import org.average.whereami.PersistentStore;
4
5 import java.io.IOException;
6
7 import com.google.api.client.googleapis.json.GoogleJsonResponseException;
8 import com.google.api.client.http.HttpResponseException;
9 import com.google.api.client.util.DateTime;
10 import com.google.api.services.calendar.Calendar;
11 import com.google.api.services.calendar.CalendarRequest;
12 import com.google.api.services.calendar.model.CalendarList;
13 import com.google.api.services.calendar.model.CalendarListEntry;
14 import com.google.api.services.calendar.model.Event;
15 import com.google.api.services.calendar.model.Events;
16 import com.google.common.collect.Lists;
17
18 import android.content.Context;
19 import android.content.res.Resources;
20 import android.text.format.Time;
21 import android.util.Log;
22
23 public final class PhoneLog extends Oracle {
24
25         final String TAG = getClass().getName();
26
27         private Calendar calendar;
28
29         private PersistentStore store;
30         private String calendar_name;
31         private String peer_name;
32         private String calendar_id;
33         private String nocalendar;
34         private String noevents;
35
36         public PhoneLog(final Resources res, final PersistentStore store) {
37                 super(res, store);
38                 this.store = store;
39                 nocalendar = res.getString(R.string.nocalendar);
40                 noevents = res.getString(R.string.noevents);
41                 calendar_name = store.get("calendar_name");
42                 peer_name = store.get("peer_name");
43                 calendar_id = store.get("calendar_id");
44
45                 calendar = Calendar.builder(httpTransport, jsonFactory)
46                         .setHttpRequestInitializer(accessProtectedResource)
47                         .setApplicationName("WhereAmI/1.0").build();
48         }
49
50         @Override
51         public final String getResult() {
52                 try {
53                         Log.v(TAG, "entering getResult");
54                         if (calendar_id == null || calendar_id == "") {
55                                 calendar_id = scanCalendars(calendar_name);
56                         }
57                         if (calendar_id == null || calendar_id == "") {
58                                 return nocalendar + " \"" +
59                                         calendar_name + "\"";
60                         } else {
61                                 store.put("calendar_id", calendar_id);
62                                 return scanEvents(calendar_id, peer_name);
63                         }
64                 } catch (GoogleJsonResponseException e) {
65                         Log.e(TAG, "GoogleJsonResponseException: " + e);
66                         if (e.getResponse().getStatusCode() == 401) {
67                                 return authErrorMessage;
68                         } else {
69                                 return e.getMessage();
70                         }
71                 } catch (HttpResponseException e) {
72                         Log.e(TAG, "HttpResponseException: " + e);
73                         return e.getMessage();
74                 } catch (IOException e) {
75                         e.printStackTrace();
76                         return e.getMessage();
77                 }
78         }
79
80         private String scanCalendars(String calendar_name)
81                                                 throws java.io.IOException {
82                 String res = "";
83                 Log.v(TAG, "Entering scanCalendars for \"" +
84                         calendar_name + "\"");
85
86                 CalendarList cl = calendar.calendarList().list().execute();
87                 while (true) {
88                         String pt = cl.getNextPageToken();
89                         Log.v(TAG, "got cl with pt=" + pt);
90                         for (CalendarListEntry le : cl.getItems()) {
91                                 Log.v(TAG, "entry " + le.getId() + ":\"" +
92                                         le.getSummary() + "\"");
93                                 if (le.getSummary().equals(calendar_name)) {
94                                         res = le.getId();
95                                         //break; // get all in the log
96                                 }
97                         }
98                         if (pt != null && pt != "") {
99                                 cl = calendar.calendarList().list()
100                                         .setPageToken(pt).execute();
101                         } else {
102                                 break;
103                         }
104                 }
105
106                 return res;
107         }
108
109         private String scanEvents(String calendar_id, String peer_name)
110                                                 throws java.io.IOException {
111                 Log.v(TAG, "Entering scanEvents for \"" +
112                         peer_name + "\" in \"" + calendar_id + "\"");
113                 Integer count = 0;
114                 Time ago = new Time();
115                 ago.set(System.currentTimeMillis() - 604800000L);
116                 Long latestStartTime = 0L;
117                 Long latestStopTime = 0L;
118                 Boolean latestWasOutgoing = false;
119                 Boolean wasFound = false;
120
121                 Events events = calendar.events().list(calendar_id)
122                         .setTimeMin(ago.format3339(false))
123                         .setQ(peer_name)
124                         .execute();
125                 while (true) {
126                         String pt = events.getNextPageToken();
127                         Log.v(TAG, "got events with pt=" + pt);
128                         for (Event event : events.getItems()) {
129                                 count++;
130                                 String evSummary = event.getSummary();
131                                 Long evStart = event.getStart()
132                                                 .getDateTime().getValue();
133                                 Long evStop = event.getEnd()
134                                                 .getDateTime().getValue();
135                                 Log.v(TAG, "event " + evStart +
136                                         " - " + evStop +
137                                         " : \"" + evSummary + "\"");
138                                 if ((evSummary.startsWith("Called ") ||
139                                      evSummary.startsWith("Call from ")) &&
140                                     evStart > latestStartTime) {
141                                                 latestStartTime = evStart;
142                                                 latestStopTime = evStop;
143                                                 latestWasOutgoing = evSummary
144                                                         .startsWith("Called ");
145                                                 wasFound = true;
146                                 }
147                         }
148                         if (count < 100 && pt != null && pt != "") {
149                                 events = calendar.events().list(calendar_id)
150                                         .setTimeMin(ago.format3339(false))
151                                         .setQ(peer_name)
152                                         .setPageToken(pt).execute();
153                         } else {
154                                 break;
155                         }
156                 }
157
158                 if (wasFound) {
159                         return makeMessage(latestStartTime, latestStopTime,
160                                                 latestWasOutgoing);
161                 } else {
162                         return noevents;
163                 }
164         }
165
166         private String makeMessage(Long latestStartTime, Long latestStopTime,
167                                         Boolean latestWasOutgoing) {
168                 Time stime = new Time();
169                 stime.set(latestStartTime);
170                 Long durms = latestStopTime - latestStartTime;
171                 Log.v(TAG, "Chosen event: start " + stime +
172                         " for " + durms + " msec");
173                 Long nowms = System.currentTimeMillis();
174                 return "" + (latestWasOutgoing?"Out ":"In  ") +
175                         (durms/3600000) + " min " +
176                         stime.format("%H:%M");
177         }
178 }