From: Eugene Crosser Date: Thu, 2 Feb 2012 22:43:36 +0000 (+0400) Subject: Calendar API WIP X-Git-Url: http://average.org/gitweb/?a=commitdiff_plain;h=fc90003666d07349f68ef0382b1e318a3ff2a3d3;p=WhereAmI.git Calendar API WIP --- diff --git a/res/values/strings.xml b/res/values/strings.xml index fb98d5f..34b65b8 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -7,6 +7,7 @@ Неудача Последнее обновление Неудачная попытка + Необходимо авторизоваться (через меню) Женя отметка больше суток назад diff --git a/src/org/average/whereami/LastLocation.java b/src/org/average/whereami/LastLocation.java index 98b7cf0..f911b6b 100644 --- a/src/org/average/whereami/LastLocation.java +++ b/src/org/average/whereami/LastLocation.java @@ -3,21 +3,15 @@ package org.average.whereami; import org.average.whereami.CredentialStore; import java.lang.Math; +import java.io.IOException; -import com.google.api.client.extensions.android2.AndroidHttp; -import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse; -import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpResponseException; -import com.google.api.client.http.HttpTransport; -import com.google.api.client.http.javanet.NetHttpTransport; -import com.google.api.client.json.JsonFactory; -import com.google.api.client.json.jackson.JacksonFactory; import com.google.api.services.latitude.Latitude; import com.google.api.services.latitude.LatitudeRequest; import com.google.api.services.latitude.model.Location; import android.content.Context; -import android.content.SharedPreferences; import android.content.res.Resources; import android.text.format.Time; import android.util.Log; @@ -41,6 +35,7 @@ public final class LastLocation extends Oracle { private String hours5up; public LastLocation(final Resources res, final CredentialStore store) { + super(res, store); loc_names = res.getStringArray(R.array.loc_names); loc_lats = res.getStringArray(R.array.loc_lats); loc_lons = res.getStringArray(R.array.loc_lons); @@ -53,37 +48,9 @@ public final class LastLocation extends Oracle { hours2to4 = res.getString(R.string.hours2to4); hours5up = res.getString(R.string.hours5up); - HttpTransport transport = AndroidHttp.newCompatibleTransport(); - JsonFactory jsonFactory = new JacksonFactory(); - AccessTokenResponse token = store.read(); - Log.v(TAG, "tokens - access: \"" + token.accessToken + - "\", refresh: \"" + token.refreshToken + - "\", client_id: \"" + ClientCredentials.CLIENT_ID + - "\", client_secret: \"" + - ClientCredentials.CLIENT_SECRET + - "\""); - GoogleAccessProtectedResource accessProtectedResource = - new GoogleAccessProtectedResource( - token.accessToken, - transport, jsonFactory, - ClientCredentials.CLIENT_ID, - ClientCredentials.CLIENT_SECRET, - token.refreshToken) - { - @Override - public void onAccessToken( - String accessToken) { - Log.v(TAG, "Update access token to \"" + accessToken + "\""); - store.updateAccessToken( - accessToken); - } - } - ; - Latitude.Builder lbldr = Latitude.builder(transport, - jsonFactory); - lbldr.setHttpRequestInitializer(accessProtectedResource); - lbldr.setApplicationName("WhereAmI/1.0"); - latitude = lbldr.build(); + latitude = Latitude.builder(httpTransport, jsonFactory) + .setHttpRequestInitializer(accessProtectedResource) + .setApplicationName("WhereAmI/1.0").build(); } @Override @@ -101,10 +68,19 @@ public final class LastLocation extends Oracle { Long tsm = Long.parseLong( cloc.getTimestampMs().toString()); return locationMessage(lat, lon, tsm); - } catch (Exception ex) { - Log.v(TAG, "exception in getResult: " + ex); - ex.printStackTrace(); - return ex.getMessage(); + } catch (GoogleJsonResponseException e) { + Log.v(TAG, "GoogleJsonResponseException: " + e); + if (e.getResponse().getStatusCode() == 401) { + return authErrorMessage; + } else { + return e.getMessage(); + } + } catch (HttpResponseException e) { + Log.v(TAG, "HttpResponseException: " + e); + return e.getMessage(); + } catch (IOException e) { + e.printStackTrace(); + return e.getMessage(); } } diff --git a/src/org/average/whereami/Oracle.java b/src/org/average/whereami/Oracle.java index 4043451..fc35c4f 100644 --- a/src/org/average/whereami/Oracle.java +++ b/src/org/average/whereami/Oracle.java @@ -1,5 +1,58 @@ package org.average.whereami; -abstract public class Oracle { - abstract String getResult(); +import org.average.whereami.CredentialStore; + +import com.google.api.client.extensions.android2.AndroidHttp; +import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse; +import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource; +import com.google.api.client.http.HttpResponseException; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson.JacksonFactory; + +import android.content.res.Resources; +import android.util.Log; + +public class Oracle { + + final String TAG = getClass().getName(); + + public HttpTransport httpTransport = AndroidHttp.newCompatibleTransport(); + public JsonFactory jsonFactory = new JacksonFactory(); + + public GoogleAccessProtectedResource accessProtectedResource; + + public String authErrorMessage; + + public Oracle(final Resources res, final CredentialStore store) { + authErrorMessage = res.getString(R.string.autherror); + + AccessTokenResponse token = store.read(); + Log.v(TAG, "tokens - access: \"" + token.accessToken + + "\", refresh: \"" + token.refreshToken + + "\", client_id: \"" + ClientCredentials.CLIENT_ID + + "\", client_secret: \"" + + ClientCredentials.CLIENT_SECRET + + "\""); + accessProtectedResource = new GoogleAccessProtectedResource( + token.accessToken, httpTransport, jsonFactory, + ClientCredentials.CLIENT_ID, + ClientCredentials.CLIENT_SECRET, + token.refreshToken) + { + @Override + public void onAccessToken( + String accessToken) { + Log.v(TAG, "Update access token to \"" + + accessToken + "\""); + store.updateAccessToken( + accessToken); + } + }; + } + + public String getResult() { + return ""; + } } diff --git a/src/org/average/whereami/PhoneLog.java b/src/org/average/whereami/PhoneLog.java index dbb7353..fca10d6 100644 --- a/src/org/average/whereami/PhoneLog.java +++ b/src/org/average/whereami/PhoneLog.java @@ -2,16 +2,10 @@ package org.average.whereami; import org.average.whereami.CredentialStore; -//import java.lang.Long; +import java.io.IOException; -import com.google.api.client.extensions.android2.AndroidHttp; -import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse; -import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpResponseException; -import com.google.api.client.http.HttpTransport; -import com.google.api.client.http.javanet.NetHttpTransport; -import com.google.api.client.json.JsonFactory; -import com.google.api.client.json.jackson.JacksonFactory; import com.google.api.client.util.DateTime; import com.google.api.services.calendar.Calendar; import com.google.api.services.calendar.CalendarRequest; @@ -23,7 +17,6 @@ import com.google.common.collect.Lists; import android.content.Context; import android.content.res.Resources; -import android.content.SharedPreferences; import android.util.Log; public final class PhoneLog extends Oracle { @@ -32,49 +25,111 @@ public final class PhoneLog extends Oracle { private Calendar calendar; + private String calendar_name = "Call Log"; + private String calendar_id = null; + private String peer_name = "Alla Cherkashina"; + public PhoneLog(final Resources res, final CredentialStore store) { - HttpTransport transport = AndroidHttp.newCompatibleTransport(); - JsonFactory jsonFactory = new JacksonFactory(); - AccessTokenResponse token = store.read(); - Log.v(TAG, "tokens - access: \"" + token.accessToken + - "\", refresh: \"" + token.refreshToken + - "\", client_id: \"" + ClientCredentials.CLIENT_ID + - "\", client_secret: \"" + - ClientCredentials.CLIENT_SECRET + - "\""); - GoogleAccessProtectedResource accessProtectedResource = - new GoogleAccessProtectedResource( - token.accessToken, - transport, jsonFactory, - ClientCredentials.CLIENT_ID, - ClientCredentials.CLIENT_SECRET, - token.refreshToken) - { - @Override - public void onAccessToken( - String accessToken) { - Log.v(TAG, "Update access token to \"" + accessToken + "\""); - store.updateAccessToken( - accessToken); - } - } - ; - Calendar.Builder cbldr = Calendar.builder(transport, - jsonFactory); - cbldr.setHttpRequestInitializer(accessProtectedResource); - cbldr.setApplicationName("WhereAmI/1.0"); - calendar = cbldr.build(); + super(res, store); + + calendar = Calendar.builder(httpTransport, jsonFactory) + .setHttpRequestInitializer(accessProtectedResource) + .setApplicationName("WhereAmI/1.0").build(); } @Override public final String getResult() { try { Log.v(TAG, "entering getResult"); - return TAG + " complete"; - } catch (Exception ex) { - Log.v(TAG, "exception in getResult: " + ex); - ex.printStackTrace(); - return ex.getMessage(); - } + if (calendar_id == null) { + calendar_id = scanCalendars(calendar_name); + } + return scanEvents(calendar_id, peer_name); + } catch (GoogleJsonResponseException e) { + Log.v(TAG, "GoogleJsonResponseException: " + e); + if (e.getResponse().getStatusCode() == 401) { + return authErrorMessage; + } else { + return e.getMessage(); + } + } catch (HttpResponseException e) { + Log.v(TAG, "HttpResponseException: " + e); + return e.getMessage(); + } catch (IOException e) { + e.printStackTrace(); + return e.getMessage(); + } + } + + private String scanCalendars(String calendar_name) + throws java.io.IOException { + String res = ""; + Log.v(TAG, "Entering scanCalendars for \"" + + calendar_name + "\""); + + CalendarList cl = calendar.calendarList().list().execute(); + while (true) { + String pt = cl.getNextPageToken(); + Log.v(TAG, "got cl with pt=" + pt); + for (CalendarListEntry le : cl.getItems()) { + Log.v(TAG, "entry " + le.getId() + ":\"" + + le.getSummary() + "\""); + if (le.getSummary().equals(calendar_name)) { + res = le.getId(); + //break; // get all in the log + } + } + if (pt != null && pt != "") { + cl = calendar.calendarList().list() + .setPageToken(pt).execute(); + } else { + break; + } + } + + return res; + } + + 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 + "\""); + + Events events = calendar.events().list(calendar_id) + //.setOrderBy("starttime") + //.setSortOrder("descending") // this does not exist + .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); + } + } + if (count < 100 && pt != null && pt != "") { + events = calendar.events().list(calendar_id) + .setPageToken(pt).execute(); + } else { + break; + } + } + + return res; + } + + private String makeMessage(Event event) { + Log.v(TAG, "Chosen event: " + event.getStatus() + + " start " + event.getStart() + + " end " + event.getEnd()); + return event.getId(); } }