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