From 2aef2aad3ff9819c6aa6a1d55325f6ab8187062c Mon Sep 17 00:00:00 2001 From: Dmitry Kunin Date: Mon, 24 Feb 2014 19:14:20 +0300 Subject: [PATCH] GooglePlayLP draft. --- .../src/com/mapswithme/maps/MWMActivity.java | 4 + .../maps/location/LocationService.java | 95 ++++++++++++++++--- 2 files changed, 86 insertions(+), 13 deletions(-) diff --git a/android/src/com/mapswithme/maps/MWMActivity.java b/android/src/com/mapswithme/maps/MWMActivity.java index 160c288f4b..1a3972734d 100644 --- a/android/src/com/mapswithme/maps/MWMActivity.java +++ b/android/src/com/mapswithme/maps/MWMActivity.java @@ -578,6 +578,10 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService // We need check for tasks both in onCreate and onNewIntent // because of bug in OS: https://code.google.com/p/android/issues/detail?id=38629 addTask(intent); + + // Initialize location service + // TODO find better solution? + getLocationService(); } private void setUpDrawer() diff --git a/android/src/com/mapswithme/maps/location/LocationService.java b/android/src/com/mapswithme/maps/location/LocationService.java index c180406471..2c250f0116 100644 --- a/android/src/com/mapswithme/maps/location/LocationService.java +++ b/android/src/com/mapswithme/maps/location/LocationService.java @@ -22,7 +22,10 @@ import android.view.Display; import android.view.Surface; import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.GooglePlayServicesClient; import com.google.android.gms.common.GooglePlayServicesUtil; +import com.google.android.gms.location.LocationClient; +import com.google.android.gms.location.LocationRequest; import com.mapswithme.maps.MWMApplication; import com.mapswithme.util.ConnectionState; import com.mapswithme.util.Utils; @@ -31,7 +34,11 @@ import com.mapswithme.util.log.SimpleLogger; import com.mapswithme.util.statistics.Statistics; -public class LocationService implements LocationListener, SensorEventListener, WifiLocation.Listener +public class LocationService implements + LocationListener, SensorEventListener, WifiLocation.Listener, + GooglePlayServicesClient.ConnectionCallbacks, + GooglePlayServicesClient.OnConnectionFailedListener, + com.google.android.gms.location.LocationListener { private final Logger mLogger = SimpleLogger.get(this.toString()); @@ -81,16 +88,14 @@ public class LocationService implements LocationListener, SensorEventListener, W { if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(mApplication) == ConnectionResult.SUCCESS) { - // TODO use GP provider + mLocationProvider = new GoogleFusedLocationProvider(); mLogger.d("Using Google Provider"); } else { - // TODO Use default provider + mLocationProvider = new AndroidNativeLocationProvider(); mLogger.d("Using native location provider"); } - // TODO remove - mLocationProvider = new AndroidNativeLocationProvider(); } /// /// @@ -429,7 +434,6 @@ public class LocationService implements LocationListener, SensorEventListener, W protected abstract void setUp(); protected abstract void startUpdates(Listener l); protected abstract void stopUpdates(); - protected abstract Location getBestLastKnown(); // "Backward compatibility" protected abstract boolean isLocationBetter(Location location); @@ -513,13 +517,6 @@ public class LocationService implements LocationListener, SensorEventListener, W mIsActive = false; } - @Override - protected Location getBestLastKnown() - { - // TODO Unused? - return null; - } - @Override protected void setUp() { @@ -624,6 +621,78 @@ public class LocationService implements LocationListener, SensorEventListener, W // End of inner class AndroidNativeLocationProvider } + private class GoogleFusedLocationProvider extends LocationProvider + { + private LocationClient mLocationClient; + private LocationRequest mLocationRequest; + + @Override + protected void setUp() + { + mLocationClient = new LocationClient(mApplication, mHost, mHost); + mLocationClient.connect(); + + mLocationRequest = LocationRequest.create(); + mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); + mLocationRequest.setInterval(500); + mLocationRequest.setFastestInterval(250); + + mLogger.d("SetUp for GP provider."); + } + + @Override + protected void startUpdates(Listener l) + { + mLocationClient.requestLocationUpdates(mLocationRequest, mHost); + } + + @Override + protected void stopUpdates() + { + if (mLocationClient.isConnected()) + { + mLocationClient.removeLocationUpdates(mHost); + + // TODO when we disconnect? + //mLocationClient.disconnect(); + } + } + + @Override + protected boolean isLocationBetter(Location location) + { + // we belive that google services always returns good locations + return true; + } + + @Override + protected Location getLastGPSLocation() + { + // there is no possibility to return precise provider + return mLocationClient.getLastLocation(); + } + + // End of inner class GoogleFusedLP + } + // Google Play callbacks + + @Override + public void onConnectionFailed(ConnectionResult arg0) + { + mLogger.d("onConnectionFailed " + arg0); + } + + @Override + public void onConnected(Bundle arg0) + { + mLogger.d("onConnected " + arg0); + } + + @Override + public void onDisconnected() + { + mLogger.d("onDisconnected"); + } }