Skip to content

Commit

Permalink
Fall back to decoding resources from other packages directly.
Browse files Browse the repository at this point in the history
AppCompat tries to use internal resources to determine whether or not it's capable of decoding vector graphics. The ids of those resources will change across different versions of the support library. If the versions of the support library in the two apps don't match, there might be an id mismatch, which leads to an error. In some cases we can decode the resource anyway by bypassing the support library entirely.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=190825442
  • Loading branch information
sjudd committed Mar 30, 2018
1 parent 6eb094c commit f508d7c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.content.res.AppCompatResources;
import android.support.v7.view.ContextThemeWrapper;
Expand All @@ -23,8 +24,9 @@ private DrawableDecoderCompat() {
/**
* See {@code getDrawable(Context, int, Theme)}.
*/
public static Drawable getDrawable(Context context, @DrawableRes int id) {
return getDrawable(context, id, /*theme=*/ null);
public static Drawable getDrawable(
Context ourContext, Context targetContext, @DrawableRes int id) {
return getDrawable(ourContext, targetContext, id, /*theme=*/ null);
}

/**
Expand All @@ -34,21 +36,32 @@ public static Drawable getDrawable(Context context, @DrawableRes int id) {
* @param theme Used instead of the {@link Theme} returned from the given {@link Context} if
* non-null when loading the {@link Drawable}.
*/
public static Drawable getDrawable(Context context, @DrawableRes int id, @Nullable Theme theme) {
public static Drawable getDrawable(
Context ourContext, @DrawableRes int id, @Nullable Theme theme) {
return getDrawable(ourContext, ourContext, id, theme);
}

private static Drawable getDrawable(
Context ourContext, Context targetContext, @DrawableRes int id, @Nullable Theme theme) {
try {
// Race conditions may cause us to attempt to load using v7 more than once. That's ok since
// this check is a modest optimization and the output will be correct anyway.
if (shouldCallAppCompatResources) {
return loadDrawableV7(context, id, theme);
return loadDrawableV7(targetContext, id, theme);
}
} catch (NoClassDefFoundError error) {
shouldCallAppCompatResources = false;
} catch (IllegalStateException e) {
if (ourContext.getPackageName().equals(targetContext.getPackageName())) {
throw e;
}
return ContextCompat.getDrawable(targetContext, id);
} catch (Resources.NotFoundException e) {
// Ignored, this can be thrown when drawable compat attempts to decode a canary resource. If
// that decode attempt fails, we still want to try with the v4 ResourcesCompat below.
}

return loadDrawableV4(context, id, theme != null ? theme : context.getTheme());
return loadDrawableV4(targetContext, id, theme != null ? theme : targetContext.getTheme());
}

private static Drawable loadDrawableV7(Context context, @DrawableRes int id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public Resource<Drawable> decode(@NonNull Uri source, int width, int height,
@NonNull Options options) {
@DrawableRes int resId = loadResourceIdFromUri(source);
String packageName = source.getAuthority();
Context toUse = packageName.equals(context.getPackageName())
Context targetContext = packageName.equals(context.getPackageName())
? context : getContextForPackage(source, packageName);
// We can't get a theme from another application.
Drawable drawable = DrawableDecoderCompat.getDrawable(toUse, resId);
Drawable drawable = DrawableDecoderCompat.getDrawable(context, targetContext, resId);
return NonOwnedDrawableResource.newInstance(drawable);
}

Expand Down

0 comments on commit f508d7c

Please sign in to comment.