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

Commit 893832bd authored by Arthur Ishiguro's avatar Arthur Ishiguro Committed by Android (Google) Code Review
Browse files

Merge changes Ibf8cd200,I4ea2f293,I17b545da,Ia139fd6d,I3a7e69f8

* changes:
  Handles load and unload lifecycle callbacks
  Handles hub reset at ContextHubClientManager
  Converts old API message clients to the new API format
  Implements client close and death notification
  Creates framework for new API messaging
parents 229f0d03 0069f128
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
@@ -219,6 +219,8 @@ LOCAL_SRC_FILES += \
	core/java/android/hardware/location/IGeofenceHardwareCallback.aidl \
	core/java/android/hardware/location/IGeofenceHardwareCallback.aidl \
	core/java/android/hardware/location/IGeofenceHardwareMonitorCallback.aidl \
	core/java/android/hardware/location/IGeofenceHardwareMonitorCallback.aidl \
	core/java/android/hardware/location/IContextHubCallback.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/IContextHubService.aidl \
	core/java/android/hardware/location/IContextHubTransactionCallback.aidl \
	core/java/android/hardware/location/IContextHubTransactionCallback.aidl \
	core/java/android/hardware/radio/IRadioService.aidl \
	core/java/android/hardware/radio/IRadioService.aidl \
+45 −17
Original line number Original line Diff line number Diff line
@@ -16,43 +16,48 @@
package android.hardware.location;
package android.hardware.location;


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

import dalvik.system.CloseGuard;


import java.io.Closeable;
import java.io.Closeable;
import java.util.concurrent.atomic.AtomicBoolean;


/**
/**
 * A class describing a client of the Context Hub Service.
 * A class describing a client of the Context Hub Service.
 *
 *
 * Clients can send messages to nanoapps at a Context Hub through this object.
 * Clients can send messages to nanoapps at a Context Hub through this object. The APIs supported
 * by this object are thread-safe and can be used without external synchronization.
 *
 *
 * @hide
 * @hide
 */
 */
public class ContextHubClient implements 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 mClientProxy;
    // private final IContextHubClient mClientInterface;


    /*
    /*
     * 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.
     * The Context Hub that this client is attached to.
     */
     */
    private ContextHubInfo mAttachedHub;
    private final ContextHubInfo mAttachedHub;


    /*
    private final CloseGuard mCloseGuard = CloseGuard.get();
     * The handler to invoke mCallback.
     */
    private Handler mCallbackHandler;


    ContextHubClient(ContextHubClientCallback callback, Handler handler, ContextHubInfo hubInfo) {
    private final AtomicBoolean mIsClosed = new AtomicBoolean(false);
        mCallback = callback;

        mCallbackHandler = handler;
    /* package */ ContextHubClient(
            IContextHubClient clientProxy, IContextHubClientCallback callback,
            ContextHubInfo hubInfo) {
        mClientProxy = clientProxy;
        mCallbackInterface = callback;
        mAttachedHub = hubInfo;
        mAttachedHub = hubInfo;
        mCloseGuard.open("close");
    }
    }


    /**
    /**
@@ -71,7 +76,14 @@ public class ContextHubClient implements Closeable {
     * All futures messages targeted for this client are dropped at the service.
     * All futures messages targeted for this client are dropped at the service.
     */
     */
    public void close() {
    public void close() {
        throw new UnsupportedOperationException("TODO: Implement this");
        if (!mIsClosed.getAndSet(true)) {
            mCloseGuard.close();
            try {
                mClientProxy.close();
            } catch (RemoteException e) {
                throw e.rethrowFromSystemServer();
            }
        }
    }
    }


    /**
    /**
@@ -90,6 +102,22 @@ public class ContextHubClient implements Closeable {
    @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE)
    @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE)
    @ContextHubTransaction.Result
    @ContextHubTransaction.Result
    public int sendMessageToNanoApp(NanoAppMessage message) {
    public int sendMessageToNanoApp(NanoAppMessage message) {
        throw new UnsupportedOperationException("TODO: Implement this");
        try {
            return mClientProxy.sendMessageToNanoApp(message);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    @Override
    protected void finalize() throws Throwable {
        try {
            if (mCloseGuard != null) {
                mCloseGuard.warnIfOpen();
            }
            close();
        } finally {
            super.finalize();
        }
    }
    }
}
}
+72 −6
Original line number Original line Diff line number Diff line
@@ -455,6 +455,54 @@ public final class ContextHubManager {
        return 0;
        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.
     * 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 callback the notification callback to register
     * @param hubInfo  the hub to attach this client to
     * @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
     * @param handler  the handler to invoke the callback, if null uses the main thread's Looper
     *
     * @return the registered client object
     * @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
     * @hide
     * @see ContextHubClientCallback
     */
     */
    public ContextHubClient createClient(
    public ContextHubClient createClient(
            ContextHubClientCallback callback, ContextHubInfo hubInfo, @Nullable Handler handler) {
            ContextHubClientCallback callback, ContextHubInfo hubInfo, @Nullable Handler handler) {
        throw new UnsupportedOperationException(
        if (callback == null) {
                "TODO: Implement this, and throw an exception on error");
            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);
    }
    }


    /**
    /**
+31 −0
Original line number Original line 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);

    // Closes the connection with the Context Hub
    void close();
}
+49 −0
Original line number Original line 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