diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 76144ac40c..58ff3b17f2 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -20,7 +20,7 @@ - + @@ -395,7 +395,7 @@ android:enabled="true" /> = Build.VERSION_CODES.Q) { + startForeground(NOTIFICATION_ID, notification, FOREGROUND_SERVICE_TYPE_LOCATION); + } else { + startForeground(NOTIFICATION_ID, notification); + } + + return START_STICKY; + } + + @Override + public void onDestroy() { + super.onDestroy(); + + if (locationManager == null) { + return; + } + + try { + locationManager.removeUpdates(locationListener); + } catch (Exception ex) { + Log.i(TAG, "fail to remove location listeners, ignore", ex); + } + } + + private void requestLocationUpdate(String provider) { + try { + locationManager.requestLocationUpdates( + provider, LOCATION_INTERVAL, LOCATION_DISTANCE, + locationListener); + } catch (SecurityException | IllegalArgumentException ex) { + Log.e(TAG, String.format("Unable to request %s provider based location updates.", provider), ex); + } + } + + private void initialLocationUpdate() { + try { + Location gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); + if (gpsLocation != null && System.currentTimeMillis() - gpsLocation.getTime() < INITIAL_TIMEOUT) { + locationListener.onLocationChanged(gpsLocation); + } + + } catch (NullPointerException | SecurityException e) { + e.printStackTrace(); + } + } + + static public void createNotificationChannel(Context context) { + if(!CHANNEL_CREATED.get() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + CHANNEL_CREATED.set(true); + NotificationChannel channel = new NotificationChannel(CHANNEL_ID, + "Location Streaming Service", NotificationManager.IMPORTANCE_MIN); + channel.setDescription("Ensure app will not be killed while location is being streamed in background."); + NotificationManager notificationManager = context.getSystemService(NotificationManager.class); + notificationManager.createNotificationChannel(channel); + } + } + + private class ServiceLocationListener implements LocationListener { + + @Override + public void onLocationChanged(Location location) { + Log.d(TAG, "onLocationChanged: " + location); + if (location == null) { + return; + } + DcLocation.getInstance().updateLocation(location); + } + + @Override + public void onProviderDisabled(String provider) { + Log.e(TAG, "onProviderDisabled: " + provider); + } + + @Override + public void onProviderEnabled(String provider) { + Log.e(TAG, "onProviderEnabled: " + provider); + } + + @Override + public void onStatusChanged(String provider, int status, Bundle extras) { + Log.e(TAG, "onStatusChanged: " + provider + " status: " + status); + } + } + +} diff --git a/src/main/java/org/thoughtcrime/securesms/mms/AttachmentManager.java b/src/main/java/org/thoughtcrime/securesms/mms/AttachmentManager.java index 77bd46add4..25aa73db5e 100644 --- a/src/main/java/org/thoughtcrime/securesms/mms/AttachmentManager.java +++ b/src/main/java/org/thoughtcrime/securesms/mms/AttachmentManager.java @@ -503,11 +503,7 @@ public static void selectLocation(Activity activity, int chatId) { } }); }); - 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(); } diff --git a/src/main/java/org/thoughtcrime/securesms/service/FetchForegroundService.java b/src/main/java/org/thoughtcrime/securesms/service/FetchForegroundService.java index 3415596633..a1f0b2e26f 100644 --- a/src/main/java/org/thoughtcrime/securesms/service/FetchForegroundService.java +++ b/src/main/java/org/thoughtcrime/securesms/service/FetchForegroundService.java @@ -14,12 +14,11 @@ import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.connect.ForegroundDetector; -import org.thoughtcrime.securesms.notifications.FcmReceiveService; import org.thoughtcrime.securesms.notifications.NotificationCenter; import org.thoughtcrime.securesms.util.Util; public final class FetchForegroundService extends Service { - private static final String TAG = FcmReceiveService.class.getSimpleName(); + private static final String TAG = FetchForegroundService.class.getSimpleName(); private static final Object SERVICE_LOCK = new Object(); private static final Object STOP_NOTIFIER = new Object(); private static volatile boolean fetchingSynchronously = false;