OSDN Git Service

Remove the disableSelf() api
authorAlex Salo <asalo@google.com>
Thu, 11 Apr 2019 21:45:42 +0000 (14:45 -0700)
committerAlex Salo <asalo@google.com>
Mon, 15 Apr 2019 23:02:41 +0000 (16:02 -0700)
No longer needed, the logic will be handled in AttentionDetector which
implements the feature. Settings app makes sure to grey out the setting
if does not have sufficient permissions.

Bug: 130350903130246574
Test: manually confirmed, atest AttentionDetectorTests, make RunSettingsRoboTests
Change-Id: I324223af01b5198e3af0b84d47120f307f3aa71a

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
services/core/java/com/android/server/pm/PackageManagerService.java
services/core/java/com/android/server/power/AttentionDetector.java
services/tests/servicestests/src/com/android/server/power/AttentionDetectorTest.java

index ae27daf..e9f891b 100644 (file)
@@ -6274,7 +6274,6 @@ 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(@NonNull android.service.attention.AttentionService.AttentionCallback);
     method public abstract void onCheckAttention(@NonNull android.service.attention.AttentionService.AttentionCallback);
index fa3d3b8..941e9e2 100644 (file)
@@ -46,13 +46,6 @@ public abstract class AttentionManagerInternal {
      */
     public abstract void cancelAttentionCheck(AttentionCallbackInternal callback);
 
-    /**
-     * 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 6172ce5..49ab5db 100644 (file)
@@ -21,13 +21,11 @@ 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;
@@ -132,19 +130,6 @@ 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 callback the callback to return the result to
index 3dbea0d..411dd79 100644 (file)
@@ -17,7 +17,6 @@
 package com.android.server.attention;
 
 import static android.provider.DeviceConfig.NAMESPACE_ATTENTION_MANAGER_SERVICE;
-import static android.provider.Settings.System.ADAPTIVE_SLEEP;
 import static android.service.attention.AttentionService.ATTENTION_FAILURE_CANCELLED;
 import static android.service.attention.AttentionService.ATTENTION_FAILURE_UNKNOWN;
 
@@ -47,7 +46,6 @@ 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.AttentionService.AttentionSuccessCodes;
@@ -275,19 +273,6 @@ public class AttentionManagerService extends SystemService {
         }
     }
 
-    /** Disables service dependants. */
-    private void disableSelf() {
-        final long identity = Binder.clearCallingIdentity();
-        try {
-            if (DEBUG) {
-                Slog.d(LOG_TAG, "Disabling self.");
-            }
-            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.
@@ -418,11 +403,6 @@ public class AttentionManagerService extends SystemService {
         public void cancelAttentionCheck(AttentionCallbackInternal callbackInternal) {
             AttentionManagerService.this.cancelAttentionCheck(callbackInternal);
         }
-
-        @Override
-        public void disableSelf() {
-            AttentionManagerService.this.disableSelf();
-        }
     }
 
     private static final class AttentionCheckCache {
index 7b418b5..d2b1e8f 100644 (file)
@@ -20752,8 +20752,16 @@ public class PackageManagerService extends IPackageManager.Stub
     }
 
     @Override
-    public String getAttentionServicePackageName() {
-        return mContext.getString(R.string.config_defaultAttentionService);
+    public @Nullable String getAttentionServicePackageName() {
+        final String flattenedComponentName =
+                mContext.getString(R.string.config_defaultAttentionService);
+        if (flattenedComponentName != null) {
+            ComponentName componentName = ComponentName.unflattenFromString(flattenedComponentName);
+            if (componentName != null && componentName.getPackageName() != null) {
+                return componentName.getPackageName();
+            }
+        }
+        return null;
     }
 
     private @Nullable String getDocumenterPackageName() {
index d9d21ba..5e829b2 100644 (file)
 
 package com.android.server.power;
 
+import static android.provider.Settings.System.ADAPTIVE_SLEEP;
+
+import android.Manifest;
 import android.attention.AttentionManagerInternal;
 import android.attention.AttentionManagerInternal.AttentionCallbackInternal;
+import android.content.ContentResolver;
 import android.content.Context;
+import android.content.pm.PackageManager;
 import android.database.ContentObserver;
 import android.os.Handler;
 import android.os.PowerManager;
@@ -83,6 +88,12 @@ public class AttentionDetector {
     @VisibleForTesting
     protected AttentionManagerInternal mAttentionManager;
 
+    @VisibleForTesting
+    protected PackageManager mPackageManager;
+
+    @VisibleForTesting
+    protected ContentResolver mContentResolver;
+
     /**
      * Current wakefulness of the device. {@see PowerManagerInternal}
      */
@@ -137,6 +148,8 @@ public class AttentionDetector {
 
     public void systemReady(Context context) {
         updateEnabledFromSettings(context);
+        mPackageManager = context.getPackageManager();
+        mContentResolver = context.getContentResolver();
         mAttentionManager = LocalServices.getService(AttentionManagerInternal.class);
         mMaximumExtensionMillis = context.getResources().getInteger(
                 com.android.internal.R.integer.config_attentionMaximumExtension);
@@ -162,6 +175,11 @@ public class AttentionDetector {
             return nextScreenDimming;
         }
 
+        if (!serviceHasSufficientPermissions()) {
+            Settings.System.putInt(mContentResolver, ADAPTIVE_SLEEP, 0);
+            return nextScreenDimming;
+        }
+
         final long now = SystemClock.uptimeMillis();
         final long whenToCheck = nextScreenDimming - getAttentionTimeout();
         final long whenToStopExtending = mLastUserActivityTime + mMaximumExtensionMillis;
@@ -263,6 +281,18 @@ public class AttentionDetector {
         return mAttentionManager != null && mAttentionManager.isAttentionServiceSupported();
     }
 
+    /**
+     * Returns {@code true} if the attention service has sufficient permissions, disables the
+     * depending features otherwise.
+     */
+    @VisibleForTesting
+    boolean serviceHasSufficientPermissions() {
+        final String attentionPackage = mPackageManager.getAttentionServicePackageName();
+        return attentionPackage != null && mPackageManager.checkPermission(
+                Manifest.permission.CAMERA, attentionPackage)
+                == PackageManager.PERMISSION_GRANTED;
+    }
+
     public void dump(PrintWriter pw) {
         pw.print("AttentionDetector:");
         pw.print(" mMaximumExtensionMillis=" + mMaximumExtensionMillis);
index 5de41ea..4de00f7 100644 (file)
@@ -29,6 +29,7 @@ import static org.mockito.Mockito.when;
 
 import android.attention.AttentionManagerInternal;
 import android.attention.AttentionManagerInternal.AttentionCallbackInternal;
+import android.content.pm.PackageManager;
 import android.os.PowerManager;
 import android.os.PowerManagerInternal;
 import android.os.SystemClock;
@@ -49,6 +50,8 @@ import org.mockito.MockitoAnnotations;
 public class AttentionDetectorTest extends AndroidTestCase {
 
     @Mock
+    private PackageManager mPackageManager;
+    @Mock
     private AttentionManagerInternal mAttentionManagerInternal;
     @Mock
     private Runnable mOnUserAttention;
@@ -60,6 +63,9 @@ public class AttentionDetectorTest extends AndroidTestCase {
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
+        when(mPackageManager.getAttentionServicePackageName()).thenReturn("com.google.android.as");
+        when(mPackageManager.checkPermission(any(), any())).thenReturn(
+                PackageManager.PERMISSION_GRANTED);
         when(mAttentionManagerInternal.checkAttention(anyLong(), any()))
                 .thenReturn(true);
         mAttentionDetector = new TestableAttentionDetector();
@@ -108,6 +114,27 @@ public class AttentionDetectorTest extends AndroidTestCase {
     }
 
     @Test
+    public void testOnUserActivity_doesntCheckIfNotSufficientPermissions() {
+        when(mPackageManager.checkPermission(any(), any())).thenReturn(
+                PackageManager.PERMISSION_DENIED);
+
+        long when = registerAttention();
+        verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
+        assertThat(mNextDimming).isEqualTo(when);
+    }
+
+    @Test
+    public void testOnUserActivity_disablesSettingIfNotSufficientPermissions() {
+        when(mPackageManager.checkPermission(any(), any())).thenReturn(
+                PackageManager.PERMISSION_DENIED);
+
+        registerAttention();
+        boolean enabled = Settings.System.getIntForUser(getContext().getContentResolver(),
+                Settings.System.ADAPTIVE_SLEEP, 0, UserHandle.USER_CURRENT) == 1;
+        assertFalse(enabled);
+    }
+
+    @Test
     public void testOnUserActivity_doesntCrashIfNoAttentionService() {
         mAttentionManagerInternal = null;
         registerAttention();
@@ -211,6 +238,8 @@ public class AttentionDetectorTest extends AndroidTestCase {
         TestableAttentionDetector() {
             super(AttentionDetectorTest.this.mOnUserAttention, new Object());
             mAttentionManager = mAttentionManagerInternal;
+            mPackageManager = AttentionDetectorTest.this.mPackageManager;
+            mContentResolver = getContext().getContentResolver();
             mMaximumExtensionMillis = 10000L;
         }