[ShareLocation] Fixed cancelation intent

Fixed the intent to cancel an ongoing sharing, which is now
delivered properly.
This commit is contained in:
Alexander Dörflinger
2025-03-25 16:24:16 +01:00
parent eab7153ea1
commit aecbaf91c8
4 changed files with 11 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ package com.aldo.apps.familyhelpers;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.DEFAULT_HOME_LATITUDE; import static com.aldo.apps.familyhelpers.utils.GlobalConstants.DEFAULT_HOME_LATITUDE;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.DEFAULT_HOME_LONGITUDE; import static com.aldo.apps.familyhelpers.utils.GlobalConstants.DEFAULT_HOME_LONGITUDE;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.METER_PER_SECOND_TO_KMH_CONVERTER; import static com.aldo.apps.familyhelpers.utils.GlobalConstants.METER_PER_SECOND_TO_KMH_CONVERTER;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.SHARE_LOCATION_CANCEL_ACTION;
import android.content.Intent; import android.content.Intent;
import android.content.res.Configuration; import android.content.res.Configuration;
@@ -211,6 +212,7 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
startForegroundService(shareLocationService); startForegroundService(shareLocationService);
} else { } else {
final Intent stopLocationService = new Intent(this, ShareLocationBackgroundWorker.class); final Intent stopLocationService = new Intent(this, ShareLocationBackgroundWorker.class);
stopLocationService.setAction(SHARE_LOCATION_CANCEL_ACTION);
stopService(stopLocationService); stopService(stopLocationService);
} }
}); });

View File

@@ -38,7 +38,7 @@ public final class GlobalConstants {
/** /**
* The action to be invoked when the sharing of location should be cancelled. * The action to be invoked when the sharing of location should be cancelled.
*/ */
public static final String SHARE_LOCATION_CANCEL_ACTION = "com.aldo.apps.familyhelpers.CANCEL_LOCATION_SHARING"; public static final String SHARE_LOCATION_CANCEL_ACTION = "SHARE_LOCATION_CANCEL";
/** /**
* The key of the extra to be applied to the starting intent of the sleepTimer service, * The key of the extra to be applied to the starting intent of the sleepTimer service,
* holding the initial duration in millis. * holding the initial duration in millis.

View File

@@ -251,7 +251,7 @@ public final class LocationHelper implements LocationListener {
final Intent deleteIntent = new Intent(context, ShareLocationBackgroundWorker.class); final Intent deleteIntent = new Intent(context, ShareLocationBackgroundWorker.class);
deleteIntent.setAction(SHARE_LOCATION_CANCEL_ACTION); deleteIntent.setAction(SHARE_LOCATION_CANCEL_ACTION);
final PendingIntent pendingDeleteIntent = PendingIntent.getBroadcast(context, 0, final PendingIntent pendingDeleteIntent = PendingIntent.getService(context, 0,
deleteIntent, PendingIntent.FLAG_IMMUTABLE); deleteIntent, PendingIntent.FLAG_IMMUTABLE);
return new NotificationCompat.Builder(context, SHARE_LOCATION_CHANNEL_ID) return new NotificationCompat.Builder(context, SHARE_LOCATION_CHANNEL_ID)

View File

@@ -1,5 +1,7 @@
package com.aldo.apps.familyhelpers.workers; package com.aldo.apps.familyhelpers.workers;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.DEFAULT_MINIMUM_LOCATION_INTERVAL_METERS;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.DEFAULT_MINIMUM_LOCATION_INTERVAL_MILLIS;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.SHARE_LOCATION_CANCEL_ACTION; import static com.aldo.apps.familyhelpers.utils.GlobalConstants.SHARE_LOCATION_CANCEL_ACTION;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.SLEEP_TIMER_NOTIFICATION_ID; import static com.aldo.apps.familyhelpers.utils.GlobalConstants.SLEEP_TIMER_NOTIFICATION_ID;
@@ -50,13 +52,14 @@ public class ShareLocationBackgroundWorker extends Service {
return START_REDELIVER_INTENT; return START_REDELIVER_INTENT;
} }
if (intent.getAction() != null && intent.getAction().equals(SHARE_LOCATION_CANCEL_ACTION)) { if (intent.getAction() != null && intent.getAction().equals(SHARE_LOCATION_CANCEL_ACTION)) {
stopSelf(); Log.d(TAG, "onStartCommand: Stopping service.");
mLocationHelper.stopLocationUpdates(); mLocationHelper.stopLocationUpdates();
stopSelf();
return START_NOT_STICKY; return START_NOT_STICKY;
} }
mShareLocationRunnable = () -> mShareLocationRunnable = () ->
mLocationHelper.startLocationUpdates(ShareLocationBackgroundWorker.this, mLocationHelper.startLocationUpdates(ShareLocationBackgroundWorker.this,
1000, 0); DEFAULT_MINIMUM_LOCATION_INTERVAL_MILLIS, DEFAULT_MINIMUM_LOCATION_INTERVAL_METERS);
mHandler.post(mShareLocationRunnable); mHandler.post(mShareLocationRunnable);
startForeground(SLEEP_TIMER_NOTIFICATION_ID, mLocationHelper.buildNotification(this)); startForeground(SLEEP_TIMER_NOTIFICATION_ID, mLocationHelper.buildNotification(this));
return super.onStartCommand(intent, flags, startId); return super.onStartCommand(intent, flags, startId);
@@ -70,7 +73,8 @@ public class ShareLocationBackgroundWorker extends Service {
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); mLocationHelper.stopLocationUpdates();
mHandler.removeCallbacks(mShareLocationRunnable); mHandler.removeCallbacks(mShareLocationRunnable);
super.onDestroy();
} }
} }