Created
June 19, 2024 13:19
-
-
Save sajjadyousefnia/4e4ca7be2e1fa592d31821cfeedf2240 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SuppressLint("Range") | |
private suspend fun downloadFromScratch() { | |
try { | |
val currentDownloadModel = runningDownloadList.last() | |
val downloadManager = getSystemService(DownloadManager::class.java) | |
val request = | |
DownloadManager.Request(currentDownloadModel.url.toUri()).setMimeType("video/*") | |
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI and DownloadManager.Request.NETWORK_MOBILE) | |
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE) | |
.setTitle("در حال دانلود فیلم " + currentDownloadModel.title) | |
.setDestinationInExternalPublicDir( | |
Environment.DIRECTORY_DOWNLOADS, | |
DOWNLOADS_SUB_DIRECTORY + currentDownloadModel.title | |
) | |
val downloadId = downloadManager.enqueue(request) | |
val query = DownloadManager.Query().setFilterById(downloadId) | |
var isDownloading = true | |
db.appDao().insertNewDownload(currentDownloadModel) | |
withContext(Dispatchers.Main) { | |
runningAdapter.notifyDataSetChanged() | |
} | |
while (isDownloading) { | |
val cursor = downloadManager.query(query) | |
if (cursor.moveToFirst()) { | |
val bytesDownloaded = cursor.getLong( | |
cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR) | |
) | |
val bytesTotal = cursor.getLong( | |
cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES) | |
) | |
// if (downloadModel.totalBytes == 0L) { | |
currentDownloadModel.totalBytes = bytesTotal | |
// } | |
val status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) | |
if (status == DownloadManager.STATUS_SUCCESSFUL) { | |
isDownloading = false | |
} else if (status == DownloadManager.STATUS_FAILED) { | |
val reason = | |
cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON)) | |
// listener?.onFailed("Download failed: $reason") | |
} else if (status == DownloadManager.STATUS_PAUSED) { | |
} | |
if (status == DownloadManager.STATUS_RUNNING) { | |
Log.d(TAG, "downloadFromScratch: download running") | |
if (currentDownloadModel.isStopped == true) { | |
currentDownloadModel.isStopped = false | |
db.appDao().checkDownloadStopStatus(currentDownloadModel.id, false) | |
withContext(Dispatchers.Main) { | |
runningAdapter.notifyDataSetChanged() | |
} | |
} | |
} else { | |
Log.d(TAG, "downloadFromScratch: download stopped") | |
if (currentDownloadModel.isStopped == false) { | |
currentDownloadModel.isStopped = true | |
db.appDao().checkDownloadStopStatus(currentDownloadModel.id, true) | |
withContext(Dispatchers.Main) { | |
runningAdapter.notifyDataSetChanged() | |
} | |
} | |
} | |
if (bytesTotal > 0) { | |
val progress = (bytesDownloaded * 100L / bytesTotal).toInt() | |
db.appDao() | |
.updatePercentageDownload(downloadId, bytesDownloaded, progress) | |
val downloadItem = runningDownloadList.first() | |
downloadItem.downloadedBytes = bytesDownloaded.toLong() | |
downloadItem.percentage = progress | |
val speed = calculateDownloadSpeed(cursor) | |
if (speed != -1L && speed != 0L) { | |
downloadItem.currentSpeed = speed | |
downloadItem.remaingTime = guessRemainingTimeFinish( | |
bytesDownloaded, bytesTotal, speed.toLong() | |
) | |
} | |
if (bytesDownloaded == bytesTotal) { | |
doOnCompleteDownload(downloadItem) | |
} | |
withContext(Dispatchers.Main) { | |
runningAdapter.notifyDataSetChanged() | |
} | |
// listener?.onProgress(progress) | |
} | |
} | |
cursor.close() | |
} | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment