Home | History | Annotate | Download | only in os
      1 /*
      2  * Copyright (C) 2006 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 android.os.Handler;
     20 import android.os.Message;
     21 
     22 import java.lang.ref.WeakReference;
     23 import java.util.HashMap;
     24 
     25 /** @hide */
     26 public class Registrant
     27 {
     28     public
     29     Registrant(Handler h, int what, Object obj)
     30     {
     31         refH = new WeakReference(h);
     32         this.what = what;
     33         userObj = obj;
     34     }
     35 
     36     public void
     37     clear()
     38     {
     39         refH = null;
     40         userObj = null;
     41     }
     42 
     43     public void
     44     notifyRegistrant()
     45     {
     46         internalNotifyRegistrant (null, null);
     47     }
     48 
     49     public void
     50     notifyResult(Object result)
     51     {
     52         internalNotifyRegistrant (result, null);
     53     }
     54 
     55     public void
     56     notifyException(Throwable exception)
     57     {
     58         internalNotifyRegistrant (null, exception);
     59     }
     60 
     61     /**
     62      * This makes a copy of @param ar
     63      */
     64     public void
     65     notifyRegistrant(AsyncResult ar)
     66     {
     67         internalNotifyRegistrant (ar.result, ar.exception);
     68     }
     69 
     70     /*package*/ void
     71     internalNotifyRegistrant (Object result, Throwable exception)
     72     {
     73         Handler h = getHandler();
     74 
     75         if (h == null) {
     76             clear();
     77         } else {
     78             Message msg = Message.obtain();
     79 
     80             msg.what = what;
     81 
     82             msg.obj = new AsyncResult(userObj, result, exception);
     83 
     84             h.sendMessage(msg);
     85         }
     86     }
     87 
     88     /**
     89      * NOTE: May return null if weak reference has been collected
     90      */
     91 
     92     public Message
     93     messageForRegistrant()
     94     {
     95         Handler h = getHandler();
     96 
     97         if (h == null) {
     98             clear();
     99 
    100             return null;
    101         } else {
    102             Message msg = h.obtainMessage();
    103 
    104             msg.what = what;
    105             msg.obj = userObj;
    106 
    107             return msg;
    108         }
    109     }
    110 
    111     public Handler
    112     getHandler()
    113     {
    114         if (refH == null)
    115             return null;
    116 
    117         return (Handler) refH.get();
    118     }
    119 
    120     WeakReference   refH;
    121     int             what;
    122     Object          userObj;
    123 }
    124 
    125