1 /* 2 * Copyright (C) 2022 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.telephony.qns; 18 19 import android.os.Handler; 20 import android.os.Message; 21 22 import java.lang.ref.WeakReference; 23 24 /** @hide */ 25 class QnsRegistrant { 26 protected WeakReference<Handler> mRefH; 27 protected int mWhat; 28 protected Object mUserObj; 29 QnsRegistrant(Handler h, int what, Object obj)30 QnsRegistrant(Handler h, int what, Object obj) { 31 mRefH = new WeakReference<>(h); 32 mWhat = what; 33 mUserObj = obj; 34 } 35 36 /** Clear registrant. */ clear()37 void clear() { 38 mRefH = null; 39 mUserObj = null; 40 } 41 42 /** 43 * notify result 44 * 45 * @param result Object to notify 46 */ notifyResult(Object result)47 void notifyResult(Object result) { 48 internalNotifyRegistrant(result, null); 49 } 50 51 /** 52 * notify registrant 53 * 54 * @param ar QnsAsyncResult to notify 55 */ notifyRegistrant(QnsAsyncResult ar)56 void notifyRegistrant(QnsAsyncResult ar) { 57 internalNotifyRegistrant(ar.mResult, ar.mException); 58 } 59 internalNotifyRegistrant(Object result, Throwable exception)60 protected void internalNotifyRegistrant(Object result, Throwable exception) { 61 Handler h = getHandler(); 62 63 if (h == null) { 64 clear(); 65 } else { 66 Message msg = Message.obtain(); 67 68 msg.what = mWhat; 69 msg.obj = new QnsAsyncResult(mUserObj, result, exception); 70 h.sendMessage(msg); 71 } 72 } 73 74 /** 75 * get a handler. 76 * 77 * @return Handler 78 */ getHandler()79 Handler getHandler() { 80 if (mRefH == null) { 81 return null; 82 } 83 84 return mRefH.get(); 85 } 86 } 87