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

Commit ff092aae authored by Adam He's avatar Adam He
Browse files

Implement TranslationCallback with support for partial responses.

Translator#translate() is also updated to be asynchronous with callback
instead.

Bug: 178651352
Test: atest CtsTranslationTestCases
CTS-Coverage-Bug: 178651352
Change-Id: Ibe1f97994fe6ee593a3a3bbb452c075b49a7eb5a
parent 93501a33
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -52357,6 +52357,7 @@ package android.view.translation {
    method @NonNull public android.util.SparseArray<android.view.translation.TranslationResponseValue> getTranslationResponseValues();
    method public int getTranslationStatus();
    method @NonNull public android.util.SparseArray<android.view.translation.ViewTranslationResponse> getViewTranslationResponses();
    method public boolean isFinalResponse();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.view.translation.TranslationResponse> CREATOR;
    field public static final int TRANSLATION_STATUS_CONTEXT_UNSUPPORTED = 2; // 0x2
@@ -52367,6 +52368,7 @@ package android.view.translation {
  public static final class TranslationResponse.Builder {
    ctor public TranslationResponse.Builder(int);
    method @NonNull public android.view.translation.TranslationResponse build();
    method @NonNull public android.view.translation.TranslationResponse.Builder setFinalResponse(boolean);
    method @NonNull public android.view.translation.TranslationResponse.Builder setTranslationResponseValue(int, @NonNull android.view.translation.TranslationResponseValue);
    method @NonNull public android.view.translation.TranslationResponse.Builder setTranslationResponseValues(@NonNull android.util.SparseArray<android.view.translation.TranslationResponseValue>);
    method @NonNull public android.view.translation.TranslationResponse.Builder setTranslationStatus(int);
@@ -52408,7 +52410,7 @@ package android.view.translation {
  public class Translator {
    method public void destroy();
    method public boolean isDestroyed();
    method @Nullable @WorkerThread public android.view.translation.TranslationResponse translate(@NonNull android.view.translation.TranslationRequest);
    method @Nullable public void translate(@NonNull android.view.translation.TranslationRequest, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<android.view.translation.TranslationResponse>);
  }
  public final class UiTranslationManager {
+1 −1
Original line number Diff line number Diff line
@@ -24,6 +24,6 @@ import android.view.translation.TranslationResponse;
 * @hide
 */
oneway interface ITranslationCallback {
    void onTranslationComplete(in TranslationResponse translationResponse);
    void onTranslationResponse(in TranslationResponse translationResponse);
    void onError();
}
+3 −3
Original line number Diff line number Diff line
@@ -51,12 +51,12 @@ final class OnTranslationResultCallbackWrapper implements
    @Override
    public void onTranslationSuccess(@Nullable TranslationResponse response) {
        assertNotCalled();
        if (mCalled.getAndSet(true)) {
            throw new IllegalStateException("Already called");
        if (mCalled.getAndSet(response.isFinalResponse())) {
            throw new IllegalStateException("Already called with complete response");
        }

        try {
            mCallback.onTranslationComplete(response);
            mCallback.onTranslationResponse(response);
        } catch (RemoteException e) {
            if (e instanceof DeadObjectException) {
                Log.w(TAG, "Process is dead, ignore.");
+2 −23
Original line number Diff line number Diff line
@@ -42,7 +42,6 @@ import android.view.translation.TranslationResponse;
import android.view.translation.TranslationSpec;

import com.android.internal.os.IResultReceiver;
import com.android.internal.util.SyncResultReceiver;

/**
 * Service for translating text.
@@ -139,30 +138,10 @@ public abstract class TranslationService extends Service {

                @Override
                public void onTranslationRequest(TranslationRequest request, int sessionId,
                        ITranslationCallback callback, IResultReceiver receiver)
                        ITranslationCallback callback)
                        throws RemoteException {
                    // TODO(b/176464808): Currently, the API is used for both sync and async case.
                    // It may work now, but maybe two methods is more cleaner. To think how to
                    // define the APIs for these two cases.
                    final ITranslationCallback cb = callback != null
                            ? callback
                            : new ITranslationCallback.Stub() {
                                @Override
                                public void onTranslationComplete(
                                        TranslationResponse translationResponse)
                                        throws RemoteException {
                                    receiver.send(0,
                                            SyncResultReceiver.bundleFor(translationResponse));
                                }

                                @Override
                                public void onError() throws RemoteException {
                                    //TODO: implement default error callback
                                }
                            };
                    // TODO(b/176464808): make it a private member of client
                    final OnTranslationResultCallback translationResultCallback =
                            new OnTranslationResultCallbackWrapper(cb);
                            new OnTranslationResultCallbackWrapper(callback);
                    mHandler.sendMessage(obtainMessage(TranslationService::onTranslationRequest,
                            TranslationService.this, request, sessionId, mCancellationSignal,
                            translationResultCallback));
+1 −2
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package android.view.translation;

import android.view.translation.TranslationRequest;
import android.service.translation.ITranslationCallback;
import com.android.internal.os.IResultReceiver;

/**
  * Interface between an app (TranslationManager / Translator) and the remote TranslationService
@@ -28,6 +27,6 @@ import com.android.internal.os.IResultReceiver;
  */
oneway interface ITranslationDirectManager {
    void onTranslationRequest(in TranslationRequest request, int sessionId,
         in ITranslationCallback callback, in IResultReceiver receiver);
         in ITranslationCallback callback);
    void onFinishTranslationSession(int sessionId);
}
Loading