Skip to content

Commit

Permalink
Tighten up location manager setup
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Jun 28, 2018
1 parent 7924b5d commit 0d2aadd
Showing 1 changed file with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import org.xwalk.core.XWalkView;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.location.LocationManager.GPS_PROVIDER;
import static android.location.LocationManager.NETWORK_PROVIDER;
import static java.lang.Boolean.parseBoolean;
import static org.medicmobile.webapp.mobile.BuildConfig.APPLICATION_ID;
import static org.medicmobile.webapp.mobile.BuildConfig.DEBUG;
Expand Down Expand Up @@ -306,29 +304,38 @@ private void enableJavascript(XWalkView container) {
@Deprecated
private void enableLocationUpdates() {
if(ContextCompat.checkSelfPermission(this, permission.ACCESS_FINE_LOCATION) != PERMISSION_GRANTED) {
log("Cannot enable location updates: permission ACCESS_FINE_LOCATION not granted.");
log("EmbeddedBrowserActivity.enableLocationUpdates() :: Cannot enable location updates: permission ACCESS_FINE_LOCATION not granted.");
return;
}

LocationManager m = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

if(m.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
m.requestLocationUpdates(GPS_PROVIDER, FIVE_MINS, ANY_DISTANCE, new LocationListener() {
public void onLocationChanged(Location location) {}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
});
} else {
log("Cannot get GPS updates: GPS not enabled or phone does not have GPS.");
if(m == null) {
log("EmbeddedBrowserActivity.enableLocationUpdates() :: Cannot enable location updates: LOCATION_SERVICE could not be fetched.");
return;
}

m.requestLocationUpdates(NETWORK_PROVIDER, FIVE_MINS, ANY_DISTANCE, new LocationListener() {
public void onLocationChanged(Location location) {}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
});
requestLocationUpdates(m, LocationManager.GPS_PROVIDER);
requestLocationUpdates(m, LocationManager.NETWORK_PROVIDER);
}

private void requestLocationUpdates(LocationManager m, String locationProvider) {
try {
if(m.isProviderEnabled(locationProvider)) {
m.requestLocationUpdates(locationProvider, FIVE_MINS, ANY_DISTANCE, new LocationListener() {
public void onLocationChanged(Location location) {}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
});
} else {
log("EmbeddedBrowserActivity.requestLocationUpdates(%s) :: Cannot get updates: not enabled or phone does not have this feature.",
locationProvider);
}
} catch(SecurityException ex) {
log(ex, "EmbeddedBrowserActivity.requestLocationUpdates(%s) :: Exception thrown while checking provider.",
locationProvider);
}
}

private void enableStorage(XWalkView container) {
Expand Down

0 comments on commit 0d2aadd

Please sign in to comment.