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

Commit 59845d24 authored by Taran Singh's avatar Taran Singh
Browse files

Cancellable gesture should always have cancellation token

With the current CancellableHandwritingGesture,
cancellationToken is only attached at the time of writeToParcel. However
if editor is in same process as IME (i.e. IME contains the editor),
writeToParcel will never be called and token will never be set.

This change refactors code to not unbeam if we didn't beam cancellation Token.

Bug: 280268494
Test: atest InputConnectionEndToEndTest InsertModeGestureTest
Change-Id: Idb9292fa087d98395d778dd3cd4a528554909ffc
parent ae828a78
Loading
Loading
Loading
Loading
+10 −4
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@ package android.os;


import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.annotation.SuppressLint;
import android.system.SystemCleaner;
import android.system.SystemCleaner;
import android.util.Pair;
import android.util.Pair;
import android.view.inputmethod.CancellableHandwritingGesture;
import android.view.inputmethod.CancellableHandwritingGesture;
@@ -137,7 +138,7 @@ public class CancellationSignalBeamer {
         * MUST be forwarded to {@link Receiver#cancel} with proper ordering. See
         * MUST be forwarded to {@link Receiver#cancel} with proper ordering. See
         * {@link CancellationSignalBeamer} for details.
         * {@link CancellationSignalBeamer} for details.
         */
         */
        public abstract void onCancel(IBinder token);
        public abstract void onCancel(@NonNull IBinder token);


        /**
        /**
         * A {@link #beam}ed {@link CancellationSignal} was GC'd.
         * A {@link #beam}ed {@link CancellationSignal} was GC'd.
@@ -145,7 +146,7 @@ public class CancellationSignalBeamer {
         * MUST be forwarded to {@link Receiver#forget} with proper ordering. See
         * MUST be forwarded to {@link Receiver#forget} with proper ordering. See
         * {@link CancellationSignalBeamer} for details.
         * {@link CancellationSignalBeamer} for details.
         */
         */
        public abstract void onForget(IBinder token);
        public abstract void onForget(@NonNull IBinder token);


        private static final ThreadLocal<Pair<Sender, ArrayList<CloseableToken>>> sScope =
        private static final ThreadLocal<Pair<Sender, ArrayList<CloseableToken>>> sScope =
                new ThreadLocal<>();
                new ThreadLocal<>();
@@ -159,7 +160,8 @@ public class CancellationSignalBeamer {
         *  try-with-resources. {@code null} if {@code cs} was {@code null} or if
         *  try-with-resources. {@code null} if {@code cs} was {@code null} or if
         *  {@link HandwritingGesture} isn't {@link CancellableHandwritingGesture cancellable}.
         *  {@link HandwritingGesture} isn't {@link CancellableHandwritingGesture cancellable}.
         */
         */
        public MustClose beamScopeIfNeeded(HandwritingGesture gesture) {
        @NonNull
        public MustClose beamScopeIfNeeded(@NonNull HandwritingGesture gesture) {
            if (!(gesture instanceof CancellableHandwritingGesture)) {
            if (!(gesture instanceof CancellableHandwritingGesture)) {
                return null;
                return null;
            }
            }
@@ -189,7 +191,8 @@ public class CancellationSignalBeamer {
         * @param cs {@link CancellationSignal} for which token should be returned.
         * @param cs {@link CancellationSignal} for which token should be returned.
         * @return {@link IBinder} token.
         * @return {@link IBinder} token.
         */
         */
        public static IBinder beamFromScope(CancellationSignal cs) {
        @NonNull
        public static IBinder beamFromScope(@NonNull CancellationSignal cs) {
            var state = sScope.get();
            var state = sScope.get();
            if (state != null) {
            if (state != null) {
                var token = state.first.beam(cs);
                var token = state.first.beam(cs);
@@ -291,6 +294,7 @@ public class CancellationSignalBeamer {
         * @return a {@link CancellationSignal} linked to the given token.
         * @return a {@link CancellationSignal} linked to the given token.
         */
         */
        @Nullable
        @Nullable
        @SuppressLint("VisiblySynchronized")
        public CancellationSignal unbeam(@Nullable IBinder token) {
        public CancellationSignal unbeam(@Nullable IBinder token) {
            if (token == null) {
            if (token == null) {
                return null;
                return null;
@@ -327,6 +331,7 @@ public class CancellationSignalBeamer {
         *
         *
         * @param token the token to forget. No-op if {@code null}.
         * @param token the token to forget. No-op if {@code null}.
         */
         */
        @SuppressLint("VisiblySynchronized")
        public void forget(@Nullable IBinder token) {
        public void forget(@Nullable IBinder token) {
            synchronized (this) {
            synchronized (this) {
                if (mTokenMap.remove(token) != null) {
                if (mTokenMap.remove(token) != null) {
@@ -347,6 +352,7 @@ public class CancellationSignalBeamer {
         *
         *
         * @param token the token to forget. No-op if {@code null}.
         * @param token the token to forget. No-op if {@code null}.
         */
         */
        @SuppressLint("VisiblySynchronized")
        public void cancel(@Nullable IBinder token) {
        public void cancel(@Nullable IBinder token) {
            CancellationSignal cs;
            CancellationSignal cs;
            synchronized (this) {
            synchronized (this) {
+14 −3
Original line number Original line Diff line number Diff line
@@ -17,6 +17,7 @@
package android.view.inputmethod;
package android.view.inputmethod;


import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.TestApi;
import android.annotation.TestApi;
import android.os.CancellationSignal;
import android.os.CancellationSignal;
import android.os.CancellationSignalBeamer;
import android.os.CancellationSignalBeamer;
@@ -28,10 +29,13 @@ import android.os.IBinder;
 */
 */
@TestApi
@TestApi
public abstract class CancellableHandwritingGesture extends HandwritingGesture {
public abstract class CancellableHandwritingGesture extends HandwritingGesture {
    @NonNull
    CancellationSignal mCancellationSignal;
    CancellationSignal mCancellationSignal;


    @Nullable
    IBinder mCancellationSignalToken;
    IBinder mCancellationSignalToken;



    /**
    /**
     * Set {@link CancellationSignal} for testing only.
     * Set {@link CancellationSignal} for testing only.
     * @hide
     * @hide
@@ -41,13 +45,20 @@ public abstract class CancellableHandwritingGesture extends HandwritingGesture {
        mCancellationSignal = cancellationSignal;
        mCancellationSignal = cancellationSignal;
    }
    }


    @NonNull
    CancellationSignal getCancellationSignal() {
    CancellationSignal getCancellationSignal() {
        return mCancellationSignal;
        return mCancellationSignal;
    }
    }


    void unbeamCancellationSignal(CancellationSignalBeamer.Receiver receiver) {
    /**
     * Unbeam cancellation token.
     * @hide
     */
    public void unbeamCancellationSignal(@NonNull CancellationSignalBeamer.Receiver receiver) {
        if (mCancellationSignalToken != null) {
            mCancellationSignal = receiver.unbeam(mCancellationSignalToken);
            mCancellationSignal = receiver.unbeam(mCancellationSignalToken);
            mCancellationSignalToken = null;
            mCancellationSignalToken = null;
        }
        }
    }


}
}
+12 −0
Original line number Original line Diff line number Diff line
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertNotNull;


import android.graphics.PointF;
import android.graphics.PointF;
import android.os.CancellationSignal;
import android.os.CancellationSignal;
import android.os.CancellationSignalBeamer;
import android.platform.test.annotations.Presubmit;
import android.platform.test.annotations.Presubmit;


import androidx.test.filters.SmallTest;
import androidx.test.filters.SmallTest;
@@ -54,4 +55,15 @@ public class InsertModeGestureTest {
        assertEquals(FALLBACK_TEXT, gesture.getFallbackText());
        assertEquals(FALLBACK_TEXT, gesture.getFallbackText());
        assertEquals(CANCELLATION_SIGNAL, gesture.getCancellationSignal());
        assertEquals(CANCELLATION_SIGNAL, gesture.getCancellationSignal());
    }
    }

    @Test
    public void testCancellationSignal() {
        var cs = CANCELLATION_SIGNAL;
        var gesture = new InsertModeGesture.Builder().setInsertionPoint(INSERTION_POINT)
                .setCancellationSignal(CANCELLATION_SIGNAL)
                .setFallbackText(FALLBACK_TEXT).build();
        gesture.unbeamCancellationSignal(
                new CancellationSignalBeamer.Receiver(true /* cancelOnSenderDeath */));
        assertEquals(gesture.getCancellationSignal(), cs);
    }
}
}