Compare commits

...

15 Commits

Author SHA1 Message Date
copilot-swe-agent[bot] 469259f469 Fix LocationListenerCompat compilation error - use correct interface for LocationManagerCompat
Co-authored-by: adbenitez <24558636+adbenitez@users.noreply.github.com>
2025-11-24 21:53:16 +00:00
copilot-swe-agent[bot] 9dcd75fe54 Use LocationManagerCompat for proper location updates on modern Android
Co-authored-by: adbenitez <24558636+adbenitez@users.noreply.github.com>
2025-11-24 21:47:02 +00:00
copilot-swe-agent[bot] adffc4f6f8 Fix foreground service for Android 14+ and add NETWORK_PROVIDER fallback
Co-authored-by: adbenitez <24558636+adbenitez@users.noreply.github.com>
2025-11-24 21:22:29 +00:00
copilot-swe-agent[bot] 7633338359 Fix PermissionsBuilder method chaining - request and execute must be separate calls
Co-authored-by: adbenitez <24558636+adbenitez@users.noreply.github.com>
2025-11-24 20:43:58 +00:00
adb 2f4d239e2b Merge branch 'main' into copilot/allow-location-sharing-again 2025-11-24 21:38:50 +01:00
copilot-swe-agent[bot] ca65424653 Ensure foreground service requirements are met even on error
Co-authored-by: adbenitez <24558636+adbenitez@users.noreply.github.com>
2025-11-24 20:22:58 +00:00
copilot-swe-agent[bot] 6e48933a8f Remove orphaned code from LocationBackgroundService
Co-authored-by: adbenitez <24558636+adbenitez@users.noreply.github.com>
2025-11-24 20:21:00 +00:00
copilot-swe-agent[bot] df50a6d608 Fix method call, thread safety, and service initialization order
Co-authored-by: adbenitez <24558636+adbenitez@users.noreply.github.com>
2025-11-24 20:19:30 +00:00
copilot-swe-agent[bot] b3abc39059 Add FLAG_IMMUTABLE and improve foreground service initialization
Co-authored-by: adbenitez <24558636+adbenitez@users.noreply.github.com>
2025-11-24 20:17:30 +00:00
copilot-swe-agent[bot] 61fc7019b2 Improve foreground service lifecycle handling
Co-authored-by: adbenitez <24558636+adbenitez@users.noreply.github.com>
2025-11-24 20:15:09 +00:00
copilot-swe-agent[bot] b8d6c0b481 Fix service binding logic and foreground service lifecycle
Co-authored-by: adbenitez <24558636+adbenitez@users.noreply.github.com>
2025-11-24 20:13:51 +00:00
copilot-swe-agent[bot] 8675f816de Fix code review feedback: improve error handling and service logic
Co-authored-by: adbenitez <24558636+adbenitez@users.noreply.github.com>
2025-11-24 20:12:34 +00:00
copilot-swe-agent[bot] c9af561868 Add FOREGROUND_SERVICE_LOCATION permission
Co-authored-by: adbenitez <24558636+adbenitez@users.noreply.github.com>
2025-11-24 20:10:28 +00:00
copilot-swe-agent[bot] db10790ecd Implement foreground notification for location sharing
Co-authored-by: adbenitez <24558636+adbenitez@users.noreply.github.com>
2025-11-24 20:07:35 +00:00
copilot-swe-agent[bot] 09f389d6de Initial plan 2025-11-24 20:00:57 +00:00
7 changed files with 159 additions and 32 deletions
+1
View File
@@ -39,6 +39,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<!-- force compiling emojipicker on sdk<21; runtime checks are required then -->
<uses-sdk tools:overrideLibrary="androidx.emoji2.emojipicker"/>
@@ -5,9 +5,12 @@ import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import androidx.core.content.ContextCompat;
import org.thoughtcrime.securesms.connect.DcHelper;
import java.util.LinkedList;
@@ -23,11 +26,13 @@ public class DcLocationManager implements Observer {
private final Context context;
private DcLocation dcLocation = DcLocation.getInstance();
private final LinkedList<Integer> pendingShareLastLocation = new LinkedList<>();
private boolean serviceBound = false;
private final ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "background service connected");
serviceBinder = (LocationBackgroundService.LocationBackgroundServiceBinder) service;
serviceBound = true;
while (!pendingShareLastLocation.isEmpty()) {
shareLastLocation(pendingShareLastLocation.pop());
}
@@ -37,6 +42,7 @@ public class DcLocationManager implements Observer {
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "background service disconnected");
serviceBinder = null;
serviceBound = false;
}
};
@@ -49,19 +55,29 @@ public class DcLocationManager implements Observer {
}
public void startLocationEngine() {
if (serviceBinder == null) {
if (serviceBinder == null || !serviceBound) {
Intent intent = new Intent(context.getApplicationContext(), LocationBackgroundService.class);
// Start as foreground service
ContextCompat.startForegroundService(context, intent);
// Then bind to it
context.bindService(intent, serviceConnection, BIND_AUTO_CREATE);
}
}
public void stopLocationEngine() {
if (serviceBinder == null) {
if (serviceBinder == null || !serviceBound) {
return;
}
context.unbindService(serviceConnection);
serviceBinder.stop();
try {
context.unbindService(serviceConnection);
if (serviceBinder != null) {
serviceBinder.stop();
}
} catch (IllegalArgumentException e) {
Log.w(TAG, "Service not registered", e);
}
serviceBinder = null;
serviceBound = false;
}
public void stopSharingLocation(int chatId) {
@@ -1,18 +1,36 @@
package org.thoughtcrime.securesms.geolocation;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ServiceInfo;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.core.app.ServiceCompat;
import androidx.core.content.ContextCompat;
import androidx.core.location.LocationListenerCompat;
import androidx.core.location.LocationManagerCompat;
import androidx.core.location.LocationRequestCompat;
import org.thoughtcrime.securesms.ConversationListActivity;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.notifications.NotificationCenter;
import org.thoughtcrime.securesms.util.IntentUtils;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
public class LocationBackgroundService extends Service {
@@ -22,6 +40,7 @@ public class LocationBackgroundService extends Service {
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 25F;
ServiceLocationListener locationListener;
private final AtomicBoolean isForeground = new AtomicBoolean(false);
private final IBinder mBinder = new LocationBackgroundServiceBinder();
@@ -37,12 +56,21 @@ public class LocationBackgroundService extends Service {
@Override
public void onCreate() {
super.onCreate();
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
if (locationManager == null) {
Log.e(TAG, "Unable to initialize location service");
// Must start foreground to avoid crash, then stop immediately
initializeForegroundService();
stopForeground(true);
stopSelf();
return;
}
// Initialize foreground service after successful location manager setup
initializeForegroundService();
locationListener = new ServiceLocationListener();
Location lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastLocation != null) {
@@ -51,14 +79,19 @@ public class LocationBackgroundService extends Service {
DcLocation.getInstance().updateLocation(lastLocation);
}
}
//requestLocationUpdate(LocationManager.NETWORK_PROVIDER);
// Request location updates from both GPS and network providers for better coverage
requestLocationUpdate(LocationManager.GPS_PROVIDER);
requestLocationUpdate(LocationManager.NETWORK_PROVIDER);
initialLocationUpdate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
// Ensure foreground notification is shown (handles edge cases)
initializeForegroundService();
return START_STICKY;
}
@@ -66,23 +99,93 @@ public class LocationBackgroundService extends Service {
public void onDestroy() {
super.onDestroy();
if (locationManager == null) {
// Stop foreground notification
stopForeground(true);
if (locationManager == null || locationListener == null) {
return;
}
try {
locationManager.removeUpdates(locationListener);
LocationManagerCompat.removeUpdates(locationManager, locationListener);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listeners, ignore", ex);
}
}
private void initializeForegroundService() {
if (isForeground.compareAndSet(false, true)) {
createNotificationChannel();
Notification notification = createNotification();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
// Android 14+ requires foregroundServiceType in startForeground
ServiceCompat.startForeground(this, NotificationCenter.ID_LOCATION, notification,
ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);
} else {
startForeground(NotificationCenter.ID_LOCATION, notification);
}
Log.d(TAG, "Foreground service started with notification");
}
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
NotificationCenter.CH_LOCATION,
getString(R.string.location),
NotificationManager.IMPORTANCE_LOW
);
channel.setDescription("Location sharing notification");
NotificationManager notificationManager = getSystemService(NotificationManager.class);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
}
private Notification createNotification() {
Intent intent = new Intent(this, ConversationListActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
0,
intent,
IntentUtils.FLAG_IMMUTABLE()
);
return new NotificationCompat.Builder(this, NotificationCenter.CH_LOCATION)
.setContentTitle(getString(R.string.location_sharing_notification_title))
.setContentText(getString(R.string.location_sharing_notification_text))
.setSmallIcon(R.drawable.ic_location_on_white_24dp)
.setContentIntent(pendingIntent)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_LOW)
.build();
}
private void requestLocationUpdate(String provider) {
try {
locationManager.requestLocationUpdates(
provider, LOCATION_INTERVAL, LOCATION_DISTANCE,
locationListener);
} catch (SecurityException | IllegalArgumentException ex) {
// Check if provider is available
if (!locationManager.isProviderEnabled(provider)) {
Log.w(TAG, String.format("Provider %s is not enabled", provider));
return;
}
// Use LocationManagerCompat for better compatibility with modern Android
LocationRequestCompat locationRequest = new LocationRequestCompat.Builder(LOCATION_INTERVAL)
.setMinUpdateDistanceMeters(LOCATION_DISTANCE)
.setQuality(LocationRequestCompat.QUALITY_HIGH_ACCURACY)
.build();
Executor executor = ContextCompat.getMainExecutor(this);
LocationManagerCompat.requestLocationUpdates(
locationManager,
provider,
locationRequest,
executor,
locationListener
);
Log.d(TAG, String.format("Requested location updates from %s provider", provider));
} catch (SecurityException | IllegalArgumentException ex) {
Log.e(TAG, String.format("Unable to request %s provider based location updates.", provider), ex);
}
}
@@ -93,9 +196,16 @@ public class LocationBackgroundService extends Service {
if (gpsLocation != null && System.currentTimeMillis() - gpsLocation.getTime() < INITIAL_TIMEOUT) {
locationListener.onLocationChanged(gpsLocation);
}
// Also try network provider for initial location
Location networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (networkLocation != null && System.currentTimeMillis() - networkLocation.getTime() < INITIAL_TIMEOUT) {
// Use network location if GPS location is not available or network location is newer
if (gpsLocation == null || networkLocation.getTime() > gpsLocation.getTime()) {
locationListener.onLocationChanged(networkLocation);
}
}
} catch (NullPointerException | SecurityException e) {
e.printStackTrace();
Log.e(TAG, "Error getting initial location", e);
}
}
@@ -110,30 +220,22 @@ public class LocationBackgroundService extends Service {
}
}
private class ServiceLocationListener implements LocationListener {
private class ServiceLocationListener implements LocationListenerCompat {
@Override
public void onLocationChanged(@NonNull Location location) {
Log.d(TAG, "onLocationChanged: " + location);
if (location == null) {
return;
}
DcLocation.getInstance().updateLocation(location);
}
@Override
public void onProviderDisabled(@NonNull String provider) {
Log.e(TAG, "onProviderDisabled: " + provider);
Log.w(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(@NonNull String provider) {
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e(TAG, "onStatusChanged: " + provider + " status: " + status);
Log.d(TAG, "onProviderEnabled: " + provider);
}
}
@@ -484,7 +484,7 @@ public class AttachmentManager {
// for rationale dialog requirements
Permissions.PermissionsBuilder permissionsBuilder = Permissions.with(activity)
.ifNecessary()
.withRationaleDialog("To share your live location with chat members, allow ArcaneChat to use your location data.\n\nTo make live location work gaplessly, location data is used even when the app is closed or not in use.", R.drawable.ic_location_on_white_24dp)
.withRationaleDialog("To share your live location with chat members, allow ArcaneChat to use your location data.", R.drawable.ic_location_on_white_24dp)
.withPermanentDenialDialog(activity.getString(R.string.perm_explain_access_to_location_denied))
.onAllGranted(() -> {
ShareLocationDialog.show(activity, durationInSeconds -> {
@@ -495,11 +495,7 @@ public class AttachmentManager {
}
});
});
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
permissionsBuilder.request(Manifest.permission.ACCESS_BACKGROUND_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION);
} else {
permissionsBuilder.request(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION);
}
permissionsBuilder.request(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION);
permissionsBuilder.execute();
}
@@ -218,6 +218,7 @@ public class NotificationCenter {
public static final int ID_MSG_SUMMARY = 2;
public static final int ID_GENERIC = 3;
public static final int ID_FETCH = 4;
public static final int ID_LOCATION = 5;
public static final int ID_MSG_OFFSET = 0; // msgId is added - as msgId start at 10, there are no conflicts with lower numbers
@@ -243,6 +244,7 @@ public class NotificationCenter {
public static final String CH_MSG_VERSION = "5";
public static final String CH_PERMANENT = "dc_fg_notification_ch";
public static final String CH_GENERIC = "ch_generic";
public static final String CH_LOCATION = "ch_location";
public static final String CH_CALLS_PREFIX = "call_chan";
private boolean notificationChannelsSupported() {
@@ -39,4 +39,12 @@ public class IntentUtils {
return 0;
}
}
public static int FLAG_IMMUTABLE() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return PendingIntent.FLAG_IMMUTABLE;
} else {
return 0;
}
}
}
+2
View File
@@ -358,6 +358,8 @@
<string name="copy_json">Copy JSON</string>
<string name="replace_draft">Replace Draft</string>
<string name="title_share_location">Share location with all group members</string>
<string name="location_sharing_notification_title">Sharing location</string>
<string name="location_sharing_notification_text">Location is being shared with chat members</string>
<string name="device_talk">Device Messages</string>
<string name="device_talk_subtitle">Locally generated messages</string>
<string name="device_talk_explain">Messages in this chat are generated on your device to inform about app updates and problems during usage.</string>