Android RIL java 和 RIL C 使用的是异步通信机制,RIL java 中一个线程中发送消息给RIL C,RIL C 响应(RESPONSE_SOLICITED 和 RESPONSE_UNSOLICITED),RIL java再去处理,在这个RIL java 处理这些相应是主要是使用了RegistrantList 消息处理机制。
从设计模式的角度来看RegistrantList消息处理机制属于观察者模式,
1、观察者(Registrant 实例)将自己注册到被观察对象(RegistrantList实例)中,被观察对象将观察者存放在一个容器(registrants)里。
2、被观察对象(RegistrantList实例)发生了某种变化,从容器中得到所有注册过的观察者,将变化通知观察者。
3、被观察者要撤销观察,被观察者从容器中将观察者去除。
下面是一个列子用于说明如何使用RegistratList 消息处理机制,
protected RegistrantList mOnRegistrants = new RegistrantList();
//注册Radio On 观察者
public void registerForOn(Handler h, int what, Object obj) {
Registrant r = new Registrant (h, what, obj);
synchronized (mStateMonitor) {
mOnRegistrants.add(r);
if (mState.isOn()) {
r.notifyRegistrant(new AsyncResult(null, null, null));
}
}
}
//删除观察者
public void unregisterForOn(Handler h) {
synchronized (mStateMonitor) {
mOnRegistrants.remove(h);
}
}
//更新观察者
protected void setRadioState(RadioState newState) {
if (mState.isOn() && !oldState.isOn()) {
Log.d(LOG_TAG,"Notifying: Radio On");
mOnRegistrants.notifyRegistrants();
}
}
protected void init(Context context, PhoneNotifier notifier) {
//调用注册观察者,配置回调函数及参数
mCM.registerForOn(this, EVENT_RADIO_ON, null);
}
重点代码
1、观察者类
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.os;
import android.os.Handler;
import android.os.Message;
import java.lang.ref.WeakReference;
import java.util.HashMap;
/** @hide */
public class Registrant
{
//构造观察者
public
Registrant(Handler h, int what, Object obj)
{
refH = new WeakReference(h);
this.what = what;
userObj = obj;
}
public void
clear()
{
refH = null;
userObj = null;
}
public void
notifyRegistrant()
{
internalNotifyRegistrant (null, null); //通知更新
}
public void
notifyResult(Object result)
{
internalNotifyRegistrant (result, null);
}
public void
notifyException(Throwable exception)
{
internalNotifyRegistrant (null, exception);
}
/**
* This makes a copy of @param ar
*/
public void
notifyRegistrant(AsyncResult ar)
{
internalNotifyRegistrant (ar.result, ar.exception);
}
/*package*/ void
internalNotifyRegistrant (Object result, Throwable exception)
{
Handler h = getHandler();
if (h == null) {
clear();
} else {
//利用handler机制异步更新
Message msg = Message.obtain();
msg.what = what;
msg.obj = new AsyncResult(userObj, result, exception);
h.sendMessage(msg);
}
}
/**
* NOTE: May return null if weak reference has been collected
*/
public Message
messageForRegistrant()
{
Handler h = getHandler();
if (h == null) {
clear();
return null;
} else {
Message msg = h.obtainMessage();
msg.what = what;
msg.obj = userObj;
return msg;
}
}
public Handler
getHandler()
{
if (refH == null)
return null;
return (Handler) refH.get();
}
WeakReference refH; //注册handler
int what; //注册的消息类型
Object userObj;
}
2、被观察者类
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.os;
import android.os.Handler;
import android.os.Message;
import java.util.ArrayList;
import java.util.HashMap;
/** @hide */
public class RegistrantList
{
ArrayList registrants = new ArrayList(); // of Registrant //观察者的容器
//添加观察者对象
public synchronized void
add(Handler h, int what, Object obj)
{
add(new Registrant(h, what, obj));
}
public synchronized void
addUnique(Handler h, int what, Object obj)
{
// if the handler is already in the registrant list, remove it
remove(h);
add(new Registrant(h, what, obj));
}
public synchronized void
add(Registrant r)
{
removeCleared();
registrants.add(r);
}
public synchronized void
removeCleared()
{
for (int i = registrants.size() - 1; i >= 0 ; i--) {
Registrant r = (Registrant) registrants.get(i);
if (r.refH == null) {
registrants.remove(i);
}
}
}
public synchronized int
size()
{
return registrants.size();
}
public synchronized Object
get(int index)
{
return registrants.get(index);
}
//更新观察者
private synchronized void
internalNotifyRegistrants (Object result, Throwable exception)
{
for (int i = 0, s = registrants.size(); i < s ; i++) {
Registrant r = (Registrant) registrants.get(i);
r.internalNotifyRegistrant(result, exception);
}
}
public /*synchronized*/ void
notifyRegistrants()
{
internalNotifyRegistrants(null, null);
}
public /*synchronized*/ void
notifyException(Throwable exception)
{
internalNotifyRegistrants (null, exception);
}
public /*synchronized*/ void
notifyResult(Object result)
{
internalNotifyRegistrants (result, null);
}
public /*synchronized*/ void
notifyRegistrants(AsyncResult ar)
{
internalNotifyRegistrants(ar.result, ar.exception);
}
//移除观察者
public synchronized void
remove(Handler h)
{
for (int i = 0, s = registrants.size() ; i < s ; i++) {
Registrant r = (Registrant) registrants.get(i);
Handler rh;
rh = r.getHandler();
/* Clean up both the requested registrant and
* any now-collected registrants
*/
if (rh == null || rh == h) {
r.clear();
}
}
removeCleared();
}
}