Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
pmlodawski committed Aug 2, 2024
1 parent f355edd commit da2ed82
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> permissions = new ArrayList<String>();
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");
Expand Down Expand Up @@ -422,7 +423,13 @@ public int getAuthorizationStatus() {
}

public boolean hasPermissions() {
return hasPermissions(getContext(), PERMISSIONS);
ArrayList<String> permissions = new ArrayList<String>();
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -45,7 +42,13 @@ public Promise<Location> getCurrentLocation(final int timeout, final long maximu
final Promise<Location> promise = Promises.promise();

PermissionManager permissionManager = PermissionManager.getInstance(mContext);
permissionManager.checkPermissions(Arrays.asList(PERMISSIONS), new PermissionManager.PermissionRequestListener() {
ArrayList<String> permissions = new ArrayList<String>();
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -104,6 +106,7 @@ public void onLocationChanged(Location location) {
showDebugToast("acy:" + location.getAccuracy() + ",v:" + location.getSpeed());

lastLocation = location;
lastLocationTime = System.currentTimeMillis();
handleLocation(location);
}

Expand Down Expand Up @@ -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<DetectedActivity> detectedActivities = (ArrayList) result.getProbableActivities();

//Find the activity with the highest percentage
Expand All @@ -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
Expand Down

0 comments on commit da2ed82

Please sign in to comment.