Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download stops when the app is backgrounded #4106

Open
mkatsevVR opened this issue Nov 23, 2024 · 33 comments · Fixed by #4108 or #4118 · May be fixed by #4130
Open

Download stops when the app is backgrounded #4106

mkatsevVR opened this issue Nov 23, 2024 · 33 comments · Fixed by #4108 or #4118 · May be fixed by #4130
Assignees
Milestone

Comments

@mkatsevVR
Copy link

Describe the bug
Downloading an archive only works if the app is open and the phone screen is on. If the app is in the background or the phone is asleep, the download stops.

Expected behavior
The download should continue in the background.

Steps to reproduce the behavior:

  1. Go to the download tab
  2. Click on any large item (eg wikipedia top 50000 articles)
  3. Download starts, see the notification showing progress.
  4. Switch away to another app.
  5. After a short period of time (less than a minute) the download notification disappears
  6. Go back to kiwix, the download is shown as pending
  7. Pause/unpause, the download resumes.

Environment

  • Version of Kiwix Android : 3.11.1
  • Device : Pixel 9
  • OS version : Android 15
@kelson42
Copy link
Collaborator

Does that still happen with 3.12 (nightly)?

@mkatsevVR
Copy link
Author

Where is the nightly apk?

@kelson42
Copy link
Collaborator

Where is the nightly apk?

http://download.kiwix.org/nightly/

@mkatsevVR
Copy link
Author

Same thing on the nightly, except the status is shown as "Paused WAIT FOR RETRY" instead of "Pending" in step 6.

@kelson42
Copy link
Collaborator

@mkatsevVR Thank you for your bug report, we will look at it and come back to you in the next days.

@MohitMaliFtechiz
Copy link
Collaborator

@mkatsevVR, Thanks for opening the issue.
@mkatsevVR, @kelson42, I have tested the scenario that @mkatsevVR is describing. The notification disappears after some time if we move to the other application, which is due to limitations imposed by Android's restrictions on background execution, especially for long-running tasks (in our case, downloading). It can be easily fixed by using the foreground service for this functionality which I will fix shortly. But for stopping the downloading I have not faced this type of issue which @mkatsevVR described(In my case, the downloading continued even when I closed the application) because in the latest nightly we are using the DownloadManager which handles the downloading on its own thread. However, I have not yet tested the scenario where the internet has fluctuated. The status showing "Paused WAIT FOR RETRY" shows when the internet fluctuates.

@mkatsevVR I will provide you the APK after fixing the notification issue by adding some additional logs in that APK which will help us to debug the problem in your device.

@kelson42
Copy link
Collaborator

@MohitMaliFtechiz Thank you. To me it is really important we use @mkatsevVR willingness to fix the problem(s) to better understand what goes wrong in his use case... and then fix it/them.

@MohitMaliFtechiz
Copy link
Collaborator

MohitMaliFtechiz commented Nov 25, 2024

@mkatsevVR Thanks for your patience. Can you please try this APK https://drive.google.com/file/d/1x4aIJ7BlYBAfzEH_KtxhWxmiSg83vJYm/view?usp=sharing and try the same steps as you were doing before, and let us know the issue still exist at your end? if exist then after reproducing the error please share the device logs with us.

For sending the device logs:
Go to the Help screen from the navigation menu -> Send diagnostic report-> it will generate the logs file please attach that file here.

Edited
Please ignore the Notification controls(Pause, cancel) that are not currently working.

@kelson42
Copy link
Collaborator

The notification should be merged, but keeping the issue open so we can investigate the unstable download case. @mkatsevVR Your feedback is important.

@kelson42 kelson42 reopened this Nov 26, 2024
@kelson42 kelson42 modified the milestones: 3.12.0, 3.12.1 Nov 26, 2024
@mkatsevVR
Copy link
Author

The notification doesn't go away this time, but nothing is getting downloaded (progress bar doesn't move at all for over an hour), the status is shown as "in progress".

logs1732907962565.txt

@kelson42
Copy link
Collaborator

kelson42 commented Nov 30, 2024

Similar to #4113, it seems we have a massive regression here. Should be fixed in top priority.

@MohitMaliFtechiz
Copy link
Collaborator

@mkatsevVR Thank you for the logs, and your feedback.

@Jaifroid
Copy link
Member

Jaifroid commented Dec 1, 2024

Stumbled across this when searching for something else. Just to say it might be failing because library.kiwix.org is down and has been so for at least 8 hours. Maybe was down yesterday evening too?

@MohitMaliFtechiz
Copy link
Collaborator

@Jaifroid This problem is not related to the down server, it is old problem.
For this problem: The downloading was working in the background as the DownloadManager downloads the content on its own thread, here in this issue, the problem is with the showing progress, by the provided logs of @mkatsevVR It seems the downloading bytes comming with a negative integer, which prevent the showing downloading prgress. This problem is with very large files like @kelson42 opened a issue #4113.

11-29 14:19:15.306 24142 24384 E DOWNLOADING_STEP: updateNotification: DownloadModel(databaseId=1, downloadId=34, file=/storage/emulated/0/Android/media/org.kiwix.kiwixmobile/Kiwix/wikipedia_en_all_mini_2024-04.zim, etaInMilliSeconds=0, bytesDownloaded=-1403397573, totalSizeOfDownload=-1189632026, state=DOWNLOADING, error=NONE, progress=-1, book=org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity$Book@53acc78c)
11-29 14:19:15.314 24142 24384 E DOWNLOADING_STEP: updateDownloadStatus:
11-29 14:19:15.531 24142 24142 I ImeTracker: org.kiwix.kiwixmobile:7c3b815d: onRequestHide at ORIGIN_CLIENT reason HIDE_SOFT_INPUT fromUser true
11-29 14:19:16.215 24142 24142 I ImeTracker: org.kiwix.kiwixmobile:ed17c3f1: onRequestHide at ORIGIN_CLIENT reason HIDE_SOFT_INPUT fromUser false
11-29 14:19:16.216 24142 24142 D AutofillManager: view not autofillable - not passing ime action check
11-29 14:19:16.216 24142 24142 D AutofillManager: view not autofillable - not passing ime action check
11-29 14:19:16.216 24142 24142 W WindowOnBackDispatcher: sendCancelIfRunning: isInProgress=false callback=androidx.appcompat.widget.Toolbar$Api33Impl$$ExternalSyntheticLambda0@7ca1f31
11-29 14:19:16.232 24142 24142 I ImeTracker: org.kiwix.kiwixmobile:facc40e1: onRequestHide at ORIGIN_CLIENT reason HIDE_SOFT_INPUT_CLOSE_CURRENT_SESSION fromUser false
11-29 14:19:19.873 24142 24142 D LeakCanary: Watching instance of androidx.constraintlayout.widget.ConstraintLayout (org.kiwix.kiwixmobile.nav.destination.library.OnlineLibraryFragment received Fragment#onDestroyView() callback (references to its views should be cleared to prevent leaks)) with key f54e9441-bcf3-4b15-bc7b-d0848fe34ccc
11-29 14:19:20.278 24142 24335 E DOWNLOADING_STEP: checkDownloads: lock
11-29 14:19:20.284 24142 24335 E DOWNLOADING_STEP: checkDownloads:
11-29 14:19:20.298 24142 24384 E DOWNLOADING_STEP: updateDownloadStatus:
11-29 14:19:20.300 24142 24384 E DOWNLOADING_STEP: updateDownloadStatus:
11-29 14:19:20.300 24142 24384 E DOWNLOADING_STEP: shouldUpdateDownloadStatus:
11-29 14:19:20.303 24142 24384 E DOWNLOADING_STEP: updateNotification: DownloadModel(databaseId=1, downloadId=34, file=/storage/emulated/0/Android/media/org.kiwix.kiwixmobile/Kiwix/wikipedia_en_all_mini_2024-04.zim, etaInMilliSeconds=0, bytesDownloaded=-1385899461, totalSizeOfDownload=-1189632026, state=DOWNLOADING, error=NONE, progress=-1, book=org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity$Book@53acc78c)

@MohitMaliFtechiz
Copy link
Collaborator

@mkatsevVR Thanks for providing the logs, we have fixed the problem in #4118. Can you please try this APK and let us know the problem is fixed or not for you https://drive.google.com/file/d/1x4aIJ7BlYBAfzEH_KtxhWxmiSg83vJYm/view?usp=sharing

@kelson42
Copy link
Collaborator

kelson42 commented Dec 8, 2024

@mkatsevVR Any feedabck? I will merge the fix and this will close the issue... but we would be glad to know if it works now for you.

@mkatsevVR
Copy link
Author

mkatsevVR commented Dec 9, 2024

no, still the same problem: after a few minutes with screen off the notification disappeared and the status in the app is shown as WAITING_FOR_RETRY

logs1733702512510.txt

@mkatsevVR
Copy link
Author

but the progress bar does work now, so that's fixed at least

@kelson42 kelson42 reopened this Dec 9, 2024
@MohitMaliFtechiz
Copy link
Collaborator

no, still the same problem: after a few minutes with screen off the notification disappeared and the status in the app is shown as WAITING_FOR_RETRY

logs1733702512510.txt

@mkatsevVR Can you please tell me:

  • On which network you are trying to download the ZIM file, over WIFI or mobile network?
  • Do you have the stable network connection?

Since now we are using the DownloadManager for downloading the ZIM files which manage these all scenarios by own. WAITING_FOR_RETRY means there is some network error occurred and the download manager is waiting before retrying the request.

but the progress bar does work now, so that's fixed at least

Thank you for the confirmation.

@mkatsevVR
Copy link
Author

mkatsevVR commented Dec 9, 2024 via email

@mkatsevVR
Copy link
Author

mkatsevVR commented Dec 9, 2024 via email

@MohitMaliFtechiz
Copy link
Collaborator

I'm on WiFi, I don't think there's any problem with the connection. If I keep using the phone while it's downloading, everything is fine. If I turn the screen off, the download stops. It must be some power saving or inactivity thing that breaks the download or pauses the network connection when the phone is idle. Another issue is that when I turn the phone back on, the download doesn't resume on its own, I have to go back into the app and unpause it.

@mkatsevVR Thanks for the detailed information. It would be very helpful in debugging.

@MohitMaliFtechiz
Copy link
Collaborator

@mkatsevVR Thanks for the detailed information. Can you please try this APK https://drive.google.com/file/d/1x4aIJ7BlYBAfzEH_KtxhWxmiSg83vJYm/view?usp=sharing, and do let us know if the problem is fixed or not at your end?

@mkatsevVR
Copy link
Author

mkatsevVR commented Dec 11, 2024 via email

@MohitMaliFtechiz
Copy link
Collaborator

I think it's better now but still not fully fixed. The test I did was to start a large download, turn the screen off, wait 20 minutes, then turn the screen back on. The good news is that the notification no longer disappears. It shows "pending" and the download has not made much progress while the screen was off. However the download automatically resumed without having to open the app. So it seems like now the phone going to sleep no longer breaks the download completely, but it still gets paused while the phone is asleep.

@mkatsevVR Thank you for your feedback! We now know that the issue with tracking the download progress while the device is in sleep mode has been resolved. It successfully tracks progress, and if the download is paused (due to network throttling or certain device settings), it automatically resumes when the device is turned on.

However, I have also tested your scenario on my Pixel 7a (Which I today upgraded to Android 15) and was able to reproduce the error you mentioned. The download pauses after 15 minutes of sleep mode. This error is only happening in Android 15 devices, in below Android 15 devices it is working fine. It needs some more research I will get back to you ASAP.

@mkatsevVR
Copy link
Author

mkatsevVR commented Dec 12, 2024 via email

@MohitMaliFtechiz
Copy link
Collaborator

MohitMaliFtechiz commented Dec 13, 2024

@kelson42, @mkatsevVR I came with my research. It is not a bug it is an Android feature. When we lock our screen, after some time(Differ according to manufacture, for me in Pixel 7a it goes after 2 minutes) the device goes into the "Doze" mode for battery optimization, and in Doze mode they Suspends network access due to this the downloads are pausing. See restricting of Done mode

Since we are using the DownlaodManager for downloading the ZIM files, and we are hiding the default DownloadManager notification and showing our custom notification with pause/resume buttons(Because download manager does not have the pause/resume button feature). So due to this, when the device goes into Doze mode DownloadManager pauses the download(Probably it is giving less priority to our downloads since we are hiding it's by default notification, so it is thinking that the user is not aware of ongoing download that's why it is pausing the download in Doze mode).

  • If I configured the DownloadManager to show its own notification then the download keeps running in the background. But now there are two notifications are showing for the same download(Our custom notification, and the download manager notification).
  • The second option to disable Doze mode for our application does not work for us, because DownloadManager manages independently from our application. Also, it is not recommended by Google to use this mode unless our core feature is not dependent on this see https://developer.android.com/training/monitoring-device-state/doze-standby#support_for_other_use_cases By the way using this will not work for us.
  • A single approach is working in the current scenario is "Plug your mobile in charging", in this scenario download is continuing running in the background, because the phone not going into Doze mode.

So to properly fix this issue: We have to enable the DownloadManager's notification, but unfortunately, it does not have the pause button. So we would not be able to pause the download from the notification.

@mkatsevVR
Copy link
Author

mkatsevVR commented Dec 13, 2024 via email

@kelson42
Copy link
Collaborator

kelson42 commented Dec 13, 2024

So to properly fix this issue: We have to enable the DownloadManager's notification, but unfortunately, it does not have the pause button. So we would not be able to pause the download from the notification.

To me this is OK if we can only pause from the software itself. Not great, but preferable to a situation where the download pauses itself all the time.

@MohitMaliFtechiz
Copy link
Collaborator

Does https://developer.android.com/develop/background-work/services/fgs help?

@mkatsevVR We are already using the foreground service for tracking the progress of DownloadManager. It will not work.

@MohitMaliFtechiz
Copy link
Collaborator

@mkatsevVR Thanks for your patience, we have fixed this issue by enabling the DownloadManager's notification in #4130. Can you please try this APK https://drive.google.com/file/d/1x4aIJ7BlYBAfzEH_KtxhWxmiSg83vJYm/view?usp=sharing and give your feedback.

@mkatsevVR
Copy link
Author

mkatsevVR commented Dec 16, 2024 via email

@MohitMaliFtechiz
Copy link
Collaborator

MohitMaliFtechiz commented Dec 17, 2024

@mkatsevVR I have forcefully enabled the idle mode in my device and started downloading Bookdash which is a 10.9GB file, and close the application and then lock the screen for 30 minutes. After 30 minutes I unlocked the screen, and the download continued in the background with this new APK.
So there is no limitation now for downloading files in the background. However, if you are still facing the issue then it is probably any setting enabled.

  • I am assuming you have not enabled the "Battery Saver" mode.
  • Is your WIFI connection stable? Because this type of passing error happens when there is some network error occurs, and the download manager waits for a retry.

Image

  • Check the WIFI connection setting, and try to set the WIFI setting.
  • Click on connected WIFI and then check the "Network usage" setting. It should be set to "Detect Automatically" or "Treat as unmetered".
  • Also please check the device battery percentage(Sometimes DownloadManager pauses the downloading in the background when there is less battery available).
  • Is this error happening with a particular ZIM file or with all ZIM files?
  • Also try to reboot your device if nothing works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment