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

Commit fbf8b139 authored by sandeepbandaru's avatar sandeepbandaru
Browse files

Fixing CancellationSignal and ProcessingSignal

The signal remote instances need to be fetched from remote
implementation side to allow covering cases where signal might have been
called before /during and after the signal is handed over to the
implementation.

Context: go/isolated-aicore
Bug: 329418533
Test: CTS
Change-Id: Ic796a5f3233f421cd631592134fedcdd74480778
parent 1a1e8049
Loading
Loading
Loading
Loading
+8 −5
Original line number Diff line number Diff line
@@ -53,19 +53,22 @@
      void getFeatureDetails(in Feature feature, in IFeatureDetailsCallback featureDetailsCallback) = 4;

      @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.USE_ON_DEVICE_INTELLIGENCE)")
      void requestFeatureDownload(in Feature feature, in ICancellationSignal signal, in IDownloadCallback callback) = 5;
      void requestFeatureDownload(in Feature feature, in  AndroidFuture cancellationSignalFuture, in IDownloadCallback callback) = 5;

      @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.USE_ON_DEVICE_INTELLIGENCE)")
      void requestTokenInfo(in Feature feature, in Bundle requestBundle, in  ICancellationSignal signal,
      void requestTokenInfo(in Feature feature, in Bundle requestBundle, in  AndroidFuture cancellationSignalFuture,
                                                        in ITokenInfoCallback tokenInfocallback) = 6;

      @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.USE_ON_DEVICE_INTELLIGENCE)")
      void processRequest(in Feature feature, in Bundle requestBundle, int requestType, in  ICancellationSignal cancellationSignal,
                                                in IProcessingSignal signal, in IResponseCallback responseCallback) = 7;
      void processRequest(in Feature feature, in Bundle requestBundle, int requestType,
                                                in  AndroidFuture cancellationSignalFuture,
                                                in AndroidFuture processingSignalFuture,
                                                in IResponseCallback responseCallback) = 7;

      @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.USE_ON_DEVICE_INTELLIGENCE)")
      void processRequestStreaming(in Feature feature,
                    in Bundle requestBundle, int requestType, in  ICancellationSignal cancellationSignal, in  IProcessingSignal signal,
                    in Bundle requestBundle, int requestType, in  AndroidFuture cancellationSignalFuture,
                    in  AndroidFuture processingSignalFuture,
                    in IStreamingResponseCallback streamingCallback) = 8;

      String getRemoteServicePackageName() = 9;
+65 −46
Original line number Diff line number Diff line
@@ -26,22 +26,23 @@ import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.content.ComponentName;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Binder;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.IBinder;
import android.os.ICancellationSignal;
import android.os.OutcomeReceiver;
import android.os.PersistableBundle;
import android.os.RemoteCallback;
import android.os.RemoteException;
import android.system.OsConstants;
import android.util.Log;

import androidx.annotation.IntDef;

import com.android.internal.R;
import com.android.internal.infra.AndroidFuture;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -76,6 +77,8 @@ public final class OnDeviceIntelligenceManager {
     */
    public static final String AUGMENT_REQUEST_CONTENT_BUNDLE_KEY =
            "AugmentRequestContentBundleKey";

    private static final String TAG = "OnDeviceIntelligence";
    private final Context mContext;
    private final IOnDeviceIntelligenceManager mService;

@@ -288,18 +291,15 @@ public final class OnDeviceIntelligenceManager {
                }
            };

            ICancellationSignal transport = null;
            if (cancellationSignal != null) {
                transport = CancellationSignal.createTransport();
                cancellationSignal.setRemote(transport);
            }

            mService.requestFeatureDownload(feature, transport, downloadCallback);
            mService.requestFeatureDownload(feature,
                    configureRemoteCancellationFuture(cancellationSignal, callbackExecutor),
                    downloadCallback);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }


    /**
     * The methods computes the token related information for a given request payload using the
     * provided {@link Feature}.
@@ -337,13 +337,9 @@ public final class OnDeviceIntelligenceManager {
                }
            };

            ICancellationSignal transport = null;
            if (cancellationSignal != null) {
                transport = CancellationSignal.createTransport();
                cancellationSignal.setRemote(transport);
            }

            mService.requestTokenInfo(feature, request, transport, callback);
            mService.requestTokenInfo(feature, request,
                    configureRemoteCancellationFuture(cancellationSignal, callbackExecutor),
                    callback);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
@@ -407,19 +403,9 @@ public final class OnDeviceIntelligenceManager {
            };


            IProcessingSignal transport = null;
            if (processingSignal != null) {
                transport = ProcessingSignal.createTransport();
                processingSignal.setRemote(transport);
            }

            ICancellationSignal cancellationTransport = null;
            if (cancellationSignal != null) {
                cancellationTransport = CancellationSignal.createTransport();
                cancellationSignal.setRemote(cancellationTransport);
            }

            mService.processRequest(feature, request, requestType, cancellationTransport, transport,
            mService.processRequest(feature, request, requestType,
                    configureRemoteCancellationFuture(cancellationSignal, callbackExecutor),
                    configureRemoteProcessingSignalFuture(processingSignal, callbackExecutor),
                    callback);

        } catch (RemoteException e) {
@@ -449,7 +435,8 @@ public final class OnDeviceIntelligenceManager {
     * @param callbackExecutor          executor to run the callback on.
     */
    @RequiresPermission(Manifest.permission.USE_ON_DEVICE_INTELLIGENCE)
    public void processRequestStreaming(@NonNull Feature feature, @NonNull @InferenceParams Bundle request,
    public void processRequestStreaming(@NonNull Feature feature,
            @NonNull @InferenceParams Bundle request,
            @RequestType int requestType,
            @Nullable CancellationSignal cancellationSignal,
            @Nullable ProcessingSignal processingSignal,
@@ -500,20 +487,11 @@ public final class OnDeviceIntelligenceManager {
                }
            };

            IProcessingSignal transport = null;
            if (processingSignal != null) {
                transport = ProcessingSignal.createTransport();
                processingSignal.setRemote(transport);
            }

            ICancellationSignal cancellationTransport = null;
            if (cancellationSignal != null) {
                cancellationTransport = CancellationSignal.createTransport();
                cancellationSignal.setRemote(cancellationTransport);
            }

            mService.processRequestStreaming(
                    feature, request, requestType, cancellationTransport, transport, callback);
                    feature, request, requestType,
                    configureRemoteCancellationFuture(cancellationSignal, callbackExecutor),
                    configureRemoteProcessingSignalFuture(processingSignal, callbackExecutor),
                    callback);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
@@ -574,4 +552,45 @@ public final class OnDeviceIntelligenceManager {
    @Target({ElementType.PARAMETER, ElementType.FIELD})
    public @interface InferenceParams {
    }


    @Nullable
    private static AndroidFuture<IBinder> configureRemoteCancellationFuture(
            @Nullable CancellationSignal cancellationSignal,
            @NonNull Executor callbackExecutor) {
        if (cancellationSignal == null) {
            return null;
        }
        AndroidFuture<IBinder> cancellationFuture = new AndroidFuture<>();
        cancellationFuture.whenCompleteAsync(
                (cancellationTransport, error) -> {
                    if (error != null || cancellationTransport == null) {
                        Log.e(TAG, "Unable to receive the remote cancellation signal.", error);
                    } else {
                        cancellationSignal.setRemote(
                                ICancellationSignal.Stub.asInterface(cancellationTransport));
                    }
                }, callbackExecutor);
        return cancellationFuture;
    }

    @Nullable
    private static AndroidFuture<IBinder> configureRemoteProcessingSignalFuture(
            ProcessingSignal processingSignal, Executor executor) {
        if (processingSignal == null) {
            return null;
        }
        AndroidFuture<IBinder> processingSignalFuture = new AndroidFuture<>();
        processingSignalFuture.whenCompleteAsync(
                (transport, error) -> {
                    if (error != null || transport == null) {
                        Log.e(TAG, "Unable to receive the remote processing signal.", error);
                    } else {
                        processingSignal.setRemote(IProcessingSignal.Stub.asInterface(transport));
                    }
                }, executor);
        return processingSignalFuture;
    }


}
+5 −5
Original line number Diff line number Diff line
@@ -123,10 +123,10 @@ public final class ProcessingSignal {
     * Sets the processing signal callback to be called when signals are received.
     *
     * This method is intended to be used by the recipient of a processing signal
     * such as the remote implementation for {@link OnDeviceIntelligenceManager} to handle
     * cancellation requests while performing a long-running operation.  This method is not
     * intended
     * to be used by applications themselves.
     * such as the remote implementation in
     * {@link android.service.ondeviceintelligence.OnDeviceSandboxedInferenceService} to handle
     * processing signals while performing a long-running operation.  This method is not
     * intended to be used by the caller themselves.
     *
     * If {@link ProcessingSignal#sendSignal} has already been called, then the provided callback
     * is invoked immediately and all previously queued actions are passed to remote signal.
@@ -200,7 +200,7 @@ public final class ProcessingSignal {
    }

    /**
     * Given a locally created transport, returns its associated cancellation signal.
     * Given a locally created transport, returns its associated processing signal.
     *
     * @param transport The locally created transport, or null if none.
     * @return The associated processing signal, or null if none.
+3 −1
Original line number Diff line number Diff line
@@ -41,7 +41,9 @@ oneway interface IOnDeviceIntelligenceService {
    void getFeatureDetails(int callerUid, in Feature feature, in IFeatureDetailsCallback featureDetailsCallback);
    void getReadOnlyFileDescriptor(in String fileName, in AndroidFuture<ParcelFileDescriptor> future);
    void getReadOnlyFeatureFileDescriptorMap(in Feature feature, in RemoteCallback remoteCallback);
    void requestFeatureDownload(int callerUid, in Feature feature, in ICancellationSignal cancellationSignal, in IDownloadCallback downloadCallback);
    void requestFeatureDownload(int callerUid, in Feature feature,
                                in AndroidFuture<ICancellationSignal> cancellationSignal,
                                in IDownloadCallback downloadCallback);
    void registerRemoteServices(in IRemoteProcessingService remoteProcessingService);
    void notifyInferenceServiceConnected();
    void notifyInferenceServiceDisconnected();
+7 −3
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.app.ondeviceintelligence.Feature;
import android.os.ICancellationSignal;
import android.os.PersistableBundle;
import android.os.Bundle;
import com.android.internal.infra.AndroidFuture;
import android.service.ondeviceintelligence.IRemoteStorageService;
import android.service.ondeviceintelligence.IProcessingUpdateStatusCallback;

@@ -34,13 +35,16 @@ import android.service.ondeviceintelligence.IProcessingUpdateStatusCallback;
 */
oneway interface IOnDeviceSandboxedInferenceService {
    void registerRemoteStorageService(in IRemoteStorageService storageService);
    void requestTokenInfo(int callerUid, in Feature feature, in Bundle request, in ICancellationSignal cancellationSignal,
    void requestTokenInfo(int callerUid, in Feature feature, in Bundle request,
                            in AndroidFuture<ICancellationSignal> cancellationSignal,
                            in ITokenInfoCallback tokenInfoCallback);
    void processRequest(int callerUid, in Feature feature, in Bundle request, in int requestType,
                        in ICancellationSignal cancellationSignal, in IProcessingSignal processingSignal,
                        in AndroidFuture<ICancellationSignal> cancellationSignal,
                        in AndroidFuture<IProcessingSignal> processingSignal,
                        in IResponseCallback callback);
    void processRequestStreaming(int callerUid, in Feature feature, in Bundle request, in int requestType,
                                in ICancellationSignal cancellationSignal, in IProcessingSignal processingSignal,
                                in AndroidFuture<ICancellationSignal> cancellationSignal,
                                in AndroidFuture<IProcessingSignal> processingSignal,
                                in IStreamingResponseCallback callback);
    void updateProcessingState(in Bundle processingState,
                                     in IProcessingUpdateStatusCallback callback);
Loading