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

Commit fef662cd authored by Yuncheol Heo's avatar Yuncheol Heo Committed by Android (Google) Code Review
Browse files

Merge "Add Theme.setNewResourcesImpl() method." into sc-dev

parents edfb697d 7ff59f85
Loading
Loading
Loading
Loading
+69 −23
Original line number Diff line number Diff line
@@ -360,7 +360,7 @@ public class Resources {
                WeakReference<Theme> weakThemeRef = mThemeRefs.get(i);
                Theme theme = weakThemeRef != null ? weakThemeRef.get() : null;
                if (theme != null) {
                    theme.setImpl(mResourcesImpl.newThemeImpl(theme.getKey()));
                    theme.setNewResourcesImpl(mResourcesImpl);
                }
            }
        }
@@ -1500,6 +1500,9 @@ public class Resources {
     * retrieve XML attributes with style and theme information applied.
     */
    public final class Theme {
        private final Object mLock = new Object();

        @GuardedBy("mLock")
        @UnsupportedAppUsage
        private ResourcesImpl.ThemeImpl mThemeImpl;

@@ -1507,8 +1510,16 @@ public class Resources {
        }

        void setImpl(ResourcesImpl.ThemeImpl impl) {
            synchronized (mLock) {
                mThemeImpl = impl;
            }
        }

        void setNewResourcesImpl(ResourcesImpl resImpl) {
            synchronized (mLock) {
                mThemeImpl = resImpl.newThemeImpl(mThemeImpl.getKey());
            }
        }

        /**
         * Place new attribute values into the theme.  The style resource
@@ -1528,8 +1539,10 @@ public class Resources {
         *              if not already defined in the theme.
         */
        public void applyStyle(int resId, boolean force) {
            synchronized (mLock) {
                mThemeImpl.applyStyle(resId, force);
            }
        }

        /**
         * Set this theme to hold the same contents as the theme
@@ -1541,8 +1554,12 @@ public class Resources {
         * @param other The existing Theme to copy from.
         */
        public void setTo(Theme other) {
            synchronized (mLock) {
                synchronized (other.mLock) {
                    mThemeImpl.setTo(other.mThemeImpl);
                }
            }
        }

        /**
         * Return a TypedArray holding the values defined by
@@ -1566,8 +1583,10 @@ public class Resources {
         */
        @NonNull
        public TypedArray obtainStyledAttributes(@NonNull @StyleableRes int[] attrs) {
            synchronized (mLock) {
                return mThemeImpl.obtainStyledAttributes(this, null, attrs, 0, 0);
            }
        }

        /**
         * Return a TypedArray holding the values defined by the style
@@ -1594,8 +1613,10 @@ public class Resources {
        public TypedArray obtainStyledAttributes(@StyleRes int resId,
                @NonNull @StyleableRes int[] attrs)
                throws NotFoundException {
            synchronized (mLock) {
                return mThemeImpl.obtainStyledAttributes(this, null, attrs, 0, resId);
            }
        }

        /**
         * Return a TypedArray holding the attribute values in
@@ -1650,7 +1671,10 @@ public class Resources {
        public TypedArray obtainStyledAttributes(@Nullable AttributeSet set,
                @NonNull @StyleableRes int[] attrs, @AttrRes int defStyleAttr,
                @StyleRes int defStyleRes) {
            return mThemeImpl.obtainStyledAttributes(this, set, attrs, defStyleAttr, defStyleRes);
            synchronized (mLock) {
                return mThemeImpl.obtainStyledAttributes(this, set, attrs, defStyleAttr,
                        defStyleRes);
            }
        }

        /**
@@ -1671,8 +1695,10 @@ public class Resources {
        @NonNull
        @UnsupportedAppUsage
        public TypedArray resolveAttributes(@NonNull int[] values, @NonNull int[] attrs) {
            synchronized (mLock) {
                return mThemeImpl.resolveAttributes(this, values, attrs);
            }
        }

        /**
         * Retrieve the value of an attribute in the Theme.  The contents of
@@ -1692,8 +1718,10 @@ public class Resources {
         *         <var>outValue</var> is valid, else false.
         */
        public boolean resolveAttribute(int resid, TypedValue outValue, boolean resolveRefs) {
            synchronized (mLock) {
                return mThemeImpl.resolveAttribute(resid, outValue, resolveRefs);
            }
        }

        /**
         * Gets all of the attribute ids associated with this {@link Theme}. For debugging only.
@@ -1702,8 +1730,10 @@ public class Resources {
         * @hide
         */
        public int[] getAllAttributes() {
            synchronized (mLock) {
                return mThemeImpl.getAllAttributes();
            }
        }

        /**
         * Returns the resources to which this theme belongs.
@@ -1738,8 +1768,10 @@ public class Resources {
         * @see ActivityInfo
         */
        public @Config int getChangingConfigurations() {
            synchronized (mLock) {
                return mThemeImpl.getChangingConfigurations();
            }
        }

        /**
         * Print contents of this theme out to the log.  For debugging only.
@@ -1749,24 +1781,32 @@ public class Resources {
         * @param prefix Text to prefix each line printed.
         */
        public void dump(int priority, String tag, String prefix) {
            synchronized (mLock) {
                mThemeImpl.dump(priority, tag, prefix);
            }
        }

        // Needed by layoutlib.
        /*package*/ long getNativeTheme() {
            synchronized (mLock) {
                return mThemeImpl.getNativeTheme();
            }
        }

        /*package*/ int getAppliedStyleResId() {
            synchronized (mLock) {
                return mThemeImpl.getAppliedStyleResId();
            }
        }

        /**
         * @hide
         */
        public ThemeKey getKey() {
            synchronized (mLock) {
                return mThemeImpl.getKey();
            }
        }

        private String getResourceNameFromHexString(String hexString) {
            return getResourceName(Integer.parseInt(hexString, 16));
@@ -1781,8 +1821,10 @@ public class Resources {
         */
        @ViewDebug.ExportedProperty(category = "theme", hasAdjacentMapping = true)
        public String[] getTheme() {
            synchronized (mLock) {
                return mThemeImpl.getTheme();
            }
        }

        /** @hide */
        public void encode(@NonNull ViewHierarchyEncoder encoder) {
@@ -1800,8 +1842,10 @@ public class Resources {
         * {@link #applyStyle(int, boolean)}.
         */
        public void rebase() {
            synchronized (mLock) {
                mThemeImpl.rebase();
            }
        }

        /**
         * Returns the resource ID for the style specified using {@code style="..."} in the
@@ -1862,6 +1906,7 @@ public class Resources {
        @NonNull
        public int[] getAttributeResolutionStack(@AttrRes int defStyleAttr,
                @StyleRes int defStyleRes, @StyleRes int explicitStyleRes) {
            synchronized (mLock) {
                int[] stack = mThemeImpl.getAttributeResolutionStack(
                        defStyleAttr, defStyleRes, explicitStyleRes);
                if (stack == null) {
@@ -1871,6 +1916,7 @@ public class Resources {
                }
            }
        }
    }

    static class ThemeKey implements Cloneable {
        int[] mResId;
+54 −76
Original line number Diff line number Diff line
@@ -1314,23 +1314,17 @@ public class ResourcesImpl {
        }

        void applyStyle(int resId, boolean force) {
            synchronized (mKey) {
            mAssets.applyStyleToTheme(mTheme, resId, force);
            mThemeResId = resId;
            mKey.append(resId, force);
        }
        }

        void setTo(ThemeImpl other) {
            synchronized (mKey) {
                synchronized (other.mKey) {
            mAssets.setThemeTo(mTheme, other.mAssets, other.mTheme);

            mThemeResId = other.mThemeResId;
            mKey.setTo(other.getKey());
        }
            }
        }

        @NonNull
        TypedArray obtainStyledAttributes(@NonNull Resources.Theme wrapper,
@@ -1338,7 +1332,6 @@ public class ResourcesImpl {
                @StyleableRes int[] attrs,
                @AttrRes int defStyleAttr,
                @StyleRes int defStyleRes) {
            synchronized (mKey) {
            final int len = attrs.length;
            final TypedArray array = TypedArray.obtain(wrapper.getResources(), len);

@@ -1353,13 +1346,11 @@ public class ResourcesImpl {
            array.mXml = parser;
            return array;
        }
        }

        @NonNull
        TypedArray resolveAttributes(@NonNull Resources.Theme wrapper,
                @NonNull int[] values,
                @NonNull int[] attrs) {
            synchronized (mKey) {
            final int len = attrs.length;
            if (values == null || len != values.length) {
                throw new IllegalArgumentException(
@@ -1372,37 +1363,29 @@ public class ResourcesImpl {
            array.mXml = null;
            return array;
        }
        }

        boolean resolveAttribute(int resid, TypedValue outValue, boolean resolveRefs) {
            synchronized (mKey) {
            return mAssets.getThemeValue(mTheme, resid, outValue, resolveRefs);
        }
        }

        int[] getAllAttributes() {
            return mAssets.getStyleAttributes(getAppliedStyleResId());
        }

        @Config int getChangingConfigurations() {
            synchronized (mKey) {
            final @NativeConfig int nativeChangingConfig =
                    AssetManager.nativeThemeGetChangingConfigurations(mTheme);
            return ActivityInfo.activityInfoConfigNativeToJava(nativeChangingConfig);
        }
        }

        public void dump(int priority, String tag, String prefix) {
            synchronized (mKey) {
            mAssets.dumpTheme(mTheme, priority, tag, prefix);
        }
        }

        String[] getTheme() {
            synchronized (mKey) {
                final int N = mKey.mCount;
                final String[] themes = new String[N * 2];
                for (int i = 0, j = N - 1; i < themes.length; i += 2, --j) {
            final int n = mKey.mCount;
            final String[] themes = new String[n * 2];
            for (int i = 0, j = n - 1; i < themes.length; i += 2, --j) {
                final int resId = mKey.mResId[j];
                final boolean forced = mKey.mForce[j];
                try {
@@ -1414,7 +1397,6 @@ public class ResourcesImpl {
            }
            return themes;
        }
        }

        /**
         * Rebases the theme against the parent Resource object's current
@@ -1422,7 +1404,6 @@ public class ResourcesImpl {
         * {@link #applyStyle(int, boolean)}.
         */
        void rebase() {
            synchronized (mKey) {
            AssetManager.nativeThemeClear(mTheme);

            // Reapply the same styles in the same order.
@@ -1432,7 +1413,6 @@ public class ResourcesImpl {
                mAssets.applyStyleToTheme(mTheme, resId, force);
            }
        }
        }

        /**
         * Returns the ordered list of resource ID that are considered when resolving attribute
@@ -1455,12 +1435,10 @@ public class ResourcesImpl {
        @Nullable
        public int[] getAttributeResolutionStack(@AttrRes int defStyleAttr,
                @StyleRes int defStyleRes, @StyleRes int explicitStyleRes) {
            synchronized (mKey) {
            return mAssets.getAttributeResolutionStack(
                    mTheme, defStyleAttr, defStyleRes, explicitStyleRes);
        }
    }
    }

    private static class LookupStack {