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

Commit a80162db authored by Lei Ju's avatar Lei Ju Committed by Android (Google) Code Review
Browse files

Merge "Migrate wakelock logic."

parents 0f6ee7b9 42fd7de6
Loading
Loading
Loading
Loading
+37 −26
Original line number Original line Diff line number Diff line
@@ -31,8 +31,8 @@ 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.
 * <p>
 *
 * Clients can send messages to nanoapps at a Context Hub through this object. The APIs supported
 * <p>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.
 * by this object are thread-safe and can be used without external synchronization.
 *
 *
 * @hide
 * @hide
@@ -75,8 +75,8 @@ public class ContextHubClient implements Closeable {
    }
    }


    /**
    /**
     * Sets the proxy interface of the client at the service. This method should always be called
     * Sets the proxy interface of the client at the service. This method should always be called by
     * by the ContextHubManager after the client is registered at the service, and should only be
     * the ContextHubManager after the client is registered at the service, and should only be
     * called once.
     * called once.
     *
     *
     * @param clientProxy the proxy of the client at the service
     * @param clientProxy the proxy of the client at the service
@@ -89,7 +89,7 @@ public class ContextHubClient implements Closeable {


        mClientProxy = clientProxy;
        mClientProxy = clientProxy;
        try {
        try {
            mId = Integer.valueOf(mClientProxy.getId());
            mId = mClientProxy.getId();
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
            throw e.rethrowFromSystemServer();
        }
        }
@@ -108,16 +108,16 @@ public class ContextHubClient implements Closeable {
    /**
    /**
     * Returns the system-wide unique identifier for this ContextHubClient.
     * Returns the system-wide unique identifier for this ContextHubClient.
     *
     *
     * This value can be used as an identifier for the messaging channel between a
     * <p>This value can be used as an identifier for the messaging channel between a
     * ContextHubClient and the Context Hub. This may be used as a routing mechanism
     * ContextHubClient and the Context Hub. This may be used as a routing mechanism between various
     * between various ContextHubClient objects within an application.
     * ContextHubClient objects within an application.
     * <p>
     *
     * The value returned by this method will remain the same if it is associated with
     * <p>The value returned by this method will remain the same if it is associated with the same
     * the same client reference at the ContextHubService (for instance, the ID of a
     * client reference at the ContextHubService (for instance, the ID of a PendingIntent
     * PendingIntent ContextHubClient will remain the same even if the local object
     * ContextHubClient will remain the same even if the local object has been regenerated with the
     * has been regenerated with the equivalent PendingIntent). If the ContextHubClient
     * equivalent PendingIntent). If the ContextHubClient is newly generated (e.g. any regeneration
     * is newly generated (e.g. any regeneration of a callback client, or generation
     * of a callback client, or generation of a non-equal PendingIntent client), the ID will not be
     * of a non-equal PendingIntent client), the ID will not be the same.
     * the same.
     *
     *
     * @return The ID of this ContextHubClient, in the range [0, 65535].
     * @return The ID of this ContextHubClient, in the range [0, 65535].
     */
     */
@@ -132,13 +132,13 @@ public class ContextHubClient implements Closeable {
    /**
    /**
     * Closes the connection for this client and the Context Hub Service.
     * Closes the connection for this client and the Context Hub Service.
     *
     *
     * When this function is invoked, the messaging associated with this client is invalidated.
     * <p>When this function is invoked, the messaging associated with this client is invalidated.
     * All futures messages targeted for this client are dropped at the service, and the
     * All futures messages targeted for this client are dropped at the service, and the
     * ContextHubClient is unregistered from the service.
     * ContextHubClient is unregistered from the service.
     * <p>
     *
     * If this object has a PendingIntent, i.e. the object was generated via
     * <p>If this object has a PendingIntent, i.e. the object was generated via {@link
     * {@link ContextHubManager.createClient(PendingIntent, ContextHubInfo, long)}, then the
     * ContextHubManager#createClient(ContextHubInfo, PendingIntent, long)}, then the Intent events
     * Intent events corresponding to the PendingIntent will no longer be triggered.
     * corresponding to the PendingIntent will no longer be triggered.
     */
     */
    public void close() {
    public void close() {
        if (!mIsClosed.getAndSet(true)) {
        if (!mIsClosed.getAndSet(true)) {
@@ -174,13 +174,10 @@ public class ContextHubClient implements Closeable {
     *    have sent it a message.
     *    have sent it a message.
     *
     *
     * @param message the message object to send
     * @param message the message object to send
     *
     * @return the result of sending the message defined as in ContextHubTransaction.Result
     * @return the result of sending the message defined as in ContextHubTransaction.Result
     *
     * @throws NullPointerException if NanoAppMessage is null
     * @throws NullPointerException if NanoAppMessage is null
     * @throws SecurityException if this client doesn't have permissions to send a message to the
     * @throws SecurityException if this client doesn't have permissions to send a message to the
     *     nanoapp.
     *     nanoapp.
     *
     * @see NanoAppMessage
     * @see NanoAppMessage
     * @see ContextHubTransaction.Result
     * @see ContextHubTransaction.Result
     */
     */
@@ -192,8 +189,13 @@ public class ContextHubClient implements Closeable {
        int maxPayloadBytes = mAttachedHub.getMaxPacketLengthBytes();
        int maxPayloadBytes = mAttachedHub.getMaxPacketLengthBytes();
        byte[] payload = message.getMessageBody();
        byte[] payload = message.getMessageBody();
        if (payload != null && payload.length > maxPayloadBytes) {
        if (payload != null && payload.length > maxPayloadBytes) {
            Log.e(TAG, "Message (" + payload.length + " bytes) exceeds max payload length ("
            Log.e(
                    + maxPayloadBytes + " bytes)");
                    TAG,
                    "Message ("
                            + payload.length
                            + " bytes) exceeds max payload length ("
                            + maxPayloadBytes
                            + " bytes)");
            return ContextHubTransaction.RESULT_FAILED_BAD_PARAMS;
            return ContextHubTransaction.RESULT_FAILED_BAD_PARAMS;
        }
        }


@@ -217,4 +219,13 @@ public class ContextHubClient implements Closeable {
            super.finalize();
            super.finalize();
        }
        }
    }
    }

    /** @hide */
    public void callbackFinished() {
        try {
            mClientProxy.callbackFinished();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }
}
}
+39 −9
Original line number Original line Diff line number Diff line
@@ -726,45 +726,75 @@ public final class ContextHubManager {
        return new IContextHubClientCallback.Stub() {
        return new IContextHubClientCallback.Stub() {
            @Override
            @Override
            public void onMessageFromNanoApp(NanoAppMessage message) {
            public void onMessageFromNanoApp(NanoAppMessage message) {
                executor.execute(() -> callback.onMessageFromNanoApp(client, message));
                executor.execute(
                        () -> {
                            callback.onMessageFromNanoApp(client, message);
                            client.callbackFinished();
                        });
            }
            }


            @Override
            @Override
            public void onHubReset() {
            public void onHubReset() {
                executor.execute(() -> callback.onHubReset(client));
                executor.execute(
                        () -> {
                            callback.onHubReset(client);
                            client.callbackFinished();
                        });
            }
            }


            @Override
            @Override
            public void onNanoAppAborted(long nanoAppId, int abortCode) {
            public void onNanoAppAborted(long nanoAppId, int abortCode) {
                executor.execute(() -> callback.onNanoAppAborted(client, nanoAppId, abortCode));
                executor.execute(
                        () -> {
                            callback.onNanoAppAborted(client, nanoAppId, abortCode);
                            client.callbackFinished();
                        });
            }
            }


            @Override
            @Override
            public void onNanoAppLoaded(long nanoAppId) {
            public void onNanoAppLoaded(long nanoAppId) {
                executor.execute(() -> callback.onNanoAppLoaded(client, nanoAppId));
                executor.execute(
                        () -> {
                            callback.onNanoAppLoaded(client, nanoAppId);
                            client.callbackFinished();
                        });
            }
            }


            @Override
            @Override
            public void onNanoAppUnloaded(long nanoAppId) {
            public void onNanoAppUnloaded(long nanoAppId) {
                executor.execute(() -> callback.onNanoAppUnloaded(client, nanoAppId));
                executor.execute(
                        () -> {
                            callback.onNanoAppUnloaded(client, nanoAppId);
                            client.callbackFinished();
                        });
            }
            }


            @Override
            @Override
            public void onNanoAppEnabled(long nanoAppId) {
            public void onNanoAppEnabled(long nanoAppId) {
                executor.execute(() -> callback.onNanoAppEnabled(client, nanoAppId));
                executor.execute(
                        () -> {
                            callback.onNanoAppEnabled(client, nanoAppId);
                            client.callbackFinished();
                        });
            }
            }


            @Override
            @Override
            public void onNanoAppDisabled(long nanoAppId) {
            public void onNanoAppDisabled(long nanoAppId) {
                executor.execute(() -> callback.onNanoAppDisabled(client, nanoAppId));
                executor.execute(
                        () -> {
                            callback.onNanoAppDisabled(client, nanoAppId);
                            client.callbackFinished();
                        });
            }
            }


            @Override
            @Override
            public void onClientAuthorizationChanged(
            public void onClientAuthorizationChanged(
                    long nanoAppId, @ContextHubManager.AuthorizationState int authorization) {
                    long nanoAppId, @ContextHubManager.AuthorizationState int authorization) {
                executor.execute(
                executor.execute(
                        () -> callback.onClientAuthorizationChanged(
                        () -> {
                                client, nanoAppId, authorization));
                            callback.onClientAuthorizationChanged(client, nanoAppId, authorization);
                            client.callbackFinished();
                        });
            }
            }
        };
        };
    }
    }
+3 −0
Original line number Original line Diff line number Diff line
@@ -32,4 +32,7 @@ interface IContextHubClient {


    // Returns the unique ID for this client.
    // Returns the unique ID for this client.
    int getId();
    int getId();

    // Notify direct-call message callback completed
    void callbackFinished();
}
}
+20 −1
Original line number Original line Diff line number Diff line
@@ -41,6 +41,7 @@ import android.hardware.location.NanoAppMessage;
import android.hardware.location.NanoAppState;
import android.hardware.location.NanoAppState;
import android.os.Binder;
import android.os.Binder;
import android.os.Build;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.IBinder;
import android.os.Looper;
import android.os.Looper;
import android.os.Process;
import android.os.Process;
@@ -104,7 +105,9 @@ import java.util.function.Supplier;
 * @hide
 * @hide
 */
 */
public class ContextHubClientBroker extends IContextHubClient.Stub
public class ContextHubClientBroker extends IContextHubClient.Stub
        implements IBinder.DeathRecipient, AppOpsManager.OnOpChangedListener {
        implements IBinder.DeathRecipient,
                AppOpsManager.OnOpChangedListener,
                PendingIntent.OnFinished {
    private static final String TAG = "ContextHubClientBroker";
    private static final String TAG = "ContextHubClientBroker";


    /**
    /**
@@ -966,4 +969,20 @@ public class ContextHubClientBroker extends IContextHubClient.Stub


        return out;
        return out;
    }
    }

    /** Callback that arrives when direct-call message callback delivery completed */
    @Override
    public void callbackFinished() {
        // TODO(b/202447392): pending implementation.
    }

    @Override
    public void onSendFinished(
            PendingIntent pendingIntent,
            Intent intent,
            int resultCode,
            String resultData,
            Bundle resultExtras) {
        // TODO(b/202447392): pending implementation.
    }
}
}
+28 −3
Original line number Original line Diff line number Diff line
@@ -374,44 +374,69 @@ public class ContextHubService extends IContextHubService.Stub {
     */
     */
    private IContextHubClientCallback createDefaultClientCallback(int contextHubId) {
    private IContextHubClientCallback createDefaultClientCallback(int contextHubId) {
        return new IContextHubClientCallback.Stub() {
        return new IContextHubClientCallback.Stub() {
            private void finishCallback() {
                try {
                    IContextHubClient client = mDefaultClientMap.get(contextHubId);
                    client.callbackFinished();
                } catch (RemoteException e) {
                    Log.e(
                            TAG,
                            "RemoteException while finishing callback for hub (ID = "
                                    + contextHubId
                                    + ")",
                            e);
                }
            }

            @Override
            @Override
            public void onMessageFromNanoApp(NanoAppMessage message) {
            public void onMessageFromNanoApp(NanoAppMessage message) {
                int nanoAppHandle = mNanoAppStateManager.getNanoAppHandle(
                int nanoAppHandle =
                        contextHubId, message.getNanoAppId());
                        mNanoAppStateManager.getNanoAppHandle(contextHubId, message.getNanoAppId());


                onMessageReceiptOldApi(
                onMessageReceiptOldApi(
                        message.getMessageType(), contextHubId, nanoAppHandle,
                        message.getMessageType(),
                        contextHubId,
                        nanoAppHandle,
                        message.getMessageBody());
                        message.getMessageBody());

                finishCallback();
            }
            }


            @Override
            @Override
            public void onHubReset() {
            public void onHubReset() {
                byte[] data = {android.hardware.contexthub.V1_0.TransactionResult.SUCCESS};
                byte[] data = {android.hardware.contexthub.V1_0.TransactionResult.SUCCESS};
                onMessageReceiptOldApi(MSG_HUB_RESET, contextHubId, OS_APP_INSTANCE, data);
                onMessageReceiptOldApi(MSG_HUB_RESET, contextHubId, OS_APP_INSTANCE, data);
                finishCallback();
            }
            }


            @Override
            @Override
            public void onNanoAppAborted(long nanoAppId, int abortCode) {
            public void onNanoAppAborted(long nanoAppId, int abortCode) {
                finishCallback();
            }
            }


            @Override
            @Override
            public void onNanoAppLoaded(long nanoAppId) {
            public void onNanoAppLoaded(long nanoAppId) {
                finishCallback();
            }
            }


            @Override
            @Override
            public void onNanoAppUnloaded(long nanoAppId) {
            public void onNanoAppUnloaded(long nanoAppId) {
                finishCallback();
            }
            }


            @Override
            @Override
            public void onNanoAppEnabled(long nanoAppId) {
            public void onNanoAppEnabled(long nanoAppId) {
                finishCallback();
            }
            }


            @Override
            @Override
            public void onNanoAppDisabled(long nanoAppId) {
            public void onNanoAppDisabled(long nanoAppId) {
                finishCallback();
            }
            }


            @Override
            @Override
            public void onClientAuthorizationChanged(long nanoAppId, int authorization) {
            public void onClientAuthorizationChanged(long nanoAppId, int authorization) {
                finishCallback();
            }
            }
        };
        };
    }
    }