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

Commit b71617cf authored by Felipe Leme's avatar Felipe Leme
Browse files

Fixed SaveUi on Autofill for visible background users.

It needs to set the proper display in the FillUI context, otherwise
it's rendered in the wrong display.

Test: atest --user-type secondary_user_on_secondary_display CtsAutoFillServiceTestCases:android.autofillservice.cts.dropdown.LoginActivityTest#testAutoFillOneDatasetAndSave
Test: atest CtsAutoFillServiceTestCases # on phone
Bug: 271031908

Change-Id: I4f5700c75bbca0afb2008e51146a01b4e3d70f5b
parent 66a64d23
Loading
Loading
Loading
Loading
+1 −6
Original line number Diff line number Diff line
@@ -79,11 +79,6 @@ import static com.android.server.autofill.SaveEventLogger.SAVE_UI_SHOWN_REASON_O
import static com.android.server.autofill.SaveEventLogger.SAVE_UI_SHOWN_REASON_REQUIRED_ID_CHANGE;
import static com.android.server.autofill.SaveEventLogger.SAVE_UI_SHOWN_REASON_TRIGGER_ID_SET;
import static com.android.server.autofill.SaveEventLogger.SAVE_UI_SHOWN_REASON_UNKNOWN;
import static com.android.server.autofill.SessionCommittedEventLogger.CommitReason;
import static com.android.server.autofill.SessionCommittedEventLogger.COMMIT_REASON_ACTIVITY_FINISHED;
import static com.android.server.autofill.SessionCommittedEventLogger.COMMIT_REASON_VIEW_CHANGED;
import static com.android.server.autofill.SessionCommittedEventLogger.COMMIT_REASON_VIEW_CLICKED;
import static com.android.server.autofill.SessionCommittedEventLogger.COMMIT_REASON_VIEW_COMMITTED;
import static com.android.server.autofill.SessionCommittedEventLogger.COMMIT_REASON_SESSION_DESTROYED;
import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_RECEIVER_EXTRAS;
import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_STRUCTURE;
@@ -3406,7 +3401,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
                final long saveUiDisplayStartTimestamp = SystemClock.elapsedRealtime();
                getUiForShowing().showSaveUi(serviceLabel, serviceIcon,
                        mService.getServicePackageName(), saveInfo, this,
                        mComponentName, this, mPendingSaveUi, isUpdate, mCompatMode,
                        mComponentName, this, userId, mPendingSaveUi, isUpdate, mCompatMode,
                        response.getShowSaveDialogIcon());
                mSaveEventLogger.maybeSetLatencySaveUiDisplayMillis(
                    SystemClock.elapsedRealtime()- saveUiDisplayStartTimestamp);
+5 −4
Original line number Diff line number Diff line
@@ -325,11 +325,12 @@ public final class AutoFillUI {
    public void showSaveUi(@NonNull CharSequence serviceLabel, @NonNull Drawable serviceIcon,
            @Nullable String servicePackageName, @NonNull SaveInfo info,
            @NonNull ValueFinder valueFinder, @NonNull ComponentName componentName,
            @NonNull AutoFillUiCallback callback, @NonNull PendingUi pendingSaveUi,
            boolean isUpdate, boolean compatMode, boolean showServiceIcon) {
            @NonNull AutoFillUiCallback callback, @UserIdInt int userId,
            @NonNull PendingUi pendingSaveUi, boolean isUpdate, boolean compatMode,
            boolean showServiceIcon) {
        if (sVerbose) {
            Slog.v(TAG, "showSaveUi(update=" + isUpdate + ") for " + componentName.toShortString()
                    + ": " + info);
                    + " and user " + userId + ": " + info);
        }
        int numIds = 0;
        numIds += info.getRequiredIds() == null ? 0 : info.getRequiredIds().length;
@@ -349,7 +350,7 @@ public final class AutoFillUI {
            }
            hideAllUiThread(callback);
            mSaveUiCallback = callback;
            mSaveUi = new SaveUi(mContext, pendingSaveUi, serviceLabel, serviceIcon,
            mSaveUi = new SaveUi(mContext, userId, pendingSaveUi, serviceLabel, serviceIcon,
                    servicePackageName, componentName, info, valueFinder, mOverlayControl,
                    new SaveUi.OnSaveListener() {
                @Override
+68 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.server.autofill.ui;

import static com.android.server.autofill.Helper.sDebug;

import android.annotation.UserIdInt;
import android.content.Context;
import android.hardware.display.DisplayManager;
import android.os.UserManager;
import android.view.Display;

import com.android.server.LocalServices;
import com.android.server.pm.UserManagerInternal;
import com.android.server.utils.Slogf;

/**
 * Helper for display-related needs.
 */
final class DisplayHelper {

    private static final String TAG = "AutofillDisplayHelper";

    private static final UserManagerInternal sUmi = LocalServices
            .getService(UserManagerInternal.class);

    /**
     * Gets a context with the proper display id set for the given user.
     *
     * <p>For most cases it will return the provided context, but on devices that
     * {@link UserManager#isVisibleBackgroundUsersEnabled() support visible background users}, it
     * will return a context with the display the user started visible on.
     */
    static Context getDisplayContext(Context context, @UserIdInt int userId) {
        if (!UserManager.isVisibleBackgroundUsersEnabled()) {
            return context;
        }
        int displayId = sUmi.getMainDisplayAssignedToUser(userId);
        if (sDebug) {
            Slogf.d(TAG, "Creating context for display %d for user %d", displayId, userId);
        }
        Display display = context.getSystemService(DisplayManager.class).getDisplay(displayId);
        if (display == null) {
            Slogf.wtf(TAG, "Could not get display with id %d (which is associated with user %d; "
                    + "FillUi operations will probably fail", displayId, userId);
            return context;
        }

        return context.createDisplayContext(display);
    }

    private DisplayHelper() {
        throw new UnsupportedOperationException("Contains only static methods");
    }
}
+2 −23
Original line number Diff line number Diff line
@@ -29,8 +29,6 @@ import android.content.pm.PackageManager;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.hardware.display.DisplayManager;
import android.os.UserManager;
import android.service.autofill.Dataset;
import android.service.autofill.Dataset.DatasetFieldFilter;
import android.service.autofill.FillResponse;
@@ -39,7 +37,6 @@ import android.util.PluralsMessageFormatter;
import android.util.Slog;
import android.util.TypedValue;
import android.view.ContextThemeWrapper;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
@@ -61,12 +58,9 @@ import android.widget.RemoteViews;
import android.widget.TextView;

import com.android.internal.R;
import com.android.server.LocalServices;
import com.android.server.UiThread;
import com.android.server.autofill.AutofillManagerService;
import com.android.server.autofill.Helper;
import com.android.server.pm.UserManagerInternal;
import com.android.server.utils.Slogf;

import java.io.PrintWriter;
import java.util.ArrayList;
@@ -140,29 +134,14 @@ final class FillUi {
        return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK);
    }

    FillUi(@NonNull Context context, @UserIdInt int userId, @NonNull FillResponse response,
    FillUi(@NonNull Context systemContext, @UserIdInt int userId, @NonNull FillResponse response,
            @NonNull AutofillId focusedViewId, @Nullable String filterText,
            @NonNull OverlayControl overlayControl, @NonNull CharSequence serviceLabel,
            @NonNull Drawable serviceIcon, boolean nightMode, @NonNull Callback callback) {
        if (sVerbose) Slog.v(TAG, "nightMode: " + nightMode);
        mThemeId = nightMode ? THEME_ID_DARK : THEME_ID_LIGHT;
        mCallback = callback;

        if (UserManager.isVisibleBackgroundUsersEnabled()) {
            UserManagerInternal umi = LocalServices.getService(UserManagerInternal.class);
            int displayId = umi.getMainDisplayAssignedToUser(userId);
            if (sDebug) {
                Slogf.d(TAG, "Creating context for display %d for user %d", displayId, userId);
            }
            Display display = context.getSystemService(DisplayManager.class).getDisplay(displayId);
            if (display != null) {
                context = context.createDisplayContext(display);
            } else {
                Slogf.d(TAG, "Could not get display with id %d (which is associated with user %d; "
                        + "FillUi operations will probably fail", displayId, userId);
            }
        }

        Context context = DisplayHelper.getDisplayContext(systemContext, userId);
        mFullScreen = isFullScreen(context);
        mContext = new ContextThemeWrapper(context, mThemeId);

+3 −1
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import static com.android.server.autofill.Helper.sVerbose;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.ComponentName;
@@ -172,12 +173,13 @@ final class SaveUi {

    private boolean mDestroyed;

    SaveUi(@NonNull Context context, @NonNull PendingUi pendingUi,
    SaveUi(@NonNull Context systemContext, @UserIdInt int userId, @NonNull PendingUi pendingUi,
           @NonNull CharSequence serviceLabel, @NonNull Drawable serviceIcon,
           @Nullable String servicePackageName, @NonNull ComponentName componentName,
           @NonNull SaveInfo info, @NonNull ValueFinder valueFinder,
           @NonNull OverlayControl overlayControl, @NonNull OnSaveListener listener,
           boolean nightMode, boolean isUpdate, boolean compatMode, boolean showServiceIcon) {
        Context context = DisplayHelper.getDisplayContext(systemContext, userId);
        if (sVerbose) Slog.v(TAG, "nightMode: " + nightMode);
        mThemeId = nightMode ? THEME_ID_DARK : THEME_ID_LIGHT;
        mPendingUi = pendingUi;