LibreTube/app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt

56 lines
1.8 KiB
Kotlin
Raw Normal View History

package com.github.libretube.helpers
2022-09-10 15:21:50 +05:30
import android.content.Context
import android.content.Intent
2022-10-07 23:18:55 +05:30
import android.os.Build
import androidx.core.content.ContextCompat
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.obj.DownloadItem
import com.github.libretube.parcelable.DownloadData
import com.github.libretube.services.DownloadService
import java.nio.file.Path
2023-07-26 05:03:08 +05:30
import kotlin.io.path.createDirectories
2022-09-10 15:21:50 +05:30
object DownloadHelper {
2022-11-11 23:09:56 +05:30
const val VIDEO_DIR = "video"
const val AUDIO_DIR = "audio"
const val SUBTITLE_DIR = "subtitle"
2022-11-11 23:09:56 +05:30
const val METADATA_DIR = "metadata"
const val THUMBNAIL_DIR = "thumbnail"
const val DOWNLOAD_CHUNK_SIZE = 8L * 1024
const val DEFAULT_TIMEOUT = 15 * 1000
const val DEFAULT_RETRY = 3
2022-11-11 23:09:56 +05:30
2023-07-26 05:03:08 +05:30
fun getDownloadDir(context: Context, path: String): Path {
val storageDir = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
2022-10-07 23:18:55 +05:30
context.filesDir
} else {
try {
context.getExternalFilesDir(null)!!
} catch (e: Exception) {
context.filesDir
}
2022-10-07 23:18:55 +05:30
}
2023-07-26 05:03:08 +05:30
return storageDir.toPath().resolve(path).createDirectories()
2022-09-10 15:21:50 +05:30
}
2022-09-10 15:40:45 +05:30
fun getMaxConcurrentDownloads(): Int {
return PreferenceHelper.getString(
PreferenceKeys.MAX_CONCURRENT_DOWNLOADS,
"6"
).toFloat().toInt()
}
2023-06-09 07:04:19 +05:30
fun startDownloadService(context: Context, downloadData: DownloadData? = null) {
val intent = Intent(context, DownloadService::class.java)
2023-06-09 07:04:19 +05:30
.putExtra(IntentData.downloadData, downloadData)
ContextCompat.startForegroundService(context, intent)
}
fun DownloadItem.getNotificationId(): Int {
return Int.MAX_VALUE - id
}
2022-09-10 15:21:50 +05:30
}