Skip to content

Commit

Permalink
Support loading resources from dynamic modules (#3308)
Browse files Browse the repository at this point in the history
* Support loading resources from dynamic modules

* Try creating child context first before defaulting to parent

* Fix small nit

Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
SUPERCILEX authored and sjudd committed Oct 5, 2018
1 parent 00bae3a commit b57ef34
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,27 @@ public Resource<Drawable> decode(@NonNull Uri source, int width, int height,
@NonNull Options options) {
@DrawableRes int resId = loadResourceIdFromUri(source);
String packageName = source.getAuthority();
Context targetContext = packageName.equals(context.getPackageName())
? context : getContextForPackage(source, packageName);
Context targetContext = findContextForPackage(source, packageName);
// We can't get a theme from another application.
Drawable drawable = DrawableDecoderCompat.getDrawable(context, targetContext, resId);
return NonOwnedDrawableResource.newInstance(drawable);
}

@NonNull
private Context getContextForPackage(Uri source, String packageName) {
private Context findContextForPackage(Uri source, String packageName) {
// Fast path
if (packageName.equals(context.getPackageName())) {
return context;
}

try {
return context.createPackageContext(packageName, /*flags=*/ 0);
} catch (NameNotFoundException e) {
// The parent APK holds the correct context if the resource is located in a split
if (packageName.contains(context.getPackageName())) {
return context;
}

throw new IllegalArgumentException(
"Failed to obtain context or unrecognized Uri format for: " + source, e);
}
Expand Down

0 comments on commit b57ef34

Please sign in to comment.