amplify-android icon indicating copy to clipboard operation
amplify-android copied to clipboard

[Storage] Support downloading files in background

Open richardmcclellan opened this issue 4 years ago • 4 comments

The AWSS3StoragePlugin creates a TransferService, which is an Android Service. As of Android O (API 26), a Service cannot be started in the background (source), which means that transfers cannot be initiated while the app is in the background. Instead, we should use the new JobScheduler, which is allowed to run periodically in the background.

Attempts to start a transfer while in the background result in the following error:

AmplifyException {message=Something went wrong with your AWS S3 Storage list operation, cause=java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.netafim.tech_app/com.amazonaws.mobileconnectors.s3.transferutility.TransferService }: app is in background uid UidRecord{e945dd1 u0a281 TRNB bg:+1m5s172ms idle change:idle procs:1 seq(0,0,0)}, recoverySuggestion=See attached exception for more information and suggestions}
        at com.amplifyframework.storage.s3.operation.AWSS3StorageListOperation.lambda$start$0$AWSS3StorageListOperation(AWSS3StorageListOperation.java:95)
        at com.amplifyframework.storage.s3.operation.-$$Lambda$AWSS3StorageListOperation$h_rYgEkUSCQ04HohVqKR6853m80.run(Unknown Source:2)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)
     Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.netafim.tech_app/com.amazonaws.mobileconnectors.s3.transferutility.TransferService }: app is in background uid UidRecord{e945dd1 u0a281 TRNB bg:+1m5s172ms idle change:idle procs:1 seq(0,0,0)}
        at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1616)
        at android.app.ContextImpl.startService(ContextImpl.java:1571)
        at android.content.ContextWrapper.startService(ContextWrapper.java:669)
        at com.amplifyframework.storage.s3.service.AWSS3StorageService.startServiceIfNotAlreadyStarted(AWSS3StorageService.java:226)
        at com.amplifyframework.storage.s3.service.AWSS3StorageService.listFiles(AWSS3StorageService.java:157)
        at com.amplifyframework.storage.s3.operation.AWSS3StorageListOperation.lambda$start$0$AWSS3StorageListOperation(AWSS3StorageListOperation.java:91)
        at com.amplifyframework.storage.s3.operation.-$$Lambda$AWSS3StorageListOperation$h_rYgEkUSCQ04HohVqKR6853m80.run(Unknown Source:2) 
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462) 
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:919) 

Originally reported on StackOverflow: https://stackoverflow.com/questions/65789564/failed-to-use-amplify-storage-with-a-periodic-worker

richardmcclellan avatar Jan 20 '21 04:01 richardmcclellan

Any updates on this?

LaurieScheepers avatar Feb 10 '22 19:02 LaurieScheepers

I have figured out a workaround for this. Please see answer at https://stackoverflow.com/questions/65789564/failed-to-use-amplify-storage-with-a-periodic-worker/71171153#71171153

LaurieScheepers avatar Feb 18 '22 09:02 LaurieScheepers

Before delivering support of background download/upload, I think at least it should prevent App from crashing. This thread suggests a change to prevent the crash.

Change call of startService to startForegroundService.

https://github.com/aws-amplify/amplify-android/blob/1375235eb2d100f40f70afc8a4de8e2f77b457ab/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/service/AWSS3StorageService.java#L249-L255

HuiSF avatar May 26 '22 17:05 HuiSF

@HuiSF so this issue will be resolved in the next version of amplify in android right? Because it is so common in android to run the uploading tasks using Worker.

androidcodehunter avatar Jun 09 '22 16:06 androidcodehunter

Background support for upload & download operation is available in Amplify 2.0

sdhuka avatar Nov 22 '22 14:11 sdhuka

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Nov 22 '22 14:11 github-actions[bot]