]> average.org Git - WhereAmI.git/commitdiff
Calendar API WIP
authorEugene Crosser <crosser@average.org>
Thu, 2 Feb 2012 22:43:36 +0000 (02:43 +0400)
committerEugene Crosser <crosser@average.org>
Thu, 2 Feb 2012 22:43:36 +0000 (02:43 +0400)
res/values/strings.xml
src/org/average/whereami/LastLocation.java
src/org/average/whereami/Oracle.java
src/org/average/whereami/PhoneLog.java

index fb98d5f7740dbd3493a0e6cc1453ab3a48de24b8..34b65b812138a45501e8aab77bcfbd3f2ce3cc2e 100644 (file)
@@ -7,6 +7,7 @@
     <string name="failure">Неудача</string>
     <string name="lasttry">Последнее обновление</string>
     <string name="failtry">Неудачная попытка</string>
+    <string name="autherror">Необходимо авторизоваться (через меню)</string>
     <string name="myname">Женя</string>
     <string name="timestamp">отметка</string>
     <string name="longago">больше суток назад</string>
index 98b7cf09fd84a9d22e75d2c458fd14471e3b695e..f911b6bd3e6412dc72281d3b74141204ae6b21fa 100644 (file)
@@ -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();
                 }
        }
 
index 4043451bce61cc35d10764bd7f3bfa501efae3e9..fc35c4feb98344450ee8a78561c140be2a7b98f2 100644 (file)
@@ -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 "<need to be overriden>";
+       }
 }
index dbb735357eaff4a7086edb4e51f3236a4dbebc11..fca10d6a2b006433bef1d89d580f22b17c90bf61 100644 (file)
@@ -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();
        }
 }