diff options
author | 2024-10-03 19:36:59 +0000 | |
---|---|---|
committer | 2024-10-03 19:36:59 +0000 | |
commit | 3d817f3730cf6a4415d85968ec22f2609735502f (patch) | |
tree | 424f16f2ae80e0b8998bc402bca67fc38ccd1e85 /services/appfunctions/java | |
parent | 2addde756aba5c20dceb554d35e013dcac1378f3 (diff) | |
parent | a79c76bf5749e7566e41c0274145d3772d38148c (diff) |
Merge "Extract AppFunctionServiceCallback from AppFunctionManagerServiceImpl" into main
Diffstat (limited to 'services/appfunctions/java')
2 files changed, 107 insertions, 49 deletions
diff --git a/services/appfunctions/java/com/android/server/appfunctions/AppFunctionManagerServiceImpl.java b/services/appfunctions/java/com/android/server/appfunctions/AppFunctionManagerServiceImpl.java index c522b640bfa5..d0c3dafd6e81 100644 --- a/services/appfunctions/java/com/android/server/appfunctions/AppFunctionManagerServiceImpl.java +++ b/services/appfunctions/java/com/android/server/appfunctions/AppFunctionManagerServiceImpl.java @@ -438,55 +438,10 @@ public class AppFunctionManagerServiceImpl extends IAppFunctionManager.Stub { targetUser, mServiceConfig.getExecuteAppFunctionCancellationTimeoutMillis(), cancellationSignal, - new RunServiceCallCallback<IAppFunctionService>() { - @Override - public void onServiceConnected( - @NonNull IAppFunctionService service, - @NonNull - ServiceUsageCompleteListener - serviceUsageCompleteListener) { - try { - service.executeAppFunction( - requestInternal.getClientRequest(), - cancellationCallback, - new IExecuteAppFunctionCallback.Stub() { - @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(); - } - }, + RunAppFunctionServiceCallback.create( + requestInternal, + cancellationCallback, + safeExecuteAppFunctionCallback), callerBinder); if (!bindServiceResult) { diff --git a/services/appfunctions/java/com/android/server/appfunctions/RunAppFunctionServiceCallback.java b/services/appfunctions/java/com/android/server/appfunctions/RunAppFunctionServiceCallback.java new file mode 100644 index 000000000000..7820390dd544 --- /dev/null +++ b/services/appfunctions/java/com/android/server/appfunctions/RunAppFunctionServiceCallback.java @@ -0,0 +1,103 @@ +/* + * 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(); + } +} |