Debian Codegraph @

core/java/android/os/RemoteCallback.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2010 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.os;
18
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.compat.annotation.UnsupportedAppUsage;
23
24 /**
25 * @hide
26 */
27 @SystemApi
28 public final class RemoteCallback implements Parcelable {
29
30 public interface OnResultListener {
31 void onResult(@Nullable Bundle result);
32 }
33
34 private final OnResultListener mListener;
35 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
36 private final Handler mHandler;
37 private final IRemoteCallback mCallback;
38
39 public RemoteCallback(OnResultListener listener) {
40 this(listener, null);
41 }
42
43 public RemoteCallback(@NonNull OnResultListener listener, @Nullable Handler handler) {
44 if (listener == null) {
45 throw new NullPointerException("listener cannot be null");
46 }
47 mListener = listener;
48 mHandler = handler;
49 mCallback = new IRemoteCallback.Stub() {
50 @Override
51 public void sendResult(Bundle data) {
52 RemoteCallback.this.sendResult(data);
53 }
54 };
55 }
56
57 RemoteCallback(Parcel parcel) {
58 mListener = null;
59 mHandler = null;
60 mCallback = IRemoteCallback.Stub.asInterface(
61 parcel.readStrongBinder());
62 }
63
64 public void sendResult(@Nullable final Bundle result) {
65 // Do local dispatch
66 if (mListener != null) {
67 if (mHandler != null) {
68 mHandler.post(new Runnable() {
69 @Override
70 public void run() {
71 mListener.onResult(result);
72 }
73 });
74 } else {
75 mListener.onResult(result);
76 }
77 // Do remote dispatch
78 } else {
79 try {
80 mCallback.sendResult(result);
81 } catch (RemoteException e) {
82 /* ignore */
83 }
84 }
85 }
86
87 @Override
88 public int describeContents() {
89 return 0;
90 }
91
92 @Override
93 public void writeToParcel(Parcel parcel, int flags) {
94 parcel.writeStrongBinder(mCallback.asBinder());
95 }
96
97 public static final @android.annotation.NonNull Parcelable.Creator<RemoteCallback> CREATOR
98 = new Parcelable.Creator<RemoteCallback>() {
99 public RemoteCallback createFromParcel(Parcel parcel) {
100 return new RemoteCallback(parcel);
101 }
102
103 public RemoteCallback[] newArray(int size) {
104 return new RemoteCallback[size];
105 }
106 };
107 }