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
17package android.os;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.annotation.SystemApi;
22
23/**
24 * @hide
25 */
26@SystemApi
27public final class RemoteCallback implements Parcelable {
28
29    public interface OnResultListener {
30        public void onResult(Bundle result);
31    }
32
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);
39    }
40
41    public RemoteCallback(@NonNull OnResultListener listener, @Nullable Handler handler) {
42        if (listener == null) {
43            throw new NullPointerException("listener cannot be null");
44        }
45        mListener = listener;
46        mHandler = handler;
47        mCallback = new IRemoteCallback.Stub() {
48            @Override
49            public void sendResult(Bundle data) {
50                RemoteCallback.this.sendResult(data);
51            }
52        };
53    }
54
55    RemoteCallback(Parcel parcel) {
56        mListener = null;
57        mHandler = null;
58        mCallback = IRemoteCallback.Stub.asInterface(
59                parcel.readStrongBinder());
60    }
61
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            }
82        }
83    }
84
85    @Override
86    public int describeContents() {
87        return 0;
88    }
89
90    @Override
91    public void writeToParcel(Parcel parcel, int flags) {
92        parcel.writeStrongBinder(mCallback.asBinder());
93    }
94
95    public static final Parcelable.Creator<RemoteCallback> CREATOR
96            = new Parcelable.Creator<RemoteCallback>() {
97        public RemoteCallback createFromParcel(Parcel parcel) {
98            return new RemoteCallback(parcel);
99        }
100
101        public RemoteCallback[] newArray(int size) {
102            return new RemoteCallback[size];
103        }
104    };
105}
106