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

Commit e35a6deb authored by Arthur Ishiguro's avatar Arthur Ishiguro
Browse files

Replace Handler with Executor in new APIs

To conform with new API guideline L1 in go/android-api-guidelines.

Bug: 67734082
Test: make
Change-Id: Ib18e778368dc95c69cdec4faed6dd34cbabdf1a4
parent d7dd779e
Loading
Loading
Loading
Loading
+18 −16
Original line number Diff line number Diff line
@@ -15,7 +15,8 @@
 */
package android.hardware.location;

import android.annotation.Nullable;
import android.annotation.CallbackExecutor;
import android.annotation.NonNull;
import android.annotation.RequiresPermission;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
@@ -29,6 +30,7 @@ import android.os.ServiceManager.ServiceNotFoundException;
import android.util.Log;

import java.util.List;
import java.util.concurrent.Executor;

/**
 * A class that exposes the Context hubs on a device to applications.
@@ -513,46 +515,46 @@ public final class ContextHubManager {
     * 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
     * @param executor the executor to invoke callbacks for this client
     *
     * @return the callback interface
     */
    private IContextHubClientCallback createClientCallback(
            ContextHubClientCallback callback, Handler handler) {
            ContextHubClientCallback callback, Executor executor) {
        return new IContextHubClientCallback.Stub() {
            @Override
            public void onMessageFromNanoApp(NanoAppMessage message) {
                handler.post(() -> callback.onMessageFromNanoApp(message));
                executor.execute(() -> callback.onMessageFromNanoApp(message));
            }

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

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

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

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

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

            @Override
            public void onNanoAppDisabled(long nanoAppId) {
                handler.post(() -> callback.onNanoAppDisabled(nanoAppId));
                executor.execute(() -> callback.onNanoAppDisabled(nanoAppId));
            }
        };
    }
@@ -564,9 +566,9 @@ public final class ContextHubManager {
     * registration succeeds, the client can send messages to nanoapps through the returned
     * {@link ContextHubClient} object, and receive notifications through the provided callback.
     *
     * @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
     * @param callback the notification callback to register
     * @param executor the executor to invoke the callback
     * @return the registered client object
     *
     * @throws IllegalArgumentException if hubInfo does not represent a valid hub
@@ -576,8 +578,9 @@ public final class ContextHubManager {
     * @hide
     * @see ContextHubClientCallback
     */
    public ContextHubClient createClient(
            ContextHubClientCallback callback, ContextHubInfo hubInfo, @Nullable Handler handler) {
    @NonNull public ContextHubClient createClient(
            @NonNull ContextHubInfo hubInfo, @NonNull ContextHubClientCallback callback,
            @NonNull @CallbackExecutor Executor executor) {
        if (callback == null) {
            throw new NullPointerException("Callback cannot be null");
        }
@@ -585,8 +588,7 @@ public final class ContextHubManager {
            throw new NullPointerException("Hub info cannot be null");
        }

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

        IContextHubClient client;
        try {
+22 −32
Original line number Diff line number Diff line
@@ -15,15 +15,16 @@
 */
package android.hardware.location;

import android.annotation.CallbackExecutor;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.os.HandlerExecutor;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@@ -34,7 +35,7 @@ import java.util.concurrent.TimeoutException;
 * through the ContextHubManager APIs. The caller can either retrieve the result
 * synchronously through a blocking call ({@link #waitForResponse(long, TimeUnit)}) or
 * asynchronously through a user-defined callback
 * ({@link #setOnCompleteCallback(ContextHubTransaction.Callback, Handler)}).
 * ({@link #setOnCompleteCallback(Callback, Executor)} )}).
 *
 * @param <T> the type of the contents in the transaction response
 *
@@ -173,9 +174,9 @@ public class ContextHubTransaction<T> {
    private ContextHubTransaction.Response<T> mResponse;

    /*
     * The handler to invoke the aynsc response supplied by onComplete.
     * The executor to invoke the onComplete async callback.
     */
    private Handler mHandler = null;
    private Executor mExecutor = null;

    /*
     * The callback to invoke when the transaction completes.
@@ -262,11 +263,11 @@ public class ContextHubTransaction<T> {
     *
     * This function provides an asynchronous approach to retrieve the result of the
     * transaction. When the transaction response has been provided by the Context Hub,
     * the given callback will be posted by the provided handler.
     * the given callback is invoked.
     *
     * If the transaction has already completed at the time of invocation, the callback
     * will be immediately posted by the handler. If the transaction has been invalidated,
     * the callback will never be invoked.
     * will be immedately invoked. If the transaction has been invalidated, the callback will
     * never be invoked.
     *
     * A transaction can be invalidated if the process owning the transaction is no longer active
     * and the reference to this object is lost.
@@ -275,19 +276,20 @@ public class ContextHubTransaction<T> {
     * invoked once, or an IllegalStateException will be thrown.
     *
     * @param callback the callback to be invoked upon completion
     * @param handler the handler to post the callback
     * @param executor the executor to invoke the callback
     *
     * @throws IllegalStateException if this method is called multiple times
     * @throws NullPointerException if the callback or handler is null
     */
    public void setOnCompleteCallback(
            @NonNull ContextHubTransaction.Callback<T> callback, @NonNull Handler handler) {
            @NonNull ContextHubTransaction.Callback<T> callback,
            @NonNull @CallbackExecutor Executor executor) {
        synchronized (this) {
            if (callback == null) {
                throw new NullPointerException("Callback cannot be null");
            }
            if (handler == null) {
                throw new NullPointerException("Handler cannot be null");
            if (executor == null) {
                throw new NullPointerException("Executor cannot be null");
            }
            if (mCallback != null) {
                throw new IllegalStateException(
@@ -295,16 +297,10 @@ public class ContextHubTransaction<T> {
            }

            mCallback = callback;
            mHandler = handler;
            mExecutor = executor;

            if (mDoneSignal.getCount() == 0) {
                boolean callbackPosted = mHandler.post(() -> {
                    mCallback.onComplete(this, mResponse);
                });

                if (!callbackPosted) {
                    Log.e(TAG, "Failed to post callback to Handler");
                }
                mExecutor.execute(() -> mCallback.onComplete(this, mResponse));
            }
        }
    }
@@ -312,10 +308,10 @@ public class ContextHubTransaction<T> {
    /**
     * Sets a callback to be invoked when the transaction completes.
     *
     * Equivalent to {@link #setOnCompleteCallback(ContextHubTransaction.Callback, Handler)}
     * with the handler being that of the main thread's Looper.
     * Equivalent to {@link #setOnCompleteCallback(ContextHubTransaction.Callback, Executor)}
     * with the executor using the main thread's Looper.
     *
     * This method or {@link #setOnCompleteCallback(ContextHubTransaction.Callback, Handler)}
     * This method or {@link #setOnCompleteCallback(ContextHubTransaction.Callback, Executor)}
     * can only be invoked once, or an IllegalStateException will be thrown.
     *
     * @param callback the callback to be invoked upon completion
@@ -324,7 +320,7 @@ public class ContextHubTransaction<T> {
     * @throws NullPointerException if the callback is null
     */
    public void setOnCompleteCallback(@NonNull ContextHubTransaction.Callback<T> callback) {
        setOnCompleteCallback(callback, new Handler(Looper.getMainLooper()));
        setOnCompleteCallback(callback, new HandlerExecutor(Handler.getMain()));
    }

    /**
@@ -339,7 +335,7 @@ public class ContextHubTransaction<T> {
     * @throws IllegalStateException if this method is invoked multiple times
     * @throws NullPointerException if the response is null
     */
    void setResponse(ContextHubTransaction.Response<T> response) {
    /* package */ void setResponse(ContextHubTransaction.Response<T> response) {
        synchronized (this) {
            if (response == null) {
                throw new NullPointerException("Response cannot be null");
@@ -354,13 +350,7 @@ public class ContextHubTransaction<T> {

            mDoneSignal.countDown();
            if (mCallback != null) {
                boolean callbackPosted = mHandler.post(() -> {
                    mCallback.onComplete(this, mResponse);
                });

                if (!callbackPosted) {
                    Log.e(TAG, "Failed to post callback to Handler");
                }
                mExecutor.execute(() -> mCallback.onComplete(this, mResponse));
            }
        }
    }