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

Commit 8871b31a authored by Alex Salo's avatar Alex Salo Committed by Android (Google) Code Review
Browse files

Merge "Add disableSelf() and more failure codes"

parents 5d15e6e7 b4a61776
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -6307,12 +6307,15 @@ package android.service.attention {
  public abstract class AttentionService extends android.app.Service {
    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);
    field public static final int ATTENTION_FAILURE_PREEMPTED = 2; // 0x2
    field public static final int ATTENTION_FAILURE_TIMED_OUT = 3; // 0x3
    field public static final int ATTENTION_FAILURE_UNKNOWN = 4; // 0x4
    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
    field public static final int ATTENTION_FAILURE_TIMED_OUT = 5; // 0x5
    field public static final int ATTENTION_FAILURE_UNKNOWN = 2; // 0x2
    field public static final int ATTENTION_SUCCESS_ABSENT = 0; // 0x0
    field public static final int ATTENTION_SUCCESS_PRESENT = 1; // 0x1
    field public static final String SERVICE_INTERFACE = "android.service.attention.AttentionService";
+7 −0
Original line number Diff line number Diff line
@@ -50,6 +50,13 @@ public abstract class AttentionManagerInternal {
     */
    public abstract void cancelAttentionCheck(int requestCode);

    /**
     * Disables the dependants.
     *
     * Example: called if the service does not have sufficient permissions to perform the task.
     */
    public abstract void disableSelf();

    /** Internal interface for attention callback. */
    public abstract static class AttentionCallbackInternal {
        /**
+28 −7
Original line number Diff line number Diff line
@@ -21,11 +21,13 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.app.Service;
import android.attention.AttentionManagerInternal;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import com.android.internal.util.Preconditions;
import com.android.server.LocalServices;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -65,14 +67,20 @@ public abstract class AttentionService extends Service {
    /** Attention is present. */
    public static final int ATTENTION_SUCCESS_PRESENT = 1;

    /** Unknown reasons for failing to determine the attention. */
    public static final int ATTENTION_FAILURE_UNKNOWN = 2;

    /** Request has been cancelled. */
    public static final int ATTENTION_FAILURE_CANCELLED = 3;

    /** Preempted by other client. */
    public static final int ATTENTION_FAILURE_PREEMPTED = 2;
    public static final int ATTENTION_FAILURE_PREEMPTED = 4;

    /** Request timed out. */
    public static final int ATTENTION_FAILURE_TIMED_OUT = 3;
    public static final int ATTENTION_FAILURE_TIMED_OUT = 5;

    /** Unknown reasons for failing to determine the attention. */
    public static final int ATTENTION_FAILURE_UNKNOWN = 4;
    /** Camera permission is not granted. */
    public static final int ATTENTION_FAILURE_CAMERA_PERMISSION_ABSENT = 6;

    /**
     * Result codes for when attention check was successful.
@@ -90,8 +98,9 @@ public abstract class AttentionService extends Service {
     *
     * @hide
     */
    @IntDef(prefix = {"ATTENTION_FAILURE_"}, value = {ATTENTION_FAILURE_PREEMPTED,
            ATTENTION_FAILURE_TIMED_OUT, ATTENTION_FAILURE_UNKNOWN})
    @IntDef(prefix = {"ATTENTION_FAILURE_"}, value = {ATTENTION_FAILURE_UNKNOWN,
            ATTENTION_FAILURE_CANCELLED, ATTENTION_FAILURE_PREEMPTED, ATTENTION_FAILURE_TIMED_OUT,
            ATTENTION_FAILURE_CAMERA_PERMISSION_ABSENT})
    @Retention(RetentionPolicy.SOURCE)
    public @interface AttentionFailureCodes {
    }
@@ -121,6 +130,19 @@ public abstract class AttentionService extends Service {
        return null;
    }

    /**
     * Disables the dependants.
     *
     * Example: called if the service does not have sufficient permissions to perform the task.
     */
    public final void disableSelf() {
        AttentionManagerInternal attentionManager = LocalServices.getService(
                AttentionManagerInternal.class);
        if (attentionManager != null) {
            attentionManager.disableSelf();
        }
    }

    /**
     * Checks the user attention and calls into the provided callback.
     *
@@ -132,7 +154,6 @@ public abstract class AttentionService extends Service {
    /** Cancels the attention check for a given request code. */
    public abstract void onCancelAttentionCheck(int requestCode);


    /** Callbacks for AttentionService results. */
    public static final class AttentionCallback {
        private final IAttentionCallback mCallback;
+17 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.server.attention;

import static android.provider.DeviceConfig.NAMESPACE_ATTENTION_MANAGER_SERVICE;
import static android.provider.Settings.System.ADAPTIVE_SLEEP;

import android.Manifest;
import android.annotation.NonNull;
@@ -45,6 +46,7 @@ import android.os.RemoteException;
import android.os.SystemClock;
import android.os.UserHandle;
import android.provider.DeviceConfig;
import android.provider.Settings;
import android.service.attention.AttentionService;
import android.service.attention.AttentionService.AttentionFailureCodes;
import android.service.attention.IAttentionCallback;
@@ -253,6 +255,16 @@ public class AttentionManagerService extends SystemService {
        }
    }

    /** Disables service dependants. */
    private void disableSelf() {
        final long identity = Binder.clearCallingIdentity();
        try {
            Settings.System.putInt(mContext.getContentResolver(), ADAPTIVE_SLEEP, 0);
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
    }

    @GuardedBy("mLock")
    private void freeIfInactiveLocked() {
        // If we are called here, it means someone used the API again - reset the timer then.
@@ -384,6 +396,11 @@ public class AttentionManagerService extends SystemService {
        public void cancelAttentionCheck(int requestCode) {
            AttentionManagerService.this.cancelAttentionCheck(requestCode);
        }

        @Override
        public void disableSelf() {
            AttentionManagerService.this.disableSelf();
        }
    }

    private static final class AttentionCheckCache {