Merge "Allow throwing error on creating translation session (context not supported by service)." into sc-dev

This commit is contained in:
Adam He
2021-04-20 10:00:42 +00:00
committed by Android (Google) Code Review
2 changed files with 50 additions and 16 deletions

View File

@@ -10397,7 +10397,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>>);

View File

@@ -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;
@@ -207,13 +209,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);
@@ -253,19 +275,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) {
try {
final Bundle extras = new Bundle();
extras.putBinder(EXTRA_SERVICE_BINDER, mClientInterface.asBinder());
extras.putInt(EXTRA_SESSION_ID, sessionId);
resultReceiver.send(0, extras);
} catch (RemoteException e) {
Log.w(TAG, "RemoteException sending client interface: " + e);
}
onCreateTranslationSession(translationContext, sessionId);
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(STATUS_SYNC_CALL_SUCCESS, extras);
} catch (RemoteException e) {
Log.w(TAG, "RemoteException sending client interface: " + e);
}
}
});
}
private void handleOnTranslationCapabilitiesRequest(
@@ -280,7 +313,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);
}
});
}