Skip to content

Commit

Permalink
notification stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
LagradOst committed Sep 20, 2022
1 parent aee49f4 commit 8423aad
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class MainActivity : AppCompatActivity() {
UUID.randomUUID().toString(),
4337,
filesDir.path,
"${filesDir.path}/session"
//"${filesDir.path}/session"
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ data class NotificationMetaData(
}

object DefaultNotificationBuilder {
@DrawableRes
var imgWaiting = R.drawable.download_icon_load

@DrawableRes
var imgDone = R.drawable.download_icon_done

Expand All @@ -99,12 +102,16 @@ object DefaultNotificationBuilder {
@DrawableRes
var pressToStopIcon = R.drawable.ic_baseline_stop_24


private var hasCreatedNotificationChannel = false
private fun createNotificationChannel(
context: Context,
channelId: String,
channelName: String,
channelDescription: String
) {
if (hasCreatedNotificationChannel) return
hasCreatedNotificationChannel = true
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Expand Down Expand Up @@ -138,6 +145,8 @@ object DefaultNotificationBuilder {
): NotificationData? {
try {
if (download.items.isEmpty()) return null // crash, invalid data
// if(download.status == DownloadStatusTell.Waiting) return null

createNotificationChannel(
context,
context.getString(R.string.download_channel_id),
Expand All @@ -163,9 +172,10 @@ object DefaultNotificationBuilder {
when (download.status) {
DownloadStatusTell.Complete -> imgDone
DownloadStatusTell.Active -> imgDownloading
DownloadStatusTell.Waiting, DownloadStatusTell.Paused -> imgPaused
DownloadStatusTell.Paused -> imgPaused
DownloadStatusTell.Error -> imgError
DownloadStatusTell.Removed -> imgStopped
DownloadStatusTell.Waiting -> imgWaiting
else -> imgDownloading
}
)
Expand All @@ -178,30 +188,41 @@ object DefaultNotificationBuilder {
builder.setContentIntent(pendingIntent)
}

if (download.status == DownloadStatusTell.Active || download.status == DownloadStatusTell.Paused) {
if (download.progressPercentage < 0) {
if (download.status == DownloadStatusTell.Active
|| download.status == DownloadStatusTell.Paused
|| download.status == DownloadStatusTell.Waiting
) {
if (download.downloadedLength <= 1024 || download.status == DownloadStatusTell.Waiting) {
builder.setProgress(0, 0, true)
} else {
builder.setProgress(100, download.progressPercentage, false)
}
}

val downloadFormat = context.getString(R.string.download_format)
val downloadProgressFormat = context.getString(R.string.download_progress_format)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val posterBitmap = notificationMetaData.posterBitmap
if (posterBitmap != null)
builder.setLargeIcon(posterBitmap)

val progressMbString = "%.1f MB".format(download.downloadedLength / 1000000f)
val totalMbString = "%.1f MB".format(download.totalLength / 1000000f)
val suffix = ""
val progressMb = download.downloadedLength / 1000000f
val totalMb = download.totalLength / 1000000f
val downloadSpeedMb = download.downloadSpeed / 1000000f

val bigText =
when (download.status) {
DownloadStatusTell.Active, DownloadStatusTell.Paused -> {
(notificationMetaData.linkName?.let { "$it\n" }
?: "") + "${notificationMetaData.secondRow}\n${download.progressPercentage} % ($progressMbString/$totalMbString)$suffix"
?: "") +
downloadProgressFormat.format(
notificationMetaData.secondRow,
download.progressPercentage,
progressMb,
totalMb,
downloadSpeedMb
)
}
DownloadStatusTell.Error -> {
downloadFormat.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ abstract class AbstractClient(
}

fun download(request: UriRequest, callback: (String) -> Unit) {
println("REQUESTED: ${request.uris}")
scope.launch {
val result = sendUri(request)
if (result.isFailure) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ data class Metadata(
val totalLength by lazy { items.sumOf { it.totalLength } }
val downloadedLength by lazy { items.sumOf { it.completedLength } }
val progressPercentage by lazy { (downloadedLength * 100L / (totalLength + 1L)).toInt() }
val downloadSpeed by lazy { items.sumOf { it.downloadSpeed } }
}

fun getStatus(status: ArrayList<AbstractClient.JsonTell>): DownloadStatusTell? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package com.lagradost.fetchbutton.aria2c

import android.app.Activity
import android.util.Log
import androidx.core.app.NotificationManagerCompat
import com.lagradost.fetchbutton.Aria2Save.removeKey
import com.lagradost.fetchbutton.aria2c.AbstractClient.DownloadListener.sessionIdToLastRequest
import com.lagradost.fetchbutton.utils.Coroutines.mainThread
import kotlinx.coroutines.sync.withLock
import java.io.File
import java.lang.ref.WeakReference
Expand All @@ -11,7 +14,7 @@ data class Aria2Settings(
val token: String,
val port: Int,
val dir: String,
val sessionDir: String?, // null for not save
val sessionDir: String? = null, // null for not save
//val checkCertificate : Boolean = false,
)

Expand Down Expand Up @@ -76,25 +79,34 @@ object Aria2Starter {
deleteFiles(files)
}

// remove keys
id?.let { pid ->
saveActivity.get()?.removeKey(pid)
gid?.let { localGid ->
AbstractClient.DownloadListener.sessionIdToLastRequest.remove(pid)

// remove id from session
AbstractClient.DownloadListener.remove(localGid, pid)
// remove keys
if (id == null) return

saveActivity.get()?.removeKey(id)
sessionIdToLastRequest[id]?.notificationMetaData?.id?.let { nid ->
mainThread {
NotificationManagerCompat.from(
saveActivity.get() ?: return@mainThread
).cancel(nid)
}
}
sessionIdToLastRequest.remove(id)

// remove aria2
Aria2Starter.client?.run {
AbstractClient.DownloadListener.failQueueMapMutex.withLock {
AbstractClient.DownloadListener.failQueueMap.remove(localGid)
}
gid?.let { localGid ->

AbstractClient.DownloadListener.currentDownloadStatus.remove(localGid)
// remove id from session
AbstractClient.DownloadListener.remove(localGid, id)

remove(localGid)
// remove aria2
client?.run {
AbstractClient.DownloadListener.failQueueMapMutex.withLock {
AbstractClient.DownloadListener.failQueueMap.remove(localGid)
}

AbstractClient.DownloadListener.currentDownloadStatus.remove(localGid)

remove(localGid)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import com.lagradost.fetchbutton.aria2c.Aria2Starter
class VideoDownloadService : IntentService("VideoDownloadService") {
@Deprecated("Deprecated in Java")
override fun onHandleIntent(intent: Intent?) {
println("onHandleIntent!!!")
if (intent != null) {
val gid = intent.getStringExtra("gid")
val id = intent.getLongExtra("id", 0L)
val type = intent.getIntExtra("type", -1)
println(">>>> gid = $gid id = $id type = $type!!!")
if (!gid.isNullOrBlank() && type != -1 && id != 0L) {
val state = DownloadActionType.values().getOrNull(type) ?: return
when (state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,24 @@ abstract class BaseFetchButton(context: Context, attributeSet: AttributeSet) :
return true
}
} ?: run {
//val req = lastRequest ?: return@run
//var hasAny = false
//for (file in files) {
// try {
// val filePath = file.path + ".aria2"
// val f = File(filePath)
// if (f.exists()) {
// performDownload(req.copy(uris = listOf(filePath)))
// hasAny = true
// }
// } catch (e: Exception) {
// println(e)
// }
//}
//
//if (!hasAny)
performDownload(lastRequest ?: return@run)
return false
return true
}
return false
}
Expand Down
3 changes: 2 additions & 1 deletion library/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="download_format" translatable="false">%s - %s</string>
<string name="download_format" translatable="false" formatted="true">%s - %s</string>
<string name="download_progress_format" translatable="false" formatted="true">%s\n%d %% (%.1f MB/%.1f MB) %.1f MB/s</string>
<string name="download_paused">Download Paused</string>
<!--<string name="download_started">Download Started</string>-->
<string name="download_failed">Download Failed</string>
Expand Down

0 comments on commit 8423aad

Please sign in to comment.