Search in sources :

Example 16 with ResourcesKey

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

the class ResourcesManager method applyConfigurationToResourcesLocked.

public final boolean applyConfigurationToResourcesLocked(@NonNull Configuration config, @Nullable CompatibilityInfo compat) {
    try {
        Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "ResourcesManager#applyConfigurationToResourcesLocked");
        if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
            if (DEBUG || DEBUG_CONFIGURATION)
                Slog.v(TAG, "Skipping new config: curSeq=" + mResConfiguration.seq + ", newSeq=" + config.seq);
            return false;
        }
        int changes = mResConfiguration.updateFrom(config);
        // Things might have changed in display manager, so clear the cached displays.
        mDisplays.clear();
        DisplayMetrics defaultDisplayMetrics = getDisplayMetrics();
        if (compat != null && (mResCompatibilityInfo == null || !mResCompatibilityInfo.equals(compat))) {
            mResCompatibilityInfo = compat;
            changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT | ActivityInfo.CONFIG_SCREEN_SIZE | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
        }
        Resources.updateSystemConfiguration(config, defaultDisplayMetrics, compat);
        ApplicationPackageManager.configurationChanged();
        //Slog.i(TAG, "Configuration changed in " + currentPackageName());
        Configuration tmpConfig = null;
        for (int i = mResourceImpls.size() - 1; i >= 0; i--) {
            ResourcesKey key = mResourceImpls.keyAt(i);
            WeakReference<ResourcesImpl> weakImplRef = mResourceImpls.valueAt(i);
            ResourcesImpl r = weakImplRef != null ? weakImplRef.get() : null;
            if (r != null) {
                if (DEBUG || DEBUG_CONFIGURATION)
                    Slog.v(TAG, "Changing resources " + r + " config to: " + config);
                int displayId = key.mDisplayId;
                boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
                DisplayMetrics dm = defaultDisplayMetrics;
                final boolean hasOverrideConfiguration = key.hasOverrideConfiguration();
                if (!isDefaultDisplay || hasOverrideConfiguration) {
                    if (tmpConfig == null) {
                        tmpConfig = new Configuration();
                    }
                    tmpConfig.setTo(config);
                    // Get new DisplayMetrics based on the DisplayAdjustments given
                    // to the ResourcesImpl. Update a copy if the CompatibilityInfo
                    // changed, because the ResourcesImpl object will handle the
                    // update internally.
                    DisplayAdjustments daj = r.getDisplayAdjustments();
                    if (compat != null) {
                        daj = new DisplayAdjustments(daj);
                        daj.setCompatibilityInfo(compat);
                    }
                    dm = getDisplayMetrics(displayId, daj);
                    if (!isDefaultDisplay) {
                        applyNonDefaultDisplayMetricsToConfiguration(dm, tmpConfig);
                    }
                    if (hasOverrideConfiguration) {
                        tmpConfig.updateFrom(key.mOverrideConfiguration);
                    }
                    r.updateConfiguration(tmpConfig, dm, compat);
                } else {
                    r.updateConfiguration(config, dm, compat);
                }
            //Slog.i(TAG, "Updated app resources " + v.getKey()
            //        + " " + r + ": " + r.getConfiguration());
            } else {
                //Slog.i(TAG, "Removing old resources " + v.getKey());
                mResourceImpls.removeAt(i);
            }
        }
        return changes != 0;
    } finally {
        Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
    }
}
Also used : Configuration(android.content.res.Configuration) ResourcesKey(android.content.res.ResourcesKey) DisplayAdjustments(android.view.DisplayAdjustments) ResourcesImpl(android.content.res.ResourcesImpl) DisplayMetrics(android.util.DisplayMetrics)

Example 17 with ResourcesKey

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

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 18 with ResourcesKey

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

the class ResourcesManager method applyConfigurationToResourcesLocked.

public final boolean applyConfigurationToResourcesLocked(@NonNull Configuration config, @Nullable CompatibilityInfo compat) {
    try {
        Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "ResourcesManager#applyConfigurationToResourcesLocked");
        if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
            if (DEBUG || DEBUG_CONFIGURATION)
                Slog.v(TAG, "Skipping new config: curSeq=" + mResConfiguration.seq + ", newSeq=" + config.seq);
            return false;
        }
        int changes = mResConfiguration.updateFrom(config);
        // Things might have changed in display manager, so clear the cached displays.
        mDisplays.clear();
        DisplayMetrics defaultDisplayMetrics = getDisplayMetrics();
        if (compat != null && (mResCompatibilityInfo == null || !mResCompatibilityInfo.equals(compat))) {
            mResCompatibilityInfo = compat;
            changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT | ActivityInfo.CONFIG_SCREEN_SIZE | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
        }
        Resources.updateSystemConfiguration(config, defaultDisplayMetrics, compat);
        ApplicationPackageManager.configurationChanged();
        //Slog.i(TAG, "Configuration changed in " + currentPackageName());
        Configuration tmpConfig = null;
        for (int i = mResourceImpls.size() - 1; i >= 0; i--) {
            ResourcesKey key = mResourceImpls.keyAt(i);
            ResourcesImpl r = mResourceImpls.valueAt(i).get();
            if (r != null) {
                if (DEBUG || DEBUG_CONFIGURATION)
                    Slog.v(TAG, "Changing resources " + r + " config to: " + config);
                int displayId = key.mDisplayId;
                boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
                DisplayMetrics dm = defaultDisplayMetrics;
                final boolean hasOverrideConfiguration = key.hasOverrideConfiguration();
                if (!isDefaultDisplay || hasOverrideConfiguration) {
                    if (tmpConfig == null) {
                        tmpConfig = new Configuration();
                    }
                    tmpConfig.setTo(config);
                    // Get new DisplayMetrics based on the DisplayAdjustments given
                    // to the ResourcesImpl. Update a copy if the CompatibilityInfo
                    // changed, because the ResourcesImpl object will handle the
                    // update internally.
                    DisplayAdjustments daj = r.getDisplayAdjustments();
                    if (compat != null) {
                        daj = new DisplayAdjustments(daj);
                        daj.setCompatibilityInfo(compat);
                    }
                    dm = getDisplayMetrics(displayId, daj);
                    if (!isDefaultDisplay) {
                        applyNonDefaultDisplayMetricsToConfiguration(dm, tmpConfig);
                    }
                    if (hasOverrideConfiguration) {
                        tmpConfig.updateFrom(key.mOverrideConfiguration);
                    }
                    r.updateConfiguration(tmpConfig, dm, compat);
                } else {
                    r.updateConfiguration(config, dm, compat);
                }
            //Slog.i(TAG, "Updated app resources " + v.getKey()
            //        + " " + r + ": " + r.getConfiguration());
            } else {
                //Slog.i(TAG, "Removing old resources " + v.getKey());
                mResourceImpls.removeAt(i);
            }
        }
        return changes != 0;
    } finally {
        Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
    }
}
Also used : Configuration(android.content.res.Configuration) ResourcesKey(android.content.res.ResourcesKey) DisplayAdjustments(android.view.DisplayAdjustments) ResourcesImpl(android.content.res.ResourcesImpl) DisplayMetrics(android.util.DisplayMetrics)

Example 19 with ResourcesKey

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

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 20 with ResourcesKey

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

the class ResourcesManager method getResources.

/**
     * Gets or creates a new Resources object associated with the IBinder token. References returned
     * by this method live as long as the Activity, meaning they can be cached and used by the
     * Activity even after a configuration change. If any other parameter is changed
     * (resDir, splitResDirs, overrideConfig) for a given Activity, the same Resources object
     * is updated and handed back to the caller. However, changing the class loader will result in a
     * new Resources object.
     * <p/>
     * If activityToken is null, a cached Resources object will be returned if it matches the
     * input parameters. Otherwise a new Resources object that satisfies these parameters is
     * returned.
     *
     * @param activityToken Represents an Activity. If null, global resources are assumed.
     * @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. Mostly used with Activities that are in multi-window which may override width and
     * height properties from the base config.
     * @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 getResources(@Nullable 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#getResources");
        final ResourcesKey key = new ResourcesKey(resDir, splitResDirs, overlayDirs, libDirs, displayId, // Copy
        overrideConfig != null ? new Configuration(overrideConfig) : null, compatInfo);
        classLoader = classLoader != null ? classLoader : ClassLoader.getSystemClassLoader();
        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)

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