Merge "Remove onTranslationResultCallback and use a Consumer instead." into sc-dev

This commit is contained in:
TreeHugger Robot
2021-05-13 18:51:17 +00:00
committed by Android (Google) Code Review
3 changed files with 54 additions and 23 deletions

View File

@@ -10405,19 +10405,20 @@ package android.service.translation {
method @Nullable public final android.os.IBinder onBind(@NonNull android.content.Intent);
method public void onConnected();
method public void onCreateTranslationSession(@NonNull android.view.translation.TranslationContext, int, @NonNull java.util.function.Consumer<java.lang.Boolean>);
method @Deprecated public abstract void onCreateTranslationSession(@NonNull android.view.translation.TranslationContext, int);
method @Deprecated public void onCreateTranslationSession(@NonNull android.view.translation.TranslationContext, int);
method public void onDisconnected();
method public abstract void onFinishTranslationSession(int);
method public abstract void onTranslationCapabilitiesRequest(int, int, @NonNull java.util.function.Consumer<java.util.Set<android.view.translation.TranslationCapability>>);
method public abstract void onTranslationRequest(@NonNull android.view.translation.TranslationRequest, int, @Nullable android.os.CancellationSignal, @NonNull android.service.translation.TranslationService.OnTranslationResultCallback);
method @Deprecated public void onTranslationRequest(@NonNull android.view.translation.TranslationRequest, int, @Nullable android.os.CancellationSignal, @NonNull android.service.translation.TranslationService.OnTranslationResultCallback);
method public void onTranslationRequest(@NonNull android.view.translation.TranslationRequest, int, @Nullable android.os.CancellationSignal, @NonNull java.util.function.Consumer<android.view.translation.TranslationResponse>);
method public final void updateTranslationCapability(@NonNull android.view.translation.TranslationCapability);
field public static final String SERVICE_INTERFACE = "android.service.translation.TranslationService";
field public static final String SERVICE_META_DATA = "android.translation_service";
}
public static interface TranslationService.OnTranslationResultCallback {
@Deprecated public static interface TranslationService.OnTranslationResultCallback {
method @Deprecated public void onError();
method public void onTranslationSuccess(@NonNull android.view.translation.TranslationResponse);
method @Deprecated public void onTranslationSuccess(@NonNull android.view.translation.TranslationResponse);
}
}

View File

@@ -17,7 +17,6 @@
package android.service.translation;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.DeadObjectException;
import android.os.RemoteException;
import android.util.Log;
@@ -25,6 +24,7 @@ import android.view.translation.TranslationResponse;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
/**
* Callback to receive the {@link TranslationResponse} on successful translation.
@@ -32,13 +32,13 @@ import java.util.concurrent.atomic.AtomicBoolean;
* @hide
*/
final class OnTranslationResultCallbackWrapper implements
TranslationService.OnTranslationResultCallback {
Consumer<TranslationResponse> {
private static final String TAG = "OnTranslationResultCallback";
private final @NonNull ITranslationCallback mCallback;
private AtomicBoolean mCalled;
private final AtomicBoolean mCalled;
/**
* @hide
@@ -49,7 +49,7 @@ final class OnTranslationResultCallbackWrapper implements
}
@Override
public void onTranslationSuccess(@Nullable TranslationResponse response) {
public void accept(TranslationResponse response) {
assertNotCalled();
if (mCalled.getAndSet(response.isFinalResponse())) {
throw new IllegalStateException("Already called with complete response");
@@ -66,15 +66,6 @@ final class OnTranslationResultCallbackWrapper implements
}
}
/**
* @deprecated use {@link #onTranslationSuccess} with error response instead.
*/
@Override
@Deprecated
public void onError() {
// no-op.
}
private void assertNotCalled() {
if (mCalled.get()) {
throw new IllegalStateException("Already called");

View File

@@ -125,7 +125,9 @@ public abstract class TranslationService extends Service {
/**
* Interface definition for a callback to be invoked when the translation is compleled.
* @deprecated use a {@link Consumer} instead.
*/
@Deprecated
public interface OnTranslationResultCallback {
/**
* Notifies the Android System that a translation request
@@ -162,12 +164,12 @@ public abstract class TranslationService extends Service {
public void onTranslationRequest(TranslationRequest request, int sessionId,
ICancellationSignal transport, ITranslationCallback callback)
throws RemoteException {
final OnTranslationResultCallback translationResultCallback =
final Consumer<TranslationResponse> consumer =
new OnTranslationResultCallbackWrapper(callback);
mHandler.sendMessage(obtainMessage(TranslationService::onTranslationRequest,
TranslationService.this, request, sessionId,
CancellationSignal.fromTransport(transport),
translationResultCallback));
consumer));
}
@Override
@@ -242,8 +244,10 @@ public abstract class TranslationService extends Service {
* instead.
*/
@Deprecated
public abstract void onCreateTranslationSession(@NonNull TranslationContext translationContext,
int sessionId);
public void onCreateTranslationSession(@NonNull TranslationContext translationContext,
int sessionId) {
// no-op
}
/**
* TODO: fill in javadoc.
@@ -259,10 +263,45 @@ public abstract class TranslationService extends Service {
* @param sessionId
* @param callback
* @param cancellationSignal
* @deprecated use
* {@link #onTranslationRequest(TranslationRequest, int, CancellationSignal, Consumer)} instead.
*/
public abstract void onTranslationRequest(@NonNull TranslationRequest request, int sessionId,
@Deprecated
public void onTranslationRequest(@NonNull TranslationRequest request, int sessionId,
@Nullable CancellationSignal cancellationSignal,
@NonNull OnTranslationResultCallback callback);
@NonNull OnTranslationResultCallback callback) {
// no-op
}
/**
* Called to the service with a {@link TranslationRequest} to be translated.
*
* <p>The service must call {@code callback.accept()} with the {@link TranslationResponse}. If
* {@link TranslationRequest#FLAG_PARTIAL_RESPONSES} was set, the service may call
* {@code callback.accept()} multiple times with partial responses.</p>
*
* @param request
* @param sessionId
* @param callback
* @param cancellationSignal
*/
//TODO: make abstract once aiai transitions.
public void onTranslationRequest(@NonNull TranslationRequest request, int sessionId,
@Nullable CancellationSignal cancellationSignal,
@NonNull Consumer<TranslationResponse> callback) {
onTranslationRequest(request, sessionId, cancellationSignal,
new OnTranslationResultCallback() {
@Override
public void onTranslationSuccess(@NonNull TranslationResponse response) {
callback.accept(response);
}
@Override
public void onError() {
// null-op
}
});
}
/**
* TODO: fill in javadoc