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.appsearch.functions;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.app.appsearch.AppSearchResult;
22 import android.app.appsearch.aidl.AppSearchResultParcel;
23 import android.app.appsearch.aidl.IAppSearchResultCallback;
24 import android.os.RemoteException;
25 import android.util.Log;
26 
27 import java.util.Objects;
28 import java.util.concurrent.atomic.AtomicBoolean;
29 import java.util.function.Consumer;
30 
31 /**
32  * A wrapper of IAppSearchResultCallback which swallows the {@link RemoteException}. This callback
33  * is intended for one-time use only. Subsequent calls to onResult() will be ignored.
34  *
35  * @hide
36  */
37 public class SafeOneTimeAppSearchResultCallback {
38     private static final String TAG = "AppSearchAppFunction";
39 
40     private final AtomicBoolean mOnResultCalled = new AtomicBoolean(false);
41 
42     @NonNull private final IAppSearchResultCallback mCallback;
43 
44     @Nullable private final Consumer<AppSearchResult<?>> mOnDispatchCallback;
45 
SafeOneTimeAppSearchResultCallback(@onNull IAppSearchResultCallback callback)46     public SafeOneTimeAppSearchResultCallback(@NonNull IAppSearchResultCallback callback) {
47         this(callback, /* onDispatchCallback= */ null);
48     }
49 
50     /**
51      * @param callback The callback to wrap.
52      * @param onDispatchCallback An optional callback invoked after the wrapped callback has been
53      *     dispatched with a result. This callback receives the result that has been dispatched.
54      */
SafeOneTimeAppSearchResultCallback( @onNull IAppSearchResultCallback callback, @Nullable Consumer<AppSearchResult<?>> onDispatchCallback)55     public SafeOneTimeAppSearchResultCallback(
56             @NonNull IAppSearchResultCallback callback,
57             @Nullable Consumer<AppSearchResult<?>> onDispatchCallback) {
58         mCallback = Objects.requireNonNull(callback);
59         mOnDispatchCallback = onDispatchCallback;
60     }
61 
onFailedResult(@onNull AppSearchResult<?> result)62     public void onFailedResult(@NonNull AppSearchResult<?> result) {
63         onResult(AppSearchResultParcel.fromFailedResult(result));
64     }
65 
onResult(@onNull AppSearchResultParcel<?> result)66     public void onResult(@NonNull AppSearchResultParcel<?> result) {
67         if (!mOnResultCalled.compareAndSet(false, true)) {
68             Log.w(TAG, "Ignore subsequent calls to onResult()");
69             return;
70         }
71         try {
72             mCallback.onResult(result);
73         } catch (RemoteException ex) {
74             // Failed to notify the other end. Ignore.
75             Log.w(TAG, "Failed to invoke the callback", ex);
76         }
77         if (mOnDispatchCallback != null) {
78             mOnDispatchCallback.accept(result.getResult());
79         }
80     }
81 }
82