Merge "Add support for multiple callback registration in." into nyc-dev
am: fdb26b9
* commit 'fdb26b90cbbd1db31d2ee1b2c4c38cfcc7d6a46c':
Add support for multiple callback registration in.
Change-Id: I52d4f7f2bc7d79ebd80759a94c7cdd23ba724144
This commit is contained in:
@@ -42,6 +42,12 @@ public final class ContextHubManager {
|
|||||||
private Callback mCallback;
|
private Callback mCallback;
|
||||||
private Handler mCallbackHandler;
|
private Handler mCallbackHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use {@code mCallback} instead.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
private ICallback mLocalCallback;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An interface to receive asynchronous communication from the context hub.
|
* An interface to receive asynchronous communication from the context hub.
|
||||||
*/
|
*/
|
||||||
@@ -63,6 +69,24 @@ public final class ContextHubManager {
|
|||||||
ContextHubMessage message);
|
ContextHubMessage message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use {@link Callback} instead.
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public interface ICallback {
|
||||||
|
/**
|
||||||
|
* Callback function called on message receipt from context hub.
|
||||||
|
*
|
||||||
|
* @param hubHandle Handle (system-wide unique identifier) of the hub of the message.
|
||||||
|
* @param nanoAppHandle Handle (unique identifier) for app instance that sent the message.
|
||||||
|
* @param message The context hub message.
|
||||||
|
*
|
||||||
|
* @see ContextHubMessage
|
||||||
|
*/
|
||||||
|
void onMessageReceipt(int hubHandle, int nanoAppHandle, ContextHubMessage message);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a handle to all the context hubs in the system
|
* Get a handle to all the context hubs in the system
|
||||||
* @return array of context hub handles
|
* @return array of context hub handles
|
||||||
@@ -222,6 +246,20 @@ public final class ContextHubManager {
|
|||||||
return registerCallback(callback, null);
|
return registerCallback(callback, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use {@link #registerCallback(Callback)} instead.
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public int registerCallback(ICallback callback) {
|
||||||
|
if (mLocalCallback != null) {
|
||||||
|
Log.w(TAG, "Max number of local callbacks reached!");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
mLocalCallback = callback;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a callback to receive messages from the context hub
|
* Set a callback to receive messages from the context hub
|
||||||
*
|
*
|
||||||
@@ -266,6 +304,19 @@ public final class ContextHubManager {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use {@link #unregisterCallback(Callback)} instead.
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
public synchronized int unregisterCallback(ICallback callback) {
|
||||||
|
if (callback != mLocalCallback) {
|
||||||
|
Log.w(TAG, "Cannot recognize local callback!");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
mLocalCallback = null;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
private IContextHubCallback.Stub mClientCallback = new IContextHubCallback.Stub() {
|
private IContextHubCallback.Stub mClientCallback = new IContextHubCallback.Stub() {
|
||||||
@Override
|
@Override
|
||||||
public void onMessageReceipt(final int hubId, final int nanoAppId,
|
public void onMessageReceipt(final int hubId, final int nanoAppId,
|
||||||
@@ -282,6 +333,12 @@ public final class ContextHubManager {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
} else if (mLocalCallback != null) {
|
||||||
|
// we always ensure that mCallback takes precedence, because mLocalCallback is only
|
||||||
|
// for internal compatibility
|
||||||
|
synchronized (this) {
|
||||||
|
mLocalCallback.onMessageReceipt(hubId, nanoAppId, message);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Log.d(TAG, "Context hub manager client callback is NULL");
|
Log.d(TAG, "Context hub manager client callback is NULL");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ package android.hardware.location;
|
|||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.os.RemoteCallbackList;
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -57,19 +58,17 @@ public class ContextHubService extends IContextHubService.Stub {
|
|||||||
private static final int OS_APP_INSTANCE = -1;
|
private static final int OS_APP_INSTANCE = -1;
|
||||||
|
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
|
private final HashMap<Integer, NanoAppInstanceInfo> mNanoAppHash = new HashMap<>();
|
||||||
private HashMap<Integer, NanoAppInstanceInfo> mNanoAppHash;
|
private final ContextHubInfo[] mContextHubInfo;
|
||||||
private ContextHubInfo[] mContextHubInfo;
|
private final RemoteCallbackList<IContextHubCallback> mCallbacksList =
|
||||||
private IContextHubCallback mCallback;
|
new RemoteCallbackList<>();
|
||||||
|
|
||||||
private native int nativeSendMessage(int[] header, byte[] data);
|
private native int nativeSendMessage(int[] header, byte[] data);
|
||||||
private native ContextHubInfo[] nativeInitialize();
|
private native ContextHubInfo[] nativeInitialize();
|
||||||
|
|
||||||
|
|
||||||
public ContextHubService(Context context) {
|
public ContextHubService(Context context) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mContextHubInfo = nativeInitialize();
|
mContextHubInfo = nativeInitialize();
|
||||||
mNanoAppHash = new HashMap<Integer, NanoAppInstanceInfo>();
|
|
||||||
|
|
||||||
for (int i = 0; i < mContextHubInfo.length; i++) {
|
for (int i = 0; i < mContextHubInfo.length; i++) {
|
||||||
Log.d(TAG, "ContextHub[" + i + "] id: " + mContextHubInfo[i].getId()
|
Log.d(TAG, "ContextHub[" + i + "] id: " + mContextHubInfo[i].getId()
|
||||||
@@ -80,9 +79,7 @@ public class ContextHubService extends IContextHubService.Stub {
|
|||||||
@Override
|
@Override
|
||||||
public int registerCallback(IContextHubCallback callback) throws RemoteException {
|
public int registerCallback(IContextHubCallback callback) throws RemoteException {
|
||||||
checkPermissions();
|
checkPermissions();
|
||||||
synchronized (this) {
|
mCallbacksList.register(callback);
|
||||||
mCallback = callback;
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,26 +234,26 @@ public class ContextHubService extends IContextHubService.Stub {
|
|||||||
if (header == null || data == null || header.length < MSG_HEADER_SIZE) {
|
if (header == null || data == null || header.length < MSG_HEADER_SIZE) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
int callbacksCount = mCallbacksList.beginBroadcast();
|
||||||
synchronized (this) {
|
if (callbacksCount < 1) {
|
||||||
if (mCallback != null) {
|
Log.v(TAG, "No message callbacks registered.");
|
||||||
ContextHubMessage msg = new ContextHubMessage(header[MSG_FIELD_TYPE],
|
return 0;
|
||||||
header[MSG_FIELD_VERSION],
|
}
|
||||||
data);
|
ContextHubMessage message =
|
||||||
|
new ContextHubMessage(header[MSG_FIELD_TYPE], header[MSG_FIELD_VERSION], data);
|
||||||
try {
|
for (int i = 0; i < callbacksCount; ++i) {
|
||||||
mCallback.onMessageReceipt(header[MSG_FIELD_HUB_HANDLE],
|
IContextHubCallback callback = mCallbacksList.getBroadcastItem(i);
|
||||||
header[MSG_FIELD_APP_INSTANCE],
|
try {
|
||||||
msg);
|
callback.onMessageReceipt(
|
||||||
} catch (Exception e) {
|
header[MSG_FIELD_HUB_HANDLE],
|
||||||
Log.w(TAG, "Exception " + e + " when calling remote callback");
|
header[MSG_FIELD_APP_INSTANCE],
|
||||||
return -1;
|
message);
|
||||||
}
|
} catch (RemoteException e) {
|
||||||
} else {
|
Log.i(TAG, "Exception (" + e + ") calling remote callback (" + callback + ").");
|
||||||
Log.d(TAG, "Message Callback is NULL");
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
mCallbacksList.finishBroadcast();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,10 +17,14 @@
|
|||||||
package android.hardware.location;
|
package android.hardware.location;
|
||||||
|
|
||||||
|
|
||||||
|
import android.annotation.NonNull;
|
||||||
|
import android.annotation.Nullable;
|
||||||
import android.annotation.SystemApi;
|
import android.annotation.SystemApi;
|
||||||
import android.os.Parcel;
|
import android.os.Parcel;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
|
|
||||||
|
import libcore.util.EmptyArray;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
@@ -43,6 +47,8 @@ public class NanoAppInstanceInfo {
|
|||||||
private int mHandle;
|
private int mHandle;
|
||||||
|
|
||||||
public NanoAppInstanceInfo() {
|
public NanoAppInstanceInfo() {
|
||||||
|
mNeededSensors = EmptyArray.INT;
|
||||||
|
mOutputEvents = EmptyArray.INT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -193,6 +199,7 @@ public class NanoAppInstanceInfo {
|
|||||||
*
|
*
|
||||||
* @return int[] all the required sensors needed by this app
|
* @return int[] all the required sensors needed by this app
|
||||||
*/
|
*/
|
||||||
|
@NonNull
|
||||||
public int[] getNeededSensors() {
|
public int[] getNeededSensors() {
|
||||||
return mNeededSensors;
|
return mNeededSensors;
|
||||||
}
|
}
|
||||||
@@ -204,8 +211,8 @@ public class NanoAppInstanceInfo {
|
|||||||
*
|
*
|
||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
public void setNeededSensors(int[] neededSensors) {
|
public void setNeededSensors(@Nullable int[] neededSensors) {
|
||||||
mNeededSensors = neededSensors;
|
mNeededSensors = neededSensors != null ? neededSensors : EmptyArray.INT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -213,6 +220,7 @@ public class NanoAppInstanceInfo {
|
|||||||
*
|
*
|
||||||
* @return all the events that can be generated by this app
|
* @return all the events that can be generated by this app
|
||||||
*/
|
*/
|
||||||
|
@NonNull
|
||||||
public int[] getOutputEvents() {
|
public int[] getOutputEvents() {
|
||||||
return mOutputEvents;
|
return mOutputEvents;
|
||||||
}
|
}
|
||||||
@@ -225,8 +233,8 @@ public class NanoAppInstanceInfo {
|
|||||||
*
|
*
|
||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
public void setOutputEvents(int[] outputEvents) {
|
public void setOutputEvents(@Nullable int[] outputEvents) {
|
||||||
mOutputEvents = outputEvents;
|
mOutputEvents = outputEvents != null ? outputEvents : EmptyArray.INT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -280,12 +288,12 @@ public class NanoAppInstanceInfo {
|
|||||||
mNeededWriteMemBytes = in.readInt();
|
mNeededWriteMemBytes = in.readInt();
|
||||||
mNeededExecMemBytes = in.readInt();
|
mNeededExecMemBytes = in.readInt();
|
||||||
|
|
||||||
int mNeededSensorsLength = in.readInt();
|
int neededSensorsLength = in.readInt();
|
||||||
mNeededSensors = new int[mNeededSensorsLength];
|
mNeededSensors = new int[neededSensorsLength];
|
||||||
in.readIntArray(mNeededSensors);
|
in.readIntArray(mNeededSensors);
|
||||||
|
|
||||||
int mOutputEventsLength = in.readInt();
|
int outputEventsLength = in.readInt();
|
||||||
mOutputEvents = new int[mOutputEventsLength];
|
mOutputEvents = new int[outputEventsLength];
|
||||||
in.readIntArray(mOutputEvents);
|
in.readIntArray(mOutputEvents);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -303,9 +311,9 @@ public class NanoAppInstanceInfo {
|
|||||||
out.writeInt(mNeededWriteMemBytes);
|
out.writeInt(mNeededWriteMemBytes);
|
||||||
out.writeInt(mNeededExecMemBytes);
|
out.writeInt(mNeededExecMemBytes);
|
||||||
|
|
||||||
|
// arrays are never null
|
||||||
out.writeInt(mNeededSensors.length);
|
out.writeInt(mNeededSensors.length);
|
||||||
out.writeIntArray(mNeededSensors);
|
out.writeIntArray(mNeededSensors);
|
||||||
|
|
||||||
out.writeInt(mOutputEvents.length);
|
out.writeInt(mOutputEvents.length);
|
||||||
out.writeIntArray(mOutputEvents);
|
out.writeIntArray(mOutputEvents);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user