Loading services/core/java/com/android/server/attention/AttentionManagerService.java +33 −8 Original line number Diff line number Diff line Loading @@ -77,21 +77,28 @@ public class AttentionManagerService extends SystemService { private static final String LOG_TAG = "AttentionManagerService"; private static final boolean DEBUG = false; /** Service will unbind if connection is not used for that amount of time. */ private static final long CONNECTION_TTL_MILLIS = 60_000; /** DeviceConfig flag name, if {@code true}, enables AttentionManagerService features. */ private static final String KEY_SERVICE_ENABLED = "service_enabled"; /** Default value in absence of {@link DeviceConfig} override. */ private static final boolean DEFAULT_SERVICE_ENABLED = true; /** Service will unbind if connection is not used for that amount of time. */ private static final long CONNECTION_TTL_MILLIS = 60_000; /** * DeviceConfig flag name, describes how much time we consider a result fresh; if the check * attention called within that period - cached value will be returned. */ @VisibleForTesting static final String KEY_STALE_AFTER_MILLIS = "stale_after_millis"; /** If the check attention called within that period - cached value will be returned. */ private static final long STALE_AFTER_MILLIS = 5_000; /** Default value in absence of {@link DeviceConfig} override. */ @VisibleForTesting static final long DEFAULT_STALE_AFTER_MILLIS = 1_000; /** The size of the buffer that stores recent attention check results. */ @VisibleForTesting protected static final int ATTENTION_CACHE_BUFFER_SIZE = 5; /** DeviceConfig flag name, if {@code true}, enables AttentionManagerService features. */ private static final String SERVICE_ENABLED = "service_enabled"; private static String sTestAttentionServicePackage; private final Context mContext; private final PowerManager mPowerManager; Loading Loading @@ -160,10 +167,28 @@ public class AttentionManagerService extends SystemService { @VisibleForTesting protected boolean isServiceEnabled() { return DeviceConfig.getBoolean(NAMESPACE_ATTENTION_MANAGER_SERVICE, SERVICE_ENABLED, return DeviceConfig.getBoolean(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_SERVICE_ENABLED, DEFAULT_SERVICE_ENABLED); } /** * How much time we consider a result fresh; if the check attention called within that period - * cached value will be returned. */ @VisibleForTesting protected long getStaleAfterMillis() { final long millis = DeviceConfig.getLong(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_STALE_AFTER_MILLIS, DEFAULT_STALE_AFTER_MILLIS); if (millis < 0 || millis > 10_000) { Slog.w(LOG_TAG, "Bad flag value supplied for: " + KEY_STALE_AFTER_MILLIS); return DEFAULT_STALE_AFTER_MILLIS; } return millis; } /** * Checks whether user attention is at the screen and calls in the provided callback. * Loading Loading @@ -199,7 +224,7 @@ public class AttentionManagerService extends SystemService { // throttle frequent requests final AttentionCheckCache cache = userState.mAttentionCheckCacheBuffer == null ? null : userState.mAttentionCheckCacheBuffer.getLast(); if (cache != null && now < cache.mLastComputed + STALE_AFTER_MILLIS) { if (cache != null && now < cache.mLastComputed + getStaleAfterMillis()) { callbackInternal.onSuccess(cache.mResult, cache.mTimestamp); return true; } Loading services/tests/servicestests/src/com/android/server/attention/AttentionManagerServiceTest.java +44 −0 Original line number Diff line number Diff line Loading @@ -16,7 +16,11 @@ package com.android.server.attention; import static android.provider.DeviceConfig.NAMESPACE_ATTENTION_MANAGER_SERVICE; import static com.android.server.attention.AttentionManagerService.ATTENTION_CACHE_BUFFER_SIZE; import static com.android.server.attention.AttentionManagerService.DEFAULT_STALE_AFTER_MILLIS; import static com.android.server.attention.AttentionManagerService.KEY_STALE_AFTER_MILLIS; import static com.google.common.truth.Truth.assertThat; Loading @@ -35,6 +39,7 @@ import android.os.IBinder; import android.os.IPowerManager; import android.os.PowerManager; import android.os.RemoteException; import android.provider.DeviceConfig; import android.service.attention.IAttentionCallback; import android.service.attention.IAttentionService; Loading Loading @@ -180,6 +185,45 @@ public class AttentionManagerServiceTest { assertThat(buffer.get(0)).isEqualTo(cache); } @Test public void testGetStaleAfterMillis_handlesGoodFlagValue() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_STALE_AFTER_MILLIS, "123", false); assertThat(mSpyAttentionManager.getStaleAfterMillis()).isEqualTo(123); } @Test public void testGetStaleAfterMillis_handlesBadFlagValue_1() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_STALE_AFTER_MILLIS, "-123", false); assertThat(mSpyAttentionManager.getStaleAfterMillis()).isEqualTo( DEFAULT_STALE_AFTER_MILLIS); } @Test public void testGetStaleAfterMillis_handlesBadFlagValue_2() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_STALE_AFTER_MILLIS, "15000", false); assertThat(mSpyAttentionManager.getStaleAfterMillis()).isEqualTo( DEFAULT_STALE_AFTER_MILLIS); } @Test public void testGetStaleAfterMillis_handlesBadFlagValue_3() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_STALE_AFTER_MILLIS, "abracadabra", false); assertThat(mSpyAttentionManager.getStaleAfterMillis()).isEqualTo( DEFAULT_STALE_AFTER_MILLIS); } @Test public void testGetStaleAfterMillis_handlesBadFlagValue_4() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_STALE_AFTER_MILLIS, "15_000L", false); assertThat(mSpyAttentionManager.getStaleAfterMillis()).isEqualTo( DEFAULT_STALE_AFTER_MILLIS); } private class MockIAttentionService implements IAttentionService { public void checkAttention(IAttentionCallback callback) throws RemoteException { callback.onSuccess(0, 0); Loading Loading
services/core/java/com/android/server/attention/AttentionManagerService.java +33 −8 Original line number Diff line number Diff line Loading @@ -77,21 +77,28 @@ public class AttentionManagerService extends SystemService { private static final String LOG_TAG = "AttentionManagerService"; private static final boolean DEBUG = false; /** Service will unbind if connection is not used for that amount of time. */ private static final long CONNECTION_TTL_MILLIS = 60_000; /** DeviceConfig flag name, if {@code true}, enables AttentionManagerService features. */ private static final String KEY_SERVICE_ENABLED = "service_enabled"; /** Default value in absence of {@link DeviceConfig} override. */ private static final boolean DEFAULT_SERVICE_ENABLED = true; /** Service will unbind if connection is not used for that amount of time. */ private static final long CONNECTION_TTL_MILLIS = 60_000; /** * DeviceConfig flag name, describes how much time we consider a result fresh; if the check * attention called within that period - cached value will be returned. */ @VisibleForTesting static final String KEY_STALE_AFTER_MILLIS = "stale_after_millis"; /** If the check attention called within that period - cached value will be returned. */ private static final long STALE_AFTER_MILLIS = 5_000; /** Default value in absence of {@link DeviceConfig} override. */ @VisibleForTesting static final long DEFAULT_STALE_AFTER_MILLIS = 1_000; /** The size of the buffer that stores recent attention check results. */ @VisibleForTesting protected static final int ATTENTION_CACHE_BUFFER_SIZE = 5; /** DeviceConfig flag name, if {@code true}, enables AttentionManagerService features. */ private static final String SERVICE_ENABLED = "service_enabled"; private static String sTestAttentionServicePackage; private final Context mContext; private final PowerManager mPowerManager; Loading Loading @@ -160,10 +167,28 @@ public class AttentionManagerService extends SystemService { @VisibleForTesting protected boolean isServiceEnabled() { return DeviceConfig.getBoolean(NAMESPACE_ATTENTION_MANAGER_SERVICE, SERVICE_ENABLED, return DeviceConfig.getBoolean(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_SERVICE_ENABLED, DEFAULT_SERVICE_ENABLED); } /** * How much time we consider a result fresh; if the check attention called within that period - * cached value will be returned. */ @VisibleForTesting protected long getStaleAfterMillis() { final long millis = DeviceConfig.getLong(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_STALE_AFTER_MILLIS, DEFAULT_STALE_AFTER_MILLIS); if (millis < 0 || millis > 10_000) { Slog.w(LOG_TAG, "Bad flag value supplied for: " + KEY_STALE_AFTER_MILLIS); return DEFAULT_STALE_AFTER_MILLIS; } return millis; } /** * Checks whether user attention is at the screen and calls in the provided callback. * Loading Loading @@ -199,7 +224,7 @@ public class AttentionManagerService extends SystemService { // throttle frequent requests final AttentionCheckCache cache = userState.mAttentionCheckCacheBuffer == null ? null : userState.mAttentionCheckCacheBuffer.getLast(); if (cache != null && now < cache.mLastComputed + STALE_AFTER_MILLIS) { if (cache != null && now < cache.mLastComputed + getStaleAfterMillis()) { callbackInternal.onSuccess(cache.mResult, cache.mTimestamp); return true; } Loading
services/tests/servicestests/src/com/android/server/attention/AttentionManagerServiceTest.java +44 −0 Original line number Diff line number Diff line Loading @@ -16,7 +16,11 @@ package com.android.server.attention; import static android.provider.DeviceConfig.NAMESPACE_ATTENTION_MANAGER_SERVICE; import static com.android.server.attention.AttentionManagerService.ATTENTION_CACHE_BUFFER_SIZE; import static com.android.server.attention.AttentionManagerService.DEFAULT_STALE_AFTER_MILLIS; import static com.android.server.attention.AttentionManagerService.KEY_STALE_AFTER_MILLIS; import static com.google.common.truth.Truth.assertThat; Loading @@ -35,6 +39,7 @@ import android.os.IBinder; import android.os.IPowerManager; import android.os.PowerManager; import android.os.RemoteException; import android.provider.DeviceConfig; import android.service.attention.IAttentionCallback; import android.service.attention.IAttentionService; Loading Loading @@ -180,6 +185,45 @@ public class AttentionManagerServiceTest { assertThat(buffer.get(0)).isEqualTo(cache); } @Test public void testGetStaleAfterMillis_handlesGoodFlagValue() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_STALE_AFTER_MILLIS, "123", false); assertThat(mSpyAttentionManager.getStaleAfterMillis()).isEqualTo(123); } @Test public void testGetStaleAfterMillis_handlesBadFlagValue_1() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_STALE_AFTER_MILLIS, "-123", false); assertThat(mSpyAttentionManager.getStaleAfterMillis()).isEqualTo( DEFAULT_STALE_AFTER_MILLIS); } @Test public void testGetStaleAfterMillis_handlesBadFlagValue_2() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_STALE_AFTER_MILLIS, "15000", false); assertThat(mSpyAttentionManager.getStaleAfterMillis()).isEqualTo( DEFAULT_STALE_AFTER_MILLIS); } @Test public void testGetStaleAfterMillis_handlesBadFlagValue_3() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_STALE_AFTER_MILLIS, "abracadabra", false); assertThat(mSpyAttentionManager.getStaleAfterMillis()).isEqualTo( DEFAULT_STALE_AFTER_MILLIS); } @Test public void testGetStaleAfterMillis_handlesBadFlagValue_4() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_STALE_AFTER_MILLIS, "15_000L", false); assertThat(mSpyAttentionManager.getStaleAfterMillis()).isEqualTo( DEFAULT_STALE_AFTER_MILLIS); } private class MockIAttentionService implements IAttentionService { public void checkAttention(IAttentionCallback callback) throws RemoteException { callback.onSuccess(0, 0); Loading