Catch SecurityExceptions; unpin apps if they couldn't be launched

This commit is contained in:
MM20 2022-03-27 14:17:47 +02:00
parent 2a9339926d
commit 3a15b59526
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
5 changed files with 12 additions and 5 deletions

View File

@ -15,6 +15,7 @@ import android.os.UserHandle
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import de.mm20.launcher2.icons.LauncherIcon import de.mm20.launcher2.icons.LauncherIcon
import de.mm20.launcher2.ktx.getSerialNumber import de.mm20.launcher2.ktx.getSerialNumber
import de.mm20.launcher2.ktx.tryStartActivity
import de.mm20.launcher2.preferences.Settings.IconSettings.LegacyIconBackground import de.mm20.launcher2.preferences.Settings.IconSettings.LegacyIconBackground
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -80,7 +81,7 @@ class LauncherApp(
val intent = Intent() val intent = Intent()
intent.component = ComponentName(`package`, activity) intent.component = ComponentName(`package`, activity)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent, options) return context.tryStartActivity(intent, options)
} else { } else {
try { try {
launcherApps.startMainActivity( launcherApps.startMainActivity(

View File

@ -25,5 +25,7 @@ fun Context.tryStartActivity(intent: Intent, bundle: Bundle? = null): Boolean {
true true
} catch (e: ActivityNotFoundException) { } catch (e: ActivityNotFoundException) {
false false
} catch (e: SecurityException) {
false
} }
} }

View File

@ -50,4 +50,5 @@ dependencies {
implementation(project(":database")) implementation(project(":database"))
implementation(project(":preferences")) implementation(project(":preferences"))
implementation(project(":crashreporter")) implementation(project(":crashreporter"))
implementation(project(":ktx"))
} }

View File

@ -6,6 +6,7 @@ import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.widget.Toast import android.widget.Toast
import de.mm20.launcher2.icons.LauncherIcon import de.mm20.launcher2.icons.LauncherIcon
import de.mm20.launcher2.ktx.tryStartActivity
import de.mm20.launcher2.preferences.Settings import de.mm20.launcher2.preferences.Settings
import de.mm20.launcher2.preferences.Settings.IconSettings.LegacyIconBackground import de.mm20.launcher2.preferences.Settings.IconSettings.LegacyIconBackground
import de.mm20.launcher2.search.R import de.mm20.launcher2.search.R
@ -22,10 +23,9 @@ abstract class Searchable : Comparable<Searchable> {
open fun launch(context: Context, options: Bundle?): Boolean { open fun launch(context: Context, options: Bundle?): Boolean {
val intent = getLaunchIntent(context) ?: return false val intent = getLaunchIntent(context) ?: return false
return try { return if (context.tryStartActivity(intent, options)) {
context.startActivity(intent, options)
true true
} catch (e: ActivityNotFoundException) { } else {
Toast.makeText(context, context.getString(R.string.error_activity_not_found, label), Toast.LENGTH_SHORT).show() Toast.makeText(context, context.getString(R.string.error_activity_not_found, label), Toast.LENGTH_SHORT).show()
false false
} }

View File

@ -9,6 +9,7 @@ import de.mm20.launcher2.favorites.FavoritesRepository
import de.mm20.launcher2.icons.IconRepository import de.mm20.launcher2.icons.IconRepository
import de.mm20.launcher2.icons.LauncherIcon import de.mm20.launcher2.icons.LauncherIcon
import de.mm20.launcher2.ktx.isAtLeastApiLevel import de.mm20.launcher2.ktx.isAtLeastApiLevel
import de.mm20.launcher2.search.data.Application
import de.mm20.launcher2.search.data.Searchable import de.mm20.launcher2.search.data.Searchable
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import org.koin.core.component.KoinComponent import org.koin.core.component.KoinComponent
@ -46,7 +47,7 @@ abstract class SearchableItemVM(
} }
fun launch(context: Context, bounds: Rect? = null): Boolean { open fun launch(context: Context, bounds: Rect? = null): Boolean {
val view = (context as? AppCompatActivity)?.window?.decorView val view = (context as? AppCompatActivity)?.window?.decorView
val options = if (bounds != null && view != null) { val options = if (bounds != null && view != null) {
ActivityOptionsCompat.makeClipRevealAnimation( ActivityOptionsCompat.makeClipRevealAnimation(
@ -66,6 +67,8 @@ abstract class SearchableItemVM(
if (searchable.launch(context, bundle)) { if (searchable.launch(context, bundle)) {
favoritesRepository.incrementLaunchCounter(searchable) favoritesRepository.incrementLaunchCounter(searchable)
return true return true
} else if (searchable is Application) {
favoritesRepository.unpinItem(searchable)
} }
return false return false
} }