Skip to content

Commit

Permalink
Drop location code and startup permissions requests (#110)
Browse files Browse the repository at this point in the history
All code that worked with Location is dropped, because we are solely relying on the browser's Navigation service to obtain geolocation data. 
Drops code that requests or demands permissions on startup. Instead, Location permissions requests are triggered by cht-core, at runtime, when editing/creating reports. 

#111
medic/cht-core#5917
  • Loading branch information
SCdF authored Aug 19, 2020
1 parent 7c8a9d1 commit 412326d
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 338 deletions.
2 changes: 0 additions & 2 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
android:configChanges="orientation|screenSize"/>
<activity android:name="FreeSpaceWarningActivity"
android:screenOrientation="portrait"/>
<activity android:name="MmPromptForPermissionsActivity"
android:screenOrientation="portrait"/>
<activity android:name="SettingsDialogActivity"
android:screenOrientation="portrait"/>
<activity android:name="AppUrlIntentActivity" android:launchMode="singleInstance">
Expand Down
166 changes: 0 additions & 166 deletions src/main/java/medic/android/PromptForPermissionsActivity.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package org.medicmobile.webapp.mobile;

import android.Manifest.permission;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.app.ActivityManager;
import android.net.Uri;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.Menu;
Expand All @@ -19,6 +17,7 @@
import android.view.Window;
import android.webkit.ValueCallback;
import android.widget.Toast;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;

import java.io.ByteArrayInputStream;
import java.util.Collections;
Expand All @@ -32,7 +31,6 @@
import org.xwalk.core.XWalkWebResourceRequest;
import org.xwalk.core.XWalkWebResourceResponse;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static java.lang.Boolean.parseBoolean;
import static org.medicmobile.webapp.mobile.BuildConfig.DEBUG;
import static org.medicmobile.webapp.mobile.BuildConfig.DISABLE_APP_URL_VALIDATION;
Expand All @@ -51,8 +49,7 @@ public class EmbeddedBrowserActivity extends LockableActivity {
static final int GRAB_PHOTO = (0 << 3) | NON_SIMPRINTS_FLAGS;
static final int GRAB_MRDT_PHOTO = (1 << 3) | NON_SIMPRINTS_FLAGS;

private static final long FIVE_MINS = 5 * 60 * 1000;
private static final float ANY_DISTANCE = 0f;
private final static int ACCESS_FINE_LOCATION_PERMISSION_REQUEST = (int)Math.random();

private static final ValueCallback<String> IGNORE_RESULT = new ValueCallback<String>() {
public void onReceiveValue(String result) { /* ignore */ }
Expand Down Expand Up @@ -108,7 +105,6 @@ public void onReceiveValue(String result) {

configureUseragent();

enableLocationUpdates();
setUpUiClient(container);
enableRemoteChromeDebugging();
enableJavascript(container);
Expand Down Expand Up @@ -326,74 +322,39 @@ public void onGeolocationPermissionsShowPrompt(
});
}

public boolean getLocationPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PERMISSION_GRANTED) {
return true;
}

String[] permissions = { Manifest.permission.ACCESS_FINE_LOCATION };
ActivityCompat.requestPermissions(this, permissions, ACCESS_FINE_LOCATION_PERMISSION_REQUEST);
return false;
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode != ACCESS_FINE_LOCATION_PERMISSION_REQUEST) {
return;
}
String javaScript = "angular.element(document.body).injector().get('AndroidApi').v1.locationPermissionRequestResolved();";
evaluateJavascript(String.format(javaScript));
}

@SuppressLint("SetJavaScriptEnabled")
private void enableJavascript(XWalkView container) {
container.getSettings().setJavaScriptEnabled(true);

MedicAndroidJavascript maj = new MedicAndroidJavascript(this);
maj.setAlert(new Alert(this));

maj.setLocationManager((LocationManager) this.getSystemService(Context.LOCATION_SERVICE));

maj.setActivityManager((ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE));

maj.setConnectivityManager((ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE));

container.addJavascriptInterface(maj, "medicmobile_android");
}

/**
* Make the app poll the location providers periodically. This should mean that calls
* to MedicAndroidJavascript.getLocation() are reasonably up-to-date, and an initial
* location is likely to have been resolved by the time the user first fills a form and
* getLocation() is called. However, longer-term we are likely to want a more explicit
* async getLocation() implementation which is triggered only when needed.
* @see https://github.com/medic/medic-projects/issues/2629
* @deprecated @see https://github.com/medic/medic-webapp/issues/3781
*/
@Deprecated
private void enableLocationUpdates() {
if(ContextCompat.checkSelfPermission(this, permission.ACCESS_FINE_LOCATION) != PERMISSION_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 == null) {
log("EmbeddedBrowserActivity.enableLocationUpdates() :: Cannot enable location updates: LOCATION_SERVICE could not be fetched.");
return;
}

requestLocationUpdates(m, LocationManager.GPS_PROVIDER);
requestLocationUpdates(m, LocationManager.NETWORK_PROVIDER);
}

private void requestLocationUpdates(LocationManager m, String locationProvider) {
try {
if(m.isProviderEnabled(locationProvider)) {
// Method bodies are empty because we need the location updates to be running constantly so
// that recent location can be requested when required from Javascript.
m.requestLocationUpdates(locationProvider, FIVE_MINS, ANY_DISTANCE, new LocationListener() {
@SuppressWarnings("PMD.UncommentedEmptyMethodBody")
public void onLocationChanged(Location location) {}
@SuppressWarnings("PMD.UncommentedEmptyMethodBody")
public void onProviderDisabled(String provider) {}
@SuppressWarnings("PMD.UncommentedEmptyMethodBody")
public void onProviderEnabled(String provider) {}
@SuppressWarnings("PMD.UncommentedEmptyMethodBody")
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) {
XWalkSettings settings = container.getSettings();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package org.medicmobile.webapp.mobile;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.app.DatePickerDialog;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
Expand Down Expand Up @@ -47,7 +43,6 @@ public class MedicAndroidJavascript {
private final MrdtSupport mrdt;
private final SmsSender smsSender;

private LocationManager locationManager;
private ActivityManager activityManager;
private ConnectivityManager connectivityManager;
private Alert soundAlert;
Expand All @@ -63,10 +58,6 @@ public void setAlert(Alert soundAlert) {
this.soundAlert = soundAlert;
}

public void setLocationManager(LocationManager locationManager) {
this.locationManager = locationManager;
}

public void setActivityManager(ActivityManager activityManager) {
this.activityManager = activityManager;
}
Expand Down Expand Up @@ -122,32 +113,10 @@ private JSONObject getDataUsage(long rx, long tx) throws JSONException {
.put("tx", tx);
}

@Deprecated
@org.xwalk.core.JavascriptInterface
@android.webkit.JavascriptInterface
@SuppressLint("MissingPermission") // handled by catch(Exception)
/**
* @deprecated Location should be fetched directly from the browser.
* @see https://github.com/medic/medic-webapp/issues/3781
*/
public String getLocation() {
try {
if(locationManager == null) return jsonError("LocationManager not set. Cannot retrieve location.");

String provider = locationManager.getBestProvider(new Criteria(), true);
if(provider == null) return jsonError("No location provider available.");

Location loc = locationManager.getLastKnownLocation(provider);

if(loc == null) return jsonError("Provider '" + provider + "' did not provide a location.");

return new JSONObject()
.put("lat", loc.getLatitude())
.put("long", loc.getLongitude())
.toString();
} catch(Exception ex) {
return jsonError("Problem fetching location: ", ex);
}
public boolean getLocationPermissions() {
return this.parent.getLocationPermissions();
}

@org.xwalk.core.JavascriptInterface
Expand Down
Loading

0 comments on commit 412326d

Please sign in to comment.