InstallerStatusReceiver: Pass required data as extra instead of parcel

Old Android versions experience a crash otherwise for some reason

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2024-06-11 16:52:33 +05:30
parent d8ba59ad53
commit c92c4220d7
6 changed files with 68 additions and 40 deletions

View File

@@ -57,7 +57,11 @@ class AppInstaller @Inject constructor(
) { ) {
companion object { companion object {
const val EXTRA_DOWNLOAD = "com.aurora.store.data.installer.AppInstaller.EXTRA_DOWNLOAD" const val ACTION_INSTALL_STATUS = "com.aurora.store.data.installer.AppInstaller.INSTALL_STATUS"
const val EXTRA_PACKAGE_NAME = "com.aurora.store.data.installer.AppInstaller.EXTRA_PACKAGE_NAME"
const val EXTRA_VERSION_CODE = "com.aurora.store.data.installer.AppInstaller.EXTRA_VERSION_CODE"
const val EXTRA_DISPLAY_NAME = "com.aurora.store.data.installer.AppInstaller.EXTRA_DISPLAY_NAME"
fun getAvailableInstallersInfo(context: Context): List<InstallerInfo> { fun getAvailableInstallersInfo(context: Context): List<InstallerInfo> {
val installers = mutableListOf( val installers = mutableListOf(
@@ -84,11 +88,11 @@ class AppInstaller @Inject constructor(
return installers return installers
} }
fun notifyInstallation(context: Context, download: Download) { fun notifyInstallation(context: Context, displayName: String, packageName: String) {
val notificationManager = val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notification = NotificationUtil.getInstallNotification(context, download) val notification = NotificationUtil.getInstallNotification(context, displayName, packageName)
notificationManager.notify(download.packageName.hashCode(), notification) notificationManager.notify(packageName.hashCode(), notification)
} }
fun getErrorString(context: Context, status: Int): String { fun getErrorString(context: Context, status: Int): String {

View File

@@ -56,7 +56,7 @@ abstract class InstallerBase(protected var context: Context) : IInstaller {
open fun onInstallationSuccess() { open fun onInstallationSuccess() {
download?.let { download?.let {
AppInstaller.notifyInstallation(context, it) AppInstaller.notifyInstallation(context, it.displayName, it.packageName)
if (Preferences.getBoolean(context, PREFERENCE_AUTO_DELETE)) { if (Preferences.getBoolean(context, PREFERENCE_AUTO_DELETE)) {
PathUtil.getAppDownloadDir(context, it.packageName, it.versionCode) PathUtil.getAppDownloadDir(context, it.packageName, it.versionCode)
.deleteRecursively() .deleteRecursively()

View File

@@ -37,7 +37,10 @@ import com.aurora.extensions.isTAndAbove
import com.aurora.extensions.isUAndAbove import com.aurora.extensions.isUAndAbove
import com.aurora.extensions.runOnUiThread import com.aurora.extensions.runOnUiThread
import com.aurora.store.R import com.aurora.store.R
import com.aurora.store.data.installer.AppInstaller.Companion.EXTRA_DOWNLOAD import com.aurora.store.data.installer.AppInstaller.Companion.ACTION_INSTALL_STATUS
import com.aurora.store.data.installer.AppInstaller.Companion.EXTRA_DISPLAY_NAME
import com.aurora.store.data.installer.AppInstaller.Companion.EXTRA_PACKAGE_NAME
import com.aurora.store.data.installer.AppInstaller.Companion.EXTRA_VERSION_CODE
import com.aurora.store.data.model.InstallerInfo import com.aurora.store.data.model.InstallerInfo
import com.aurora.store.data.receiver.InstallerStatusReceiver import com.aurora.store.data.receiver.InstallerStatusReceiver
import com.aurora.store.data.room.download.Download import com.aurora.store.data.room.download.Download
@@ -195,10 +198,11 @@ class SessionInstaller @Inject constructor(
private fun getCallBackIntent(packageName: String, sessionId: Int): PendingIntent? { private fun getCallBackIntent(packageName: String, sessionId: Int): PendingIntent? {
val callBackIntent = Intent(context, InstallerStatusReceiver::class.java).apply { val callBackIntent = Intent(context, InstallerStatusReceiver::class.java).apply {
action = InstallerStatusReceiver.ACTION_INSTALL_STATUS action = ACTION_INSTALL_STATUS
setPackage(context.packageName) setPackage(context.packageName)
putExtra(PackageInstaller.EXTRA_PACKAGE_NAME, packageName) putExtra(EXTRA_PACKAGE_NAME, packageName)
putExtra(EXTRA_DOWNLOAD, download) putExtra(EXTRA_VERSION_CODE, download!!.versionCode)
putExtra(EXTRA_DISPLAY_NAME, download!!.displayName)
addFlags(Intent.FLAG_RECEIVER_FOREGROUND) addFlags(Intent.FLAG_RECEIVER_FOREGROUND)
} }

View File

@@ -32,7 +32,10 @@ import androidx.core.app.PendingIntentCompat
import com.aurora.extensions.isOAndAbove import com.aurora.extensions.isOAndAbove
import com.aurora.extensions.isSAndAbove import com.aurora.extensions.isSAndAbove
import com.aurora.store.R import com.aurora.store.R
import com.aurora.store.data.installer.AppInstaller.Companion.EXTRA_DOWNLOAD import com.aurora.store.data.installer.AppInstaller.Companion.ACTION_INSTALL_STATUS
import com.aurora.store.data.installer.AppInstaller.Companion.EXTRA_DISPLAY_NAME
import com.aurora.store.data.installer.AppInstaller.Companion.EXTRA_PACKAGE_NAME
import com.aurora.store.data.installer.AppInstaller.Companion.EXTRA_VERSION_CODE
import com.aurora.store.data.model.InstallerInfo import com.aurora.store.data.model.InstallerInfo
import com.aurora.store.data.receiver.InstallerStatusReceiver import com.aurora.store.data.receiver.InstallerStatusReceiver
import com.aurora.store.data.room.download.Download import com.aurora.store.data.room.download.Download
@@ -97,14 +100,27 @@ class ShizukuInstaller @Inject constructor(
download.sharedLibs.forEach { download.sharedLibs.forEach {
// Shared library packages cannot be updated // Shared library packages cannot be updated
if (!isSharedLibraryInstalled(context, it.packageName, it.versionCode)) { if (!isSharedLibraryInstalled(context, it.packageName, it.versionCode)) {
install(download.packageName, download.versionCode, it.packageName) install(
packageName = download.packageName,
versionCode = download.versionCode,
sharedLibPkgName = it.packageName
)
} }
} }
install(download.packageName, download.versionCode) install(
packageName = download.packageName,
versionCode = download.versionCode,
displayName = download.displayName
)
} }
} }
private fun install(packageName: String, versionCode: Int, sharedLibPkgName: String = "") { private fun install(
packageName: String,
versionCode: Int,
sharedLibPkgName: String = "",
displayName: String = ""
) {
Log.i("Received session install request for ${sharedLibPkgName.ifBlank { packageName }}") Log.i("Received session install request for ${sharedLibPkgName.ifBlank { packageName }}")
val (sessionId, session) = kotlin.runCatching { val (sessionId, session) = kotlin.runCatching {
@@ -148,11 +164,12 @@ class ShizukuInstaller @Inject constructor(
} }
val callBackIntent = Intent(context, InstallerStatusReceiver::class.java).apply { val callBackIntent = Intent(context, InstallerStatusReceiver::class.java).apply {
action = InstallerStatusReceiver.ACTION_INSTALL_STATUS action = ACTION_INSTALL_STATUS
setPackage(context.packageName) setPackage(context.packageName)
addFlags(Intent.FLAG_RECEIVER_FOREGROUND) addFlags(Intent.FLAG_RECEIVER_FOREGROUND)
putExtra(PackageInstaller.EXTRA_PACKAGE_NAME, sharedLibPkgName.ifBlank { packageName }) putExtra(EXTRA_PACKAGE_NAME, sharedLibPkgName.ifBlank { packageName })
putExtra(EXTRA_DOWNLOAD, download) putExtra(EXTRA_VERSION_CODE, versionCode)
putExtra(EXTRA_DISPLAY_NAME, displayName)
} }
val pendingIntent = PendingIntentCompat.getBroadcast( val pendingIntent = PendingIntentCompat.getBroadcast(

View File

@@ -29,8 +29,10 @@ import com.aurora.extensions.runOnUiThread
import com.aurora.store.R import com.aurora.store.R
import com.aurora.store.data.event.InstallerEvent import com.aurora.store.data.event.InstallerEvent
import com.aurora.store.data.installer.AppInstaller import com.aurora.store.data.installer.AppInstaller
import com.aurora.store.data.installer.AppInstaller.Companion.EXTRA_DOWNLOAD import com.aurora.store.data.installer.AppInstaller.Companion.ACTION_INSTALL_STATUS
import com.aurora.store.data.room.download.Download import com.aurora.store.data.installer.AppInstaller.Companion.EXTRA_DISPLAY_NAME
import com.aurora.store.data.installer.AppInstaller.Companion.EXTRA_PACKAGE_NAME
import com.aurora.store.data.installer.AppInstaller.Companion.EXTRA_VERSION_CODE
import com.aurora.store.util.CommonUtil.inForeground import com.aurora.store.util.CommonUtil.inForeground
import com.aurora.store.util.NotificationUtil import com.aurora.store.util.NotificationUtil
import com.aurora.store.util.PackageUtil import com.aurora.store.util.PackageUtil
@@ -43,30 +45,25 @@ import org.greenrobot.eventbus.EventBus
@AndroidEntryPoint @AndroidEntryPoint
class InstallerStatusReceiver : BroadcastReceiver() { class InstallerStatusReceiver : BroadcastReceiver() {
companion object {
const val ACTION_INSTALL_STATUS =
"com.aurora.store.data.receiver.InstallReceiver.INSTALL_STATUS"
}
private val TAG = InstallerStatusReceiver::class.java.simpleName private val TAG = InstallerStatusReceiver::class.java.simpleName
override fun onReceive(context: Context?, intent: Intent?) { override fun onReceive(context: Context?, intent: Intent?) {
if (context != null && intent?.action == ACTION_INSTALL_STATUS) { if (context != null && intent?.action == ACTION_INSTALL_STATUS) {
val packageName = intent.getStringExtra(PackageInstaller.EXTRA_PACKAGE_NAME)!! val packageName = intent.getStringExtra(EXTRA_PACKAGE_NAME)!!
val displayName = intent.getStringExtra(EXTRA_DISPLAY_NAME)!!
val versionCode = intent.getIntExtra(EXTRA_VERSION_CODE, -1)
val status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, -1) val status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, -1)
val extra = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE) val extra = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE)
val download = IntentCompat.getParcelableExtra(
intent, EXTRA_DOWNLOAD, Download::class.java
)!!
// If package was successfully installed, exit after notifying user and doing cleanup // If package was successfully installed, exit after notifying user and doing cleanup
if (status == PackageInstaller.STATUS_SUCCESS) { if (status == PackageInstaller.STATUS_SUCCESS) {
// No post-install steps for shared libraries // No post-install steps for shared libraries
if (PackageUtil.isSharedLibrary(context, packageName)) return if (PackageUtil.isSharedLibrary(context, packageName)) return
AppInstaller.notifyInstallation(context, download) AppInstaller.notifyInstallation(context, displayName, packageName)
if (Preferences.getBoolean(context, PREFERENCE_AUTO_DELETE)) { if (Preferences.getBoolean(context, PREFERENCE_AUTO_DELETE)) {
PathUtil.getAppDownloadDir(context, download.packageName, download.versionCode) PathUtil.getAppDownloadDir(context, packageName, versionCode)
.deleteRecursively() .deleteRecursively()
} }
return return
@@ -76,20 +73,21 @@ class InstallerStatusReceiver : BroadcastReceiver() {
promptUser(intent, context) promptUser(intent, context)
} else { } else {
postStatus(status, packageName, extra, context) postStatus(status, packageName, extra, context)
notifyUser(context, download, status) notifyUser(context, packageName, displayName, status)
} }
} }
} }
private fun notifyUser(context: Context, download: Download, status: Int) { private fun notifyUser(context: Context, packageName: String, displayName: String, status: Int) {
val notificationManager = val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notification = NotificationUtil.getInstallerStatusNotification( val notification = NotificationUtil.getInstallerStatusNotification(
context, context,
download, packageName,
displayName,
AppInstaller.getErrorString(context, status) AppInstaller.getErrorString(context, status)
) )
notificationManager.notify(download.packageName.hashCode(), notification) notificationManager.notify(packageName.hashCode(), notification)
} }
private fun promptUser(intent: Intent, context: Context) { private fun promptUser(intent: Intent, context: Context) {

View File

@@ -154,28 +154,33 @@ object NotificationUtil {
return builder.build() return builder.build()
} }
fun getInstallNotification(context: Context, download: AuroraDownload): Notification { fun getInstallNotification(
context: Context,
displayName: String,
packageName: String
): Notification {
return NotificationCompat.Builder(context, Constants.NOTIFICATION_CHANNEL_ALERT) return NotificationCompat.Builder(context, Constants.NOTIFICATION_CHANNEL_ALERT)
.setSmallIcon(R.drawable.ic_install) .setSmallIcon(R.drawable.ic_install)
.setLargeIcon(PackageUtil.getIconForPackage(context, download.packageName)) .setLargeIcon(PackageUtil.getIconForPackage(context, packageName))
.setColor(context.getStyledAttributeColor(R.color.colorAccent)) .setColor(context.getStyledAttributeColor(R.color.colorAccent))
.setContentTitle(download.displayName) .setContentTitle(displayName)
.setContentText(context.getString(R.string.installer_status_success)) .setContentText(context.getString(R.string.installer_status_success))
.setContentIntent(getContentIntentForDetails(context, download.packageName)) .setContentIntent(getContentIntentForDetails(context, packageName))
.build() .build()
} }
fun getInstallerStatusNotification( fun getInstallerStatusNotification(
context: Context, context: Context,
download: Download, packageName: String,
displayName: String,
content: String? content: String?
): Notification { ): Notification {
return NotificationCompat.Builder(context, Constants.NOTIFICATION_CHANNEL_ALERT) return NotificationCompat.Builder(context, Constants.NOTIFICATION_CHANNEL_ALERT)
.setSmallIcon(R.drawable.ic_install) .setSmallIcon(R.drawable.ic_install)
.setColor(context.getStyledAttributeColor(R.color.colorAccent)) .setColor(context.getStyledAttributeColor(R.color.colorAccent))
.setContentTitle(download.displayName) .setContentTitle(displayName)
.setContentText(content) .setContentText(content)
.setContentIntent(getContentIntentForDetails(context, download.packageName)) .setContentIntent(getContentIntentForDetails(context, packageName))
.build() .build()
} }