blob: 7dbcb950718541337db22de03e3f1dcbf02fb823 [file] [log] [blame]
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001/*
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
17package android.os;
18
Svet Ganov9c165d72015-12-01 19:52:26 -080019import android.annotation.NonNull;
20import android.annotation.Nullable;
Svet Ganovae0e03a2016-02-25 18:22:10 -080021import android.annotation.SystemApi;
Svet Ganov9c165d72015-12-01 19:52:26 -080022
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080023/**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080024 * @hide
25 */
Svet Ganovae0e03a2016-02-25 18:22:10 -080026@SystemApi
Svet Ganov9c165d72015-12-01 19:52:26 -080027public final class RemoteCallback implements Parcelable {
28
29 public interface OnResultListener {
30 public void onResult(Bundle result);
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080031 }
Svet Ganov9c165d72015-12-01 19:52:26 -080032
33 private final OnResultListener mListener;
34 private final Handler mHandler;
35 private final IRemoteCallback mCallback;
36
37 public RemoteCallback(OnResultListener listener) {
38 this(listener, null);
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080039 }
Svet Ganov9c165d72015-12-01 19:52:26 -080040
41 public RemoteCallback(@NonNull OnResultListener listener, @Nullable Handler handler) {
42 if (listener == null) {
43 throw new NullPointerException("listener cannot be null");
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080044 }
Svet Ganov9c165d72015-12-01 19:52:26 -080045 mListener = listener;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080046 mHandler = handler;
Svet Ganov9c165d72015-12-01 19:52:26 -080047 mCallback = new IRemoteCallback.Stub() {
48 @Override
49 public void sendResult(Bundle data) {
50 RemoteCallback.this.sendResult(data);
51 }
52 };
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080053 }
Svet Ganov9c165d72015-12-01 19:52:26 -080054
55 RemoteCallback(Parcel parcel) {
56 mListener = null;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080057 mHandler = null;
Svet Ganov9c165d72015-12-01 19:52:26 -080058 mCallback = IRemoteCallback.Stub.asInterface(
59 parcel.readStrongBinder());
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080060 }
Svet Ganov9c165d72015-12-01 19:52:26 -080061
62 public void sendResult(@Nullable final Bundle result) {
63 // Do local dispatch
64 if (mListener != null) {
65 if (mHandler != null) {
66 mHandler.post(new Runnable() {
67 @Override
68 public void run() {
69 mListener.onResult(result);
70 }
71 });
72 } else {
73 mListener.onResult(result);
74 }
75 // Do remote dispatch
76 } else {
77 try {
78 mCallback.sendResult(result);
79 } catch (RemoteException e) {
80 /* ignore */
81 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080082 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080083 }
Svet Ganov9c165d72015-12-01 19:52:26 -080084
85 @Override
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080086 public int describeContents() {
87 return 0;
88 }
89
Svet Ganov9c165d72015-12-01 19:52:26 -080090 @Override
91 public void writeToParcel(Parcel parcel, int flags) {
92 parcel.writeStrongBinder(mCallback.asBinder());
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080093 }
94
95 public static final Parcelable.Creator<RemoteCallback> CREATOR
96 = new Parcelable.Creator<RemoteCallback>() {
Svet Ganov9c165d72015-12-01 19:52:26 -080097 public RemoteCallback createFromParcel(Parcel parcel) {
98 return new RemoteCallback(parcel);
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080099 }
100
101 public RemoteCallback[] newArray(int size) {
102 return new RemoteCallback[size];
103 }
104 };
105}