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

Commit 16b2de5a authored by Feng Cao's avatar Feng Cao
Browse files

Add the ime package name to the inline suggestions request

Test: manual
Bug: 147229274

Change-Id: I3d81b1d56989e46f65f9b9fc6e497952486335b5
parent 3df3e3b0
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -54679,6 +54679,7 @@ package android.view.inputmethod {
  public final class InlineSuggestionsRequest implements android.os.Parcelable {
    method public int describeContents();
    method @NonNull public String getHostPackageName();
    method public int getMaxSuggestionCount();
    method @NonNull public java.util.List<android.view.inline.InlinePresentationSpec> getPresentationSpecs();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
+73 −9
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
package android.view.inputmethod;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.ActivityThread;
import android.os.Parcelable;
import android.view.inline.InlinePresentationSpec;

@@ -49,6 +51,21 @@ public final class InlineSuggestionsRequest implements Parcelable {
     */
    private final @NonNull List<InlinePresentationSpec> mPresentationSpecs;

    /**
     * The package name of the app that requests for the inline suggestions and will host the
     * embedded suggestion views. The app does not have to set the value for the field because
     * it'll be set by the system for safety reasons.
     */
    private @NonNull String mHostPackageName;

    /**
     * @hide
     * @see {@link #mHostPackageName}.
     */
    public void setHostPackageName(@NonNull String hostPackageName) {
        mHostPackageName = hostPackageName;
    }

    private void onConstructed() {
        Preconditions.checkState(mMaxSuggestionCount >= mPresentationSpecs.size());
    }
@@ -57,9 +74,15 @@ public final class InlineSuggestionsRequest implements Parcelable {
        return SUGGESTION_COUNT_UNLIMITED;
    }

    private static String defaultHostPackageName() {
        return ActivityThread.currentPackageName();
    }

    /** @hide */
    abstract static class BaseBuilder {
        abstract Builder setPresentationSpecs(@NonNull List<InlinePresentationSpec> value);

        abstract Builder setHostPackageName(@Nullable String value);
    }


@@ -80,11 +103,15 @@ public final class InlineSuggestionsRequest implements Parcelable {
    @DataClass.Generated.Member
    /* package-private */ InlineSuggestionsRequest(
            int maxSuggestionCount,
            @NonNull List<InlinePresentationSpec> presentationSpecs) {
            @NonNull List<InlinePresentationSpec> presentationSpecs,
            @NonNull String hostPackageName) {
        this.mMaxSuggestionCount = maxSuggestionCount;
        this.mPresentationSpecs = presentationSpecs;
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, mPresentationSpecs);
        this.mHostPackageName = hostPackageName;
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, mHostPackageName);

        onConstructed();
    }
@@ -108,6 +135,16 @@ public final class InlineSuggestionsRequest implements Parcelable {
        return mPresentationSpecs;
    }

    /**
     * The package name of the app that requests for the inline suggestions and will host the
     * embedded suggestion views. The app does not have to set the value for the field because
     * it'll be set by the system for safety reasons.
     */
    @DataClass.Generated.Member
    public @NonNull String getHostPackageName() {
        return mHostPackageName;
    }

    @Override
    @DataClass.Generated.Member
    public String toString() {
@@ -116,13 +153,14 @@ public final class InlineSuggestionsRequest implements Parcelable {

        return "InlineSuggestionsRequest { " +
                "maxSuggestionCount = " + mMaxSuggestionCount + ", " +
                "presentationSpecs = " + mPresentationSpecs +
                "presentationSpecs = " + mPresentationSpecs + ", " +
                "hostPackageName = " + mHostPackageName +
        " }";
    }

    @Override
    @DataClass.Generated.Member
    public boolean equals(@android.annotation.Nullable Object o) {
    public boolean equals(@Nullable Object o) {
        // You can override field equality logic by defining either of the methods like:
        // boolean fieldNameEquals(InlineSuggestionsRequest other) { ... }
        // boolean fieldNameEquals(FieldType otherValue) { ... }
@@ -134,7 +172,8 @@ public final class InlineSuggestionsRequest implements Parcelable {
        //noinspection PointlessBooleanExpression
        return true
                && mMaxSuggestionCount == that.mMaxSuggestionCount
                && java.util.Objects.equals(mPresentationSpecs, that.mPresentationSpecs);
                && java.util.Objects.equals(mPresentationSpecs, that.mPresentationSpecs)
                && java.util.Objects.equals(mHostPackageName, that.mHostPackageName);
    }

    @Override
@@ -146,6 +185,7 @@ public final class InlineSuggestionsRequest implements Parcelable {
        int _hash = 1;
        _hash = 31 * _hash + mMaxSuggestionCount;
        _hash = 31 * _hash + java.util.Objects.hashCode(mPresentationSpecs);
        _hash = 31 * _hash + java.util.Objects.hashCode(mHostPackageName);
        return _hash;
    }

@@ -157,6 +197,7 @@ public final class InlineSuggestionsRequest implements Parcelable {

        dest.writeInt(mMaxSuggestionCount);
        dest.writeParcelableList(mPresentationSpecs, flags);
        dest.writeString(mHostPackageName);
    }

    @Override
@@ -173,11 +214,15 @@ public final class InlineSuggestionsRequest implements Parcelable {
        int maxSuggestionCount = in.readInt();
        List<InlinePresentationSpec> presentationSpecs = new ArrayList<>();
        in.readParcelableList(presentationSpecs, InlinePresentationSpec.class.getClassLoader());
        String hostPackageName = in.readString();

        this.mMaxSuggestionCount = maxSuggestionCount;
        this.mPresentationSpecs = presentationSpecs;
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, mPresentationSpecs);
        this.mHostPackageName = hostPackageName;
        com.android.internal.util.AnnotationValidations.validate(
                NonNull.class, null, mHostPackageName);

        onConstructed();
    }
@@ -205,6 +250,7 @@ public final class InlineSuggestionsRequest implements Parcelable {

        private int mMaxSuggestionCount;
        private @NonNull List<InlinePresentationSpec> mPresentationSpecs;
        private @NonNull String mHostPackageName;

        private long mBuilderFieldsSet = 0L;

@@ -260,22 +306,40 @@ public final class InlineSuggestionsRequest implements Parcelable {
            return this;
        }

        /**
         * The package name of the app that requests for the inline suggestions and will host the
         * embedded suggestion views. The app does not have to set the value for the field because
         * it'll be set by the system for safety reasons.
         */
        @DataClass.Generated.Member
        @Override
        @NonNull Builder setHostPackageName(@NonNull String value) {
            checkNotUsed();
            mBuilderFieldsSet |= 0x4;
            mHostPackageName = value;
            return this;
        }

        /** Builds the instance. This builder should not be touched after calling this! */
        public @NonNull InlineSuggestionsRequest build() {
            checkNotUsed();
            mBuilderFieldsSet |= 0x4; // Mark builder used
            mBuilderFieldsSet |= 0x8; // Mark builder used

            if ((mBuilderFieldsSet & 0x1) == 0) {
                mMaxSuggestionCount = defaultMaxSuggestionCount();
            }
            if ((mBuilderFieldsSet & 0x4) == 0) {
                mHostPackageName = defaultHostPackageName();
            }
            InlineSuggestionsRequest o = new InlineSuggestionsRequest(
                    mMaxSuggestionCount,
                    mPresentationSpecs);
                    mPresentationSpecs,
                    mHostPackageName);
            return o;
        }

        private void checkNotUsed() {
            if ((mBuilderFieldsSet & 0x4) != 0) {
            if ((mBuilderFieldsSet & 0x8) != 0) {
                throw new IllegalStateException(
                        "This Builder should not be reused. Use a new Builder instance instead");
            }
@@ -283,10 +347,10 @@ public final class InlineSuggestionsRequest implements Parcelable {
    }

    @DataClass.Generated(
            time = 1576637222199L,
            time = 1578948035951L,
            codegenVersion = "1.0.14",
            sourceFile = "frameworks/base/core/java/android/view/inputmethod/InlineSuggestionsRequest.java",
            inputSignatures = "public static final  int SUGGESTION_COUNT_UNLIMITED\nprivate final  int mMaxSuggestionCount\nprivate final @android.annotation.NonNull java.util.List<android.view.inline.InlinePresentationSpec> mPresentationSpecs\nprivate  void onConstructed()\nprivate static  int defaultMaxSuggestionCount()\nclass InlineSuggestionsRequest extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genEqualsHashCode=true, genToString=true, genBuilder=true)\nabstract  android.view.inputmethod.InlineSuggestionsRequest.Builder setPresentationSpecs(java.util.List<android.view.inline.InlinePresentationSpec>)\nclass BaseBuilder extends java.lang.Object implements []")
            inputSignatures = "public static final  int SUGGESTION_COUNT_UNLIMITED\nprivate final  int mMaxSuggestionCount\nprivate final @android.annotation.NonNull java.util.List<android.view.inline.InlinePresentationSpec> mPresentationSpecs\nprivate @android.annotation.NonNull java.lang.String mHostPackageName\npublic  void setHostPackageName(java.lang.String)\nprivate  void onConstructed()\nprivate static  int defaultMaxSuggestionCount()\nprivate static  java.lang.String defaultHostPackageName()\nclass InlineSuggestionsRequest extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genEqualsHashCode=true, genToString=true, genBuilder=true)\nabstract  android.view.inputmethod.InlineSuggestionsRequest.Builder setPresentationSpecs(java.util.List<android.view.inline.InlinePresentationSpec>)\nabstract  android.view.inputmethod.InlineSuggestionsRequest.Builder setHostPackageName(java.lang.String)\nclass BaseBuilder extends java.lang.Object implements []")
    @Deprecated
    private void __metadata() {}

+5 −1
Original line number Diff line number Diff line
@@ -162,6 +162,9 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
    /** uid the session is for */
    public final int uid;

    /** user id the session is for */
    public final int userId;

    /** ID of the task associated with this session's activity */
    public final int taskId;

@@ -611,7 +614,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
            int newState, int flags) {
        if (isInlineSuggestionsEnabled()) {
            mInlineSuggestionsRequestCallback = new InlineSuggestionsRequestCallbackImpl();
            mInputMethodManagerInternal.onCreateInlineSuggestionsRequest(
            mInputMethodManagerInternal.onCreateInlineSuggestionsRequest(userId,
                    mComponentName, mCurrentViewId, mInlineSuggestionsRequestCallback);
        }

@@ -757,6 +760,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
        mFlags = flags;
        this.taskId = taskId;
        this.uid = uid;
        this.userId = userId;
        mStartTime = SystemClock.elapsedRealtime();
        mService = service;
        mLock = lock;
+6 −4
Original line number Diff line number Diff line
@@ -68,8 +68,9 @@ public abstract class InputMethodManagerInternal {
     * @param autofillId {@link AutofillId} of currently focused field.
     * @param cb {@link IInlineSuggestionsRequestCallback} used to pass back the request object.
     */
    public abstract void onCreateInlineSuggestionsRequest(ComponentName componentName,
            AutofillId autofillId, IInlineSuggestionsRequestCallback cb);
    public abstract void onCreateInlineSuggestionsRequest(@UserIdInt int userId,
            ComponentName componentName, AutofillId autofillId,
            IInlineSuggestionsRequestCallback cb);

    /**
     * Fake implementation of {@link InputMethodManagerInternal}.  All the methods do nothing.
@@ -95,8 +96,9 @@ public abstract class InputMethodManagerInternal {
                }

                @Override
                public void onCreateInlineSuggestionsRequest(ComponentName componentName,
                        AutofillId autofillId, IInlineSuggestionsRequestCallback cb) {
                public void onCreateInlineSuggestionsRequest(int userId,
                        ComponentName componentName, AutofillId autofillId,
                        IInlineSuggestionsRequestCallback cb) {
                }
            };

+52 −10
Original line number Diff line number Diff line
@@ -111,6 +111,7 @@ import android.view.WindowManager.LayoutParams;
import android.view.WindowManager.LayoutParams.SoftInputModeFlags;
import android.view.autofill.AutofillId;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InlineSuggestionsRequest;
import android.view.inputmethod.InputBinding;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputConnectionInspector;
@@ -142,6 +143,7 @@ import com.android.internal.os.TransferPipe;
import com.android.internal.util.DumpUtils;
import com.android.internal.util.IndentingPrintWriter;
import com.android.internal.view.IInlineSuggestionsRequestCallback;
import com.android.internal.view.IInlineSuggestionsResponseCallback;
import com.android.internal.view.IInputContext;
import com.android.internal.view.IInputMethod;
import com.android.internal.view.IInputMethodClient;
@@ -1790,15 +1792,18 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
    }

    @GuardedBy("mMethodMap")
    private void onCreateInlineSuggestionsRequestLocked(ComponentName componentName,
            AutofillId autofillId, IInlineSuggestionsRequestCallback callback) {

    private void onCreateInlineSuggestionsRequestLocked(@UserIdInt int userId,
            ComponentName componentName, AutofillId autofillId,
            IInlineSuggestionsRequestCallback callback) {
        final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
        try {
            if (imi != null && imi.isInlineSuggestionsEnabled() && mCurMethod != null) {
            if (userId == mSettings.getCurrentUserId() && imi != null
                    && imi.isInlineSuggestionsEnabled() && mCurMethod != null) {
                executeOrSendMessage(mCurMethod,
                        mCaller.obtainMessageOOOO(MSG_INLINE_SUGGESTIONS_REQUEST, mCurMethod,
                                componentName, autofillId, callback));
                                componentName, autofillId,
                                new InlineSuggestionsRequestCallbackDecorator(callback,
                                        imi.getPackageName())));
            } else {
                callback.onInlineSuggestionsUnsupported();
            }
@@ -1807,6 +1812,42 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
        }
    }

    /**
     * The decorator which validates the host package name in the
     * {@link InlineSuggestionsRequest} argument to make sure it matches the IME package name.
     */
    private static final class InlineSuggestionsRequestCallbackDecorator
            extends IInlineSuggestionsRequestCallback.Stub {
        @NonNull
        private final IInlineSuggestionsRequestCallback mCallback;
        @NonNull
        private final String mImePackageName;

        InlineSuggestionsRequestCallbackDecorator(
                @NonNull IInlineSuggestionsRequestCallback callback,
                @NonNull String imePackageName) {
            mCallback = callback;
            mImePackageName = imePackageName;
        }

        @Override
        public void onInlineSuggestionsUnsupported() throws RemoteException {
            mCallback.onInlineSuggestionsUnsupported();
        }

        @Override
        public void onInlineSuggestionsRequest(InlineSuggestionsRequest request,
                IInlineSuggestionsResponseCallback callback) throws RemoteException {
            if (!mImePackageName.equals(request.getHostPackageName())) {
                throw new SecurityException(
                        "Host package name in the provide request=[" + request.getHostPackageName()
                                + "] doesn't match the IME package name=[" + mImePackageName
                                + "].");
            }
            mCallback.onInlineSuggestionsRequest(request, callback);
        }
    }

    /**
     * @param imiId if null, returns enabled subtypes for the current imi
     * @return enabled subtypes of the specified imi
@@ -4471,10 +4512,11 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
        }
    }

    private void onCreateInlineSuggestionsRequest(ComponentName componentName,
            AutofillId autofillId, IInlineSuggestionsRequestCallback callback) {
    private void onCreateInlineSuggestionsRequest(@UserIdInt int userId,
            ComponentName componentName, AutofillId autofillId,
            IInlineSuggestionsRequestCallback callback) {
        synchronized (mMethodMap) {
            onCreateInlineSuggestionsRequestLocked(componentName, autofillId, callback);
            onCreateInlineSuggestionsRequestLocked(userId, componentName, autofillId, callback);
        }
    }

@@ -4510,9 +4552,9 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
        }

        @Override
        public void onCreateInlineSuggestionsRequest(ComponentName componentName,
        public void onCreateInlineSuggestionsRequest(int userId, ComponentName componentName,
                AutofillId autofillId, IInlineSuggestionsRequestCallback cb) {
            mService.onCreateInlineSuggestionsRequest(componentName, autofillId, cb);
            mService.onCreateInlineSuggestionsRequest(userId, componentName, autofillId, cb);
        }
    }

Loading