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

Commit 154df470 authored by Nihar Thakkar's avatar Nihar Thakkar
Browse files

Allow cancelling of app downloads

parent 0c364308
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -12,7 +12,6 @@ import io.eelo.appinstaller.application.model.data.BasicData
import io.eelo.appinstaller.application.model.data.FullData
import io.eelo.appinstaller.utils.Error
import io.eelo.appinstaller.utils.Constants
import java.io.IOException
import java.util.concurrent.atomic.AtomicInteger

class Application(val packageName: String, private val installManager: InstallManager) {
@@ -59,7 +58,9 @@ class Application(val packageName: String, private val installManager: InstallMa
                }
            }
            DOWNLOADING -> {
                // TODO Cancel APK download
                stateManager.changeState(NOT_DOWNLOADED)
                installManager.removeDownload(packageName)
                cancelDownload()
            }
            INSTALLING -> {
            }
@@ -79,6 +80,12 @@ class Application(val packageName: String, private val installManager: InstallMa
        }
    }

    private fun cancelDownload() {
        if (downloader != null) {
            downloader!!.cancel()
        }
    }

    fun download(context: Context) {
        assertFullData(context)
        downloader = Downloader()
+14 −6
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ import io.eelo.appinstaller.application.model.data.FullData
import io.eelo.appinstaller.utils.Constants
import java.io.*
import java.net.URL
import java.net.URLConnection
import javax.net.ssl.HttpsURLConnection

class Downloader {
    var count = 0
@@ -17,13 +17,21 @@ class Downloader {
        listeners.forEach { it.invoke(count, total) }
    }

    private lateinit var connection: HttpsURLConnection
    private var isCancelled = false

    @Throws(IOException::class)
    fun download(data: FullData, apkFile: File) {
        createApkFile(apkFile)
        val url = URL(Constants.DOWNLOAD_URL + data.getLastVersion().downloadLink)
        val connection = url.openConnection()
        connection = url.openConnection() as HttpsURLConnection
        total = connection.contentLength
        transferBytes(connection, apkFile)
        transferBytes(apkFile)
    }

    fun cancel() {
        isCancelled = true
        connection.disconnect()
    }

    private fun createApkFile(apkFile: File) {
@@ -36,12 +44,12 @@ class Downloader {
    }

    @Throws(IOException::class)
    private fun transferBytes(connection: URLConnection, apkFile: File) {
        connection.getInputStream().use { input ->
    private fun transferBytes(apkFile: File) {
        connection.inputStream.use { input ->
            FileOutputStream(apkFile).use { output ->
                notifier.start()
                val buffer = ByteArray(1024)
                while (readAndWrite(input, output, buffer)) {
                while (!isCancelled && readAndWrite(input, output, buffer)) {
                }
            }
        }
+7 −0
Original line number Diff line number Diff line
@@ -27,6 +27,13 @@ class InstallManager {
        }
    }

    @Synchronized
    fun removeDownload(packageName: String) {
        if (downloading.contains(packageName)) {
            downloading.remove(packageName)
        }
    }

    @Synchronized
    fun install(packageName: String) {
        if (!installing.contains(packageName)) {