1 /*
2  * Copyright (C) 2017 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 com.android.internal.telephony.uicc.euicc.async;
18 
19 import android.annotation.Nullable;
20 import android.os.Handler;
21 
22 /**
23  * Helper on {@link AsyncResultCallback}.
24  *
25  * @hide
26  */
27 public final class AsyncResultHelper {
28     /**
29      * Calls the {@code callback} to return the {@code result} object. The {@code callback} will be
30      * run in the {@code handler}. If the {@code handler} is null, the callback will be called
31      * immediately.
32      *
33      * @param <T> Result type.
34      */
returnResult( final T result, final AsyncResultCallback<T> callback, @Nullable Handler handler)35     public static <T> void returnResult(
36             final T result, final AsyncResultCallback<T> callback, @Nullable Handler handler) {
37         if (handler == null) {
38             callback.onResult(result);
39         } else {
40             handler.post(
41                     new Runnable() {
42                         @Override
43                         public void run() {
44                             callback.onResult(result);
45                         }
46                     });
47         }
48     }
49 
50     /**
51      * Calls the {@code callback} to return the thrown {@code e} exception. The {@code callback}
52      * will be run in the {@code handler}. If the {@code handler} is null, the callback will be
53      * called immediately.
54      */
throwException( final Throwable e, final AsyncResultCallback<?> callback, @Nullable Handler handler)55     public static void throwException(
56             final Throwable e, final AsyncResultCallback<?> callback, @Nullable Handler handler) {
57         if (handler == null) {
58             callback.onException(e);
59         } else {
60             handler.post(
61                     new Runnable() {
62                         @Override
63                         public void run() {
64                             callback.onException(e);
65                         }
66                     });
67         }
68     }
69 
AsyncResultHelper()70     private AsyncResultHelper() {}
71 }
72