Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PackageInstaller on Android 15 #1531

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lagradost.cloudstream3.utils

import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
Expand Down Expand Up @@ -39,6 +40,7 @@ class ApkInstaller(private val service: PackageInstallerService) {
session.commit(intent)
true
} catch (e: Exception) {
logError(e)
false
}.also { delayedInstaller = null }
}
Expand All @@ -54,6 +56,7 @@ class ApkInstaller(private val service: PackageInstallerService) {
}

private val installActionReceiver = object : BroadcastReceiver() {
@SuppressLint("UnsafeIntentLaunch")
override fun onReceive(context: Context, intent: Intent) {
when (intent.getIntExtra(
PackageInstaller.EXTRA_STATUS,
Expand Down Expand Up @@ -107,12 +110,18 @@ class ApkInstaller(private val service: PackageInstallerService) {
inputStream.close()
}

// We must create an explicit intent or it will fail on Android 15+
val installIntent = Intent(service, PackageInstallerService::class.java).apply {
action = INSTALL_ACTION
}

val intentSender = PendingIntent.getBroadcast(
service,
activeSession,
Intent(INSTALL_ACTION),
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_IMMUTABLE else 0,
installIntent,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.FLAG_MUTABLE
} else 0,
).intentSender

// Use delayed installations on android 13 and only if "allow from unknown sources" is enabled
Expand Down Expand Up @@ -162,8 +171,12 @@ class ApkInstaller(private val service: PackageInstallerService) {
fun unregisterInstallActionReceiver() {
if (isReceiverRegistered) {
Log.d(TAG, "Unregistering install action event receiver")
context?.unregisterReceiver(installActionReceiver)
try {
context?.unregisterReceiver(installActionReceiver)
} catch (e: Exception) {
logError(e)
}
isReceiverRegistered = false
}
}
}
}