OSDN Git Service

Add disableSelf() and more failure codes
authorAlex Salo <asalo@google.com>
Tue, 19 Mar 2019 23:18:31 +0000 (16:18 -0700)
committerAlex Salo <asalo@google.com>
Thu, 21 Mar 2019 21:43:51 +0000 (14:43 -0700)
disableSelf() allows the implementer of the service to notify the system
about the intent to disable self, e.g. in case it does not have sufficient
permissions to perform the task.

New failure codes are introduced to communicate the absense of the
camera permission and a cancellation event.

Bug: 128921381111939367
Test: manually
Change-Id: Id9a2a8e8d51d422e966a5858c85f8a73ca37ded7

api/system-current.txt
core/java/android/attention/AttentionManagerInternal.java
core/java/android/service/attention/AttentionService.java
services/core/java/com/android/server/attention/AttentionManagerService.java

index 3a9d3da..703e12c 100644 (file)
@@ -6322,12 +6322,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";
index 6b7f10e..ac19504 100644 (file)
@@ -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 {
         /**
index 32f4ea9..84f440f 100644 (file)
@@ -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 {
     }
@@ -122,6 +131,19 @@ public abstract class AttentionService extends Service {
     }
 
     /**
+     * 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.
      *
      * @param requestCode an identifier that could be used to cancel the request
@@ -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;
index 7da848c..7b4ac6b 100644 (file)
@@ -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.Nullable;
@@ -43,6 +44,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;
@@ -239,6 +241,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.
@@ -370,6 +382,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 {