Skip to content

Commit

Permalink
Fix notifying of installation status
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed Nov 23, 2023
1 parent 625956a commit 38491ff
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.sanmer.pi.compat

import android.content.IntentSender
import android.content.pm.ApplicationInfo
import android.content.pm.IPackageInstaller
import android.content.pm.IPackageInstallerSession
Expand All @@ -9,13 +8,10 @@ import android.content.pm.PackageInfo
import android.content.pm.PackageInstaller
import android.content.pm.PackageInstallerHidden
import android.content.pm.PackageInstallerHidden.SessionParamsHidden
import android.content.pm.PackageManager
import android.content.pm.PackageManagerHidden
import android.content.pm.ParceledListSlice
import android.content.pm.VersionedPackage
import android.os.Process
import dev.rikka.tools.refine.Refine
import dev.sanmer.pi.BuildConfig
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import rikka.shizuku.ShizukuBinderWrapper
Expand Down Expand Up @@ -139,7 +135,9 @@ object PackageManagerCompat {
session.commit(intentSender)
}

Timber.i("Install successful: $packageName")
val msg = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE)
if (msg != null) Timber.d("Install finished: $msg")

intent.getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE)
}
} catch (e: Exception) {
Expand Down
47 changes: 33 additions & 14 deletions app/src/main/kotlin/dev/sanmer/pi/service/InstallService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ import javax.inject.Inject
@AndroidEntryPoint
class InstallService: LifecycleService() {
private val context: Context by lazy { applicationContext }
private val taskCount = MutableStateFlow(0)
private val tasks = MutableStateFlow(0)

@Inject
lateinit var userPreferencesRepository: UserPreferencesRepository

init {
taskCount.drop(1)
tasks.drop(1)
.onEach {
if (it == 0) {
delay(5000L)
stopSelf()
delay(10_000L)
if (tasks.value == 0) stopSelf()
}
}.launchIn(lifecycleScope)
}
Expand All @@ -62,8 +62,8 @@ class InstallService: LifecycleService() {
val originating = userPreferencesRepository.getRequesterPackageNameOrDefault()
val installer = userPreferencesRepository.getExecutorPackageNameOrDefault()

taskCount.value += 1
val id = taskCount.value
tasks.value += 1
val id = tasks.value

val archiveInfo = getArchiveInfo(packageFile) ?: return@launch
val label = archiveInfo.applicationInfo
Expand All @@ -86,11 +86,11 @@ class InstallService: LifecycleService() {
val state = install.await()
when (state) {
PackageInstaller.STATUS_SUCCESS -> notifySuccess(id, label, appIcon)
PackageInstaller.STATUS_FAILURE -> notifyFailure(id, label)
else -> notifyFailure(id, label, appIcon)
}

packageFile.delete()
taskCount.value -= 1
tasks.value -= 1
}

return super.onStartCommand(intent, flags, startId)
Expand All @@ -115,6 +115,7 @@ class InstallService: LifecycleService() {
val notification = NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_INSTALL)
.setSmallIcon(R.drawable.launcher_outline)
.setSilent(true)
.setOngoing(true)
.setGroup(GROUP_KEY)
.setGroupSummary(true)
.build()
Expand All @@ -125,13 +126,15 @@ class InstallService: LifecycleService() {
private fun buildNotification(
title: String,
message: String,
largeIcon: Bitmap? = null,
silent: Boolean = false,
largeIcon: Bitmap? = null
ongoing: Boolean = false,
) = NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_INSTALL)
.setSmallIcon(R.drawable.launcher_outline)
.setContentTitle(title)
.setContentText(message)
.setSilent(silent)
.setOngoing(ongoing)
.setGroup(GROUP_KEY)
.apply {
largeIcon?.let { setLargeIcon(it) }
Expand All @@ -151,21 +154,37 @@ class InstallService: LifecycleService() {

private fun notifyInstalling(id: Int, title: String, largeIcon: Bitmap) {
val message = context.getString(R.string.message_installing)
val notification = buildNotification(title, message, true, largeIcon)
val notification = buildNotification(
title = title,
message = message,
largeIcon = largeIcon,
silent = true,
ongoing = true
)

notify(id, notification)
}

private fun notifySuccess(id: Int, title: String, largeIcon: Bitmap) {
val message = context.getString(R.string.message_install_success)
val notification = buildNotification(title, message, true, largeIcon)
val notification = buildNotification(
title = title,
message = message,
largeIcon = largeIcon,
silent = true
)

notify(id, notification)
}

private fun notifyFailure(id: Int, title: String) {
private fun notifyFailure(id: Int, title: String, largeIcon: Bitmap) {
val message = context.getString(R.string.message_install_fail)
val notification = buildNotification(title, message, false)
val notification = buildNotification(
title = title,
message = message,
largeIcon = largeIcon,
silent = false
)

notify(id, notification)
}
Expand All @@ -177,7 +196,7 @@ class InstallService: LifecycleService() {
private val Intent.packagePathOrNull get() = getStringExtra(PARAM_PACKAGE_PATH)
private val Intent.packageFile get() = checkNotNull(packagePathOrNull).let(::File)

fun Context.startInstallService(packageFile: File, ) {
fun Context.startInstallService(packageFile: File) {
val intent = Intent(this, InstallService::class.java)
intent.putExtra(PARAM_PACKAGE_PATH, packageFile.path)

Expand Down

0 comments on commit 38491ff

Please sign in to comment.