• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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 com.android.internal.os.IResultReceiver;
20 
21 /**
22  * Generic interface for receiving a callback result from someone.  Use this
23  * by creating a subclass and implement {@link #onReceiveResult}, which you can
24  * then pass to others and send through IPC, and receive results they
25  * supply with {@link #send}.
26  */
27 public class ResultReceiver implements Parcelable {
28     final boolean mLocal;
29     final Handler mHandler;
30 
31     IResultReceiver mReceiver;
32 
33     class MyRunnable implements Runnable {
34         final int mResultCode;
35         final Bundle mResultData;
36 
MyRunnable(int resultCode, Bundle resultData)37         MyRunnable(int resultCode, Bundle resultData) {
38             mResultCode = resultCode;
39             mResultData = resultData;
40         }
41 
run()42         public void run() {
43             onReceiveResult(mResultCode, mResultData);
44         }
45     }
46 
47     class MyResultReceiver extends IResultReceiver.Stub {
send(int resultCode, Bundle resultData)48         public void send(int resultCode, Bundle resultData) {
49             if (mHandler != null) {
50                 mHandler.post(new MyRunnable(resultCode, resultData));
51             } else {
52                 onReceiveResult(resultCode, resultData);
53             }
54         }
55     }
56 
57     /**
58      * Create a new ResultReceive to receive results.  Your
59      * {@link #onReceiveResult} method will be called from the thread running
60      * <var>handler</var> if given, or from an arbitrary thread if null.
61      */
ResultReceiver(Handler handler)62     public ResultReceiver(Handler handler) {
63         mLocal = true;
64         mHandler = handler;
65     }
66 
67     /**
68      * Deliver a result to this receiver.  Will call {@link #onReceiveResult},
69      * always asynchronously if the receiver has supplied a Handler in which
70      * to dispatch the result.
71      * @param resultCode Arbitrary result code to deliver, as defined by you.
72      * @param resultData Any additional data provided by you.
73      */
send(int resultCode, Bundle resultData)74     public void send(int resultCode, Bundle resultData) {
75         if (mLocal) {
76             if (mHandler != null) {
77                 mHandler.post(new MyRunnable(resultCode, resultData));
78             } else {
79                 onReceiveResult(resultCode, resultData);
80             }
81             return;
82         }
83 
84         if (mReceiver != null) {
85             try {
86                 mReceiver.send(resultCode, resultData);
87             } catch (RemoteException e) {
88             }
89         }
90     }
91 
92     /**
93      * Override to receive results delivered to this object.
94      *
95      * @param resultCode Arbitrary result code delivered by the sender, as
96      * defined by the sender.
97      * @param resultData Any additional data provided by the sender.
98      */
onReceiveResult(int resultCode, Bundle resultData)99     protected void onReceiveResult(int resultCode, Bundle resultData) {
100     }
101 
describeContents()102     public int describeContents() {
103         return 0;
104     }
105 
writeToParcel(Parcel out, int flags)106     public void writeToParcel(Parcel out, int flags) {
107         synchronized (this) {
108             if (mReceiver == null) {
109                 mReceiver = new MyResultReceiver();
110             }
111             out.writeStrongBinder(mReceiver.asBinder());
112         }
113     }
114 
ResultReceiver(Parcel in)115     ResultReceiver(Parcel in) {
116         mLocal = false;
117         mHandler = null;
118         mReceiver = IResultReceiver.Stub.asInterface(in.readStrongBinder());
119     }
120 
121     public static final Parcelable.Creator<ResultReceiver> CREATOR
122             = new Parcelable.Creator<ResultReceiver>() {
123         public ResultReceiver createFromParcel(Parcel in) {
124             return new ResultReceiver(in);
125         }
126         public ResultReceiver[] newArray(int size) {
127             return new ResultReceiver[size];
128         }
129     };
130 }
131