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

Commit 943c10b8 authored by Hongwei Wang's avatar Hongwei Wang
Browse files

Use user context when loading pip menu icons

This is a counterpart of ag/28640753, but for pip2

There is a bug involving trying to load app icons when using secondary
users (or HSUM). Since we load icons using the systemui context which
is always user 0, we might not be able to load the icons if the app
is not installed on user 0.

Fixed by using the current user context when loading menu icons.

Flag: com.android.wm.shell.enable_pip2
Bug: 419687679
Test: Check PiP menu in secondary user and/or HSUM
Change-Id: I33aa241ce44220b5132ce7b3d65c4617a4245bdd
parent fbc1309a
Loading
Loading
Loading
Loading
+35 −1
Original line number Diff line number Diff line
@@ -34,11 +34,13 @@ import android.animation.ValueAnimator;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.ActivityManager;
import android.app.PendingIntent;
import android.app.RemoteAction;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
@@ -149,6 +151,10 @@ public class PipMenuView extends FrameLayout {
    // How long the shell will wait for the app to close the PiP if a custom action is set.
    private final int mPipForceCloseDelay;

    // Context for the currently active user. This may differ from the regular systemui Context
    // in cases such as secondary users or HSUM.
    private Context mContextForUser;

    public PipMenuView(Context context, PhonePipMenuController controller,
            ShellExecutor mainExecutor, Handler mainHandler, PipUiEventLogger pipUiEventLogger) {
        super(context, null, 0);
@@ -196,6 +202,7 @@ public class PipMenuView extends FrameLayout {
                .getInteger(R.integer.config_pipExitAnimationDuration);

        initAccessibility();
        setContextForUser();
    }

    private void initAccessibility() {
@@ -466,7 +473,7 @@ public class PipMenuView extends FrameLayout {
                        actionView.setImageDrawable(null);
                    } else {
                        // TODO: Check if the action drawable has changed before we reload it
                        action.getIcon().loadDrawableAsync(mContext, d -> {
                        action.getIcon().loadDrawableAsync(mContextForUser, d -> {
                            if (d != null) {
                                d.setTint(Color.WHITE);
                                actionView.setImageDrawable(d);
@@ -500,6 +507,33 @@ public class PipMenuView extends FrameLayout {
        expandContainer.requestLayout();
    }

    /**
     * Sets the Context for the current user. If the user is the same as systemui, then simply
     * use systemui Context.
     */
    private void setContextForUser() {
        int userId = ActivityManager.getCurrentUser();

        if (mContext.getUserId() != userId) {
            try {
                mContextForUser = mContext.createPackageContextAsUser(mContext.getPackageName(),
                        Context.CONTEXT_RESTRICTED, new UserHandle(userId));
            } catch (PackageManager.NameNotFoundException e) {
                // Shouldn't happen, use systemui context as backup
                ProtoLog.w(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
                        "%s: Failed to get context for user. Sysui userid=%d,"
                                + " current userid=%d, error=%s",
                        TAG,
                        mContext.getUserId(),
                        userId,
                        e);
                mContextForUser = mContext;
            }
        } else {
            mContextForUser = mContext;
        }
    }

    private void notifyMenuStateChangeStart(int menuState, boolean resize, Runnable callback) {
        mController.onMenuStateChangeStart(menuState, resize, callback);
    }