Loading app/src/main/java/io/eelo/appinstaller/application/model/Application.kt +9 −2 Original line number Diff line number Diff line Loading @@ -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) { Loading Loading @@ -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 -> { } Loading @@ -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() Loading app/src/main/java/io/eelo/appinstaller/application/model/Downloader.kt +14 −6 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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) { Loading @@ -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)) { } } } Loading app/src/main/java/io/eelo/appinstaller/application/model/InstallManager.kt +7 −0 Original line number Diff line number Diff line Loading @@ -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)) { Loading Loading
app/src/main/java/io/eelo/appinstaller/application/model/Application.kt +9 −2 Original line number Diff line number Diff line Loading @@ -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) { Loading Loading @@ -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 -> { } Loading @@ -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() Loading
app/src/main/java/io/eelo/appinstaller/application/model/Downloader.kt +14 −6 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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) { Loading @@ -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)) { } } } Loading
app/src/main/java/io/eelo/appinstaller/application/model/InstallManager.kt +7 −0 Original line number Diff line number Diff line Loading @@ -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)) { Loading