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