Search in sources :

Example 31 with ResourcesKey

use of android.content.res.ResourcesKey in project android_frameworks_base by ResurrectionRemix.

the class ResourcesManager method updateResourcesForActivity.

/**
     * Updates an Activity's Resources object with overrideConfig. The Resources object
     * that was previously returned by
     * {@link #getResources(IBinder, String, String[], String[], String[], int, Configuration,
     * CompatibilityInfo, ClassLoader)} is
     * still valid and will have the updated configuration.
     * @param activityToken The Activity token.
     * @param overrideConfig The configuration override to update.
     */
public void updateResourcesForActivity(@NonNull IBinder activityToken, @Nullable Configuration overrideConfig) {
    try {
        Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "ResourcesManager#updateResourcesForActivity");
        synchronized (this) {
            final ActivityResources activityResources = getOrCreateActivityResourcesStructLocked(activityToken);
            if (Objects.equals(activityResources.overrideConfig, overrideConfig)) {
                // They are the same, no work to do.
                return;
            }
            // Grab a copy of the old configuration so we can create the delta's of each
            // Resources object associated with this Activity.
            final Configuration oldConfig = new Configuration(activityResources.overrideConfig);
            // Update the Activity's base override.
            if (overrideConfig != null) {
                activityResources.overrideConfig.setTo(overrideConfig);
            } else {
                activityResources.overrideConfig.setToDefaults();
            }
            if (DEBUG) {
                Throwable here = new Throwable();
                here.fillInStackTrace();
                Slog.d(TAG, "updating resources override for activity=" + activityToken + " from oldConfig=" + Configuration.resourceQualifierString(oldConfig) + " to newConfig=" + Configuration.resourceQualifierString(activityResources.overrideConfig), here);
            }
            final boolean activityHasOverrideConfig = !activityResources.overrideConfig.equals(Configuration.EMPTY);
            // Rebase each Resources associated with this Activity.
            final int refCount = activityResources.activityResources.size();
            for (int i = 0; i < refCount; i++) {
                WeakReference<Resources> weakResRef = activityResources.activityResources.get(i);
                Resources resources = weakResRef.get();
                if (resources == null) {
                    continue;
                }
                // Extract the ResourcesKey that was last used to create the Resources for this
                // activity.
                final ResourcesKey oldKey = findKeyForResourceImplLocked(resources.getImpl());
                if (oldKey == null) {
                    Slog.e(TAG, "can't find ResourcesKey for resources impl=" + resources.getImpl());
                    continue;
                }
                // Build the new override configuration for this ResourcesKey.
                final Configuration rebasedOverrideConfig = new Configuration();
                if (overrideConfig != null) {
                    rebasedOverrideConfig.setTo(overrideConfig);
                }
                if (activityHasOverrideConfig && oldKey.hasOverrideConfiguration()) {
                    // Generate a delta between the old base Activity override configuration and
                    // the actual final override configuration that was used to figure out the
                    // real delta this Resources object wanted.
                    Configuration overrideOverrideConfig = Configuration.generateDelta(oldConfig, oldKey.mOverrideConfiguration);
                    rebasedOverrideConfig.updateFrom(overrideOverrideConfig);
                }
                // Create the new ResourcesKey with the rebased override config.
                final ResourcesKey newKey = new ResourcesKey(oldKey.mResDir, oldKey.mSplitResDirs, oldKey.mOverlayDirs, oldKey.mLibDirs, oldKey.mDisplayId, rebasedOverrideConfig, oldKey.mCompatInfo);
                if (DEBUG) {
                    Slog.d(TAG, "rebasing ref=" + resources + " from oldKey=" + oldKey + " to newKey=" + newKey);
                }
                ResourcesImpl resourcesImpl = findResourcesImplForKeyLocked(newKey);
                if (resourcesImpl == null) {
                    resourcesImpl = createResourcesImpl(newKey);
                    if (resourcesImpl != null) {
                        mResourceImpls.put(newKey, new WeakReference<>(resourcesImpl));
                    }
                }
                if (resourcesImpl != null && resourcesImpl != resources.getImpl()) {
                    // Set the ResourcesImpl, updating it for all users of this Resources
                    // object.
                    resources.setImpl(resourcesImpl);
                }
            }
        }
    } finally {
        Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
    }
}
Also used : Configuration(android.content.res.Configuration) ResourcesKey(android.content.res.ResourcesKey) Resources(android.content.res.Resources) ResourcesImpl(android.content.res.ResourcesImpl)

Example 32 with ResourcesKey

use of android.content.res.ResourcesKey in project android_frameworks_base by ResurrectionRemix.

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

Example 33 with ResourcesKey

use of android.content.res.ResourcesKey in project android_frameworks_base by ResurrectionRemix.

the class ResourcesManager method createBaseActivityResources.

/**
     * Creates base resources for an Activity. Calls to
     * {@link #getResources(IBinder, String, String[], String[], String[], int, Configuration,
     * CompatibilityInfo, ClassLoader)} with the same activityToken will have their override
     * configurations merged with the one specified here.
     *
     * @param activityToken Represents an Activity.
     * @param resDir The base resource path. Can be null (only framework resources will be loaded).
     * @param splitResDirs An array of split resource paths. Can be null.
     * @param overlayDirs An array of overlay paths. Can be null.
     * @param libDirs An array of resource library paths. Can be null.
     * @param displayId The ID of the display for which to create the resources.
     * @param overrideConfig The configuration to apply on top of the base configuration. Can be
     *                       null. This provides the base override for this Activity.
     * @param compatInfo The compatibility settings to use. Cannot be null. A default to use is
     *                   {@link CompatibilityInfo#DEFAULT_COMPATIBILITY_INFO}.
     * @param classLoader The class loader to use when inflating Resources. If null, the
     *                    {@link ClassLoader#getSystemClassLoader()} is used.
     * @return a Resources object from which to access resources.
     */
@Nullable
public Resources createBaseActivityResources(@NonNull IBinder activityToken, @Nullable String resDir, @Nullable String[] splitResDirs, @Nullable String[] overlayDirs, @Nullable String[] libDirs, int displayId, @Nullable Configuration overrideConfig, @NonNull CompatibilityInfo compatInfo, @Nullable ClassLoader classLoader) {
    try {
        Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "ResourcesManager#createBaseActivityResources");
        final ResourcesKey key = new ResourcesKey(resDir, splitResDirs, overlayDirs, libDirs, displayId, // Copy
        overrideConfig != null ? new Configuration(overrideConfig) : null, compatInfo);
        classLoader = classLoader != null ? classLoader : ClassLoader.getSystemClassLoader();
        if (DEBUG) {
            Slog.d(TAG, "createBaseActivityResources activity=" + activityToken + " with key=" + key);
        }
        synchronized (this) {
            // Force the creation of an ActivityResourcesStruct.
            getOrCreateActivityResourcesStructLocked(activityToken);
        }
        // Update any existing Activity Resources references.
        updateResourcesForActivity(activityToken, overrideConfig);
        // Now request an actual Resources object.
        return getOrCreateResources(activityToken, key, classLoader);
    } finally {
        Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
    }
}
Also used : Configuration(android.content.res.Configuration) ResourcesKey(android.content.res.ResourcesKey) Nullable(android.annotation.Nullable)

Example 34 with ResourcesKey

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

the class ResourcesManager method updateResourcesForActivity.

/**
     * Updates an Activity's Resources object with overrideConfig. The Resources object
     * that was previously returned by
     * {@link #getResources(IBinder, String, String[], String[], String[], int, Configuration,
     * CompatibilityInfo, ClassLoader)} is
     * still valid and will have the updated configuration.
     * @param activityToken The Activity token.
     * @param overrideConfig The configuration override to update.
     */
public void updateResourcesForActivity(@NonNull IBinder activityToken, @Nullable Configuration overrideConfig) {
    try {
        Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "ResourcesManager#updateResourcesForActivity");
        synchronized (this) {
            final ActivityResources activityResources = getOrCreateActivityResourcesStructLocked(activityToken);
            if (Objects.equals(activityResources.overrideConfig, overrideConfig)) {
                // They are the same, no work to do.
                return;
            }
            // Grab a copy of the old configuration so we can create the delta's of each
            // Resources object associated with this Activity.
            final Configuration oldConfig = new Configuration(activityResources.overrideConfig);
            // Update the Activity's base override.
            if (overrideConfig != null) {
                activityResources.overrideConfig.setTo(overrideConfig);
            } else {
                activityResources.overrideConfig.setToDefaults();
            }
            if (DEBUG) {
                Throwable here = new Throwable();
                here.fillInStackTrace();
                Slog.d(TAG, "updating resources override for activity=" + activityToken + " from oldConfig=" + Configuration.resourceQualifierString(oldConfig) + " to newConfig=" + Configuration.resourceQualifierString(activityResources.overrideConfig), here);
            }
            final boolean activityHasOverrideConfig = !activityResources.overrideConfig.equals(Configuration.EMPTY);
            // Rebase each Resources associated with this Activity.
            final int refCount = activityResources.activityResources.size();
            for (int i = 0; i < refCount; i++) {
                WeakReference<Resources> weakResRef = activityResources.activityResources.get(i);
                Resources resources = weakResRef.get();
                if (resources == null) {
                    continue;
                }
                // Extract the ResourcesKey that was last used to create the Resources for this
                // activity.
                final ResourcesKey oldKey = findKeyForResourceImplLocked(resources.getImpl());
                if (oldKey == null) {
                    Slog.e(TAG, "can't find ResourcesKey for resources impl=" + resources.getImpl());
                    continue;
                }
                // Build the new override configuration for this ResourcesKey.
                final Configuration rebasedOverrideConfig = new Configuration();
                if (overrideConfig != null) {
                    rebasedOverrideConfig.setTo(overrideConfig);
                }
                if (activityHasOverrideConfig && oldKey.hasOverrideConfiguration()) {
                    // Generate a delta between the old base Activity override configuration and
                    // the actual final override configuration that was used to figure out the
                    // real delta this Resources object wanted.
                    Configuration overrideOverrideConfig = Configuration.generateDelta(oldConfig, oldKey.mOverrideConfiguration);
                    rebasedOverrideConfig.updateFrom(overrideOverrideConfig);
                }
                // Create the new ResourcesKey with the rebased override config.
                final ResourcesKey newKey = new ResourcesKey(oldKey.mResDir, oldKey.mSplitResDirs, oldKey.mOverlayDirs, oldKey.mLibDirs, oldKey.mDisplayId, rebasedOverrideConfig, oldKey.mCompatInfo);
                if (DEBUG) {
                    Slog.d(TAG, "rebasing ref=" + resources + " from oldKey=" + oldKey + " to newKey=" + newKey);
                }
                ResourcesImpl resourcesImpl = findResourcesImplForKeyLocked(newKey);
                if (resourcesImpl == null) {
                    resourcesImpl = createResourcesImpl(newKey);
                    if (resourcesImpl != null) {
                        mResourceImpls.put(newKey, new WeakReference<>(resourcesImpl));
                    }
                }
                if (resourcesImpl != null && resourcesImpl != resources.getImpl()) {
                    // Set the ResourcesImpl, updating it for all users of this Resources
                    // object.
                    resources.setImpl(resourcesImpl);
                }
            }
        }
    } finally {
        Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
    }
}
Also used : Configuration(android.content.res.Configuration) ResourcesKey(android.content.res.ResourcesKey) Resources(android.content.res.Resources) ResourcesImpl(android.content.res.ResourcesImpl)

Example 35 with ResourcesKey

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

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

Aggregations

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