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 file in foreground service #31

Open
p3g4asus opened this issue Jun 27, 2022 · 10 comments
Open

Download file in foreground service #31

p3g4asus opened this issue Jun 27, 2022 · 10 comments
Labels
enhancement New feature or request

Comments

@p3g4asus
Copy link
Contributor

Hi
Are you thinking about placing download task in a foreground service so that it is more unlikely to be killed by android OS? If you like the idea and you are not already working on it, I can work to do it. I am thinking about placing the download task in a foreground service, so that, if the app is killed, the downloads continues. If the app is manually restarted after being killed while the service is still alive, the app connects to the service and shows the download progress.
What do you think?

@JunkFood02
Copy link
Owner

JunkFood02 commented Jun 27, 2022

In fact I know little about Android's background management. OEMs are usually likely to override this part of Android OS as well. Behaviors become more unpredictable due to several reasons.
youtubedl-andoird packages a whole python runtime in it, which is a native library running yt-dlp python codes (very creative and impressive). I've tried cancelling coroutine job during download process, and it just cancelled the Java/Native(Python) interaction, with python keep running in background and finish downloading. This makes me confused and decide to throw back such problems back to users - let them just whitelist Seal.
I didn't use WorkManager suggests by Android because dvd using this library also get killed on my phone when running in background. It could be weird that developers follow the best practices meanwhile the OEMs not. 🥶
Off-topic talks aside, I'm not planned to work on this currently, and I think the idea of foreground service look classical and neat. So feel free to implement your ideas!

@JunkFood02 JunkFood02 added the enhancement New feature or request label Jun 27, 2022
@p3g4asus
Copy link
Contributor Author

p3g4asus commented Jun 30, 2022

This is what can happen when putting Seal in background in background in my Pixel 4XL (latest Android 12 update, no battery restriction)

[06-29 23:41:36.444 1667:1794 I/ActivityManager]
Killing 2580:com.junkfood.seal/u0a463 (adj 940): excessive cpu 19250 during 300000 dur=1169410 limit=2

[06-29 23:41:36.456 1667:1806 I/libprocessgroup]
Successfully killed process cgroup uid 10463 pid 2580 in 11ms

Will this be fixed putting the download in a background service? I think so but I am not sure.
EDIT: From this github issue thread, it looks like foreground service will fix the problem.

@JunkFood02
Copy link
Owner

I see. Could we just start a foreground service which only display a "Downloading" message without doing anything, and prevent Seal get killed at the same time? I feel pain dealing with interaction between UI and service.

@p3g4asus
Copy link
Contributor Author

I see. Could we just start a foreground service which only display a "Downloading" message without doing anything, and prevent Seal get killed at the same time? I feel pain dealing with interaction between UI and service.

I don't know if this can help but I don't think so. However I can try. This weekend I will make some tests and think a way to minimize the service / UI interaction work.

@Iamlooker
Copy link

Could we just start a foreground service which only display a "Downloading" message without doing anything, and prevent Seal get killed at the same time?

Yes that would work, but that would not be suggested.
I would suggest at least showing the Title and a indeterminate progress bar and hide notification as the download finishes, Or change the title to finished.

I feel pain dealing with interaction between UI and service.

Well yeah the Intent handling part is really really bad, the code for all Android Components are written in Java and most of the stuff is nullable hence you need to take care of all the nullability.

A different way that I personally like is that you can keep a service running as soon as user clickes Download, service "observes"/"collects" from a flow and if the flow is empty it will show empty notification and as the flow keeps stacking you can show the Titles in the notification.

@JunkFood02
Copy link
Owner

JunkFood02 commented Aug 13, 2022

We discussed this in #38 before. Currently, I'm using the workaround I've mentioned here and the background downloading works well.

@xibr
Copy link

xibr commented Aug 21, 2022

Even with battery optimization disable, Android still kills the download process in the background, try removing the app from recent apps the download will stop.

Tested on android 10

@JunkFood02
Copy link
Owner

Even with battery optimization disable, Android still kills the download process in the background, try removing the app from recent apps the download will stop.

Tested on android 10

Removing the app from recent apps means killing the app by force. I think the current workaround works well with long-time downloading and also provides the flexibility for not modifying many existing codes

@xibr
Copy link

xibr commented Aug 21, 2022

Yes, I know that, but this also happens when converting to mp3 especially for long clips, but when using work manager I don't think such problems will happen.

@mhss1
Copy link

mhss1 commented Nov 15, 2022

Hey, @JunkFood02 Nice app 👍
Did you try to use the WorkManager setForeground() feature? It's useful for long running operations like downloading so the Android system does not kill the app.
Docs:https://developer.android.com/topic/libraries/architecture/workmanager/advanced/long-running

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants