diff options
author | Chris Warrington <cmw@google.com> | 2016-10-18 12:29:21 +0100 |
---|---|---|
committer | Chris Warrington <cmw@google.com> | 2016-10-18 12:34:18 +0100 |
commit | e3780081075c01aa1dff6d1f373cb43192b33e68 (patch) | |
tree | fb734615933a39f3d009210dc0d1457160479b35 /libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/helpers/LocationHelper.java | |
parent | 7e05eb7e57827eddc885570bc00aed8a50320dbf (diff) | |
parent | 025b8b226c8d8edba2b309ca878572f40512eca7 (diff) | |
download | gradle-perf-android-medium-mirror-goog-studio-master-dev.tar.gz |
Merge remote-tracking branch 'origin/upstream-master' into masterHEADstudio-3.4.0studio-3.2.1studio-3.1.2studio-3.0studio-2.3gradle_3.4.0gradle_3.1.2gradle_3.0.0gradle_2.3.0studio-master-devmirror-goog-studio-master-devmastermain
Change-Id: I63f5e16d09297c48432192761b840310935eb903
Diffstat (limited to 'libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/helpers/LocationHelper.java')
-rw-r--r-- | libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/helpers/LocationHelper.java | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/helpers/LocationHelper.java b/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/helpers/LocationHelper.java new file mode 100644 index 000000000..ff472c2ed --- /dev/null +++ b/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/helpers/LocationHelper.java @@ -0,0 +1,144 @@ +//This Handy-Dandy class acquired and tweaked from http://stackoverflow.com/a/3145655/309558 +package org.wordpress.android.util.helpers; + +import android.annotation.SuppressLint; +import android.app.Activity; +import android.content.Context; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; +import android.os.Bundle; + +import java.util.Timer; +import java.util.TimerTask; + +public class LocationHelper { + Timer mTimer; + LocationManager mLocationManager; + LocationResult mLocationResult; + boolean mGpsEnabled = false; + boolean mNetworkEnabled = false; + + @SuppressLint("MissingPermission") + public boolean getLocation(Activity activity, LocationResult result) { + mLocationResult = result; + if (mLocationManager == null) { + mLocationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE); + } + + // exceptions will be thrown if provider is not permitted. + try { + mGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); + } catch (Exception ex) { + } + try { + mNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); + } catch (Exception ex) { + } + + // don't start listeners if no provider is enabled + if (!mGpsEnabled && !mNetworkEnabled) { + return false; + } + + if (mGpsEnabled) { + mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps); + } + + if (mNetworkEnabled) { + mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork); + } + + mTimer = new Timer(); + mTimer.schedule(new GetLastLocation(), 30000); + return true; + } + + LocationListener locationListenerGps = new LocationListener() { + @SuppressLint("MissingPermission") + public void onLocationChanged(Location location) { + mTimer.cancel(); + mLocationResult.gotLocation(location); + mLocationManager.removeUpdates(this); + mLocationManager.removeUpdates(locationListenerNetwork); + } + + public void onProviderDisabled(String provider) { + } + + public void onProviderEnabled(String provider) { + } + + public void onStatusChanged(String provider, int status, Bundle extras) { + } + }; + + LocationListener locationListenerNetwork = new LocationListener() { + @SuppressLint("MissingPermission") + public void onLocationChanged(Location location) { + mTimer.cancel(); + mLocationResult.gotLocation(location); + mLocationManager.removeUpdates(this); + mLocationManager.removeUpdates(locationListenerGps); + } + + public void onProviderDisabled(String provider) { + } + + public void onProviderEnabled(String provider) { + } + + public void onStatusChanged(String provider, int status, Bundle extras) { + } + }; + + class GetLastLocation extends TimerTask { + @Override + @SuppressLint("MissingPermission") + public void run() { + mLocationManager.removeUpdates(locationListenerGps); + mLocationManager.removeUpdates(locationListenerNetwork); + + Location net_loc = null, gps_loc = null; + if (mGpsEnabled) { + gps_loc = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); + } + if (mNetworkEnabled) { + net_loc = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); + } + + // if there are both values use the latest one + if (gps_loc != null && net_loc != null) { + if (gps_loc.getTime() > net_loc.getTime()) { + mLocationResult.gotLocation(gps_loc); + } else { + mLocationResult.gotLocation(net_loc); + } + return; + } + + if (gps_loc != null) { + mLocationResult.gotLocation(gps_loc); + return; + } + if (net_loc != null) { + mLocationResult.gotLocation(net_loc); + return; + } + mLocationResult.gotLocation(null); + } + } + + public static abstract class LocationResult { + public abstract void gotLocation(Location location); + } + + @SuppressLint("MissingPermission") + public void cancelTimer() { + if (mTimer != null) { + mTimer.cancel(); + mLocationManager.removeUpdates(locationListenerGps); + mLocationManager.removeUpdates(locationListenerNetwork); + } + } +} |