2023-01-31 21:13:39 +05:30
|
|
|
package com.github.libretube.helpers
|
2022-09-10 15:21:50 +05:30
|
|
|
|
|
|
|
import android.content.Context
|
2022-12-16 17:47:09 +05:30
|
|
|
import android.content.Intent
|
2022-10-07 23:18:55 +05:30
|
|
|
import android.os.Build
|
2023-01-18 09:36:50 +05:30
|
|
|
import androidx.core.content.ContextCompat
|
2022-12-16 17:47:09 +05:30
|
|
|
import com.github.libretube.constants.IntentData
|
2022-12-25 15:33:28 +05:30
|
|
|
import com.github.libretube.constants.PreferenceKeys
|
2022-12-21 21:10:48 +05:30
|
|
|
import com.github.libretube.db.obj.DownloadItem
|
2023-06-17 06:55:37 +05:30
|
|
|
import com.github.libretube.parcelable.DownloadData
|
2022-12-16 17:47:09 +05:30
|
|
|
import com.github.libretube.services.DownloadService
|
2023-03-07 07:14:09 +05:30
|
|
|
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"
|
2022-12-16 22:14:21 +05:30
|
|
|
const val SUBTITLE_DIR = "subtitle"
|
2022-11-11 23:09:56 +05:30
|
|
|
const val METADATA_DIR = "metadata"
|
|
|
|
const val THUMBNAIL_DIR = "thumbnail"
|
2022-12-16 22:14:21 +05:30
|
|
|
const val DOWNLOAD_CHUNK_SIZE = 8L * 1024
|
2022-12-21 21:10:48 +05:30
|
|
|
const val DEFAULT_TIMEOUT = 15 * 1000
|
2022-12-25 10:56:26 +05:30
|
|
|
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
|
2023-03-07 07:14:09 +05:30
|
|
|
} 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
|
|
|
|
2022-12-25 15:33:28 +05:30
|
|
|
fun getMaxConcurrentDownloads(): Int {
|
|
|
|
return PreferenceHelper.getString(
|
|
|
|
PreferenceKeys.MAX_CONCURRENT_DOWNLOADS,
|
2023-06-24 23:27:00 +05:30
|
|
|
"6"
|
2022-12-25 15:33:28 +05:30
|
|
|
).toFloat().toInt()
|
|
|
|
}
|
|
|
|
|
2023-06-09 07:04:19 +05:30
|
|
|
fun startDownloadService(context: Context, downloadData: DownloadData? = null) {
|
2022-12-16 17:47:09 +05:30
|
|
|
val intent = Intent(context, DownloadService::class.java)
|
2023-06-09 07:04:19 +05:30
|
|
|
.putExtra(IntentData.downloadData, downloadData)
|
2022-12-16 17:47:09 +05:30
|
|
|
|
2023-01-18 09:36:50 +05:30
|
|
|
ContextCompat.startForegroundService(context, intent)
|
2022-12-16 17:47:09 +05:30
|
|
|
}
|
2022-12-21 21:18:30 +05:30
|
|
|
|
|
|
|
fun DownloadItem.getNotificationId(): Int {
|
|
|
|
return Int.MAX_VALUE - id
|
|
|
|
}
|
2022-09-10 15:21:50 +05:30
|
|
|
}
|