Switching extension install activity to broadcast

so updating from a notification does not open the app anymore
This commit is contained in:
Jays2Kings 2021-07-25 13:49:17 -04:00
parent e185422c9f
commit ccd08f7bdc
3 changed files with 20 additions and 29 deletions

View File

@ -174,11 +174,6 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity
android:name=".extension.util.ExtensionInstallActivity"
android:icon="@mipmap/ic_launcher"
android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
<activity <activity
android:name="com.google.android.gms.oss.licenses.OssLicensesMenuActivity" android:name="com.google.android.gms.oss.licenses.OssLicensesMenuActivity"
android:theme="@style/Theme.OSS" /> android:theme="@style/Theme.OSS" />
@ -200,6 +195,10 @@
android:name=".data.notification.NotificationReceiver" android:name=".data.notification.NotificationReceiver"
android:exported="false" /> android:exported="false" />
<receiver
android:name=".extension.util.ExtensionInstallBroadcast"
android:exported="false" />
<receiver <receiver
android:name=".data.updater.UpdaterBroadcast" android:name=".data.updater.UpdaterBroadcast"
android:exported="false"> android:exported="false">

View File

@ -1,15 +1,14 @@
package eu.kanade.tachiyomi.extension.util package eu.kanade.tachiyomi.extension.util
import android.app.Activity
import android.app.DownloadManager import android.app.DownloadManager
import android.app.PendingIntent import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageInstaller import android.content.pm.PackageInstaller
import android.content.pm.PackageInstaller.SessionParams import android.content.pm.PackageInstaller.SessionParams
import android.content.pm.PackageInstaller.SessionParams.USER_ACTION_NOT_REQUIRED import android.content.pm.PackageInstaller.SessionParams.USER_ACTION_NOT_REQUIRED
import android.os.Build import android.os.Build
import android.os.Bundle
import android.widget.Toast import android.widget.Toast
import com.hippo.unifile.UniFile import com.hippo.unifile.UniFile
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
@ -22,19 +21,17 @@ import uy.kohesive.injekt.injectLazy
* Activity used to install extensions, because we can only receive the result of the installation * Activity used to install extensions, because we can only receive the result of the installation
* with [startActivityForResult], which we need to update the UI. * with [startActivityForResult], which we need to update the UI.
*/ */
class ExtensionInstallActivity : Activity() { class ExtensionInstallBroadcast : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
try { try {
if (PACKAGE_INSTALLED_ACTION == intent.action) { if (PACKAGE_INSTALLED_ACTION == intent.action) {
packageInstallStep(intent) packageInstallStep(context, intent)
return return
} }
val downloadId = intent.extras!!.getLong(ExtensionInstaller.EXTRA_DOWNLOAD_ID) val downloadId = intent.extras!!.getLong(ExtensionInstaller.EXTRA_DOWNLOAD_ID)
val packageInstaller = packageManager.packageInstaller val packageInstaller = context.packageManager.packageInstaller
val data = UniFile.fromUri(this, intent.data).openInputStream() val data = UniFile.fromUri(context, intent.data).openInputStream()
val params = SessionParams( val params = SessionParams(
SessionParams.MODE_FULL_INSTALL SessionParams.MODE_FULL_INSTALL
@ -48,29 +45,28 @@ class ExtensionInstallActivity : Activity() {
data.copyTo(packageInSession) data.copyTo(packageInSession)
} }
val newIntent = Intent(this, ExtensionInstallActivity::class.java) val newIntent = Intent(context, ExtensionInstallBroadcast::class.java)
.setAction(PACKAGE_INSTALLED_ACTION) .setAction(PACKAGE_INSTALLED_ACTION)
.putExtra(ExtensionInstaller.EXTRA_DOWNLOAD_ID, downloadId) .putExtra(ExtensionInstaller.EXTRA_DOWNLOAD_ID, downloadId)
.putExtra(EXTRA_SESSION_ID, sessionId) .putExtra(EXTRA_SESSION_ID, sessionId)
val pendingIntent = PendingIntent.getActivity(this, downloadId.hashCode(), newIntent, PendingIntent.FLAG_UPDATE_CURRENT) val pendingIntent = PendingIntent.getBroadcast(context, downloadId.hashCode(), newIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val statusReceiver = pendingIntent.intentSender val statusReceiver = pendingIntent.intentSender
session.commit(statusReceiver) session.commit(statusReceiver)
val extensionManager: ExtensionManager by injectLazy() val extensionManager: ExtensionManager by injectLazy()
extensionManager.setInstalling(downloadId, sessionId) extensionManager.setInstalling(downloadId, sessionId)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
(getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager).remove(downloadId) (context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager).remove(downloadId)
} }
data.close() data.close()
} catch (error: Exception) { } catch (error: Exception) {
// Either install package can't be found (probably bots) or there's a security exception // Either install package can't be found (probably bots) or there's a security exception
// with the download manager. Nothing we can workaround. // with the download manager. Nothing we can workaround.
toast(error.message) context.toast(error.message)
} }
finish()
} }
private fun packageInstallStep(intent: Intent) { private fun packageInstallStep(context: Context, intent: Intent) {
val extras = intent.extras ?: return val extras = intent.extras ?: return
if (PACKAGE_INSTALLED_ACTION == intent.action) { if (PACKAGE_INSTALLED_ACTION == intent.action) {
val downloadId = extras.getLong(ExtensionInstaller.EXTRA_DOWNLOAD_ID) val downloadId = extras.getLong(ExtensionInstaller.EXTRA_DOWNLOAD_ID)
@ -78,27 +74,23 @@ class ExtensionInstallActivity : Activity() {
when (val status = extras.getInt(PackageInstaller.EXTRA_STATUS)) { when (val status = extras.getInt(PackageInstaller.EXTRA_STATUS)) {
PackageInstaller.STATUS_PENDING_USER_ACTION -> { PackageInstaller.STATUS_PENDING_USER_ACTION -> {
val confirmIntent = extras[Intent.EXTRA_INTENT] as? Intent val confirmIntent = extras[Intent.EXTRA_INTENT] as? Intent
startActivityForResult(confirmIntent, INSTALL_REQUEST_CODE) context.startActivity(confirmIntent?.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
finish()
} }
PackageInstaller.STATUS_SUCCESS -> { PackageInstaller.STATUS_SUCCESS -> {
extensionManager.setInstallationResult(downloadId, true) extensionManager.setInstallationResult(downloadId, true)
finish()
} }
PackageInstaller.STATUS_FAILURE, PackageInstaller.STATUS_FAILURE_ABORTED, PackageInstaller.STATUS_FAILURE_BLOCKED, PackageInstaller.STATUS_FAILURE_CONFLICT, PackageInstaller.STATUS_FAILURE_INCOMPATIBLE, PackageInstaller.STATUS_FAILURE_INVALID, PackageInstaller.STATUS_FAILURE_STORAGE -> { PackageInstaller.STATUS_FAILURE, PackageInstaller.STATUS_FAILURE_ABORTED, PackageInstaller.STATUS_FAILURE_BLOCKED, PackageInstaller.STATUS_FAILURE_CONFLICT, PackageInstaller.STATUS_FAILURE_INCOMPATIBLE, PackageInstaller.STATUS_FAILURE_INVALID, PackageInstaller.STATUS_FAILURE_STORAGE -> {
extensionManager.setInstallationResult(downloadId, false) extensionManager.setInstallationResult(downloadId, false)
if (status != PackageInstaller.STATUS_FAILURE_ABORTED) { if (status != PackageInstaller.STATUS_FAILURE_ABORTED) {
if (MiuiUtil.isMiui()) { if (MiuiUtil.isMiui()) {
toast(R.string.extensions_miui_warning, Toast.LENGTH_LONG) context.toast(R.string.extensions_miui_warning, Toast.LENGTH_LONG)
} else { } else {
toast(R.string.could_not_install_extension) context.toast(R.string.could_not_install_extension)
} }
} }
finish()
} }
else -> { else -> {
extensionManager.setInstallationResult(downloadId, false) extensionManager.setInstallationResult(downloadId, false)
finish()
} }
} }
} }

View File

@ -211,12 +211,12 @@ internal class ExtensionInstaller(private val context: Context) {
* @param uri The uri of the extension to install. * @param uri The uri of the extension to install.
*/ */
fun installApk(downloadId: Long, uri: Uri) { fun installApk(downloadId: Long, uri: Uri) {
val intent = Intent(context, ExtensionInstallActivity::class.java) val intent = Intent(context, ExtensionInstallBroadcast::class.java)
.setDataAndType(uri, APK_MIME) .setDataAndType(uri, APK_MIME)
.putExtra(EXTRA_DOWNLOAD_ID, downloadId) .putExtra(EXTRA_DOWNLOAD_ID, downloadId)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION) .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION)
context.startActivity(intent) context.sendBroadcast(intent)
} }
/** /**