use of android.content.res.ResourcesImpl in project platform_frameworks_base by android.
the class ResourcesManager method getOrCreateResources.
/**
* Gets an existing Resources object set with a ResourcesImpl object matching the given key,
* or creates one if it doesn't exist.
*
* @param activityToken The Activity this Resources object should be associated with.
* @param key The key describing the parameters of the ResourcesImpl object.
* @param classLoader The classloader to use for the Resources object.
* If null, {@link ClassLoader#getSystemClassLoader()} is used.
* @return A Resources object that gets updated when
* {@link #applyConfigurationToResourcesLocked(Configuration, CompatibilityInfo)}
* is called.
*/
@Nullable
private Resources getOrCreateResources(@Nullable IBinder activityToken, @NonNull ResourcesKey key, @NonNull ClassLoader classLoader) {
synchronized (this) {
if (DEBUG) {
Throwable here = new Throwable();
here.fillInStackTrace();
Slog.w(TAG, "!! Get resources for activity=" + activityToken + " key=" + key, here);
}
if (activityToken != null) {
final ActivityResources activityResources = getOrCreateActivityResourcesStructLocked(activityToken);
// Clean up any dead references so they don't pile up.
ArrayUtils.unstableRemoveIf(activityResources.activityResources, sEmptyReferencePredicate);
// Rebase the key's override config on top of the Activity's base override.
if (key.hasOverrideConfiguration() && !activityResources.overrideConfig.equals(Configuration.EMPTY)) {
final Configuration temp = new Configuration(activityResources.overrideConfig);
temp.updateFrom(key.mOverrideConfiguration);
key.mOverrideConfiguration.setTo(temp);
}
ResourcesImpl resourcesImpl = findResourcesImplForKeyLocked(key);
if (resourcesImpl != null) {
if (DEBUG) {
Slog.d(TAG, "- using existing impl=" + resourcesImpl);
}
return getOrCreateResourcesForActivityLocked(activityToken, classLoader, resourcesImpl);
}
// We will create the ResourcesImpl object outside of holding this lock.
} else {
// Clean up any dead references so they don't pile up.
ArrayUtils.unstableRemoveIf(mResourceReferences, sEmptyReferencePredicate);
// Not tied to an Activity, find a shared Resources that has the right ResourcesImpl
ResourcesImpl resourcesImpl = findResourcesImplForKeyLocked(key);
if (resourcesImpl != null) {
if (DEBUG) {
Slog.d(TAG, "- using existing impl=" + resourcesImpl);
}
return getOrCreateResourcesLocked(classLoader, resourcesImpl);
}
// We will create the ResourcesImpl object outside of holding this lock.
}
}
// If we're here, we didn't find a suitable ResourcesImpl to use, so create one now.
ResourcesImpl resourcesImpl = createResourcesImpl(key);
if (resourcesImpl == null) {
return null;
}
synchronized (this) {
ResourcesImpl existingResourcesImpl = findResourcesImplForKeyLocked(key);
if (existingResourcesImpl != null) {
if (DEBUG) {
Slog.d(TAG, "- got beat! existing impl=" + existingResourcesImpl + " new impl=" + resourcesImpl);
}
resourcesImpl.getAssets().close();
resourcesImpl = existingResourcesImpl;
} else {
// Add this ResourcesImpl to the cache.
mResourceImpls.put(key, new WeakReference<>(resourcesImpl));
}
final Resources resources;
if (activityToken != null) {
resources = getOrCreateResourcesForActivityLocked(activityToken, classLoader, resourcesImpl);
} else {
resources = getOrCreateResourcesLocked(classLoader, resourcesImpl);
}
return resources;
}
}
use of android.content.res.ResourcesImpl in project android_frameworks_base by DirtyUnicorns.
the class ResourcesManager method findKeyForResourceImplLocked.
/**
* Find the ResourcesKey that this ResourcesImpl object is associated with.
* @return the ResourcesKey or null if none was found.
*/
@Nullable
private ResourcesKey findKeyForResourceImplLocked(@NonNull ResourcesImpl resourceImpl) {
final int refCount = mResourceImpls.size();
for (int i = 0; i < refCount; i++) {
WeakReference<ResourcesImpl> weakImplRef = mResourceImpls.valueAt(i);
ResourcesImpl impl = weakImplRef != null ? weakImplRef.get() : null;
if (impl != null && resourceImpl == impl) {
return mResourceImpls.keyAt(i);
}
}
return null;
}
use of android.content.res.ResourcesImpl in project android_frameworks_base by DirtyUnicorns.
the class ResourcesManager method applyNewResourceDirsLocked.
final void applyNewResourceDirsLocked(@NonNull final String baseCodePath, @NonNull final String[] newResourceDirs) {
try {
Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "ResourcesManager#applyNewResourceDirsLocked");
ApplicationPackageManager.configurationChanged();
if (Process.myUid() == Process.SYSTEM_UID) {
// Resources.getSystem() will *not* use overlays for applications.)
if (FRAMEWORK_RESOURCES_PATH.equals(baseCodePath)) {
final ResourcesKey key = new ResourcesKey(FRAMEWORK_RESOURCES_PATH, null, newResourceDirs, null, Display.DEFAULT_DISPLAY, null, null);
final ResourcesImpl impl = createResourcesImpl(key);
Resources.getSystem().setImpl(impl);
}
}
final ArrayMap<ResourcesImpl, ResourcesKey> updatedResourceKeys = new ArrayMap<>();
final int implCount = mResourceImpls.size();
for (int i = 0; i < implCount; i++) {
final ResourcesImpl impl = mResourceImpls.valueAt(i).get();
final ResourcesKey key = mResourceImpls.keyAt(i);
if (impl != null && key.mResDir != null && key.mResDir.equals(baseCodePath)) {
updatedResourceKeys.put(impl, new ResourcesKey(key.mResDir, key.mSplitResDirs, newResourceDirs, key.mLibDirs, key.mDisplayId, key.mOverrideConfiguration, key.mCompatInfo));
}
}
invalidatePath("/");
redirectResourcesToNewImplLocked(updatedResourceKeys);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
}
}
use of android.content.res.ResourcesImpl in project android_frameworks_base by DirtyUnicorns.
the class ResourcesManager method redirectResourcesToNewImplLocked.
private void redirectResourcesToNewImplLocked(@NonNull final ArrayMap<ResourcesImpl, ResourcesKey> updatedResourceKeys) {
// Bail early if there is no work to do.
if (updatedResourceKeys.isEmpty()) {
return;
}
// Update any references to ResourcesImpl that require reloading.
final int resourcesCount = mResourceReferences.size();
for (int i = 0; i < resourcesCount; i++) {
final Resources r = mResourceReferences.get(i).get();
if (r != null) {
final ResourcesKey key = updatedResourceKeys.get(r.getImpl());
if (key != null) {
final ResourcesImpl impl = findOrCreateResourcesImplForKeyLocked(key);
if (impl == null) {
throw new Resources.NotFoundException("failed to load");
}
r.setImpl(impl);
}
}
}
// Update any references to ResourcesImpl that require reloading for each Activity.
for (final ActivityResources activityResources : mActivityResourceReferences.values()) {
final int resCount = activityResources.activityResources.size();
for (int i = 0; i < resCount; i++) {
final Resources r = activityResources.activityResources.get(i).get();
if (r != null) {
final ResourcesKey key = updatedResourceKeys.get(r.getImpl());
if (key != null) {
final ResourcesImpl impl = findOrCreateResourcesImplForKeyLocked(key);
if (impl == null) {
throw new Resources.NotFoundException("failed to load");
}
r.setImpl(impl);
}
}
}
}
}
use of android.content.res.ResourcesImpl in project android_frameworks_base by DirtyUnicorns.
the class ResourcesManager method invalidatePath.
/**
* Invalidate and destroy any resources that reference content under the
* given filesystem path. Typically used when unmounting a storage device to
* try as hard as possible to release any open FDs.
*/
public void invalidatePath(String path) {
synchronized (this) {
int count = 0;
for (int i = 0; i < mResourceImpls.size(); ) {
final ResourcesKey key = mResourceImpls.keyAt(i);
if (key.isPathReferenced(path)) {
final ResourcesImpl res = mResourceImpls.removeAt(i).get();
if (res != null) {
res.flushLayoutCache();
}
count++;
} else {
i++;
}
}
Log.i(TAG, "Invalidated " + count + " asset managers that referenced " + path);
}
}
Aggregations