1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.app.appfunctions; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.os.RemoteException; 22 import android.util.Log; 23 24 import java.util.Objects; 25 import java.util.concurrent.atomic.AtomicBoolean; 26 import java.util.concurrent.atomic.AtomicLong; 27 28 /** 29 * A wrapper of IExecuteAppFunctionCallback which swallows the {@link RemoteException}. This 30 * callback is intended for one-time use only. Subsequent calls to onResult() or onError() will be 31 * ignored. 32 * 33 * @hide 34 */ 35 public class SafeOneTimeExecuteAppFunctionCallback { 36 private static final String TAG = "SafeOneTimeExecuteApp"; 37 38 private final AtomicBoolean mOnResultCalled = new AtomicBoolean(false); 39 40 @NonNull private final IExecuteAppFunctionCallback mCallback; 41 42 @Nullable private final CompletionCallback mCompletionCallback; 43 44 private final AtomicLong mExecutionStartTimeAfterBindMillis = new AtomicLong(); 45 SafeOneTimeExecuteAppFunctionCallback(@onNull IExecuteAppFunctionCallback callback)46 public SafeOneTimeExecuteAppFunctionCallback(@NonNull IExecuteAppFunctionCallback callback) { 47 this(callback, /* completionCallback= */ null); 48 } 49 SafeOneTimeExecuteAppFunctionCallback( @onNull IExecuteAppFunctionCallback callback, @Nullable CompletionCallback completionCallback)50 public SafeOneTimeExecuteAppFunctionCallback( 51 @NonNull IExecuteAppFunctionCallback callback, 52 @Nullable CompletionCallback completionCallback) { 53 mCallback = Objects.requireNonNull(callback); 54 mCompletionCallback = completionCallback; 55 } 56 57 /** Invoke wrapped callback with the result. */ onResult(@onNull ExecuteAppFunctionResponse result)58 public void onResult(@NonNull ExecuteAppFunctionResponse result) { 59 if (!mOnResultCalled.compareAndSet(false, true)) { 60 Log.w(TAG, "Ignore subsequent calls to onResult/onError()"); 61 return; 62 } 63 try { 64 mCallback.onSuccess(result); 65 if (mCompletionCallback != null) { 66 mCompletionCallback.finalizeOnSuccess( 67 result, mExecutionStartTimeAfterBindMillis.get()); 68 } 69 } catch (RemoteException ex) { 70 // Failed to notify the other end. Ignore. 71 Log.w(TAG, "Failed to invoke the callback", ex); 72 } 73 } 74 75 /** Invoke wrapped callback with the error. */ onError(@onNull AppFunctionException error)76 public void onError(@NonNull AppFunctionException error) { 77 if (!mOnResultCalled.compareAndSet(false, true)) { 78 Log.w(TAG, "Ignore subsequent calls to onResult/onError()"); 79 return; 80 } 81 try { 82 mCallback.onError(error); 83 if (mCompletionCallback != null) { 84 mCompletionCallback.finalizeOnError( 85 error, mExecutionStartTimeAfterBindMillis.get()); 86 } 87 } catch (RemoteException ex) { 88 // Failed to notify the other end. Ignore. 89 Log.w(TAG, "Failed to invoke the callback", ex); 90 } 91 } 92 93 /** 94 * Disables this callback. Subsequent calls to {@link #onResult(ExecuteAppFunctionResponse)} or 95 * {@link #onError(AppFunctionException)} will be ignored. 96 */ disable()97 public void disable() { 98 mOnResultCalled.set(true); 99 } 100 101 /** 102 * Sets the execution start time of the request. Used to calculate the overhead latency of 103 * requests. 104 */ setExecutionStartTimeAfterBindMillis(long executionStartTimeAfterBindMillis)105 public void setExecutionStartTimeAfterBindMillis(long executionStartTimeAfterBindMillis) { 106 if (!mExecutionStartTimeAfterBindMillis.compareAndSet( 107 0, executionStartTimeAfterBindMillis)) { 108 Log.w(TAG, "Ignore subsequent calls to setExecutionStartTimeAfterBindMillis()"); 109 } 110 } 111 112 /** 113 * Provides a hook to execute additional actions after the {@link IExecuteAppFunctionCallback} 114 * has been invoked. 115 */ 116 public interface CompletionCallback { 117 /** Called after {@link IExecuteAppFunctionCallback#onSuccess}. */ finalizeOnSuccess( @onNull ExecuteAppFunctionResponse result, long executionStartTimeMillis)118 void finalizeOnSuccess( 119 @NonNull ExecuteAppFunctionResponse result, long executionStartTimeMillis); 120 121 /** Called after {@link IExecuteAppFunctionCallback#onError}. */ finalizeOnError(@onNull AppFunctionException error, long executionStartTimeMillis)122 void finalizeOnError(@NonNull AppFunctionException error, long executionStartTimeMillis); 123 } 124 } 125