Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 51f00d66 authored by Nihar Thakkar's avatar Nihar Thakkar
Browse files

Handle download cancellation

parent 3edcc1fc
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -68,7 +68,8 @@ class Application(val packageName: String, private val applicationManager: Appli
                return
            }
            DOWNLOADING -> {
                // TODO Cancel download
                downloader?.cancelDownload()
                return
            }
        }
        stateManager.find(activity, basicData!!)
@@ -101,6 +102,8 @@ class Application(val packageName: String, private val applicationManager: Appli
            applicationManager.install(this)
        } else {
            info.getApkFile(context, basicData!!).delete()
            applicationManager.stopDownloading(this)
            downloader = null
        }
        stateManager.find(context, basicData!!)
    }
+23 −9
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ class Downloader(private val applicationInfo: ApplicationInfo, private val fullD
                 private val downloaderInterface: DownloaderInterface) {
    private lateinit var downloadManager: DownloadManager
    private lateinit var request: DownloadManager.Request
    private var downloadId: Long = 0

    private val listeners = ArrayList<(Int, Int) -> Unit>()
    private var totalBytes = 0
@@ -27,7 +28,8 @@ class Downloader(private val applicationInfo: ApplicationInfo, private val fullD

    private val notifier = ThreadedListeners {
        listeners.forEach {
            it.invoke(downloadedBytes, totalBytes) }
            it.invoke(downloadedBytes, totalBytes)
        }
    }

    fun addListener(listener: (Int, Int) -> Unit) {
@@ -35,11 +37,12 @@ class Downloader(private val applicationInfo: ApplicationInfo, private val fullD
    }

    fun download(context: Context) {
        if (fullData.getLastVersion() != null) {
            downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
            registerReceivers(context)
        if (fullData.getLastVersion() != null) {
            initialiseDownloadManagerRequest(context)
            handleDownloadUpdates(downloadManager.enqueue(request))
            downloadId = downloadManager.enqueue(request)
            handleDownloadUpdates()
        } else {
            downloaderInterface.onDownloadComplete(context, DownloadManager.STATUS_FAILED)
        }
@@ -50,6 +53,10 @@ class Downloader(private val applicationInfo: ApplicationInfo, private val fullD
                IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
    }

    private fun unregisterReceivers(context: Context) {
        context.unregisterReceiver(onComplete)
    }

    private fun initialiseDownloadManagerRequest(context: Context) {
        request = DownloadManager.Request(
                Uri.parse(
@@ -64,15 +71,17 @@ class Downloader(private val applicationInfo: ApplicationInfo, private val fullD
                }
    }

    private fun handleDownloadUpdates(downloadId: Long) {
    private fun handleDownloadUpdates() {
        notifier.start()
        var isDownloading = true
        while (isDownloading) {
        while (true) {
            val query = DownloadManager.Query().apply {
                setFilterById(downloadId)
            }
            val cursor = downloadManager.query(query)
            cursor.moveToFirst()
            if (!cursor.moveToFirst()) {
                break
            }

            downloadedBytes = cursor.getInt(
                    cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))
            totalBytes = cursor.getInt(
@@ -81,12 +90,16 @@ class Downloader(private val applicationInfo: ApplicationInfo, private val fullD
            val downloadStatus = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
            if (downloadStatus == DownloadManager.STATUS_SUCCESSFUL ||
                    downloadStatus == DownloadManager.STATUS_FAILED) {
                isDownloading = false
                break
            }
        }
        notifier.stop()
    }

    fun cancelDownload() {
        downloadManager.remove(downloadId)
    }

    @Throws(Exception::class)
    private fun getApkFileSha1(file: File): String {
        val messageDigest = MessageDigest.getInstance("SHA-1")
@@ -115,6 +128,7 @@ class Downloader(private val applicationInfo: ApplicationInfo, private val fullD
                exception.printStackTrace()
            }
            downloaderInterface.onDownloadComplete(context, DownloadManager.STATUS_FAILED)
            unregisterReceivers(context)
        }
    }
}