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.provider.mediacognitionutils;
18 
19 import android.os.RemoteException;
20 import android.provider.MediaCognitionGetVersionsCallback;
21 import android.provider.MediaCognitionProcessingVersions;
22 import android.provider.MediaCognitionService;
23 import android.util.Log;
24 
25 import androidx.annotation.NonNull;
26 
27 
28 /**
29  * @hide
30  */
31 public final class GetProcessingVersionsCallbackImpl implements MediaCognitionGetVersionsCallback {
32 
33     private final ICognitionGetVersionsCallbackInternal mBinderCallback;
34     /**
35      * To ensure a callback instance is called only once.
36      */
37     private boolean mCalled;
38 
GetProcessingVersionsCallbackImpl(ICognitionGetVersionsCallbackInternal binderCallback)39     public GetProcessingVersionsCallbackImpl(ICognitionGetVersionsCallbackInternal binderCallback) {
40         mBinderCallback = binderCallback;
41         mCalled = false;
42     }
43 
44     @Override
onSuccess(@onNull MediaCognitionProcessingVersions processingVersions)45     public void onSuccess(@NonNull MediaCognitionProcessingVersions processingVersions) {
46         if (mCalled) {
47             Log.w(MediaCognitionService.TAG, "The callback can only be called once");
48             return;
49         }
50         mCalled = true;
51         try {
52             mBinderCallback.onGetProcessingVersionsSuccess(processingVersions);
53         } catch (RemoteException e) {
54             Log.w(MediaCognitionService.TAG,
55                     "Unable to send callback of Get Versions result", e);
56         }
57     }
58 
59     @Override
onFailure(@onNull String message)60     public void onFailure(@NonNull String message) {
61         if (mCalled) {
62             Log.w(MediaCognitionService.TAG, "The callback can only be called once");
63             return;
64         }
65         mCalled = true;
66         try {
67             mBinderCallback.onGetProcessingVersionsFailure(message);
68         } catch (RemoteException e) {
69             Log.w(MediaCognitionService.TAG,
70                     "Unable to send callback of Get Versions Failure", e);
71         }
72     }
73 }
74