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

Commit 3d817f37 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Extract AppFunctionServiceCallback from AppFunctionManagerServiceImpl" into main

parents 2addde75 a79c76bf
Loading
Loading
Loading
Loading
+4 −49
Original line number Original line Diff line number Diff line
@@ -438,55 +438,10 @@ public class AppFunctionManagerServiceImpl extends IAppFunctionManager.Stub {
                        targetUser,
                        targetUser,
                        mServiceConfig.getExecuteAppFunctionCancellationTimeoutMillis(),
                        mServiceConfig.getExecuteAppFunctionCancellationTimeoutMillis(),
                        cancellationSignal,
                        cancellationSignal,
                        new RunServiceCallCallback<IAppFunctionService>() {
                        RunAppFunctionServiceCallback.create(
                            @Override
                                requestInternal,
                            public void onServiceConnected(
                                    @NonNull IAppFunctionService service,
                                    @NonNull
                                            ServiceUsageCompleteListener
                                                    serviceUsageCompleteListener) {
                                try {
                                    service.executeAppFunction(
                                            requestInternal.getClientRequest(),
                                cancellationCallback,
                                cancellationCallback,
                                            new IExecuteAppFunctionCallback.Stub() {
                                safeExecuteAppFunctionCallback),
                                                @Override
                                                public void onResult(
                                                        ExecuteAppFunctionResponse response) {
                                                    safeExecuteAppFunctionCallback.onResult(
                                                            response);
                                                    serviceUsageCompleteListener.onCompleted();
                                                }
                                            });
                                } catch (Exception e) {
                                    safeExecuteAppFunctionCallback.onResult(
                                            ExecuteAppFunctionResponse.newFailure(
                                                    ExecuteAppFunctionResponse
                                                            .RESULT_APP_UNKNOWN_ERROR,
                                                    e.getMessage(),
                                                    /* extras= */ null));
                                    serviceUsageCompleteListener.onCompleted();
                                }
                            }

                            @Override
                            public void onFailedToConnect() {
                                Slog.e(TAG, "Failed to connect to service");
                                safeExecuteAppFunctionCallback.onResult(
                                        ExecuteAppFunctionResponse.newFailure(
                                                ExecuteAppFunctionResponse.RESULT_APP_UNKNOWN_ERROR,
                                                "Failed to connect to AppFunctionService",
                                                /* extras= */ null));
                            }

                            @Override
                            public void onCancelled() {
                                // Do not forward the result back to the caller once it has been
                                // canceled. The caller does not need a notification and should
                                // proceed after initiating a cancellation.
                                safeExecuteAppFunctionCallback.disable();
                            }
                        },
                        callerBinder);
                        callerBinder);


        if (!bindServiceResult) {
        if (!bindServiceResult) {
+103 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.server.appfunctions;

import android.annotation.NonNull;
import android.app.appfunctions.ExecuteAppFunctionAidlRequest;
import android.app.appfunctions.ExecuteAppFunctionResponse;
import android.app.appfunctions.IAppFunctionService;
import android.app.appfunctions.ICancellationCallback;
import android.app.appfunctions.IExecuteAppFunctionCallback;
import android.app.appfunctions.SafeOneTimeExecuteAppFunctionCallback;
import android.util.Slog;

import com.android.server.appfunctions.RemoteServiceCaller.RunServiceCallCallback;
import com.android.server.appfunctions.RemoteServiceCaller.ServiceUsageCompleteListener;


/**
 * A callback to forward a request to the {@link IAppFunctionService} and report back the result.
 */
public class RunAppFunctionServiceCallback implements RunServiceCallCallback<IAppFunctionService> {

    private final ExecuteAppFunctionAidlRequest mRequestInternal;
    private final SafeOneTimeExecuteAppFunctionCallback mSafeExecuteAppFunctionCallback;
    private final ICancellationCallback mCancellationCallback;

    private RunAppFunctionServiceCallback(
            ExecuteAppFunctionAidlRequest requestInternal,
            ICancellationCallback cancellationCallback,
            SafeOneTimeExecuteAppFunctionCallback safeExecuteAppFunctionCallback) {
        this.mRequestInternal = requestInternal;
        this.mSafeExecuteAppFunctionCallback = safeExecuteAppFunctionCallback;
        this.mCancellationCallback = cancellationCallback;
    }

    /**
     * Creates a new instance of {@link RunAppFunctionServiceCallback}.
     *
     * @param requestInternal a request to send to the service.
     * @param cancellationCallback a callback to forward cancellation signal to the service.
     * @param safeExecuteAppFunctionCallback a callback to report back the result of the operation.
     */
    public static RunAppFunctionServiceCallback create(
            ExecuteAppFunctionAidlRequest requestInternal,
            ICancellationCallback cancellationCallback,
            SafeOneTimeExecuteAppFunctionCallback safeExecuteAppFunctionCallback) {
        return new RunAppFunctionServiceCallback(
                requestInternal, cancellationCallback, safeExecuteAppFunctionCallback);
    }

    @Override
    public void onServiceConnected(
            @NonNull IAppFunctionService service,
            @NonNull ServiceUsageCompleteListener serviceUsageCompleteListener) {
        try {
            service.executeAppFunction(
                    mRequestInternal.getClientRequest(),
                    mCancellationCallback,
                    new IExecuteAppFunctionCallback.Stub() {
                        @Override
                        public void onResult(ExecuteAppFunctionResponse response) {
                            mSafeExecuteAppFunctionCallback.onResult(response);
                            serviceUsageCompleteListener.onCompleted();
                        }
                    });
        } catch (Exception e) {
            mSafeExecuteAppFunctionCallback.onResult(
                    ExecuteAppFunctionResponse.newFailure(
                            ExecuteAppFunctionResponse.RESULT_APP_UNKNOWN_ERROR,
                            e.getMessage(),
                            /* extras= */ null));
            serviceUsageCompleteListener.onCompleted();
        }
    }

    @Override
    public void onFailedToConnect() {
        Slog.e("AppFunctionManagerServiceImpl", "Failed to connect to service");
        mSafeExecuteAppFunctionCallback.onResult(
                ExecuteAppFunctionResponse.newFailure(
                        ExecuteAppFunctionResponse.RESULT_APP_UNKNOWN_ERROR,
                        "Failed to connect to AppFunctionService",
                        /* extras= */ null));
    }

    @Override
    public void onCancelled() {
        mSafeExecuteAppFunctionCallback.disable();
    }
}