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