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

Commit a54aa305 authored by Oluwarotimi Adesina's avatar Oluwarotimi Adesina
Browse files

Handle function not found Exception

Flag: android.app.appfunctions.flags.enable_app_function_manager
Test: atest CtsAppFunctionTests
Bug: 394571314
Change-Id: I9fef3f5cd20b49a687a1b35573c52da0fe43039c
parent 67e5e61c
Loading
Loading
Loading
Loading
+22 −1
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.annotation.NonNull;
import android.annotation.RequiresPermission;
import android.annotation.SystemService;
import android.annotation.UserHandleAware;
import android.app.appfunctions.AppFunctionManagerHelper.AppFunctionNotFoundException;
import android.app.appsearch.AppSearchManager;
import android.content.Context;
import android.os.CancellationSignal;
@@ -325,8 +326,28 @@ public final class AppFunctionManager {
            return;
        }

        // Wrap the callback to convert AppFunctionNotFoundException to IllegalArgumentException
        // to match the documentation.
        OutcomeReceiver<Boolean, Exception> callbackWithExceptionInterceptor =
                new OutcomeReceiver<>() {
                    @Override
                    public void onResult(@NonNull Boolean result) {
                        callback.onResult(result);
                    }

                    @Override
                    public void onError(@NonNull Exception exception) {
                        if (exception instanceof AppFunctionNotFoundException) {
                            exception = new IllegalArgumentException(exception);
                        }
                        callback.onError(exception);
                    }
                };

        AppFunctionManagerHelper.isAppFunctionEnabled(
                functionIdentifier, targetPackage, appSearchManager, executor, callback);
                functionIdentifier, targetPackage, appSearchManager, executor,
                callbackWithExceptionInterceptor);

    }

    private static class CallbackWrapper extends IAppFunctionEnabledCallback.Stub {
+14 −3
Original line number Diff line number Diff line
@@ -60,8 +60,8 @@ public class AppFunctionManagerHelper {
     * <p>If operation fails, the callback's {@link OutcomeReceiver#onError} is called with errors:
     *
     * <ul>
     *   <li>{@link IllegalArgumentException}, if the function is not found or the caller does not
     *       have access to it.
     *   <li>{@link AppFunctionNotFoundException}, if the function is not found or the caller does
     *       not have access to it.
     * </ul>
     *
     * @param functionIdentifier the identifier of the app function to check (unique within the
@@ -216,7 +216,7 @@ public class AppFunctionManagerHelper {
    private static @NonNull Exception failedResultToException(
            @NonNull AppSearchResult appSearchResult) {
        return switch (appSearchResult.getResultCode()) {
            case AppSearchResult.RESULT_INVALID_ARGUMENT -> new IllegalArgumentException(
            case AppSearchResult.RESULT_INVALID_ARGUMENT -> new AppFunctionNotFoundException(
                    appSearchResult.getErrorMessage());
            case AppSearchResult.RESULT_IO_ERROR -> new IOException(
                    appSearchResult.getErrorMessage());
@@ -225,4 +225,15 @@ public class AppFunctionManagerHelper {
            default -> new IllegalStateException(appSearchResult.getErrorMessage());
        };
    }

    /**
     * Throws when the app function is not found.
     *
     * @hide
     */
    public static class AppFunctionNotFoundException extends RuntimeException {
        private AppFunctionNotFoundException(@NonNull String errorMessage) {
            super(errorMessage);
        }
    }
}
+4 −1
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.annotation.WorkerThread;
import android.app.appfunctions.AppFunctionException;
import android.app.appfunctions.AppFunctionManager;
import android.app.appfunctions.AppFunctionManagerHelper;
import android.app.appfunctions.AppFunctionManagerHelper.AppFunctionNotFoundException;
import android.app.appfunctions.AppFunctionRuntimeMetadata;
import android.app.appfunctions.AppFunctionStaticMetadataHelper;
import android.app.appfunctions.ExecuteAppFunctionAidlRequest;
@@ -513,7 +514,9 @@ public class AppFunctionManagerServiceImpl extends IAppFunctionManager.Stub {
            e = e.getCause();
        }
        int resultCode = AppFunctionException.ERROR_SYSTEM_ERROR;
        if (e instanceof AppSearchException appSearchException) {
        if (e instanceof AppFunctionNotFoundException) {
            resultCode = AppFunctionException.ERROR_FUNCTION_NOT_FOUND;
        } else if (e instanceof AppSearchException appSearchException) {
            resultCode =
                    mapAppSearchResultFailureCodeToExecuteAppFunctionResponse(
                            appSearchException.getResultCode());