Loading core/java/android/os/CancellationSignalBeamer.java +10 −4 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ package android.os; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.SuppressLint; import android.system.SystemCleaner; import android.util.Pair; import android.view.inputmethod.CancellableHandwritingGesture; Loading Loading @@ -137,7 +138,7 @@ public class CancellationSignalBeamer { * MUST be forwarded to {@link Receiver#cancel} with proper ordering. See * {@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. Loading @@ -145,7 +146,7 @@ public class CancellationSignalBeamer { * MUST be forwarded to {@link Receiver#forget} with proper ordering. See * {@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 = new ThreadLocal<>(); Loading @@ -159,7 +160,8 @@ public class CancellationSignalBeamer { * try-with-resources. {@code null} if {@code cs} was {@code null} or if * {@link HandwritingGesture} isn't {@link CancellableHandwritingGesture cancellable}. */ public MustClose beamScopeIfNeeded(HandwritingGesture gesture) { @NonNull public MustClose beamScopeIfNeeded(@NonNull HandwritingGesture gesture) { if (!(gesture instanceof CancellableHandwritingGesture)) { return null; } Loading Loading @@ -189,7 +191,8 @@ public class CancellationSignalBeamer { * @param cs {@link CancellationSignal} for which token should be returned. * @return {@link IBinder} token. */ public static IBinder beamFromScope(CancellationSignal cs) { @NonNull public static IBinder beamFromScope(@NonNull CancellationSignal cs) { var state = sScope.get(); if (state != null) { var token = state.first.beam(cs); Loading Loading @@ -291,6 +294,7 @@ public class CancellationSignalBeamer { * @return a {@link CancellationSignal} linked to the given token. */ @Nullable @SuppressLint("VisiblySynchronized") public CancellationSignal unbeam(@Nullable IBinder token) { if (token == null) { return null; Loading Loading @@ -327,6 +331,7 @@ public class CancellationSignalBeamer { * * @param token the token to forget. No-op if {@code null}. */ @SuppressLint("VisiblySynchronized") public void forget(@Nullable IBinder token) { synchronized (this) { if (mTokenMap.remove(token) != null) { Loading @@ -347,6 +352,7 @@ public class CancellationSignalBeamer { * * @param token the token to forget. No-op if {@code null}. */ @SuppressLint("VisiblySynchronized") public void cancel(@Nullable IBinder token) { CancellationSignal cs; synchronized (this) { Loading core/java/android/view/inputmethod/CancellableHandwritingGesture.java +14 −3 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ package android.view.inputmethod; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.TestApi; import android.os.CancellationSignal; import android.os.CancellationSignalBeamer; Loading @@ -28,10 +29,13 @@ import android.os.IBinder; */ @TestApi public abstract class CancellableHandwritingGesture extends HandwritingGesture { @NonNull CancellationSignal mCancellationSignal; @Nullable IBinder mCancellationSignalToken; /** * Set {@link CancellationSignal} for testing only. * @hide Loading @@ -41,13 +45,20 @@ public abstract class CancellableHandwritingGesture extends HandwritingGesture { mCancellationSignal = cancellationSignal; } @NonNull CancellationSignal getCancellationSignal() { 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); mCancellationSignalToken = null; } } } core/tests/coretests/src/android/view/inputmethod/InsertModeGestureTest.java +12 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ import static org.junit.Assert.assertNotNull; import android.graphics.PointF; import android.os.CancellationSignal; import android.os.CancellationSignalBeamer; import android.platform.test.annotations.Presubmit; import androidx.test.filters.SmallTest; Loading Loading @@ -54,4 +55,15 @@ public class InsertModeGestureTest { assertEquals(FALLBACK_TEXT, gesture.getFallbackText()); 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); } } Loading
core/java/android/os/CancellationSignalBeamer.java +10 −4 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ package android.os; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.SuppressLint; import android.system.SystemCleaner; import android.util.Pair; import android.view.inputmethod.CancellableHandwritingGesture; Loading Loading @@ -137,7 +138,7 @@ public class CancellationSignalBeamer { * MUST be forwarded to {@link Receiver#cancel} with proper ordering. See * {@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. Loading @@ -145,7 +146,7 @@ public class CancellationSignalBeamer { * MUST be forwarded to {@link Receiver#forget} with proper ordering. See * {@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 = new ThreadLocal<>(); Loading @@ -159,7 +160,8 @@ public class CancellationSignalBeamer { * try-with-resources. {@code null} if {@code cs} was {@code null} or if * {@link HandwritingGesture} isn't {@link CancellableHandwritingGesture cancellable}. */ public MustClose beamScopeIfNeeded(HandwritingGesture gesture) { @NonNull public MustClose beamScopeIfNeeded(@NonNull HandwritingGesture gesture) { if (!(gesture instanceof CancellableHandwritingGesture)) { return null; } Loading Loading @@ -189,7 +191,8 @@ public class CancellationSignalBeamer { * @param cs {@link CancellationSignal} for which token should be returned. * @return {@link IBinder} token. */ public static IBinder beamFromScope(CancellationSignal cs) { @NonNull public static IBinder beamFromScope(@NonNull CancellationSignal cs) { var state = sScope.get(); if (state != null) { var token = state.first.beam(cs); Loading Loading @@ -291,6 +294,7 @@ public class CancellationSignalBeamer { * @return a {@link CancellationSignal} linked to the given token. */ @Nullable @SuppressLint("VisiblySynchronized") public CancellationSignal unbeam(@Nullable IBinder token) { if (token == null) { return null; Loading Loading @@ -327,6 +331,7 @@ public class CancellationSignalBeamer { * * @param token the token to forget. No-op if {@code null}. */ @SuppressLint("VisiblySynchronized") public void forget(@Nullable IBinder token) { synchronized (this) { if (mTokenMap.remove(token) != null) { Loading @@ -347,6 +352,7 @@ public class CancellationSignalBeamer { * * @param token the token to forget. No-op if {@code null}. */ @SuppressLint("VisiblySynchronized") public void cancel(@Nullable IBinder token) { CancellationSignal cs; synchronized (this) { Loading
core/java/android/view/inputmethod/CancellableHandwritingGesture.java +14 −3 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ package android.view.inputmethod; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.TestApi; import android.os.CancellationSignal; import android.os.CancellationSignalBeamer; Loading @@ -28,10 +29,13 @@ import android.os.IBinder; */ @TestApi public abstract class CancellableHandwritingGesture extends HandwritingGesture { @NonNull CancellationSignal mCancellationSignal; @Nullable IBinder mCancellationSignalToken; /** * Set {@link CancellationSignal} for testing only. * @hide Loading @@ -41,13 +45,20 @@ public abstract class CancellableHandwritingGesture extends HandwritingGesture { mCancellationSignal = cancellationSignal; } @NonNull CancellationSignal getCancellationSignal() { 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); mCancellationSignalToken = null; } } }
core/tests/coretests/src/android/view/inputmethod/InsertModeGestureTest.java +12 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ import static org.junit.Assert.assertNotNull; import android.graphics.PointF; import android.os.CancellationSignal; import android.os.CancellationSignalBeamer; import android.platform.test.annotations.Presubmit; import androidx.test.filters.SmallTest; Loading Loading @@ -54,4 +55,15 @@ public class InsertModeGestureTest { assertEquals(FALLBACK_TEXT, gesture.getFallbackText()); 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); } }