Search in sources :

Example 11 with ResourcesImpl

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

the class ResourcesManager method appendLibAssetForMainAssetPath.

/**
     * Appends the library asset path to any ResourcesImpl object that contains the main
     * assetPath.
     * @param assetPath The main asset path for which to add the library asset path.
     * @param libAsset The library asset path to add.
     */
public void appendLibAssetForMainAssetPath(String assetPath, String libAsset) {
    synchronized (this) {
        // Record which ResourcesImpl need updating
        // (and what ResourcesKey they should update to).
        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.equals(assetPath)) {
                if (!ArrayUtils.contains(key.mLibDirs, libAsset)) {
                    final int newLibAssetCount = 1 + (key.mLibDirs != null ? key.mLibDirs.length : 0);
                    final String[] newLibAssets = new String[newLibAssetCount];
                    if (key.mLibDirs != null) {
                        System.arraycopy(key.mLibDirs, 0, newLibAssets, 0, key.mLibDirs.length);
                    }
                    newLibAssets[newLibAssetCount - 1] = libAsset;
                    updatedResourceKeys.put(impl, new ResourcesKey(key.mResDir, key.mSplitResDirs, key.mOverlayDirs, newLibAssets, key.mDisplayId, key.mOverrideConfiguration, key.mCompatInfo));
                }
            }
        }
        redirectResourcesToNewImplLocked(updatedResourceKeys);
    }
}
Also used : ResourcesKey(android.content.res.ResourcesKey) ArrayMap(android.util.ArrayMap) ResourcesImpl(android.content.res.ResourcesImpl)

Example 12 with ResourcesImpl

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

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 13 with ResourcesImpl

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

the class ResourcesManager method createResourcesImpl.

@Nullable
private ResourcesImpl createResourcesImpl(@NonNull ResourcesKey key) {
    final DisplayAdjustments daj = new DisplayAdjustments(key.mOverrideConfiguration);
    daj.setCompatibilityInfo(key.mCompatInfo);
    final AssetManager assets = createAssetManager(key);
    if (assets == null) {
        return null;
    }
    final DisplayMetrics dm = getDisplayMetrics(key.mDisplayId, daj);
    final Configuration config = generateConfig(key, dm);
    final ResourcesImpl impl = new ResourcesImpl(assets, dm, config, daj);
    if (DEBUG) {
        Slog.d(TAG, "- creating impl=" + impl + " with key: " + key);
    }
    return impl;
}
Also used : AssetManager(android.content.res.AssetManager) Configuration(android.content.res.Configuration) DisplayAdjustments(android.view.DisplayAdjustments) ResourcesImpl(android.content.res.ResourcesImpl) DisplayMetrics(android.util.DisplayMetrics) Nullable(android.annotation.Nullable)

Example 14 with ResourcesImpl

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

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)

Example 15 with ResourcesImpl

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

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)

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