From da2ed82ee19ce7d5e1ff2386d87952f36e97d8b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20M=C5=82odawski?= Date: Wed, 8 Feb 2023 10:26:56 +0000 Subject: [PATCH] Reapply patches from https://github.com/restaumatic/cordova-plugin-background-geolocation --- .../bgloc/BackgroundGeolocationFacade.java | 23 ++++++++++++------- .../marianhello/bgloc/LocationManager.java | 15 +++++++----- .../ActivityRecognitionLocationProvider.java | 12 +++++++++- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/android/common/src/main/java/com/marianhello/bgloc/BackgroundGeolocationFacade.java b/android/common/src/main/java/com/marianhello/bgloc/BackgroundGeolocationFacade.java index 87f332cb..86ea66f0 100644 --- a/android/common/src/main/java/com/marianhello/bgloc/BackgroundGeolocationFacade.java +++ b/android/common/src/main/java/com/marianhello/bgloc/BackgroundGeolocationFacade.java @@ -18,6 +18,7 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager; import android.text.TextUtils; +import java.util.ArrayList; import com.github.jparkie.promise.Promise; import com.intentfilter.androidpermissions.PermissionManager; import com.intentfilter.androidpermissions.models.DeniedPermissions; @@ -52,12 +53,6 @@ public class BackgroundGeolocationFacade { public static final int SERVICE_STOPPED = 0; public static final int AUTHORIZATION_AUTHORIZED = 1; public static final int AUTHORIZATION_DENIED = 0; - - public static final String[] PERMISSIONS = { - Manifest.permission.ACCESS_COARSE_LOCATION, - Manifest.permission.ACCESS_FINE_LOCATION, - }; - private boolean mServiceBroadcastReceiverRegistered = false; private boolean mLocationModeChangeReceiverRegistered = false; private boolean mIsPaused = false; @@ -216,7 +211,13 @@ public void start() { logger.debug("Starting service"); PermissionManager permissionManager = PermissionManager.getInstance(getContext()); - permissionManager.checkPermissions(Arrays.asList(PERMISSIONS), new PermissionManager.PermissionRequestListener() { + ArrayList permissions = new ArrayList(); + permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION); + permissions.add(Manifest.permission.ACCESS_FINE_LOCATION); + if (Build.VERSION.SDK_INT >= 29){ + permissions.add(Manifest.permission.ACTIVITY_RECOGNITION); + } + permissionManager.checkPermissions(permissions, new PermissionManager.PermissionRequestListener() { @Override public void onPermissionGranted() { logger.info("User granted requested permissions"); @@ -422,7 +423,13 @@ public int getAuthorizationStatus() { } public boolean hasPermissions() { - return hasPermissions(getContext(), PERMISSIONS); + ArrayList permissions = new ArrayList(); + permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION); + permissions.add(Manifest.permission.ACCESS_FINE_LOCATION); + if (Build.VERSION.SDK_INT >= 29){ + permissions.add(Manifest.permission.ACTIVITY_RECOGNITION); + } + return hasPermissions(getContext(), permissions.toArray(new String[permissions.size()])); } public boolean locationServicesEnabled() throws PluginException { diff --git a/android/common/src/main/java/com/marianhello/bgloc/LocationManager.java b/android/common/src/main/java/com/marianhello/bgloc/LocationManager.java index d377591b..70ea4a7a 100644 --- a/android/common/src/main/java/com/marianhello/bgloc/LocationManager.java +++ b/android/common/src/main/java/com/marianhello/bgloc/LocationManager.java @@ -7,8 +7,10 @@ import android.location.Location; import android.location.LocationListener; import android.os.Bundle; +import android.os.Build; import android.os.Looper; +import java.util.ArrayList; import com.github.jparkie.promise.Promise; import com.github.jparkie.promise.Promises; import com.intentfilter.androidpermissions.PermissionManager; @@ -23,11 +25,6 @@ public class LocationManager { private Context mContext; private static LocationManager mLocationManager; - public static final String[] PERMISSIONS = { - Manifest.permission.ACCESS_COARSE_LOCATION, - Manifest.permission.ACCESS_FINE_LOCATION - }; - private LocationManager(Context context) { mContext = context; } @@ -45,7 +42,13 @@ public Promise getCurrentLocation(final int timeout, final long maximu final Promise promise = Promises.promise(); PermissionManager permissionManager = PermissionManager.getInstance(mContext); - permissionManager.checkPermissions(Arrays.asList(PERMISSIONS), new PermissionManager.PermissionRequestListener() { + ArrayList permissions = new ArrayList(); + permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION); + permissions.add(Manifest.permission.ACCESS_FINE_LOCATION); + if (Build.VERSION.SDK_INT >= 29){ + permissions.add(Manifest.permission.ACTIVITY_RECOGNITION); + } + permissionManager.checkPermissions(permissions, new PermissionManager.PermissionRequestListener() { @Override public void onPermissionGranted() { try { diff --git a/android/common/src/main/java/com/marianhello/bgloc/provider/ActivityRecognitionLocationProvider.java b/android/common/src/main/java/com/marianhello/bgloc/provider/ActivityRecognitionLocationProvider.java index 2d185ded..19c2f42d 100644 --- a/android/common/src/main/java/com/marianhello/bgloc/provider/ActivityRecognitionLocationProvider.java +++ b/android/common/src/main/java/com/marianhello/bgloc/provider/ActivityRecognitionLocationProvider.java @@ -33,6 +33,7 @@ public class ActivityRecognitionLocationProvider extends AbstractLocationProvide private static final String TAG = ActivityRecognitionLocationProvider.class.getSimpleName(); private static final String P_NAME = " com.marianhello.bgloc"; private static final String DETECTED_ACTIVITY_UPDATE = P_NAME + ".DETECTED_ACTIVITY_UPDATE"; + private static final int LONGEST_INTERVAL = 120000; private GoogleApiClient googleApiClient; private PendingIntent detectedActivitiesPI; @@ -41,6 +42,7 @@ public class ActivityRecognitionLocationProvider extends AbstractLocationProvide private boolean isTracking = false; private boolean isWatchingActivity = false; private Location lastLocation; + private long lastLocationTime; private DetectedActivity lastActivity = new DetectedActivity(DetectedActivity.UNKNOWN, 100); public ActivityRecognitionLocationProvider(Context context) { @@ -104,6 +106,7 @@ public void onLocationChanged(Location location) { showDebugToast("acy:" + location.getAccuracy() + ",v:" + location.getSpeed()); lastLocation = location; + lastLocationTime = System.currentTimeMillis(); handleLocation(location); } @@ -246,6 +249,10 @@ private class DetectedActivitiesReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent); + if (result == null) { + logger.error("ActivityRecognitionResult is null"); + return; + } ArrayList detectedActivities = (ArrayList) result.getProbableActivities(); //Find the activity with the highest percentage @@ -255,7 +262,10 @@ public void onReceive(Context context, Intent intent) { handleActivity(lastActivity); - if (lastActivity.getType() == DetectedActivity.STILL) { + if (lastLocationTime + LONGEST_INTERVAL < System.currentTimeMillis()) { + showDebugToast("Detected TIMEOUT Activity"); + startTracking(); + } else if (lastActivity.getType() == DetectedActivity.STILL) { showDebugToast("Detected STILL Activity"); // stopTracking(); // we will delay stop tracking after position is found