mirror of
https://github.com/ankidroid/Anki-Android.git
synced 2024-09-20 20:03:05 +02:00
fix queryIntentActivities deprecation (#13309)
* fix queryIntentActivities deprecation * Fix remaining lint and tests isssues Because we moved from a platform method call on a class written in Java to a method written completely in kotlin, one of the tests failed because mockito and kotlin don't play along very well(mockito passing null values for kotlin methods although they expect non null types). I fixed this by using the actual `intent` value used in the method call in the class for which the test was failing(see CustomTabsHelper). --------- Co-authored-by: lukstbit <52494258+lukstbit@users.noreply.github.com>
This commit is contained in:
parent
aba407d783
commit
490493792b
@ -88,6 +88,7 @@ interface Compat {
|
||||
fun <T> readSparseArray(parcel: Parcel, loader: ClassLoader, clazz: Class<T>): SparseArray<T>?
|
||||
fun <T : Parcelable> getParcelableArrayList(bundle: Bundle, key: String, clazz: Class<T>): ArrayList<T>?
|
||||
fun resolveService(packageManager: PackageManager, intent: Intent, flags: ResolveInfoFlagsCompat): ResolveInfo?
|
||||
fun queryIntentActivities(packageManager: PackageManager, intent: Intent, flags: ResolveInfoFlagsCompat): List<ResolveInfo>
|
||||
fun <T> getParcelable(bundle: Bundle, key: String?, clazz: Class<T>): T?
|
||||
|
||||
/**
|
||||
|
@ -147,6 +147,25 @@ class CompatHelper private constructor() {
|
||||
return compat.resolveService(this, intent, flags)
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all activities that can be performed for the given intent.
|
||||
*
|
||||
* @param intent The desired intent as per resolveActivity().
|
||||
* @param flags Additional option flags to modify the data returned. The
|
||||
* most important is [MATCH_DEFAULT_ONLY], to limit the
|
||||
* resolution to only those activities that support the
|
||||
* [CATEGORY_DEFAULT]. Or, set
|
||||
* [MATCH_ALL] to prevent any filtering of the results.
|
||||
* @return Returns a List of ResolveInfo objects containing one entry for
|
||||
* each matching activity, ordered from best to worst. In other
|
||||
* words, the first item is what would be returned by
|
||||
* {@link #resolveActivity}. If there are no matching activities, an
|
||||
* empty list is returned.
|
||||
*/
|
||||
fun PackageManager.queryIntentActivitiesCompat(intent: Intent, flags: ResolveInfoFlagsCompat): List<ResolveInfo> {
|
||||
return compat.queryIntentActivities(this, intent, flags)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value associated with the given key or `null` if:
|
||||
* * No mapping of the desired type exists for the given key.
|
||||
|
@ -91,6 +91,14 @@ open class CompatV21 : Compat {
|
||||
return packageManager.resolveService(intent, flags.value.toInt())
|
||||
}
|
||||
|
||||
override fun queryIntentActivities(
|
||||
packageManager: PackageManager,
|
||||
intent: Intent,
|
||||
flags: ResolveInfoFlagsCompat
|
||||
): List<ResolveInfo> {
|
||||
return packageManager.queryIntentActivities(intent, flags.value.toInt())
|
||||
}
|
||||
|
||||
override fun <T> getParcelable(bundle: Bundle, key: String?, clazz: Class<T>): T? {
|
||||
return bundle.getParcelable(key)
|
||||
}
|
||||
|
@ -68,6 +68,10 @@ open class CompatV33 : CompatV31(), Compat {
|
||||
return packageManager.resolveService(intent, PackageManager.ResolveInfoFlags.of(flags.value))
|
||||
}
|
||||
|
||||
override fun queryIntentActivities(packageManager: PackageManager, intent: Intent, flags: ResolveInfoFlagsCompat): List<ResolveInfo> {
|
||||
return packageManager.queryIntentActivities(intent, PackageManager.ResolveInfoFlags.of(flags.value))
|
||||
}
|
||||
|
||||
override fun <T> readList(
|
||||
parcel: Parcel,
|
||||
outVal: MutableList<in T>,
|
||||
|
@ -19,6 +19,7 @@ import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import androidx.browser.customtabs.CustomTabsService
|
||||
import com.ichi2.compat.CompatHelper.Companion.queryIntentActivitiesCompat
|
||||
import com.ichi2.compat.CompatHelper.Companion.resolveServiceCompat
|
||||
import com.ichi2.compat.ResolveInfoFlagsCompat
|
||||
import timber.log.Timber
|
||||
@ -52,7 +53,7 @@ object CustomTabsHelper {
|
||||
* @param context [Context] to use for accessing [PackageManager].
|
||||
* @return The package name recommended to use for connecting to custom tabs related components.
|
||||
*/
|
||||
@Suppress("deprecation") // resolveActivity queryIntentActivities
|
||||
@Suppress("deprecation") // resolveActivity
|
||||
fun getPackageNameToUse(context: Context): String? {
|
||||
if (sPackageNameToUse != null) return sPackageNameToUse
|
||||
val pm = context.packageManager
|
||||
@ -65,7 +66,7 @@ object CustomTabsHelper {
|
||||
}
|
||||
|
||||
// Get all apps that can handle VIEW intents.
|
||||
val resolvedActivityList = pm.queryIntentActivities(activityIntent, 0)
|
||||
val resolvedActivityList = pm.queryIntentActivitiesCompat(activityIntent, ResolveInfoFlagsCompat.EMPTY)
|
||||
val packagesSupportingCustomTabs: MutableList<String?> = ArrayList(resolvedActivityList.size)
|
||||
for (info in resolvedActivityList) {
|
||||
val serviceIntent = Intent()
|
||||
@ -104,13 +105,12 @@ object CustomTabsHelper {
|
||||
* @param intent The intent to check with.
|
||||
* @return Whether there is a specialized handler for the given intent.
|
||||
*/
|
||||
@Suppress("deprecation") // queryIntentActivities
|
||||
private fun hasSpecializedHandlerIntents(context: Context, intent: Intent): Boolean {
|
||||
try {
|
||||
val pm = context.packageManager
|
||||
val handlers = pm.queryIntentActivities(
|
||||
val handlers = pm.queryIntentActivitiesCompat(
|
||||
intent,
|
||||
PackageManager.GET_RESOLVED_FILTER
|
||||
ResolveInfoFlagsCompat.of(PackageManager.GET_RESOLVED_FILTER.toLong())
|
||||
)
|
||||
if (handlers.isEmpty()) {
|
||||
return false
|
||||
|
@ -33,6 +33,8 @@ import com.ichi2.anki.BuildConfig
|
||||
import com.ichi2.anki.CollectionHelper
|
||||
import com.ichi2.anki.R
|
||||
import com.ichi2.compat.CompatHelper.Companion.compat
|
||||
import com.ichi2.compat.CompatHelper.Companion.queryIntentActivitiesCompat
|
||||
import com.ichi2.compat.ResolveInfoFlagsCompat
|
||||
import com.ichi2.libanki.Consts.FIELD_SEPARATOR
|
||||
import com.ichi2.utils.HashUtil.HashMapInit
|
||||
import com.ichi2.utils.HashUtil.HashSetInit
|
||||
@ -848,7 +850,6 @@ object Utils {
|
||||
|
||||
@KotlinCleanup("Use @JmOverloads, remove fun passing null for ComponentName")
|
||||
@KotlinCleanup("Simplify function body")
|
||||
@Suppress("deprecation") // queryIntentActivities
|
||||
fun isIntentAvailable(
|
||||
context: Context,
|
||||
action: String?,
|
||||
@ -857,7 +858,7 @@ object Utils {
|
||||
val packageManager = context.packageManager
|
||||
val intent = Intent(action)
|
||||
intent.component = componentName
|
||||
val list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
|
||||
val list = packageManager.queryIntentActivitiesCompat(intent, ResolveInfoFlagsCompat.of(PackageManager.MATCH_DEFAULT_ONLY.toLong()))
|
||||
return list.isNotEmpty()
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,9 @@ import android.os.Build
|
||||
import android.provider.Settings
|
||||
import com.ichi2.anki.AnkiDroidApp
|
||||
import com.ichi2.compat.CompatHelper.Companion.getPackageInfoCompat
|
||||
import com.ichi2.compat.CompatHelper.Companion.queryIntentActivitiesCompat
|
||||
import com.ichi2.compat.PackageInfoFlagsCompat
|
||||
import com.ichi2.compat.ResolveInfoFlagsCompat
|
||||
import timber.log.Timber
|
||||
import java.util.*
|
||||
|
||||
@ -65,7 +67,6 @@ object AdaptionUtil {
|
||||
return "true" == testLabSetting
|
||||
}
|
||||
|
||||
@Suppress("deprecation") // queryIntentActivities
|
||||
private fun checkHasWebBrowser(context: Context): Boolean {
|
||||
// The test monkey often gets stuck on the Shared Decks WebView, ignore it as it shouldn't crash.
|
||||
if (isUserATestClient) {
|
||||
@ -73,7 +74,7 @@ object AdaptionUtil {
|
||||
}
|
||||
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"))
|
||||
val pm = context.packageManager
|
||||
val list = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
|
||||
val list = pm.queryIntentActivitiesCompat(intent, ResolveInfoFlagsCompat.of(PackageManager.MATCH_DEFAULT_ONLY.toLong()))
|
||||
for (ri in list) {
|
||||
if (!isValidBrowser(ri)) {
|
||||
continue
|
||||
|
@ -16,17 +16,19 @@
|
||||
package com.ichi2.compat.customtabs
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import androidx.annotation.CheckResult
|
||||
import androidx.browser.customtabs.CustomTabsClient
|
||||
import com.ichi2.compat.CompatHelper.Companion.queryIntentActivitiesCompat
|
||||
import com.ichi2.compat.ResolveInfoFlagsCompat
|
||||
import org.hamcrest.CoreMatchers.not
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.ArgumentMatchers.anyInt
|
||||
import org.mockito.ArgumentMatchers.anyLong
|
||||
import org.mockito.ArgumentMatchers.anyString
|
||||
import org.mockito.ArgumentMatchers.*
|
||||
import org.mockito.Mockito.times
|
||||
import org.mockito.Mockito.verify
|
||||
import org.mockito.kotlin.any
|
||||
@ -53,13 +55,17 @@ class CustomTabActivityHelperTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Suppress("deprecation") // queryIntentActivities
|
||||
fun invalidClientMeansFallbackIsCalled() {
|
||||
getValidTabHandler().onServiceConnected(getClientThrowingSecurityException())
|
||||
|
||||
val fallback = mock<CustomTabActivityHelper.CustomTabFallback>()
|
||||
val packageManager = mock<PackageManager> {
|
||||
on { it.queryIntentActivities(any(), anyInt()) } doReturn emptyList()
|
||||
on {
|
||||
it.queryIntentActivitiesCompat(
|
||||
Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com")),
|
||||
ResolveInfoFlagsCompat.EMPTY
|
||||
)
|
||||
} doReturn emptyList()
|
||||
}
|
||||
val activity = mock<Activity> {
|
||||
on { it.packageManager } doReturn packageManager
|
||||
|
Loading…
Reference in New Issue
Block a user