]> average.org Git - WhereAmI.git/commitdiff
WIP on Latitude API
authorEugene Crosser <Eugene.Crosser@ru.ibm.com>
Wed, 1 Feb 2012 18:51:50 +0000 (22:51 +0400)
committerEugene Crosser <Eugene.Crosser@ru.ibm.com>
Wed, 1 Feb 2012 18:51:50 +0000 (22:51 +0400)
.gitignore [new file with mode: 0644]
src/org/average/whereami/Authorize.java
src/org/average/whereami/CredentialStore.java
src/org/average/whereami/LastLocation.java [new file with mode: 0644]
src/org/average/whereami/Location.java [deleted file]
src/org/average/whereami/PhoneLog.java
src/org/average/whereami/WhereAmI.java

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..efeb4a2
--- /dev/null
@@ -0,0 +1,2 @@
+local.properties
+src/org/average/whereami/ClientCredentials.java
index 44a88a9b470d11943ed2c772d4311b91161a58e0..aa3b834bb0cca7ddd1b6b0a8bcbef86e686521c6 100644 (file)
@@ -11,9 +11,9 @@ import org.average.whereami.CredentialStore;
 import android.app.Activity;
 import android.os.Bundle;
 import android.graphics.Bitmap;
+import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
-import android.content.SharedPreferences.Editor;
 import android.preference.PreferenceManager;
 import android.util.Log;
 import android.view.View;
@@ -29,12 +29,7 @@ import com.google.api.client.json.jackson.JacksonFactory;
 public class Authorize extends Activity {
 
     final String TAG = getClass().getName();
-
-    private SharedPreferences prefs;
-    private static final String ACCESS_TOKEN = "access_token";
-    private static final String EXPIRES_IN = "expires_in";
-    private static final String REFRESH_TOKEN = "refresh_token";
-    private static final String SCOPE = "scope";
+    SharedPreferences prefs;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -74,18 +69,12 @@ public class Authorize extends Activity {
                                 code,
                                 ClientCredentials.REDIRECT_URI).execute();
                             CredentialStore credentialStore =
-                                               new CredentialStore(prefs);
+                                                    new CredentialStore(prefs);
                             credentialStore.write(accessTokenResponse);
                             finish();
-                            //view.setVisibility(View.INVISIBLE);
-                            //startActivity(new Intent(Authorize.this,
-                            //                         WhereAmI.class));
                         } else if (url.indexOf("error=")!=-1) {
                             new CredentialStore(prefs).clear();
                             finish();
-                            //view.setVisibility(View.INVISIBLE);
-                            //startActivity(new Intent(Authorize.this,
-                            //                         WhereAmI.class));
                         }
                     } catch (IOException e) {
                         e.printStackTrace();
index 2dcbffb2f6a8d9f2f4ae36ac951aabc7d841e090..7b1fd19ea6fa7d5fdf8606b322b3fb863176ed20 100644 (file)
@@ -1,5 +1,6 @@
 package org.average.whereami;
 
+import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 
@@ -44,4 +45,10 @@ public class CredentialStore {
         editor.remove(SCOPE);
         editor.commit();
     }
+
+    public void updateAccessToken(String accessToken) {
+        Editor editor = prefs.edit();
+        editor.putString(ACCESS_TOKEN,accessToken);
+        editor.commit();
+    }
 }
diff --git a/src/org/average/whereami/LastLocation.java b/src/org/average/whereami/LastLocation.java
new file mode 100644 (file)
index 0000000..52b9098
--- /dev/null
@@ -0,0 +1,74 @@
+package org.average.whereami;
+
+import org.average.whereami.CredentialStore;
+//import org.average.whereami.WhereAmIAccessResource;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+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.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.util.Log;
+
+public final class LastLocation extends Oracle {
+
+       final String TAG = getClass().getName();
+
+       private Latitude latitude;
+
+       public LastLocation(CredentialStore store) {
+               HttpTransport transport = new NetHttpTransport();
+               JsonFactory jsonFactory = new JacksonFactory();
+               AccessTokenResponse token = store.read();
+               GoogleAccessProtectedResource accessProtectedResource =
+                       new GoogleAccessProtectedResource(
+                               token.accessToken,
+                               transport, jsonFactory,
+                               ClientCredentials.CLIENT_ID,
+                               ClientCredentials.CLIENT_SECRET,
+                               token.refreshToken)
+                               //{
+                               //@Override
+                               //public void onAccessToken(String accessToken) {
+                               //      store.updateAccessToken(accessToken);
+                               //  }
+                               //}
+                               ;
+               Latitude.Builder lbldr = Latitude.builder(transport,
+                                                               jsonFactory);
+               lbldr.setHttpRequestInitializer(accessProtectedResource);
+               lbldr.setApplicationName("WhereAmI/1.0");
+               latitude = lbldr.build();
+       }
+
+       @Override
+       public final String getResult() {
+               try {
+                       Latitude.CurrentLocation.Get request =
+                               latitude.currentLocation().get();
+                       request.setGranularity("best");
+                       Location currentLocation = request.execute();
+                       return locationMessage(currentLocation);
+                } catch (Exception ex) {
+                       return ex.getMessage();
+                }
+       }
+
+       private String locationMessage(Location currentLocation) {
+               // lat = currentLocation.getLatitude();
+               // lon = currentLocation.getLongitude();
+               // tsm = currentLocation.getTimestampMs();
+               return "Current location: " + currentLocation;
+       }
+}
diff --git a/src/org/average/whereami/Location.java b/src/org/average/whereami/Location.java
deleted file mode 100644 (file)
index da9d6fb..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-package org.average.whereami;
-
-import com.google.api.client.extensions.android2.AndroidHttp;
-import com.google.api.client.googleapis.GoogleHeaders;
-import com.google.api.client.http.HttpRequest;
-import com.google.api.client.http.HttpResponse;
-import com.google.api.client.http.HttpResponseException;
-import com.google.api.client.http.HttpTransport;
-import com.google.api.client.util.DateTime;
-
-import android.util.Log;
-import android.os.SystemClock;
-
-public final class Location extends Oracle {
-
-       final String TAG = getClass().getName();
-
-       @Override
-       public final String getResult() {
-               SystemClock.sleep(6000);
-               return "Response from Location.getResult";
-       }
-}
index cf157a4b3cabb9ebfae70748d2a233cb6b262c26..020690b89367e0fa1b2c986d82ce4cde70d84b15 100644 (file)
@@ -1,5 +1,7 @@
 package org.average.whereami;
 
+import org.average.whereami.CredentialStore;
+
 import com.google.api.client.extensions.android2.AndroidHttp;
 import com.google.api.client.googleapis.GoogleHeaders;
 import com.google.api.client.http.HttpRequest;
@@ -17,11 +19,16 @@ import com.google.api.client.util.DateTime;
 
 import android.util.Log;
 import android.os.SystemClock;
+import android.content.Context;
+import android.content.SharedPreferences;
 
 public final class PhoneLog extends Oracle {
 
        final String TAG = getClass().getName();
 
+       public PhoneLog(CredentialStore store) {
+       }
+
        @Override
        public final String getResult() {
                SystemClock.sleep(5000);
index 16d11256c67ed5ca549eb9afdf3b772b0f27caea..e042505ed83dc163767ae736659ed56f0a99dac2 100644 (file)
@@ -3,7 +3,7 @@ package org.average.whereami;
 import org.average.whereami.WhereAmIprefs;
 import org.average.whereami.Oracle;
 import org.average.whereami.PhoneLog;
-import org.average.whereami.Location;
+import org.average.whereami.LastLocation;
 
 import android.app.Activity;
 import android.net.wifi.WifiManager;
@@ -38,6 +38,7 @@ public class WhereAmI extends Activity
     private Long updatedelay = 60000L;
     private Integer runningtasks = 0;
     private SharedPreferences prefs;
+    private CredentialStore store;
 
     private class UpdateTarget {
         private TextView tv;
@@ -196,6 +197,8 @@ public class WhereAmI extends Activity
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        prefs = PreferenceManager.getDefaultSharedPreferences(this);
+        store = new CredentialStore(prefs);
         wifiman = (WifiManager)getSystemService(Context.WIFI_SERVICE);
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
@@ -208,9 +211,9 @@ public class WhereAmI extends Activity
         tvs = (TextView)findViewById(R.id.timestamp);
        ut = new UpdateTarget[] {
             new UpdateTarget((TextView)findViewById(R.id.location),
-                             new PhoneLog()),
+                             new PhoneLog(store)),
             new UpdateTarget((TextView)findViewById(R.id.phonecall),
-                             new Location())
+                             new LastLocation(store))
         };
         Log.w(TAG, "created UI, about to start update task");
        mHandler.post(updateClock);
@@ -223,7 +226,6 @@ public class WhereAmI extends Activity
     @Override
     public void onResume() {
         super.onResume();
-        prefs = PreferenceManager.getDefaultSharedPreferences(this);
         boolean fullscreen = prefs.getBoolean("fullscreen", false);
         managewifi = prefs.getBoolean("managewifi", false);
         updatedelay = Long.parseLong(prefs.getString("updateperiod", "1200000"));