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

Commit fad821e3 authored by Hongwei Wang's avatar Hongwei Wang Committed by Android (Google) Code Review
Browse files

Merge "Use user context when loading pip menu icons" into main

parents d2666d67 943c10b8
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);
    }