Merge "Throw proper exception in ResourcesManager" into nyc-dev

This commit is contained in:
TreeHugger Robot
2016-06-09 03:45:19 +00:00
committed by Android (Google) Code Review
2 changed files with 20 additions and 17 deletions

View File

@@ -1245,15 +1245,18 @@ public class ApplicationPackageManager extends PackageManager {
return mContext.mMainThread.getSystemContext().getResources(); return mContext.mMainThread.getSystemContext().getResources();
} }
final boolean sameUid = (app.uid == Process.myUid()); final boolean sameUid = (app.uid == Process.myUid());
final Resources r = mContext.mMainThread.getTopLevelResources( try {
return mContext.mMainThread.getTopLevelResources(
sameUid ? app.sourceDir : app.publicSourceDir, sameUid ? app.sourceDir : app.publicSourceDir,
sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs, sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY, app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
mContext.mPackageInfo); mContext.mPackageInfo);
if (r != null) { } catch (Resources.NotFoundException cause) {
return r; final NameNotFoundException ex =
new NameNotFoundException("Unable to open " + app.publicSourceDir);
ex.initCause(cause);
throw ex;
} }
throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
} }
@Override @Override

View File

@@ -156,7 +156,7 @@ public class ResourcesManager {
* Protected so that tests can override and returns something a fixed value. * Protected so that tests can override and returns something a fixed value.
*/ */
@VisibleForTesting @VisibleForTesting
protected DisplayMetrics getDisplayMetrics(int displayId) { protected @NonNull DisplayMetrics getDisplayMetrics(int displayId) {
DisplayMetrics dm = new DisplayMetrics(); DisplayMetrics dm = new DisplayMetrics();
final Display display = final Display display =
getAdjustedDisplay(displayId, DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS); getAdjustedDisplay(displayId, DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS);
@@ -250,14 +250,14 @@ public class ResourcesManager {
// already. // already.
if (key.mResDir != null) { if (key.mResDir != null) {
if (assets.addAssetPath(key.mResDir) == 0) { if (assets.addAssetPath(key.mResDir) == 0) {
throw new IllegalArgumentException("failed to add asset path " + key.mResDir); throw new Resources.NotFoundException("failed to add asset path " + key.mResDir);
} }
} }
if (key.mSplitResDirs != null) { if (key.mSplitResDirs != null) {
for (final String splitResDir : key.mSplitResDirs) { for (final String splitResDir : key.mSplitResDirs) {
if (assets.addAssetPath(splitResDir) == 0) { if (assets.addAssetPath(splitResDir) == 0) {
throw new IllegalArgumentException( throw new Resources.NotFoundException(
"failed to add split asset path " + splitResDir); "failed to add split asset path " + splitResDir);
} }
} }
@@ -303,7 +303,7 @@ public class ResourcesManager {
return config; return config;
} }
private ResourcesImpl createResourcesImpl(@NonNull ResourcesKey key) { private @NonNull ResourcesImpl createResourcesImpl(@NonNull ResourcesKey key) {
AssetManager assets = createAssetManager(key); AssetManager assets = createAssetManager(key);
DisplayMetrics dm = getDisplayMetrics(key.mDisplayId); DisplayMetrics dm = getDisplayMetrics(key.mDisplayId);
Configuration config = generateConfig(key, dm); Configuration config = generateConfig(key, dm);
@@ -359,7 +359,7 @@ public class ResourcesManager {
* Gets an existing Resources object tied to this Activity, or creates one if it doesn't exist * Gets an existing Resources object tied to this Activity, or creates one if it doesn't exist
* or the class loader is different. * or the class loader is different.
*/ */
private Resources getOrCreateResourcesForActivityLocked(@NonNull IBinder activityToken, private @NonNull Resources getOrCreateResourcesForActivityLocked(@NonNull IBinder activityToken,
@NonNull ClassLoader classLoader, @NonNull ResourcesImpl impl) { @NonNull ClassLoader classLoader, @NonNull ResourcesImpl impl) {
final ActivityResources activityResources = getOrCreateActivityResourcesStructLocked( final ActivityResources activityResources = getOrCreateActivityResourcesStructLocked(
activityToken); activityToken);
@@ -393,7 +393,7 @@ public class ResourcesManager {
* Gets an existing Resources object if the class loader and ResourcesImpl are the same, * Gets an existing Resources object if the class loader and ResourcesImpl are the same,
* otherwise creates a new Resources object. * otherwise creates a new Resources object.
*/ */
private Resources getOrCreateResourcesLocked(@NonNull ClassLoader classLoader, private @NonNull Resources getOrCreateResourcesLocked(@NonNull ClassLoader classLoader,
@NonNull ResourcesImpl impl) { @NonNull ResourcesImpl impl) {
// Find an existing Resources that has this ResourcesImpl set. // Find an existing Resources that has this ResourcesImpl set.
final int refCount = mResourceReferences.size(); final int refCount = mResourceReferences.size();
@@ -441,7 +441,7 @@ public class ResourcesManager {
* {@link ClassLoader#getSystemClassLoader()} is used. * {@link ClassLoader#getSystemClassLoader()} is used.
* @return a Resources object from which to access resources. * @return a Resources object from which to access resources.
*/ */
public Resources createBaseActivityResources(@NonNull IBinder activityToken, public @NonNull Resources createBaseActivityResources(@NonNull IBinder activityToken,
@Nullable String resDir, @Nullable String resDir,
@Nullable String[] splitResDirs, @Nullable String[] splitResDirs,
@Nullable String[] overlayDirs, @Nullable String[] overlayDirs,
@@ -495,7 +495,7 @@ public class ResourcesManager {
* {@link #applyConfigurationToResourcesLocked(Configuration, CompatibilityInfo)} * {@link #applyConfigurationToResourcesLocked(Configuration, CompatibilityInfo)}
* is called. * is called.
*/ */
private Resources getOrCreateResources(@Nullable IBinder activityToken, private @NonNull Resources getOrCreateResources(@Nullable IBinder activityToken,
@NonNull ResourcesKey key, @NonNull ClassLoader classLoader) { @NonNull ResourcesKey key, @NonNull ClassLoader classLoader) {
synchronized (this) { synchronized (this) {
if (DEBUG) { if (DEBUG) {
@@ -603,7 +603,7 @@ public class ResourcesManager {
* {@link ClassLoader#getSystemClassLoader()} is used. * {@link ClassLoader#getSystemClassLoader()} is used.
* @return a Resources object from which to access resources. * @return a Resources object from which to access resources.
*/ */
public Resources getResources(@Nullable IBinder activityToken, public @NonNull Resources getResources(@Nullable IBinder activityToken,
@Nullable String resDir, @Nullable String resDir,
@Nullable String[] splitResDirs, @Nullable String[] splitResDirs,
@Nullable String[] overlayDirs, @Nullable String[] overlayDirs,