Search in sources :

Example 46 with ResourcesImpl

use of android.content.res.ResourcesImpl in project android_frameworks_base by crdroidandroid.

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);
                }
            }
        }
    }
}
Also used : ResourcesKey(android.content.res.ResourcesKey) Resources(android.content.res.Resources) ResourcesImpl(android.content.res.ResourcesImpl)

Example 47 with ResourcesImpl

use of android.content.res.ResourcesImpl in project android_frameworks_base by crdroidandroid.

the class ResourcesManager method findResourcesImplForKeyLocked.

/**
     * Finds a cached ResourcesImpl object that matches the given ResourcesKey.
     *
     * @param key The key to match.
     * @return a ResourcesImpl if the key matches a cache entry, null otherwise.
     */
@Nullable
private ResourcesImpl findResourcesImplForKeyLocked(@NonNull ResourcesKey key) {
    WeakReference<ResourcesImpl> weakImplRef = mResourceImpls.get(key);
    ResourcesImpl impl = weakImplRef != null ? weakImplRef.get() : null;
    if (impl != null && impl.getAssets().isUpToDate()) {
        return impl;
    }
    return null;
}
Also used : ResourcesImpl(android.content.res.ResourcesImpl) Nullable(android.annotation.Nullable)

Example 48 with ResourcesImpl

use of android.content.res.ResourcesImpl in project android_frameworks_base by crdroidandroid.

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;
    }
}
Also used : Configuration(android.content.res.Configuration) Resources(android.content.res.Resources) ResourcesImpl(android.content.res.ResourcesImpl) Nullable(android.annotation.Nullable)

Aggregations

ResourcesImpl (android.content.res.ResourcesImpl)48 ResourcesKey (android.content.res.ResourcesKey)28 Nullable (android.annotation.Nullable)20 Configuration (android.content.res.Configuration)20 Resources (android.content.res.Resources)15 DisplayMetrics (android.util.DisplayMetrics)10 DisplayAdjustments (android.view.DisplayAdjustments)10 ArrayMap (android.util.ArrayMap)9 AssetManager (android.content.res.AssetManager)5