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

Commit 8b6001e1 authored by Clark Scheff's avatar Clark Scheff
Browse files

Allow specifying a theme when creating a Context

A Context normally loads the resources associated with the given
package that this context is for.  This patch allows us to specify
a theme we want attached to a given context's resources.

Change-Id: I1e619f038566233bd89ee96eb8eb87fa6894e630
parent 8feaa5bf
Loading
Loading
Loading
Loading
+24 −9
Original line number Diff line number Diff line
@@ -1935,24 +1935,30 @@ class ContextImpl extends Context {
    @Override
    public Context createPackageContext(String packageName, int flags)
            throws NameNotFoundException {
        return createPackageContextAsUser(packageName, flags,
        return createPackageContextAsUser(packageName, null, flags,
                mUser != null ? mUser : Process.myUserHandle());
    }

    @Override
    public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
            throws NameNotFoundException {
        return createPackageContextAsUser(packageName, null, flags, user);
    }

    @Override
    public Context createPackageContextAsUser(String packageName, String themePackageName,
            int flags, UserHandle user) throws NameNotFoundException {
        final boolean restricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
        if (packageName.equals("system") || packageName.equals("android")) {
            return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
                    user, restricted, mDisplay, mOverrideConfiguration);
                    user, restricted, mDisplay, mOverrideConfiguration, themePackageName);
        }

        LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
                flags, user.getIdentifier());
        if (pi != null) {
            ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
                    user, restricted, mDisplay, mOverrideConfiguration);
                    user, restricted, mDisplay, mOverrideConfiguration, themePackageName);
            if (c.mResources != null) {
                return c;
            }
@@ -2024,7 +2030,7 @@ class ContextImpl extends Context {
    static ContextImpl createSystemContext(ActivityThread mainThread) {
        LoadedApk packageInfo = new LoadedApk(mainThread);
        ContextImpl context = new ContextImpl(null, mainThread,
                packageInfo, null, null, false, null, null);
                packageInfo, null, null, false, null, null, null);
        context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
                context.mResourcesManager.getDisplayMetricsLocked(Display.DEFAULT_DISPLAY));
        return context;
@@ -2033,7 +2039,7 @@ class ContextImpl extends Context {
    static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
        if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
        return new ContextImpl(null, mainThread,
                packageInfo, null, null, false, null, null);
                packageInfo, null, null, false, null, null, null);
    }

    static ContextImpl createActivityContext(ActivityThread mainThread,
@@ -2041,12 +2047,19 @@ class ContextImpl extends Context {
        if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
        if (activityToken == null) throw new IllegalArgumentException("activityInfo");
        return new ContextImpl(null, mainThread,
                packageInfo, activityToken, null, false, null, null);
                packageInfo, activityToken, null, false, null, null, null);
    }

    private ContextImpl(ContextImpl container, ActivityThread mainThread,
            LoadedApk packageInfo, IBinder activityToken, UserHandle user, boolean restricted,
            Display display, Configuration overrideConfiguration) {
        this(container, mainThread, packageInfo, activityToken, user, restricted, display,
                overrideConfiguration, null);
    }

    private ContextImpl(ContextImpl container, ActivityThread mainThread,
            LoadedApk packageInfo, IBinder activityToken, UserHandle user, boolean restricted,
            Display display, Configuration overrideConfiguration, String themePackageName) {
        mOuterContext = this;

        mMainThread = mainThread;
@@ -2077,14 +2090,16 @@ class ContextImpl extends Context {

        Resources resources = packageInfo.getResources(mainThread);
        if (resources != null) {
            if (activityToken != null
            if (activityToken != null || themePackageName != null
                    || displayId != Display.DEFAULT_DISPLAY
                    || overrideConfiguration != null
                    || (compatInfo != null && compatInfo.applicationScale
                            != resources.getCompatibilityInfo().applicationScale)) {
                resources = mResourcesManager.getTopLevelResources(
                resources = themePackageName == null ? mResourcesManager.getTopLevelResources(
                        packageInfo.getResDir(), packageInfo.getOverlayDirs(), displayId,
                        packageInfo.getAppDir(), overrideConfiguration, compatInfo, activityToken, mOuterContext);
                        packageInfo.getAppDir(), overrideConfiguration, compatInfo, activityToken, mOuterContext) :
                mResourcesManager.getTopLevelThemedResources(packageInfo.getResDir(), displayId,
                        packageInfo.getPackageName(), themePackageName, compatInfo ,activityToken);
            }
        }
        mResources = resources;
+12 −0
Original line number Diff line number Diff line
@@ -2912,6 +2912,18 @@ public abstract class Context {
            String packageName, int flags, UserHandle user)
            throws PackageManager.NameNotFoundException;

    /**
     * Similar to {@link #createPackageContext(String, int)}, but with a
     * different {@link UserHandle}. For example, {@link #getContentResolver()}
     * will open any {@link Uri} as the given user.  A theme package can be
     * specified which will be used when adding resources to this context
     *
     * @hide
     */
    public abstract Context createPackageContextAsUser(
            String packageName, String themePackageName, int flags, UserHandle user)
            throws PackageManager.NameNotFoundException;

    /**
     * Get the userId associated with this context
     * @return user id
+7 −0
Original line number Diff line number Diff line
@@ -644,6 +644,13 @@ public class ContextWrapper extends Context {
        return mBase.createPackageContextAsUser(packageName, flags, user);
    }

    /** @hide */
    @Override
    public Context createPackageContextAsUser(String packageName, String themePackageName,
            int flags, UserHandle user) throws PackageManager.NameNotFoundException {
        return mBase.createPackageContextAsUser(packageName, themePackageName, flags, user);
    }

    /** @hide */
    @Override
    public int getUserId() {
+7 −0
Original line number Diff line number Diff line
@@ -557,6 +557,13 @@ public class MockContext extends Context {
        throw new UnsupportedOperationException();
    }

    /** {@hide} */
    @Override
    public Context createPackageContextAsUser(String packageName, String themePackageName,
            int flags, UserHandle user) throws PackageManager.NameNotFoundException {
        throw new UnsupportedOperationException();
    }

    /** {@hide} */
    @Override
    public int getUserId() {