Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cfac92b3 authored by Biswarup Pal's avatar Biswarup Pal
Browse files

Cache ViewConfiguration instances per device

Currently, ViewConfiguration instances are cached based on
display density. This means that displays belonging to different
devices can end up using the same ViewConfiguration instance, if
they have the same density (this can happen when an app is having
multiple activities on multiple devices at the same time, such as
one activity on the phone and another on Chromebook). This change
ensures that we make the caching per device by constructing a key
made of deviceId (associated with the context) and the display
density.

Test: atest ViewConfigurationTest
Bug: 405726390
Flag: android.companion.virtualdevice.flags.viewconfiguration_apis
Change-Id: Ie5953150af530c6f48ea57f15b011da097f353bf
parent 9346183e
Loading
Loading
Loading
Loading
+14 −8
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ import android.os.RemoteException;
import android.os.StrictMode;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.SparseArray;
import android.util.LongSparseArray;
import android.util.TypedValue;
import android.view.flags.Flags;

@@ -398,8 +398,7 @@ public class ViewConfiguration {
    @UnsupportedAppUsage
    private boolean sHasPermanentMenuKeySet;

    @UnsupportedAppUsage
    static final SparseArray<ViewConfiguration> sConfigurations = new SparseArray<>(2);
    static final LongSparseArray<ViewConfiguration> sConfigurations = new LongSparseArray<>(2);

    /**
     * @deprecated Use {@link android.view.ViewConfiguration#get(android.content.Context)} instead.
@@ -608,12 +607,11 @@ public class ViewConfiguration {
    public static ViewConfiguration get(@NonNull @UiContext Context context) {
        StrictMode.assertConfigurationContext(context, "ViewConfiguration");

        final int density = getDisplayDensity(context);

        ViewConfiguration configuration = sConfigurations.get(density);
        final long key = createKey(context);
        ViewConfiguration configuration = sConfigurations.get(key);
        if (configuration == null) {
            configuration = new ViewConfiguration(context);
            sConfigurations.put(density, configuration);
            sConfigurations.put(key, configuration);
        }

        return configuration;
@@ -639,7 +637,7 @@ public class ViewConfiguration {
     */
    @VisibleForTesting
    public static void setInstanceForTesting(Context context, ViewConfiguration instance) {
        sConfigurations.put(getDisplayDensity(context), instance);
        sConfigurations.put(createKey(context), instance);
    }

    /**
@@ -1516,6 +1514,14 @@ public class ViewConfiguration {
        return (int) (100.0f * metrics.density);
    }

    /**
     * Returns a key of type long using the display density and deviceId from the {@link Context}.
     */
    private static long createKey(Context context) {
        int displayDensity = getDisplayDensity(context);
        return (((long) displayDensity) << 32) | (context.getDeviceId() & 0xffffffffL);
    }

    /**
     * Fetches resource values statically and caches them locally for fast lookup. Note that these
     * values will not be updated during the lifetime of a process, even if resource overlays are