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

Commit 92fd9a9e authored by Christofer Åkersten's avatar Christofer Åkersten
Browse files

Remove Context requirement from updatable

Bug: 74843539
Test: runtest-cts-MediaComponents && atest VideoView2Test
Change-Id: I1e377435556bdb786322165e3a0563a54bd8fab6
parent 7dccfdd8
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -16,5 +16,5 @@

# Keep entry point for updatable Java classes
-keep public class com.android.media.update.ApiFactory {
   public static java.lang.Object initialize(android.content.res.Resources, android.content.res.Resources$Theme);
   public static com.android.media.update.ApiFactory initialize(android.content.pm.ApplicationInfo);
}
+6 −6
Original line number Diff line number Diff line
@@ -18,8 +18,7 @@ package com.android.media.update;

import android.app.Notification;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.content.pm.ApplicationInfo;
import android.media.MediaBrowser2;
import android.media.MediaBrowser2.BrowserCallback;
import android.media.MediaController2;
@@ -83,10 +82,11 @@ import com.android.widget.VideoView2Impl;

import java.util.concurrent.Executor;

public class ApiFactory implements StaticProvider {
    public static Object initialize(Resources libResources, Theme libTheme)
            throws ReflectiveOperationException {
        ApiHelper.initialize(libResources, libTheme);
public final class ApiFactory implements StaticProvider {
    private ApiFactory() { }

    public static ApiFactory initialize(ApplicationInfo updatableInfo) {
        ApiHelper.initialize(updatableInfo);
        return new ApiFactory();
    }

+43 −23
Original line number Diff line number Diff line
@@ -19,9 +19,12 @@ package com.android.media.update;
import android.annotation.Nullable;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.content.res.XmlResourceParser;
import android.support.annotation.GuardedBy;
import android.support.v4.widget.Space;
import android.support.v7.widget.ButtonBarLayout;
import android.util.AttributeSet;
@@ -35,45 +38,47 @@ import com.android.support.mediarouter.app.MediaRouteExpandCollapseButton;
import com.android.support.mediarouter.app.MediaRouteVolumeSlider;
import com.android.support.mediarouter.app.OverlayListView;

public class ApiHelper {
    private static ApiHelper sInstance;
    private final Resources mLibResources;
    private final Theme mLibTheme;
public final class ApiHelper {
    private static ApplicationInfo sUpdatableInfo;

    public static ApiHelper getInstance() {
        return sInstance;
    }
    @GuardedBy("this")
    private static Theme sLibTheme;

    static void initialize(Resources libResources, Theme libTheme) {
        if (sInstance == null) {
            sInstance = new ApiHelper(libResources, libTheme);
        }
    private ApiHelper() { }

    static void initialize(ApplicationInfo updatableInfo) {
        if (sUpdatableInfo != null) {
            throw new IllegalStateException("initialize should only be called once");
        }

    private ApiHelper(Resources libResources, Theme libTheme) {
        mLibResources = libResources;
        mLibTheme = libTheme;
        sUpdatableInfo = updatableInfo;
    }

    public static Resources getLibResources() {
        return sInstance.mLibResources;
    public static Resources getLibResources(Context context) {
        return getLibTheme(context).getResources();
    }

    public static Theme getLibTheme() {
        return sInstance.mLibTheme;
    public static Theme getLibTheme(Context context) {
        if (sLibTheme != null) return sLibTheme;

        return getLibThemeSynchronized(context);
    }

    public static Theme getLibTheme(int themeId) {
        Theme theme = sInstance.mLibResources.newTheme();
    public static Theme getLibTheme(Context context, int themeId) {
        Theme theme = getLibResources(context).newTheme();
        theme.applyStyle(themeId, true);
        return theme;
    }

    public static LayoutInflater getLayoutInflater(Context context) {
        return getLayoutInflater(context, getLibTheme());
        return getLayoutInflater(context, null);
    }

    public static LayoutInflater getLayoutInflater(Context context, Theme theme) {
        if (theme == null) {
            theme = getLibTheme(context);
        }

        // TODO (b/72975976): Avoid to use ContextThemeWrapper with app context and lib theme.
        LayoutInflater layoutInflater = LayoutInflater.from(context).cloneInContext(
                new ContextThemeWrapper(context, theme));
@@ -106,7 +111,7 @@ public class ApiHelper {
    }

    public static View inflateLibLayout(Context context, int libResId) {
        return inflateLibLayout(context, getLibTheme(), libResId, null, false);
        return inflateLibLayout(context, getLibTheme(context), libResId, null, false);
    }

    public static View inflateLibLayout(Context context, Theme theme, int libResId) {
@@ -115,8 +120,23 @@ public class ApiHelper {

    public static View inflateLibLayout(Context context, Theme theme, int libResId,
            @Nullable ViewGroup root, boolean attachToRoot) {
        try (XmlResourceParser parser = getLibResources().getLayout(libResId)) {
        try (XmlResourceParser parser = getLibResources(context).getLayout(libResId)) {
            return getLayoutInflater(context, theme).inflate(parser, root, attachToRoot);
        }
    }

    private static synchronized Theme getLibThemeSynchronized(Context context) {
        if (sLibTheme != null) return sLibTheme;

        if (sUpdatableInfo == null) {
            throw new IllegalStateException("initialize hasn't been called yet");
        }

        try {
            return sLibTheme = context.getPackageManager()
                    .getResourcesForApplication(sUpdatableInfo).newTheme();
        } catch (NameNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
+5 −4
Original line number Diff line number Diff line
@@ -130,7 +130,7 @@ public class MediaRouteButton extends View {
        mRouter = MediaRouter.getInstance(context);
        mCallback = new MediaRouterCallback();

        Resources.Theme theme = ApiHelper.getLibResources().newTheme();
        Resources.Theme theme = ApiHelper.getLibResources(context).newTheme();
        theme.applyStyle(MediaRouterThemeHelper.getRouterThemeId(context), true);
        TypedArray a = theme.obtainStyledAttributes(attrs,
                R.styleable.MediaRouteButton, defStyleAttr, 0);
@@ -304,7 +304,8 @@ public class MediaRouteButton extends View {
     */
    void setCheatSheetEnabled(boolean enable) {
        TooltipCompat.setTooltipText(this, enable
                ? ApiHelper.getLibResources().getString(R.string.mr_button_content_description)
                ? ApiHelper.getLibResources(getContext())
                    .getString(R.string.mr_button_content_description)
                : null);
    }

@@ -547,7 +548,7 @@ public class MediaRouteButton extends View {
        } else {
            resId = R.string.mr_cast_button_disconnected;
        }
        setContentDescription(ApiHelper.getLibResources().getString(resId));
        setContentDescription(ApiHelper.getLibResources(getContext()).getString(resId));
    }

    private final class MediaRouterCallback extends MediaRouter.Callback {
@@ -604,7 +605,7 @@ public class MediaRouteButton extends View {

        @Override
        protected Drawable doInBackground(Void... params) {
            return ApiHelper.getLibResources().getDrawable(mResId);
            return ApiHelper.getLibResources(getContext()).getDrawable(mResId);
        }

        @Override
+7 −7
Original line number Diff line number Diff line
@@ -99,8 +99,8 @@ public class MediaRouteChooserDialog extends Dialog {

    public MediaRouteChooserDialog(Context context, int theme) {
        // TODO (b/72975976): Avoid to use ContextThemeWrapper with app context and lib theme.
        super(new ContextThemeWrapper(context,
                ApiHelper.getLibTheme(MediaRouterThemeHelper.getRouterThemeId(context))), theme);
        super(new ContextThemeWrapper(context, ApiHelper.getLibTheme(context,
                MediaRouterThemeHelper.getRouterThemeId(context))), theme);
        context = getContext();

        mRouter = MediaRouter.getInstance(context);
@@ -187,8 +187,8 @@ public class MediaRouteChooserDialog extends Dialog {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(ApiHelper.inflateLibLayout(getContext(),
                ApiHelper.getLibTheme(MediaRouterThemeHelper.getRouterThemeId(getContext())),
        setContentView(ApiHelper.inflateLibLayout(getContext(), ApiHelper.getLibTheme(getContext(),
                MediaRouterThemeHelper.getRouterThemeId(getContext())),
                R.layout.mr_chooser_dialog));

        mRoutes = new ArrayList<>();
@@ -206,7 +206,7 @@ public class MediaRouteChooserDialog extends Dialog {
     * Sets the width of the dialog. Also called when configuration changes.
     */
    void updateLayout() {
        getWindow().setLayout(MediaRouteDialogHelper.getDialogWidth(),
        getWindow().setLayout(MediaRouteDialogHelper.getDialogWidth(getContext()),
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }

@@ -263,7 +263,7 @@ public class MediaRouteChooserDialog extends Dialog {
        public RouteAdapter(Context context, List<MediaRouter.RouteInfo> routes) {
            super(context, 0, routes);

            TypedArray styledAttributes = ApiHelper.getLibTheme(
            TypedArray styledAttributes = ApiHelper.getLibTheme(context,
                    MediaRouterThemeHelper.getRouterThemeId(context)).obtainStyledAttributes(
                            new int[] {
                                R.attr.mediaRouteDefaultIconDrawable,
@@ -294,7 +294,7 @@ public class MediaRouteChooserDialog extends Dialog {
            View view = convertView;
            if (view == null) {
                view = ApiHelper.inflateLibLayout(getContext(),
                        ApiHelper.getLibTheme(
                        ApiHelper.getLibTheme(getContext(),
                                MediaRouterThemeHelper.getRouterThemeId(getContext())),
                        R.layout.mr_chooser_list_item, parent, false);
            }
Loading