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

Commit eeba0233 authored by Alex Salo's avatar Alex Salo
Browse files

Simplify the AttentionService API

Remove the request code as the callback identifies itself

Bug: 128134941
Test: manully verified the lifecycle
Change-Id: I1f92a6bcd48f9780072ed444cc73ab8a4d2aad77
parent 6fbea174
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -6309,8 +6309,8 @@ package android.service.attention {
    ctor public AttentionService();
    method public final void disableSelf();
    method @Nullable public final android.os.IBinder onBind(@NonNull android.content.Intent);
    method public abstract void onCancelAttentionCheck(int);
    method public abstract void onCheckAttention(int, @NonNull android.service.attention.AttentionService.AttentionCallback);
    method public abstract void onCancelAttentionCheck(@NonNull android.service.attention.AttentionService.AttentionCallback);
    method public abstract void onCheckAttention(@NonNull android.service.attention.AttentionService.AttentionCallback);
    field public static final int ATTENTION_FAILURE_CAMERA_PERMISSION_ABSENT = 6; // 0x6
    field public static final int ATTENTION_FAILURE_CANCELLED = 3; // 0x3
    field public static final int ATTENTION_FAILURE_PREEMPTED = 4; // 0x4
@@ -6322,8 +6322,8 @@ package android.service.attention {
  }
  public static final class AttentionService.AttentionCallback {
    method public void onFailure(int, int);
    method public void onSuccess(int, int, long);
    method public void onFailure(int);
    method public void onSuccess(int, long);
  }
}
+6 −12
Original line number Diff line number Diff line
@@ -30,25 +30,21 @@ public abstract class AttentionManagerInternal {
    /**
     * Checks whether user attention is at the screen and calls in the provided callback.
     *
     * @param requestCode   a code associated with the attention check request; this code would be
     *                      used to call back in {@link AttentionCallbackInternal#onSuccess} and
     *                      {@link AttentionCallbackInternal#onFailure}
     * @param timeoutMillis a budget for the attention check; if it takes longer - {@link
     *                      AttentionCallbackInternal#onFailure} would be called with the {@link
     *                      android.service.attention.AttentionService#ATTENTION_FAILURE_TIMED_OUT}
     *                      code
     * @param callback      a callback for when the attention check has completed
     * @return {@code true} if the attention check should succeed; {@false} otherwise.
     * @return {@code true} if the attention check should succeed.
     */
    public abstract boolean checkAttention(int requestCode,
            long timeoutMillis, AttentionCallbackInternal callback);
    public abstract boolean checkAttention(long timeoutMillis, AttentionCallbackInternal callback);

    /**
     * Cancels the specified attention check in case it's no longer needed.
     *
     * @param requestCode a code provided during {@link #checkAttention}
     * @param callback a callback that was used in {@link #checkAttention}
     */
    public abstract void cancelAttentionCheck(int requestCode);
    public abstract void cancelAttentionCheck(AttentionCallbackInternal callback);

    /**
     * Disables the dependants.
@@ -62,19 +58,17 @@ public abstract class AttentionManagerInternal {
        /**
         * Provides the result of the attention check, if the check was successful.
         *
         * @param requestCode a code provided in {@link #checkAttention}
         * @param result      an int with the result of the check
         * @param timestamp   a {@code SystemClock.uptimeMillis()} timestamp associated with the
         *                    attention check
         */
        public abstract void onSuccess(int requestCode, int result, long timestamp);
        public abstract void onSuccess(int result, long timestamp);

        /**
         * Provides the explanation for why the attention check had failed.
         *
         * @param requestCode a code provided in {@link #checkAttention}
         * @param error       an int with the reason for failure
         */
        public abstract void onFailure(int requestCode, int error);
        public abstract void onFailure(int error);
    }
}
+27 −18
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ import java.lang.annotation.RetentionPolicy;
 * The system's default AttentionService implementation is configured in
 * {@code config_AttentionComponent}. If this config has no value, a stub is returned.
 *
 * See: {@link AttentionManagerService}.
 * See: {@link com.android.server.attention.AttentionManagerService}.
 *
 * <pre>
 * {@literal
@@ -109,15 +109,16 @@ public abstract class AttentionService extends Service {

        /** {@inheritDoc} */
        @Override
        public void checkAttention(int requestCode, IAttentionCallback callback) {
        public void checkAttention(IAttentionCallback callback) {
            Preconditions.checkNotNull(callback);
            AttentionService.this.onCheckAttention(requestCode, new AttentionCallback(callback));
            AttentionService.this.onCheckAttention(new AttentionCallback(callback));
        }

        /** {@inheritDoc} */
        @Override
        public void cancelAttentionCheck(int requestCode) {
            AttentionService.this.onCancelAttentionCheck(requestCode);
        public void cancelAttentionCheck(IAttentionCallback callback) {
            Preconditions.checkNotNull(callback);
            AttentionService.this.onCancelAttentionCheck(new AttentionCallback(callback));
        }
    };

@@ -146,35 +147,43 @@ public abstract class AttentionService extends Service {
    /**
     * Checks the user attention and calls into the provided callback.
     *
     * @param requestCode an identifier that could be used to cancel the request
     * @param callback the callback to return the result to
     */
    public abstract void onCheckAttention(int requestCode, @NonNull AttentionCallback callback);
    public abstract void onCheckAttention(@NonNull AttentionCallback callback);

    /** Cancels the attention check for a given request code. */
    public abstract void onCancelAttentionCheck(int requestCode);
    /**
     * Cancels pending work for a given callback.
     *
     * Implementation must call back with a failure code of {@link #ATTENTION_FAILURE_CANCELLED}.
     */
    public abstract void onCancelAttentionCheck(@NonNull AttentionCallback callback);

    /** Callbacks for AttentionService results. */
    public static final class AttentionCallback {
        private final IAttentionCallback mCallback;
        @NonNull private final IAttentionCallback mCallback;

        private AttentionCallback(IAttentionCallback callback) {
        private AttentionCallback(@NonNull IAttentionCallback callback) {
            mCallback = callback;
        }

        /** Returns the result. */
        public void onSuccess(int requestCode, @AttentionSuccessCodes int result, long timestamp) {
        /**
         * Signals a success and provides the result code.
         *
         * @param timestamp of when the attention signal was computed; system throttles the requests
         *                  so this is useful to know how fresh the result is.
         */
        public void onSuccess(@AttentionSuccessCodes int result, long timestamp) {
            try {
                mCallback.onSuccess(requestCode, result, timestamp);
                mCallback.onSuccess(result, timestamp);
            } catch (RemoteException e) {
                e.rethrowFromSystemServer();
            }
        }

        /** Signals a failure. */
        public void onFailure(int requestCode, @AttentionFailureCodes int error) {
        /** Signals a failure and provides the error code. */
        public void onFailure(@AttentionFailureCodes int error) {
            try {
                mCallback.onFailure(requestCode, error);
                mCallback.onFailure(error);
            } catch (RemoteException e) {
                e.rethrowFromSystemServer();
            }
+2 −2
Original line number Diff line number Diff line
@@ -22,6 +22,6 @@ package android.service.attention;
 * @hide
 */
oneway interface IAttentionCallback {
    void onSuccess(int requestCode, int result, long timestamp);
    void onFailure(int requestCode, int error);
    void onSuccess(int result, long timestamp);
    void onFailure(int error);
}
+2 −2
Original line number Diff line number Diff line
@@ -24,6 +24,6 @@ import android.service.attention.IAttentionCallback;
 * @hide
 */
oneway interface IAttentionService {
    void checkAttention(int requestCode, IAttentionCallback callback);
    void cancelAttentionCheck(int requestCode);
    void checkAttention(IAttentionCallback callback);
    void cancelAttentionCheck(IAttentionCallback callback);
}
 No newline at end of file
Loading