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

Commit 4e39aa1e authored by Arthur Ishiguro's avatar Arthur Ishiguro
Browse files

Creates framework for new API messaging

- Creates IContextHubClientCallback as a proxy to invoke
messaging/life-cycle callbacks for the client.
- Creates IContextHubClient as a proxy to send messages at the client
side.
- Creates a ContextHubClientManager at the service to manage
clients and messaging/lifecycle event callbacks.
- Implements generation of clients at ContextHubManager and sending
messages through ContextHubClient

Bug: 67734082
Test: make from root
Change-Id: I3a7e69f89cd70ea59160a651dcdb729e18027e9f
parent 3fbc4f38
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -219,6 +219,8 @@ LOCAL_SRC_FILES += \
	core/java/android/hardware/location/IGeofenceHardwareCallback.aidl \
	core/java/android/hardware/location/IGeofenceHardwareMonitorCallback.aidl \
	core/java/android/hardware/location/IContextHubCallback.aidl \
	core/java/android/hardware/location/IContextHubClient.aidl \
	core/java/android/hardware/location/IContextHubClientCallback.aidl \
	core/java/android/hardware/location/IContextHubService.aidl \
	core/java/android/hardware/location/IContextHubTransactionCallback.aidl \
	core/java/android/hardware/radio/IRadioService.aidl \
+16 −16
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@
package android.hardware.location;

import android.annotation.RequiresPermission;
import android.os.Handler;
import android.os.RemoteException;

import java.io.Closeable;

@@ -29,29 +29,25 @@ import java.io.Closeable;
 */
public class ContextHubClient implements Closeable {
    /*
     * The ContextHubClient interface associated with this client.
     * The proxy to the client interface at the service.
     */
    // TODO: Implement this interface and associate with ContextHubClient object
    // private final IContextHubClient mClientInterface;
    private final IContextHubClient mClientProxy;

    /*
     * The listening callback associated with this client.
     * The callback interface associated with this client.
     */
    private ContextHubClientCallback mCallback;
    private final IContextHubClientCallback mCallbackInterface;

    /*
     * The Context Hub that this client is attached to.
     */
    private ContextHubInfo mAttachedHub;
    private final ContextHubInfo mAttachedHub;

    /*
     * The handler to invoke mCallback.
     */
    private Handler mCallbackHandler;

    ContextHubClient(ContextHubClientCallback callback, Handler handler, ContextHubInfo hubInfo) {
        mCallback = callback;
        mCallbackHandler = handler;
    /* package */ ContextHubClient(
            IContextHubClient clientProxy, IContextHubClientCallback callback,
            ContextHubInfo hubInfo) {
        mClientProxy = clientProxy;
        mCallbackInterface = callback;
        mAttachedHub = hubInfo;
    }

@@ -90,6 +86,10 @@ public class ContextHubClient implements Closeable {
    @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE)
    @ContextHubTransaction.Result
    public int sendMessageToNanoApp(NanoAppMessage message) {
        throw new UnsupportedOperationException("TODO: Implement this");
        try {
            return mClientProxy.sendMessageToNanoApp(message);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }
}
+72 −6
Original line number Diff line number Diff line
@@ -455,6 +455,54 @@ public final class ContextHubManager {
        return 0;
    }

    /**
     * Creates an interface to the ContextHubClient to send down to the service.
     *
     * @param callback the callback to invoke at the client process
     * @param handler the handler to post callbacks for this client
     *
     * @return the callback interface
     */
    private IContextHubClientCallback createClientCallback(
            ContextHubClientCallback callback, Handler handler) {
        return new IContextHubClientCallback.Stub() {
            @Override
            public void onMessageFromNanoApp(NanoAppMessage message) {
                handler.post(() -> callback.onMessageFromNanoApp(message));
            }

            @Override
            public void onHubReset() {
                handler.post(() -> callback.onHubReset());
            }

            @Override
            public void onNanoAppAborted(long nanoAppId, int abortCode) {
                handler.post(() -> callback.onNanoAppAborted(nanoAppId, abortCode));
            }

            @Override
            public void onNanoAppLoaded(long nanoAppId) {
                handler.post(() -> callback.onNanoAppLoaded(nanoAppId));
            }

            @Override
            public void onNanoAppUnloaded(long nanoAppId) {
                handler.post(() -> callback.onNanoAppUnloaded(nanoAppId));
            }

            @Override
            public void onNanoAppEnabled(long nanoAppId) {
                handler.post(() -> callback.onNanoAppEnabled(nanoAppId));
            }

            @Override
            public void onNanoAppDisabled(long nanoAppId) {
                handler.post(() -> callback.onNanoAppDisabled(nanoAppId));
            }
        };
    }

    /**
     * Creates and registers a client and its callback with the Context Hub Service.
     *
@@ -465,17 +513,35 @@ public final class ContextHubManager {
     * @param callback the notification callback to register
     * @param hubInfo  the hub to attach this client to
     * @param handler  the handler to invoke the callback, if null uses the main thread's Looper
     *
     * @return the registered client object
     *
     * @see ContextHubClientCallback
     * @throws IllegalArgumentException if hubInfo does not represent a valid hub
     * @throws IllegalStateException    if there were too many registered clients at the service
     * @throws NullPointerException     if callback or hubInfo is null
     *
     * @hide
     * @see ContextHubClientCallback
     */
    public ContextHubClient createClient(
            ContextHubClientCallback callback, ContextHubInfo hubInfo, @Nullable Handler handler) {
        throw new UnsupportedOperationException(
                "TODO: Implement this, and throw an exception on error");
        if (callback == null) {
            throw new NullPointerException("Callback cannot be null");
        }
        if (hubInfo == null) {
            throw new NullPointerException("Hub info cannot be null");
        }

        Handler realHandler = (handler == null) ? new Handler(mMainLooper) : handler;
        IContextHubClientCallback clientInterface = createClientCallback(callback, realHandler);

        IContextHubClient client;
        try {
            client = mService.createClient(clientInterface, hubInfo.getId());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }

        return new ContextHubClient(client, clientInterface, hubInfo);
    }

    /**
+28 −0
Original line number Diff line number Diff line
/*
 * Copyright 2017 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.hardware.location;

import android.hardware.location.NanoAppMessage;

/**
 * @hide
 */
interface IContextHubClient {

    // Sends a message to a nanoapp
    int sendMessageToNanoApp(in NanoAppMessage message);
}
+49 −0
Original line number Diff line number Diff line
/*
 * Copyright 2017 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.hardware.location;

import android.hardware.location.NanoAppMessage;

/**
 * An interface used by the Context Hub Service to invoke callbacks for lifecycle notifications of a
 * Context Hub and nanoapps, as well as for nanoapp messaging.
 *
 * @hide
 */
oneway interface IContextHubClientCallback {

    // Callback invoked when receiving a message from a nanoapp.
    void onMessageFromNanoApp(in NanoAppMessage message);

    // Callback invoked when the attached Context Hub has reset.
    void onHubReset();

    // Callback invoked when a nanoapp aborts at the attached Context Hub.
    void onNanoAppAborted(long nanoAppId, int abortCode);

    // Callback invoked when a nanoapp is loaded at the attached Context Hub.
    void onNanoAppLoaded(long nanoAppId);

    // Callback invoked when a nanoapp is unloaded from the attached Context Hub.
    void onNanoAppUnloaded(long nanoAppId);

    // Callback invoked when a nanoapp is enabled at the attached Context Hub.
    void onNanoAppEnabled(long nanoAppId);

    // Callback invoked when a nanoapp is disabled at the attached Context Hub.
    void onNanoAppDisabled(long nanoAppId);
}
Loading