• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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.os;
18 
19 import android.content.Context;
20 import android.os.Handler;
21 import android.os.Looper;
22 import android.os.Message;
23 
24 public class HandlerCaller {
25     final Looper mMainLooper;
26     final Handler mH;
27 
28     final Callback mCallback;
29 
30     class MyHandler extends Handler {
MyHandler(Looper looper, boolean async)31         MyHandler(Looper looper, boolean async) {
32             super(looper, null, async);
33         }
34 
35         @Override
handleMessage(Message msg)36         public void handleMessage(Message msg) {
37             mCallback.executeMessage(msg);
38         }
39     }
40 
41     public interface Callback {
executeMessage(Message msg)42         public void executeMessage(Message msg);
43     }
44 
HandlerCaller(Context context, Looper looper, Callback callback, boolean asyncHandler)45     public HandlerCaller(Context context, Looper looper, Callback callback,
46             boolean asyncHandler) {
47         mMainLooper = looper != null ? looper : context.getMainLooper();
48         mH = new MyHandler(mMainLooper, asyncHandler);
49         mCallback = callback;
50     }
51 
getHandler()52     public Handler getHandler() {
53         return mH;
54     }
55 
executeOrSendMessage(Message msg)56     public void executeOrSendMessage(Message msg) {
57         // If we are calling this from the main thread, then we can call
58         // right through.  Otherwise, we need to send the message to the
59         // main thread.
60         if (Looper.myLooper() == mMainLooper) {
61             mCallback.executeMessage(msg);
62             msg.recycle();
63             return;
64         }
65 
66         mH.sendMessage(msg);
67     }
68 
sendMessageDelayed(Message msg, long delayMillis)69     public void sendMessageDelayed(Message msg, long delayMillis) {
70         mH.sendMessageDelayed(msg, delayMillis);
71     }
72 
hasMessages(int what)73     public boolean hasMessages(int what) {
74         return mH.hasMessages(what);
75     }
76 
removeMessages(int what)77     public void removeMessages(int what) {
78         mH.removeMessages(what);
79     }
80 
removeMessages(int what, Object obj)81     public void removeMessages(int what, Object obj) {
82         mH.removeMessages(what, obj);
83     }
84 
sendMessage(Message msg)85     public void sendMessage(Message msg) {
86         mH.sendMessage(msg);
87     }
88 
sendMessageAndWait(Message msg)89     public SomeArgs sendMessageAndWait(Message msg) {
90         if (Looper.myLooper() == mH.getLooper()) {
91             throw new IllegalStateException("Can't wait on same thread as looper");
92         }
93         SomeArgs args = (SomeArgs)msg.obj;
94         args.mWaitState = SomeArgs.WAIT_WAITING;
95         mH.sendMessage(msg);
96         synchronized (args) {
97             while (args.mWaitState == SomeArgs.WAIT_WAITING) {
98                 try {
99                     args.wait();
100                 } catch (InterruptedException e) {
101                     return null;
102                 }
103             }
104         }
105         args.mWaitState = SomeArgs.WAIT_NONE;
106         return args;
107     }
108 
obtainMessage(int what)109     public Message obtainMessage(int what) {
110         return mH.obtainMessage(what);
111     }
112 
obtainMessageBO(int what, boolean arg1, Object arg2)113     public Message obtainMessageBO(int what, boolean arg1, Object arg2) {
114         return mH.obtainMessage(what, arg1 ? 1 : 0, 0, arg2);
115     }
116 
obtainMessageBOO(int what, boolean arg1, Object arg2, Object arg3)117     public Message obtainMessageBOO(int what, boolean arg1, Object arg2, Object arg3) {
118         SomeArgs args = SomeArgs.obtain();
119         args.arg1 = arg2;
120         args.arg2 = arg3;
121         return mH.obtainMessage(what, arg1 ? 1 : 0, 0, args);
122     }
123 
obtainMessageO(int what, Object arg1)124     public Message obtainMessageO(int what, Object arg1) {
125         return mH.obtainMessage(what, 0, 0, arg1);
126     }
127 
obtainMessageI(int what, int arg1)128     public Message obtainMessageI(int what, int arg1) {
129         return mH.obtainMessage(what, arg1, 0);
130     }
131 
obtainMessageII(int what, int arg1, int arg2)132     public Message obtainMessageII(int what, int arg1, int arg2) {
133         return mH.obtainMessage(what, arg1, arg2);
134     }
135 
obtainMessageIO(int what, int arg1, Object arg2)136     public Message obtainMessageIO(int what, int arg1, Object arg2) {
137         return mH.obtainMessage(what, arg1, 0, arg2);
138     }
139 
obtainMessageIIO(int what, int arg1, int arg2, Object arg3)140     public Message obtainMessageIIO(int what, int arg1, int arg2, Object arg3) {
141         return mH.obtainMessage(what, arg1, arg2, arg3);
142     }
143 
obtainMessageIIOO(int what, int arg1, int arg2, Object arg3, Object arg4)144     public Message obtainMessageIIOO(int what, int arg1, int arg2,
145             Object arg3, Object arg4) {
146         SomeArgs args = SomeArgs.obtain();
147         args.arg1 = arg3;
148         args.arg2 = arg4;
149         return mH.obtainMessage(what, arg1, arg2, args);
150     }
151 
obtainMessageIOO(int what, int arg1, Object arg2, Object arg3)152     public Message obtainMessageIOO(int what, int arg1, Object arg2, Object arg3) {
153         SomeArgs args = SomeArgs.obtain();
154         args.arg1 = arg2;
155         args.arg2 = arg3;
156         return mH.obtainMessage(what, arg1, 0, args);
157     }
158 
obtainMessageIOOO(int what, int arg1, Object arg2, Object arg3, Object arg4)159     public Message obtainMessageIOOO(int what, int arg1, Object arg2, Object arg3, Object arg4) {
160         SomeArgs args = SomeArgs.obtain();
161         args.arg1 = arg2;
162         args.arg2 = arg3;
163         args.arg3 = arg4;
164         return mH.obtainMessage(what, arg1, 0, args);
165     }
166 
obtainMessageIIOOO(int what, int arg1, int arg2, Object arg3, Object arg4, Object arg5)167     public Message obtainMessageIIOOO(int what, int arg1, int arg2, Object arg3, Object arg4,
168             Object arg5) {
169         SomeArgs args = SomeArgs.obtain();
170         args.arg1 = arg3;
171         args.arg2 = arg4;
172         args.arg3 = arg5;
173         return mH.obtainMessage(what, arg1, arg2, args);
174     }
175 
obtainMessageOO(int what, Object arg1, Object arg2)176     public Message obtainMessageOO(int what, Object arg1, Object arg2) {
177         SomeArgs args = SomeArgs.obtain();
178         args.arg1 = arg1;
179         args.arg2 = arg2;
180         return mH.obtainMessage(what, 0, 0, args);
181     }
182 
obtainMessageOOO(int what, Object arg1, Object arg2, Object arg3)183     public Message obtainMessageOOO(int what, Object arg1, Object arg2, Object arg3) {
184         SomeArgs args = SomeArgs.obtain();
185         args.arg1 = arg1;
186         args.arg2 = arg2;
187         args.arg3 = arg3;
188         return mH.obtainMessage(what, 0, 0, args);
189     }
190 
obtainMessageOOOO(int what, Object arg1, Object arg2, Object arg3, Object arg4)191     public Message obtainMessageOOOO(int what, Object arg1, Object arg2,
192             Object arg3, Object arg4) {
193         SomeArgs args = SomeArgs.obtain();
194         args.arg1 = arg1;
195         args.arg2 = arg2;
196         args.arg3 = arg3;
197         args.arg4 = arg4;
198         return mH.obtainMessage(what, 0, 0, args);
199     }
200 
obtainMessageOOOOO(int what, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5)201     public Message obtainMessageOOOOO(int what, Object arg1, Object arg2,
202             Object arg3, Object arg4, Object arg5) {
203         SomeArgs args = SomeArgs.obtain();
204         args.arg1 = arg1;
205         args.arg2 = arg2;
206         args.arg3 = arg3;
207         args.arg4 = arg4;
208         args.arg5 = arg5;
209         return mH.obtainMessage(what, 0, 0, args);
210     }
211 
obtainMessageOOOOII(int what, Object arg1, Object arg2, Object arg3, Object arg4, int arg5, int arg6)212     public Message obtainMessageOOOOII(int what, Object arg1, Object arg2,
213             Object arg3, Object arg4, int arg5, int arg6) {
214         SomeArgs args = SomeArgs.obtain();
215         args.arg1 = arg1;
216         args.arg2 = arg2;
217         args.arg3 = arg3;
218         args.arg4 = arg4;
219         args.argi5 = arg5;
220         args.argi6 = arg6;
221         return mH.obtainMessage(what, 0, 0, args);
222     }
223 
obtainMessageIIII(int what, int arg1, int arg2, int arg3, int arg4)224     public Message obtainMessageIIII(int what, int arg1, int arg2,
225             int arg3, int arg4) {
226         SomeArgs args = SomeArgs.obtain();
227         args.argi1 = arg1;
228         args.argi2 = arg2;
229         args.argi3 = arg3;
230         args.argi4 = arg4;
231         return mH.obtainMessage(what, 0, 0, args);
232     }
233 
obtainMessageIIIIII(int what, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6)234     public Message obtainMessageIIIIII(int what, int arg1, int arg2,
235             int arg3, int arg4, int arg5, int arg6) {
236         SomeArgs args = SomeArgs.obtain();
237         args.argi1 = arg1;
238         args.argi2 = arg2;
239         args.argi3 = arg3;
240         args.argi4 = arg4;
241         args.argi5 = arg5;
242         args.argi6 = arg6;
243         return mH.obtainMessage(what, 0, 0, args);
244     }
245 
obtainMessageIIIIO(int what, int arg1, int arg2, int arg3, int arg4, Object arg5)246     public Message obtainMessageIIIIO(int what, int arg1, int arg2,
247             int arg3, int arg4, Object arg5) {
248         SomeArgs args = SomeArgs.obtain();
249         args.arg1 = arg5;
250         args.argi1 = arg1;
251         args.argi2 = arg2;
252         args.argi3 = arg3;
253         args.argi4 = arg4;
254         return mH.obtainMessage(what, 0, 0, args);
255     }
256 }
257