Home | History | Annotate | Download | only in os
      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 
     26     public final Context mContext;
     27 
     28     final Looper mMainLooper;
     29     final Handler mH;
     30 
     31     final Callback mCallback;
     32 
     33     class MyHandler extends Handler {
     34         MyHandler(Looper looper, boolean async) {
     35             super(looper, null, async);
     36         }
     37 
     38         @Override
     39         public void handleMessage(Message msg) {
     40             mCallback.executeMessage(msg);
     41         }
     42     }
     43 
     44     public interface Callback {
     45         public void executeMessage(Message msg);
     46     }
     47 
     48     public HandlerCaller(Context context, Looper looper, Callback callback,
     49             boolean asyncHandler) {
     50         mContext = context;
     51         mMainLooper = looper != null ? looper : context.getMainLooper();
     52         mH = new MyHandler(mMainLooper, asyncHandler);
     53         mCallback = callback;
     54     }
     55 
     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 
     69     public void sendMessageDelayed(Message msg, long delayMillis) {
     70         mH.sendMessageDelayed(msg, delayMillis);
     71     }
     72 
     73     public boolean hasMessages(int what) {
     74         return mH.hasMessages(what);
     75     }
     76 
     77     public void removeMessages(int what) {
     78         mH.removeMessages(what);
     79     }
     80 
     81     public void removeMessages(int what, Object obj) {
     82         mH.removeMessages(what, obj);
     83     }
     84 
     85     public void sendMessage(Message msg) {
     86         mH.sendMessage(msg);
     87     }
     88 
     89     public Message obtainMessage(int what) {
     90         return mH.obtainMessage(what);
     91     }
     92 
     93     public Message obtainMessageBO(int what, boolean arg1, Object arg2) {
     94         return mH.obtainMessage(what, arg1 ? 1 : 0, 0, arg2);
     95     }
     96 
     97     public Message obtainMessageBOO(int what, boolean arg1, Object arg2, Object arg3) {
     98         SomeArgs args = SomeArgs.obtain();
     99         args.arg1 = arg2;
    100         args.arg2 = arg3;
    101         return mH.obtainMessage(what, arg1 ? 1 : 0, 0, args);
    102     }
    103 
    104     public Message obtainMessageO(int what, Object arg1) {
    105         return mH.obtainMessage(what, 0, 0, arg1);
    106     }
    107 
    108     public Message obtainMessageI(int what, int arg1) {
    109         return mH.obtainMessage(what, arg1, 0);
    110     }
    111 
    112     public Message obtainMessageII(int what, int arg1, int arg2) {
    113         return mH.obtainMessage(what, arg1, arg2);
    114     }
    115 
    116     public Message obtainMessageIO(int what, int arg1, Object arg2) {
    117         return mH.obtainMessage(what, arg1, 0, arg2);
    118     }
    119 
    120     public Message obtainMessageIIO(int what, int arg1, int arg2, Object arg3) {
    121         return mH.obtainMessage(what, arg1, arg2, arg3);
    122     }
    123 
    124     public Message obtainMessageIIOO(int what, int arg1, int arg2,
    125             Object arg3, Object arg4) {
    126         SomeArgs args = SomeArgs.obtain();
    127         args.arg1 = arg3;
    128         args.arg2 = arg4;
    129         return mH.obtainMessage(what, arg1, arg2, args);
    130     }
    131 
    132     public Message obtainMessageIOO(int what, int arg1, Object arg2, Object arg3) {
    133         SomeArgs args = SomeArgs.obtain();
    134         args.arg1 = arg2;
    135         args.arg2 = arg3;
    136         return mH.obtainMessage(what, arg1, 0, args);
    137     }
    138 
    139     public Message obtainMessageOO(int what, Object arg1, Object arg2) {
    140         SomeArgs args = SomeArgs.obtain();
    141         args.arg1 = arg1;
    142         args.arg2 = arg2;
    143         return mH.obtainMessage(what, 0, 0, args);
    144     }
    145 
    146     public Message obtainMessageOOO(int what, Object arg1, Object arg2, Object arg3) {
    147         SomeArgs args = SomeArgs.obtain();
    148         args.arg1 = arg1;
    149         args.arg2 = arg2;
    150         args.arg3 = arg3;
    151         return mH.obtainMessage(what, 0, 0, args);
    152     }
    153 
    154     public Message obtainMessageOOOO(int what, Object arg1, Object arg2,
    155             Object arg3, Object arg4) {
    156         SomeArgs args = SomeArgs.obtain();
    157         args.arg1 = arg1;
    158         args.arg2 = arg2;
    159         args.arg3 = arg3;
    160         args.arg4 = arg4;
    161         return mH.obtainMessage(what, 0, 0, args);
    162     }
    163 
    164     public Message obtainMessageIIII(int what, int arg1, int arg2,
    165             int arg3, int arg4) {
    166         SomeArgs args = SomeArgs.obtain();
    167         args.argi1 = arg1;
    168         args.argi2 = arg2;
    169         args.argi3 = arg3;
    170         args.argi4 = arg4;
    171         return mH.obtainMessage(what, 0, 0, args);
    172     }
    173 
    174     public Message obtainMessageIIIIII(int what, int arg1, int arg2,
    175             int arg3, int arg4, int arg5, int arg6) {
    176         SomeArgs args = SomeArgs.obtain();
    177         args.argi1 = arg1;
    178         args.argi2 = arg2;
    179         args.argi3 = arg3;
    180         args.argi4 = arg4;
    181         args.argi5 = arg5;
    182         args.argi6 = arg6;
    183         return mH.obtainMessage(what, 0, 0, args);
    184     }
    185 
    186     public Message obtainMessageIIIIO(int what, int arg1, int arg2,
    187             int arg3, int arg4, Object arg5) {
    188         SomeArgs args = SomeArgs.obtain();
    189         args.arg1 = arg5;
    190         args.argi1 = arg1;
    191         args.argi2 = arg2;
    192         args.argi3 = arg3;
    193         args.argi4 = arg4;
    194         return mH.obtainMessage(what, 0, 0, args);
    195     }
    196 }
    197