Table of Contents
ContextHubManager(构造函数需要2个参数:Context,Looper)
queryNanoApps(从名字上看就是query传输,也是唯一的query)
IContextHubTransactionCallback何时被调用
使用ConxtHubClient的接口sendMessageToNanoApp
ContextHubManager是使用ContextHubService(system service: contexthub)的入口
访问contexthub使用的接口
获得contextHub信息: 使用getContextHubs,放弃getContextHubHandles and getContextHubInfo
获得NanoApp信息: 使用queryNanoApps,放弃getNanoAppInstanceInfo and findNanoAppOnHub
load/unloadNanoApp的名字没有改变,但参数变化
放弃使用ContextHubCallback: 使用ContextHubClientCallback替代,
放弃registerCallback(传入用户注册的callback),使用createClient(ContextHubInfo, ContextHubClientCallback)替代.
放弃使用sendmMessage, 使用 IContextHubClient的接口ContextHubClient#sendMessageToNanoApp(NanoAppMessage)
放弃使用unRegisterCall,使用IContextHubClient的接口ContextHubClient#close()
ContextHubManager(构造函数需要2个参数:Context,Looper)
mMainLoop是执行mClientCallback的,而registerCallback已经放弃
public ContextHubManager(Context context, Looper mainLooper) throws ServiceNotFoundException {
mMainLooper = mainLooper;
mService = IContextHubService.Stub.asInterface(//得到访问服务的代理
ServiceManager.getServiceOrThrow(Context.CONTEXTHUB_SERVICE));
try {//这个接口不是放弃了吗?
mService.registerCallback(mClientCallback);
}
}
CreateClient
这个函数很重要,向ContextHubService注册了一个ContextHubClient, 并把用户的ContextHubClientCallback传到ContexHubService, 内部也获得了访问IContextHubClient的接口。
通过创建的ContextHubClient可以向NanoApp发送Message, 关闭和ContextHubService的链接
//Creates and registers a client and its callback with the Context Hub Service.
public ContextHubClient createClient(
ContextHubInfo hubInfo, ContextHubClientCallback callback,
CallbackExecutor Executor executor) {
ContextHubClient client = new ContextHubClient(hubInfo);
//这里有个比较隐蔽的 ContextHubClientCallback -> IContextHubClientCallback
IContextHubClientCallback clientInterface = createClientCallback(
client, callback, executor);
IContextHubClient clientProxy;
try {
clientProxy = mService.createClient(clientInterface, hubInfo.getId());
}
client.setClientProxy(clientProxy);
return client;
}
public ContextHubClient createClient(
ContextHubInfo hubInfo, ContextHubClientCallback callback) {
return createClient(hubInfo, callback, new HandlerExecutor(Handler.getMain()));
}
createClientCallback
client传入的是ContextHubClientCallback, 而Server需要的是IContextHubClientCallback
/**
* Creates an interface to the ContextHubClient to send down to the service.
*
* @param client the ContextHubClient object associated with this callback
* @param callback the callback to invoke at the client process
* @param executor the executor to invoke callbacks for this client
* @return the callback interface
*/
private IContextHubClientCallback createClientCallback(
ContextHubClient client, ContextHubClientCallback callback, Executor executor) {
return new IContextHubClientCallback.Stub() {
@Override
public void onMessageFromNanoApp(NanoAppMessage message) {
executor.execute(() -> callback.onMessageFromNanoApp(client, message));
@Override
public void ...
}
获得contexHub信息
这个方法是最简单的,直接从ContextHubService的变量中取值,而值的赋值是创建contexthub service的时候,
这个赋值过程也是直接调用Hal 接口函数,而没有创建transaction, 为什么?
public List<ContextHubInfo> getContextHubs() {
try {
return mService.getContextHubs();
}
}
transaction
transaction分为两种;query (返回内容)和non-query(返回传输状态:成功、错误码)
queryNanoApps(从名字上看就是query传输,也是唯一的query)
public ContextHubTransaction<List<NanoAppState>> queryNanoApps(
ContextHubInfo hubInfo) {
ContextHubTransaction<List<NanoAppState>> transaction =
new ContextHubTransaction<>(ContextHubTransaction.TYPE_QUERY_NANOAPPS);
IContextHubTransactionCallback callback = createQueryCallback(transaction);
try {//把client创建的IContextHubTransactionCallback传入ContextHubService
mService.queryNanoApps(hubInfo.getId(), callback);
}
return transaction;
}
这里的ContextHubTransaction并没有调用 setOnCompleteListener, 所以要得到response要使用WaitForResponse
创建ContextHubTransaction对象
ContextHubTransaction<List<NanoAppState>> transaction =
new ContextHubTransaction<>(ContextHubTransaction.TYPE_QUERY_NANOAPPS);
模板参数类型是List<NanoAppState>,构造函数的参数是事件类型TYPE_QUERY_NANOAPPS
createQueryCallback
private IContextHubTransactionCallback createQueryCallback(
ContextHubTransaction<List<NanoAppState>> transaction) {
return new IContextHubTransactionCallback.Stub() {
@Override
public void onQueryResponse(int result, List<NanoAppState> nanoappList) {
transaction.setResponse(new ContextHubTransaction.Response<List<NanoAppState>>(
result, nanoappList));
}
@Override //当query时不会进入改函数
public void onTransactionComplete(int result) {
Log.e(TAG, "Received a non-query callback on a query request");
transaction.setResponse(new ContextHubTransaction.Response<List<NanoAppState>>(
ContextHubTransaction.RESULT_FAILED_SERVICE_INTERNAL_FAILURE, null));
}
};
}
onQueryResponse
创建ContextHubTransaction.Response对象
new ContextHubTransaction.Response<List<NanoAppState>>(result, nanoappList);
Response(@ContextHubTransaction.Result int result, R contents) {
mResult = result;
mContents = contents;
}
setResponse
void setResponse(ContextHubTransaction.Response<T> response) {
synchronized (this) {
mResponse = response;
mIsResponseSet = true;
mDoneSignal.countDown();
if (mListener != null) {//lemada形式
mExecutor.execute(() -> mListener.onComplete(this, mResponse));
}
}
}
IContextHubTransactionCallback何时被调用
IContextHubTransactionCallback在ContextHubService -> ContextHubServiceTransaction -> createQueryTransaction
ContextHubServiceTransaction createQueryTransaction(
int contextHubId, IContextHubTransactionCallback onCompleteCallback) {
return new ContextHubServiceTransaction(
mNextAvailableId.getAndIncrement(), ContextHubTransaction.TYPE_QUERY_NANOAPPS) {
@Override
/* package */ int onTransact() {
try {
return mContextHubProxy.queryApps(contextHubId);
}
}
@Override
/* package */ void onTransactionComplete(@ContextHubTransaction.Result int result) {
onQueryResponse(result, Collections.emptyList());
}
@Override
/* package */ void onQueryResponse(
@ContextHubTransaction.Result int result, List<NanoAppState> nanoAppStateList) {
try {
onCompleteCallback.onQueryResponse(result, nanoAppStateList);
}
}
};
}
non-query transaction
看过query的transaction再看non-query就简单了,差别就是transaction的模板参数不同,这类是Void
public ContextHubTransaction<Void> loadNanoApp(
ContextHubInfo hubInfo, NanoAppBinary appBinary) {
ContextHubTransaction<Void> transaction =
new ContextHubTransaction<>(ContextHubTransaction.TYPE_LOAD_NANOAPP);
IContextHubTransactionCallback callback = createTransactionCallback(transaction);
try {
mService.loadNanoAppOnHub(hubInfo.getId(), callback, appBinary);
}
return transaction;
}
createTransactionCallback
private IContextHubTransactionCallback createTransactionCallback(
ContextHubTransaction<Void> transaction) {
return new IContextHubTransactionCallback.Stub() {
@Override
public void onQueryResponse(int result, List<NanoAppState> nanoappList) {
Log.e(TAG, "Received a query callback on a non-query request");
transaction.setResponse(new ContextHubTransaction.Response<Void>(
ContextHubTransaction.RESULT_FAILED_SERVICE_INTERNAL_FAILURE, null));
}
@Override
public void onTransactionComplete(int result) {// transacting: R contents is null
transaction.setResponse(new ContextHubTransaction.Response<Void>(result, null));
}
};
}
sendMessage 到NanoApp
放弃了public int sendMessage(int hubHandle, int nanoAppHandle, @NonNull ContextHubMessage message)
使用ConxtHubClient的接口sendMessageToNanoApp
public int sendMessageToNanoApp(@NonNull NanoAppMessage message) {
Preconditions.checkNotNull(message, "NanoAppMessage cannot be null");
try {
return mClientProxy.sendMessageToNanoApp(message);
}
}
ContextHubClient使用的数据结构
ContextHubInfo
public class ContextHubInfo implements Parcelable {
private int mId;
private String mName;
private String mVendor;
private String mToolchain;
private int mPlatformVersion;
private int mToolchainVersion;
private float mPeakMips;
private float mStoppedPowerDrawMw;
private float mSleepPowerDrawMw;
private float mPeakPowerDrawMw;
private int mMaxPacketLengthBytes;
private byte mChreApiMajorVersion;
private byte mChreApiMinorVersion;
private short mChrePatchVersion;
private long mChrePlatformId;
private int[] mSupportedSensors;
private MemoryRegion[] mMemoryRegions;
}
NanoAppState
A class describing the nanoapp state information resulting from a query to a Context Hub
public final class NanoAppState implements Parcelable {
private long mNanoAppId;
private int mNanoAppVersion;
private boolean mIsEnabled;
public NanoAppState(long nanoAppId, int appVersion, boolean enabled) {
mNanoAppId = nanoAppId;
mNanoAppVersion = appVersion;
mIsEnabled = enabled;
}
}
NanoAppMessage
* A class describing messages send to or from nanoapps through the Context Hub Service.
* The basis of the class is in the IContextHub.hal ContextHubMsg definition.
public final class NanoAppMessage implements Parcelable {
private static final int DEBUG_LOG_NUM_BYTES = 16;
private long mNanoAppId;
private int mMessageType;
private byte[] mMessageBody;
private boolean mIsBroadcasted;
private NanoAppMessage(
long nanoAppId, int messageType, byte[] messageBody, boolean broadcasted) {
mNanoAppId = nanoAppId;
mMessageType = messageType;
mMessageBody = messageBody;
mIsBroadcasted = broadcasted;
}
}