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

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

Allow throwing error on creating translation session (context not

supported by service).

Bug: 176208267
Test: atest CtsTranslationTestCases
Change-Id: Ib1a6b6ead5428b67fcd6fc911a051a70236cc776
parent e8c798ba
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -10346,7 +10346,8 @@ package android.service.translation {
    ctor public TranslationService();
    method @Nullable public final android.os.IBinder onBind(@NonNull android.content.Intent);
    method public void onConnected();
    method public abstract void onCreateTranslationSession(@NonNull android.view.translation.TranslationContext, int);
    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 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>>);
+48 −15
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.service.translation;

import static android.view.translation.TranslationManager.STATUS_SYNC_CALL_FAIL;
import static android.view.translation.TranslationManager.STATUS_SYNC_CALL_SUCCESS;
import static android.view.translation.Translator.EXTRA_SERVICE_BINDER;
import static android.view.translation.Translator.EXTRA_SESSION_ID;

@@ -209,13 +211,33 @@ public abstract class TranslationService extends Service {
    }

    /**
     * TODO: fill in javadoc.
     * Called to notify the service that a session was created
     * (see {@link android.view.translation.Translator}).
     *
     * @param translationContext
     * @param sessionId
     * <p>The service must call {@code callback.accept()} to acknowledge whether the session is
     * supported and created successfully. If the translation context is not supported, the service
     * should call back with {@code false}.</p>
     *
     * @param translationContext the {@link TranslationContext} of the session being created.
     * @param sessionId the int id of the session.
     * @param callback {@link Consumer} to notify whether the session was successfully created.
     */
    // TODO(b/176464808): the session id won't be unique cross client/server process. Need to find
    // solution to make it's safe.
    // TODO: make abstract once aiai is implemented.
    public void onCreateTranslationSession(@NonNull TranslationContext translationContext,
            int sessionId, @NonNull Consumer<Boolean> callback) {
        onCreateTranslationSession(translationContext, sessionId);
        callback.accept(true);
    }

    /**
     * TODO: fill in javadoc.
     *
     * @deprecated use {@link #onCreateTranslationSession(TranslationContext, int, Consumer)}
     * instead.
     */
    @Deprecated
    public abstract void onCreateTranslationSession(@NonNull TranslationContext translationContext,
            int sessionId);

@@ -255,19 +277,30 @@ public abstract class TranslationService extends Service {

    // TODO(b/176464808): Need to handle client dying case

    // TODO(b/176464808): Need to handle the failure case. e.g. if the context is not supported.

    private void handleOnCreateTranslationSession(@NonNull TranslationContext translationContext,
            int sessionId, IResultReceiver resultReceiver) {
        onCreateTranslationSession(translationContext, sessionId,
                new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean created) {
                        try {
                            if (!created) {
                                Log.w(TAG, "handleOnCreateTranslationSession(): context="
                                        + translationContext + " not supported by service.");
                                resultReceiver.send(STATUS_SYNC_CALL_FAIL, null);
                                return;
                            }

                            final Bundle extras = new Bundle();
                            extras.putBinder(EXTRA_SERVICE_BINDER, mClientInterface.asBinder());
                            extras.putInt(EXTRA_SESSION_ID, sessionId);
            resultReceiver.send(0, extras);
                            resultReceiver.send(STATUS_SYNC_CALL_SUCCESS, extras);
                        } catch (RemoteException e) {
                            Log.w(TAG, "RemoteException sending client interface: " + e);
                        }
        onCreateTranslationSession(translationContext, sessionId);
                    }
                });

    }

    private void handleOnTranslationCapabilitiesRequest(
@@ -282,7 +315,7 @@ public abstract class TranslationService extends Service {
                        final Bundle bundle = new Bundle();
                        bundle.putParcelableArray(TranslationManager.EXTRA_CAPABILITIES,
                                capabilities.toArray(new TranslationCapability[0]));
                        resultReceiver.send(TranslationManager.STATUS_SYNC_CALL_SUCCESS, bundle);
                        resultReceiver.send(STATUS_SYNC_CALL_SUCCESS, bundle);
                    }
                });