Skip to content

Commit

Permalink
Fix crash when starting export or upload on Android 14+.
Browse files Browse the repository at this point in the history
  • Loading branch information
zamojski committed Nov 28, 2024
1 parent 33fd89d commit 7c8c4ff
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<uses-feature
Expand Down Expand Up @@ -129,6 +130,10 @@
android:name="android.service.quicksettings.ACTIVE_TILE"
android:value="true" />
</service>
<service
android:name="androidx.work.impl.foreground.SystemForegroundService"
android:foregroundServiceType="dataSync"
tools:node="merge" />

<receiver
android:name=".broadcast.ExternalBroadcastReceiver"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import info.zamojski.soft.towercollector.utils.GpsUtils;
import info.zamojski.soft.towercollector.utils.MobileUtils;
import info.zamojski.soft.towercollector.utils.NetworkTypeUtils;
import info.zamojski.soft.towercollector.utils.PermissionUtils;
import timber.log.Timber;

public class CollectorService extends Service {
Expand Down Expand Up @@ -207,7 +208,7 @@ public void onCreate() {
registerReceiver(locationModeOrProvidersChanged, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
Notification notification = notificationHelper.createNotification(notificationManager, getGpsStatusNotificationText(getGpsStatus()));
// start as foreground service to prevent from killing
if (GpsUtils.isBackgroundLocationAware()) {
if (PermissionUtils.isForegroundServicePermissionAware()) {
startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);
} else {
startForeground(NOTIFICATION_ID, notification);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.pm.ServiceInfo;
import android.net.Uri;
import android.os.Build;

Expand Down Expand Up @@ -45,6 +46,7 @@
import info.zamojski.soft.towercollector.io.filesystem.CompressionFormat;
import info.zamojski.soft.towercollector.model.AnalyticsStatistics;
import info.zamojski.soft.towercollector.utils.FileUtils;
import info.zamojski.soft.towercollector.utils.PermissionUtils;
import info.zamojski.soft.towercollector.utils.StringUtils;
import timber.log.Timber;

Expand Down Expand Up @@ -82,7 +84,12 @@ public ExportWorker(@NonNull Context context, @NonNull WorkerParameters workerPa
public Result doWork() {
try {
Notification notification = notificationHelper.createNotification(notificationManager);
ForegroundInfo foregroundInfo = new ForegroundInfo(NOTIFICATION_ID, notification);
ForegroundInfo foregroundInfo;
if (PermissionUtils.isForegroundServicePermissionAware()) {
foregroundInfo = new ForegroundInfo(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
} else {
foregroundInfo = new ForegroundInfo(NOTIFICATION_ID, notification);
}
setForegroundAsync(foregroundInfo);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.pm.ServiceInfo;
import android.os.Build;

import androidx.annotation.NonNull;
Expand All @@ -23,7 +24,6 @@
import java.util.List;
import java.util.Map;

import info.zamojski.soft.towercollector.BuildConfig;
import info.zamojski.soft.towercollector.MyApplication;
import info.zamojski.soft.towercollector.R;
import info.zamojski.soft.towercollector.UploaderQuickSettingsTileService;
Expand All @@ -47,6 +47,7 @@
import info.zamojski.soft.towercollector.utils.ApkUtils;
import info.zamojski.soft.towercollector.utils.NetworkUtils;
import info.zamojski.soft.towercollector.utils.OpenCellIdUtils;
import info.zamojski.soft.towercollector.utils.PermissionUtils;
import timber.log.Timber;

public class UploaderWorker extends Worker implements IProgressListener {
Expand Down Expand Up @@ -105,7 +106,12 @@ public UploaderWorker(@NonNull Context context, @NonNull WorkerParameters worker
public Result doWork() {
try {
Notification notification = notificationHelper.createNotification(notificationManager);
ForegroundInfo foregroundInfo = new ForegroundInfo(NOTIFICATION_ID, notification);
ForegroundInfo foregroundInfo;
if (PermissionUtils.isForegroundServicePermissionAware()) {
foregroundInfo = new ForegroundInfo(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
} else {
foregroundInfo = new ForegroundInfo(NOTIFICATION_ID, notification);
}
setForegroundAsync(foregroundInfo);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public static boolean isNotificationPermissionRequired() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU;
}

public static boolean isForegroundServicePermissionAware() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; // TODO: should be Build.VERSION_CODES.UPSIDE_DOWN_CAKE but I recall there were issues on Android 12 as well
}

public static String getAppPermissions() {
Map<String, Boolean> permissions = getAppPermissionsInternal();
StringBuilder sb = new StringBuilder();
Expand Down

0 comments on commit 7c8c4ff

Please sign in to comment.