Permalink
Browse files

Simplify ProviderContract.Artwork.getContentUri

Remove the PackageManager check, making this a much less expensive operation.
  • Loading branch information...
ianhanniballake committed Sep 14, 2018
1 parent bce8796 commit da08bdc7ac76eeca3b29931682f49d7f31e87db8
@@ -107,7 +107,7 @@ class ArtworkLoadWorker : Worker() {
if (BuildConfig.DEBUG) {
Log.d(TAG, "Artwork Load for $authority")
}
val contentUri = ProviderContract.Artwork.getContentUri(applicationContext, authority)
val contentUri = ProviderContract.Artwork.getContentUri(authority)
try {
ContentProviderClientCompat.getClient(applicationContext, contentUri)?.use { client ->
val result = client.call(METHOD_GET_LOAD_INFO)
@@ -132,7 +132,7 @@ class ProviderChangedWorker : Worker() {
// Make sure we're still not actively listening
if (!providerManager.hasActiveObservers()) {
val contentUri = ProviderContract.Artwork.getContentUri(
context, provider.authority)
provider.authority)
scheduleObserver(contentUri)
}
}
@@ -181,7 +181,7 @@ class ProviderChangedWorker : Worker() {
if (BuildConfig.DEBUG) {
Log.d(TAG, "Provider Change ($tag) for ${provider.authority}")
}
val contentUri = ProviderContract.Artwork.getContentUri(applicationContext, provider.authority)
val contentUri = ProviderContract.Artwork.getContentUri(provider.authority)
try {
ContentProviderClientCompat.getClient(applicationContext, contentUri)?.use { client ->
val result = client.call(METHOD_GET_LOAD_INFO)
@@ -248,7 +248,7 @@ class ProviderChangedWorker : Worker() {
MuzeiDatabase.getInstance(applicationContext).artworkDao()
.getCurrentArtworkForProvider(provider.authority)?.let { artwork ->
client.query(artwork.imageUri)?.use { cursor ->
val contentUri = ProviderContract.Artwork.getContentUri(applicationContext, provider.authority)
val contentUri = ProviderContract.Artwork.getContentUri(provider.authority)
return cursor.moveToNext() && isValidArtwork(client, contentUri, cursor)
}
}
@@ -198,15 +198,15 @@ class ProviderManager private constructor(private val context: Context)
private fun runIfValid(provider: Provider?, block: (provider: Provider) -> Unit) {
if (provider != null) {
try {
ProviderContract.Artwork.getContentUri(context, provider.authority)
// getContentUri succeeded, so it is a valid ContentProvider
val pm = context.packageManager
if (pm.resolveContentProvider(provider.authority, 0) != null) {
// resolveContentProvider succeeded, so it is a valid ContentProvider
block(provider)
} catch (e: IllegalArgumentException) {
} else {
// Invalid ContentProvider, remove it from the ProviderDao
launch {
if (BuildConfig.DEBUG) {
Log.w(TAG, "Invalid provider ${provider.authority}", e)
Log.w(TAG, "Invalid provider ${provider.authority}")
}
MuzeiDatabase.getInstance(context).providerDao().delete(provider)
}
@@ -221,7 +221,7 @@ class ProviderManager private constructor(private val context: Context)
Log.d(TAG, "Starting artwork load")
}
// Listen for MuzeiArtProvider changes
val contentUri = ProviderContract.Artwork.getContentUri(context, currentSource.authority)
val contentUri = ProviderContract.Artwork.getContentUri(currentSource.authority)
context.contentResolver.registerContentObserver(
contentUri, true, contentObserver)
ProviderChangedWorker.enqueueSelected()
@@ -304,9 +304,7 @@ class ChooseProviderFragment : Fragment() {
providerBrowse.setOnClickListener {
findNavController().navigate(
ChooseProviderFragmentDirections.browse(
ProviderContract.Artwork.getContentUri(
requireContext(),
authority)))
ProviderContract.Artwork.getContentUri(authority)))
}
}
@@ -296,10 +296,7 @@ protected final Uri getContentUri() {
throw new IllegalStateException("getContentUri() should not be called before onCreate()");
}
if (contentUri == null) {
contentUri = new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(authority)
.build();
contentUri = ProviderContract.Artwork.getContentUri(authority);
}
return contentUri;
}
@@ -146,27 +146,16 @@ public static Uri getContentUri(
* Retrieve the content URI for the given {@link MuzeiArtProvider}, allowing you to build
* custom queries, inserts, updates, and deletes using a {@link ContentResolver}.
* <p>
* This will throw an {@link IllegalArgumentException} if the provider is not valid.
* This <strong>does not</strong> check for the validity of the MuzeiArtProvider. You can
* use {@link PackageManager#resolveContentProvider(String, int)} passing in the
* authority if you need to confirm the authority is valid.
*
* @param context Context used to retrieve the content URI.
* @param authority The {@link MuzeiArtProvider} you need a content URI for
* @return The content URI for the {@link MuzeiArtProvider}
* @see MuzeiArtProvider#getContentUri()
*/
@NonNull
public static Uri getContentUri(@NonNull Context context, @NonNull String authority) {
PackageManager pm = context.getPackageManager();
@SuppressLint("InlinedApi")
ProviderInfo info = pm.resolveContentProvider(authority,
PackageManager.MATCH_DISABLED_COMPONENTS);
if (info == null) {
throw new IllegalArgumentException("Invalid MuzeiArtProvider: " + authority);
}
return getContentUri(authority);
}
@NonNull
private static Uri getContentUri(@NonNull String authority) {
public static Uri getContentUri(@NonNull String authority) {
return new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(authority)
@@ -204,7 +193,7 @@ private static Uri getContentUri(@NonNull String authority) {
@NonNull Context context,
@NonNull String authority
) {
return getLastAddedArtwork(context, getContentUri(context, authority));
return getLastAddedArtwork(context, getContentUri(authority));
}
@RequiresApi(Build.VERSION_CODES.KITKAT)
@@ -258,7 +247,7 @@ public static Uri addArtwork(
@NonNull String authority,
@NonNull com.google.android.apps.muzei.api.provider.Artwork artwork
) {
return addArtwork(context, getContentUri(context, authority), artwork);
return addArtwork(context, getContentUri(authority), artwork);
}
@RequiresApi(Build.VERSION_CODES.KITKAT)
@@ -315,7 +304,7 @@ public static Uri setArtwork(
@NonNull String authority,
@NonNull com.google.android.apps.muzei.api.provider.Artwork artwork
) {
return setArtwork(context, getContentUri(context, authority), artwork);
return setArtwork(context, getContentUri(authority), artwork);
}
@RequiresApi(Build.VERSION_CODES.KITKAT)

0 comments on commit da08bdc

Please sign in to comment.