Skip to content

Commit d1c1a4e

Browse files
authored
Fix: Prevent startForeground crash and TransferService stopping while transfers ongoing (#2961)
* Awlays call startForeground, even if notification already shown * Break is in the wrong place and couild cause transfer service to get killed when it shouldn't * Only stop transfer service on Android versions that show Foreground Notification
1 parent b5623ab commit d1c1a4e

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferService.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ public class TransferService extends Service {
5353
*/
5454
boolean isReceiverNotRegistered = true;
5555

56-
/**
57-
* A flag that indicates whether or not the Foreground notification started
58-
*/
59-
boolean hasNotificationShown = false;
60-
6156
/**
6257
* The identifier used for the notification.
6358
*/
@@ -142,8 +137,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
142137
* b) An identifier for the ongoing notification c) Flag that determines if the notification
143138
* needs to be removed when the service is moved out of the foreground state.
144139
*/
145-
if (Build.VERSION.SDK_INT >= ANDROID_OREO && !hasNotificationShown) {
146-
hasNotificationShown = true;
140+
if (Build.VERSION.SDK_INT >= ANDROID_OREO) {
147141
try {
148142
synchronized (this) {
149143
final Notification userProvidedNotification = (Notification) intent.getParcelableExtra(INTENT_KEY_NOTIFICATION);

aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferStatusUpdater.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import android.content.Context;
1919
import android.content.Intent;
20+
import android.os.Build;
2021
import android.os.Handler;
2122
import android.os.Looper;
2223

@@ -246,17 +247,19 @@ public void run() {
246247

247248
// If all transfers in local map are completed,
248249
// stop TransferService to clear foreground notification
249-
boolean stopTransferService = true;
250-
for (TransferRecord record: transfers.values()) {
251-
if (!TransferState.isFinalState(record.state)) {
252-
stopTransferService = false;
253-
LOGGER.info("Transfers still pending, keeping TransferService running.");
250+
if (Build.VERSION.SDK_INT >= 26) {
251+
boolean stopTransferService = true;
252+
for (TransferRecord record : transfers.values()) {
253+
if (!TransferState.isFinalState(record.state)) {
254+
stopTransferService = false;
255+
LOGGER.info("Transfers still pending, keeping TransferService running.");
256+
break;
257+
}
258+
}
259+
if (stopTransferService) {
260+
LOGGER.info("All transfers in final state. Stopping TransferService");
261+
context.stopService(new Intent(context, TransferService.class));
254262
}
255-
break;
256-
}
257-
if (stopTransferService) {
258-
LOGGER.info("All transfers in final state. Stopping TransferService");
259-
context.stopService(new Intent(context, TransferService.class));
260263
}
261264
}
262265

0 commit comments

Comments
 (0)