Simplify ResultCallbacks by not using WeakReference
This is a follow up CL to my previous CL [1], which introduced
ResultCallbacks in a bit conservative way, that is, to always wrap
Completable.* with WeakReference in case the target app fails to
respond for some reasons.
Using WeakReference would make sense if otherwise we would put more
stress on the memory than holding WeakReference object itself. In our
case the expected memory stress on extending lifetime of Completable.*
objects is still comparable with the memory cost of holding
WeakReference objects instead. Let's remove WeakReference from
ResultCallbacks in favor of simplicity.
This change should be completely transparent to other layers since
there is no change in the semantics (except for the lifetime itself of
Completable.* object, which is technically observable with APIs such
as java.lang.ref.Cleaner).
[1]: Ic65a95eb5d0fd56f505a02fd9083bcf6694b6734
f87f750888
Bug: 192412909
Test: atest -c CtsInputMethodTestCases
Change-Id: I43966494ccb4fceb8e7f8c60bd6ce3cc7fbfdd82
This commit is contained in:
@@ -21,7 +21,6 @@ import android.annotation.BinderThread;
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
@@ -38,15 +37,9 @@ public final class ResultCallbacks {
|
||||
|
||||
@AnyThread
|
||||
@Nullable
|
||||
private static <T> T unwrap(@NonNull AtomicReference<WeakReference<T>> atomicRef) {
|
||||
final WeakReference<T> ref = atomicRef.getAndSet(null);
|
||||
if (ref == null) {
|
||||
// Double-call is guaranteed to be ignored here.
|
||||
return null;
|
||||
}
|
||||
final T value = ref.get();
|
||||
ref.clear();
|
||||
return value;
|
||||
private static <T> T unwrap(@NonNull AtomicReference<T> atomicRef) {
|
||||
// Only the first caller will receive the non-null original object.
|
||||
return atomicRef.getAndSet(null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,8 +51,7 @@ public final class ResultCallbacks {
|
||||
*/
|
||||
@AnyThread
|
||||
public static IIntResultCallback.Stub of(@NonNull Completable.Int value) {
|
||||
final AtomicReference<WeakReference<Completable.Int>>
|
||||
atomicRef = new AtomicReference<>(new WeakReference<>(value));
|
||||
final AtomicReference<Completable.Int> atomicRef = new AtomicReference<>(value);
|
||||
|
||||
return new IIntResultCallback.Stub() {
|
||||
@BinderThread
|
||||
@@ -95,8 +87,7 @@ public final class ResultCallbacks {
|
||||
@AnyThread
|
||||
public static ICharSequenceResultCallback.Stub of(
|
||||
@NonNull Completable.CharSequence value) {
|
||||
final AtomicReference<WeakReference<Completable.CharSequence>> atomicRef =
|
||||
new AtomicReference<>(new WeakReference<>(value));
|
||||
final AtomicReference<Completable.CharSequence> atomicRef = new AtomicReference<>(value);
|
||||
|
||||
return new ICharSequenceResultCallback.Stub() {
|
||||
@BinderThread
|
||||
@@ -122,8 +113,7 @@ public final class ResultCallbacks {
|
||||
@AnyThread
|
||||
public static IExtractedTextResultCallback.Stub of(
|
||||
@NonNull Completable.ExtractedText value) {
|
||||
final AtomicReference<WeakReference<Completable.ExtractedText>>
|
||||
atomicRef = new AtomicReference<>(new WeakReference<>(value));
|
||||
final AtomicReference<Completable.ExtractedText> atomicRef = new AtomicReference<>(value);
|
||||
|
||||
return new IExtractedTextResultCallback.Stub() {
|
||||
@BinderThread
|
||||
@@ -149,8 +139,7 @@ public final class ResultCallbacks {
|
||||
@AnyThread
|
||||
public static ISurroundingTextResultCallback.Stub of(
|
||||
@NonNull Completable.SurroundingText value) {
|
||||
final AtomicReference<WeakReference<Completable.SurroundingText>>
|
||||
atomicRef = new AtomicReference<>(new WeakReference<>(value));
|
||||
final AtomicReference<Completable.SurroundingText> atomicRef = new AtomicReference<>(value);
|
||||
|
||||
return new ISurroundingTextResultCallback.Stub() {
|
||||
@BinderThread
|
||||
@@ -174,8 +163,7 @@ public final class ResultCallbacks {
|
||||
*/
|
||||
@AnyThread
|
||||
public static IBooleanResultCallback.Stub of(@NonNull Completable.Boolean value) {
|
||||
final AtomicReference<WeakReference<Completable.Boolean>>
|
||||
atomicRef = new AtomicReference<>(new WeakReference<>(value));
|
||||
final AtomicReference<Completable.Boolean> atomicRef = new AtomicReference<>(value);
|
||||
|
||||
return new IBooleanResultCallback.Stub() {
|
||||
@BinderThread
|
||||
@@ -209,8 +197,7 @@ public final class ResultCallbacks {
|
||||
*/
|
||||
@AnyThread
|
||||
public static IVoidResultCallback.Stub of(@NonNull Completable.Void value) {
|
||||
final AtomicReference<WeakReference<Completable.Void>> atomicRef =
|
||||
new AtomicReference<>(new WeakReference<>(value));
|
||||
final AtomicReference<Completable.Void> atomicRef = new AtomicReference<>(value);
|
||||
|
||||
return new IVoidResultCallback.Stub() {
|
||||
@BinderThread
|
||||
@@ -246,8 +233,8 @@ public final class ResultCallbacks {
|
||||
@AnyThread
|
||||
public static IIInputContentUriTokenResultCallback.Stub of(
|
||||
@NonNull Completable.IInputContentUriToken value) {
|
||||
final AtomicReference<WeakReference<Completable.IInputContentUriToken>>
|
||||
atomicRef = new AtomicReference<>(new WeakReference<>(value));
|
||||
final AtomicReference<Completable.IInputContentUriToken>
|
||||
atomicRef = new AtomicReference<>(value);
|
||||
|
||||
return new IIInputContentUriTokenResultCallback.Stub() {
|
||||
@BinderThread
|
||||
|
||||
Reference in New Issue
Block a user