• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.nearby.fastpair.provider.simulator.app;
18 
19 import android.util.Log;
20 
21 import com.google.common.util.concurrent.FutureCallback;
22 
23 /** Wrapper for {@link FutureCallback} to prevent the memory linkage. */
24 public abstract class FutureCallbackWrapper<T> implements FutureCallback<T> {
25     private static final String TAG = FutureCallback.class.getSimpleName();
26 
createRegisterCallback(MainActivity activity)27     public static FutureCallbackWrapper<Void> createRegisterCallback(MainActivity activity) {
28         String id = activity.mRemoteDeviceId;
29         return new FutureCallbackWrapper<Void>() {
30             @Override
31             public void onSuccess(Void result) {
32                 Log.d(TAG, String.format("%s was registered", id));
33             }
34 
35             @Override
36             public void onFailure(Throwable t) {
37                 Log.w(TAG, String.format("Failed to register %s", id), t);
38             }
39         };
40     }
41 
42     public static FutureCallbackWrapper<Void> createDefaultIOCallback(MainActivity activity) {
43         String id = activity.mRemoteDeviceId;
44         return new FutureCallbackWrapper<Void>() {
45             @Override
46             public void onSuccess(Void result) {
47             }
48 
49             @Override
50             public void onFailure(Throwable t) {
51                 Log.w(TAG, String.format("IO stream error on %s", id), t);
52             }
53         };
54     }
55 
56     public static FutureCallbackWrapper<Void> createDestroyCallback() {
57         return new FutureCallbackWrapper<Void>() {
58             @Override
59             public void onSuccess(Void result) {
60                 Log.d(TAG, "remote devices manager is destroyed");
61             }
62 
63             @Override
64             public void onFailure(Throwable t) {
65                 Log.w(TAG, "Failed to destroy remote devices manager", t);
66             }
67         };
68     }
69 }
70