make configurable calendar selection
[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.util.Log;
21
22 public final class PhoneLog extends Oracle {
23
24         final String TAG = getClass().getName();
25
26         private Calendar calendar;
27
28         private PersistentStore store;
29         private String calendar_name;
30         private String peer_name;
31         private String calendar_id;
32
33         public PhoneLog(final Resources res, final PersistentStore store) {
34                 super(res, store);
35                 this.store = store;
36                 calendar_name = store.get("calendar_name");
37                 peer_name = store.get("peer_name");
38                 calendar_id = store.get("calendar_id");
39
40                 calendar = Calendar.builder(httpTransport, jsonFactory)
41                         .setHttpRequestInitializer(accessProtectedResource)
42                         .setApplicationName("WhereAmI/1.0").build();
43         }
44
45         @Override
46         public final String getResult() {
47                 try {
48                         Log.v(TAG, "entering getResult");
49                         if (calendar_id == null || calendar_id == "") {
50                                 calendar_id = scanCalendars(calendar_name);
51                                 store.put("calendar_id", calendar_id);
52                         }
53                         return scanEvents(calendar_id, peer_name);
54                 } catch (GoogleJsonResponseException e) {
55                         Log.v(TAG, "GoogleJsonResponseException: " + e);
56                         if (e.getResponse().getStatusCode() == 401) {
57                                 return authErrorMessage;
58                         } else {
59                                 return e.getMessage();
60                         }
61                 } catch (HttpResponseException e) {
62                         Log.v(TAG, "HttpResponseException: " + e);
63                         return e.getMessage();
64                 } catch (IOException e) {
65                         e.printStackTrace();
66                         return e.getMessage();
67                 }
68         }
69
70         private String scanCalendars(String calendar_name)
71                                                 throws java.io.IOException {
72                 String res = "";
73                 Log.v(TAG, "Entering scanCalendars for \"" +
74                         calendar_name + "\"");
75
76                 CalendarList cl = calendar.calendarList().list().execute();
77                 while (true) {
78                         String pt = cl.getNextPageToken();
79                         Log.v(TAG, "got cl with pt=" + pt);
80                         for (CalendarListEntry le : cl.getItems()) {
81                                 Log.v(TAG, "entry " + le.getId() + ":\"" +
82                                         le.getSummary() + "\"");
83                                 if (le.getSummary().equals(calendar_name)) {
84                                         res = le.getId();
85                                         //break; // get all in the log
86                                 }
87                         }
88                         if (pt != null && pt != "") {
89                                 cl = calendar.calendarList().list()
90                                         .setPageToken(pt).execute();
91                         } else {
92                                 break;
93                         }
94                 }
95
96                 return res;
97         }
98
99         private String scanEvents(String calendar_id, String peer_name)
100                                                 throws java.io.IOException {
101                 Integer count = 0;
102                 String res = "No records found";
103                 Log.v(TAG, "Entering scanEvents for \"" +
104                         peer_name + "\" in \"" + calendar_id + "\"");
105
106                 Events events = calendar.events().list(calendar_id)
107                         //.setOrderBy("starttime")
108                         //.setSortOrder("descending") // this does not exist
109                         .execute();
110                 while (true) {
111                         String pt = events.getNextPageToken();
112                         Log.v(TAG, "got events with pt=" + pt);
113                         for (Event event : events.getItems()) {
114                                 count++;
115                                 Log.v(TAG, "event " +
116                                         event.getStart() + " \"" +
117                                         event.getSummary() + "\"");
118                                 if ((event.getSummary().startsWith("Called ") ||
119                                      event.getSummary().startsWith("Call from ")) &&
120                                     event.getSummary().contains(peer_name)) {
121                                         res = makeMessage(event);
122                                 }
123                         }
124                         if (count < 100 && pt != null && pt != "") {
125                                 events = calendar.events().list(calendar_id)
126                                         .setPageToken(pt).execute();
127                         } else {
128                                 break;
129                         }
130                 }
131
132                 return res;
133         }
134
135         private String makeMessage(Event event) {
136                 Log.v(TAG, "Chosen event: " + event.getStatus() +
137                         " start " + event.getStart() +
138                         " end " + event.getEnd());
139                 return event.getId();
140         }
141 }