Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fdb26b90 authored by Daniel Estrada Alva's avatar Daniel Estrada Alva Committed by Android (Google) Code Review
Browse files

Merge "Add support for multiple callback registration in." into nyc-dev

parents 4a6bd4b4 78cebca7
Loading
Loading
Loading
Loading
+57 −0
Original line number Original line Diff line number Diff line
@@ -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");
            }
            }
+24 −27
Original line number Original line Diff line number Diff line
@@ -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);
        for (int i = 0; i < callbacksCount; ++i) {
            IContextHubCallback callback = mCallbacksList.getBroadcastItem(i);
            try {
            try {
                    mCallback.onMessageReceipt(header[MSG_FIELD_HUB_HANDLE],
                callback.onMessageReceipt(
                        header[MSG_FIELD_HUB_HANDLE],
                        header[MSG_FIELD_APP_INSTANCE],
                        header[MSG_FIELD_APP_INSTANCE],
                                               msg);
                        message);
                } catch (Exception e) {
            } catch (RemoteException e) {
                    Log.w(TAG, "Exception " + e + " when calling remote callback");
                Log.i(TAG, "Exception (" + e + ") calling remote callback (" + callback + ").");
                    return -1;
                continue;
            }
            }
            } else {
                Log.d(TAG, "Message Callback is NULL");
        }
        }
        }
        mCallbacksList.finishBroadcast();

        return 0;
        return 0;
    }
    }


+17 −9
Original line number Original line Diff line number Diff line
@@ -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);
    }
    }