Commit 6d652ce7 authored by Nihar Thakkar's avatar Nihar Thakkar
Browse files

Receive broadcast when app is installed

parent 12316d9b
......@@ -18,7 +18,7 @@ import io.eelo.appinstaller.utils.Error
import java.util.concurrent.atomic.AtomicInteger
class Application(val packageName: String, private val applicationManager: ApplicationManager) :
DownloaderInterface {
DownloaderInterface, InstallerInterface {
private val uses = AtomicInteger(0)
private val info = ApplicationInfo(packageName)
......@@ -120,7 +120,12 @@ class Application(val packageName: String, private val applicationManager: Appli
}
fun install(context: Context) {
info.install(context, basicData!!)
info.install(context, basicData!!, this)
stateManager.find(context, basicData!!)
}
override fun onInstallationComplete(context: Context) {
info.getApkFile(context, basicData!!).delete()
stateManager.find(context, basicData!!)
}
......
......@@ -46,7 +46,7 @@ class ApplicationInfo(private val packageName: String) {
context.startActivity(context.packageManager.getLaunchIntentForPackage(packageName))
}
fun install(context: Context, data: BasicData) {
Installer(getApkFile(context, data)).install(context)
fun install(context: Context, data: BasicData, callback: InstallerInterface) {
Installer(data.packageName, getApkFile(context, data), callback).install(context)
}
}
package io.eelo.appinstaller.application.model
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
import android.support.v4.content.FileProvider
import java.io.File
class Installer(private val apk: File) {
class Installer(private val packageName: String,
private val apk: File,
private val callback: InstallerInterface) {
fun install(context: Context) {
val uri = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
......@@ -20,5 +24,27 @@ class Installer(private val apk: File) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
registerReceiver(context)
}
private fun registerReceiver(context: Context) {
try {
context.unregisterReceiver(receiver)
} catch (exception: Exception) {
exception.printStackTrace()
}
context.registerReceiver(receiver, IntentFilter().apply {
addAction(Intent.ACTION_PACKAGE_ADDED)
addDataScheme("package")
})
}
private var receiver = object : BroadcastReceiver() {
override fun onReceive(p0: Context, p1: Intent) {
if (p1.action == Intent.ACTION_PACKAGE_ADDED &&
(p1.data.encodedSchemeSpecificPart == packageName)) {
callback.onInstallationComplete(p0)
}
}
}
}
package io.eelo.appinstaller.application.model
import android.content.Context
interface InstallerInterface {
fun onInstallationComplete(context: Context)
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment