From bfc8f9db47cf00565026d44bc27d84def51f4a94 Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Wed, 1 Feb 2012 22:51:50 +0400 Subject: [PATCH] WIP on Latitude API --- .gitignore | 2 + src/org/average/whereami/Authorize.java | 17 +---- src/org/average/whereami/CredentialStore.java | 7 ++ src/org/average/whereami/LastLocation.java | 74 +++++++++++++++++++ src/org/average/whereami/Location.java | 23 ------ src/org/average/whereami/PhoneLog.java | 7 ++ src/org/average/whereami/WhereAmI.java | 10 ++- 7 files changed, 99 insertions(+), 41 deletions(-) create mode 100644 .gitignore create mode 100644 src/org/average/whereami/LastLocation.java delete mode 100644 src/org/average/whereami/Location.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..efeb4a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +local.properties +src/org/average/whereami/ClientCredentials.java diff --git a/src/org/average/whereami/Authorize.java b/src/org/average/whereami/Authorize.java index 44a88a9..aa3b834 100644 --- a/src/org/average/whereami/Authorize.java +++ b/src/org/average/whereami/Authorize.java @@ -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(); diff --git a/src/org/average/whereami/CredentialStore.java b/src/org/average/whereami/CredentialStore.java index 2dcbffb..7b1fd19 100644 --- a/src/org/average/whereami/CredentialStore.java +++ b/src/org/average/whereami/CredentialStore.java @@ -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 index 0000000..52b9098 --- /dev/null +++ b/src/org/average/whereami/LastLocation.java @@ -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 index da9d6fb..0000000 --- a/src/org/average/whereami/Location.java +++ /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"; - } -} diff --git a/src/org/average/whereami/PhoneLog.java b/src/org/average/whereami/PhoneLog.java index cf157a4..020690b 100644 --- a/src/org/average/whereami/PhoneLog.java +++ b/src/org/average/whereami/PhoneLog.java @@ -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); diff --git a/src/org/average/whereami/WhereAmI.java b/src/org/average/whereami/WhereAmI.java index 16d1125..e042505 100644 --- a/src/org/average/whereami/WhereAmI.java +++ b/src/org/average/whereami/WhereAmI.java @@ -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")); -- 2.43.0