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

Commit 8cd27a18 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Put AttentionDetector behind a setting"

parents 17c7c791 9af68f16
Loading
Loading
Loading
Loading
+27 −1
Original line number Diff line number Diff line
@@ -19,9 +19,13 @@ package com.android.server.power;
import android.attention.AttentionManagerInternal;
import android.attention.AttentionManagerInternal.AttentionCallbackInternal;
import android.content.Context;
import android.database.ContentObserver;
import android.os.Handler;
import android.os.PowerManager;
import android.os.PowerManagerInternal;
import android.os.SystemClock;
import android.os.UserHandle;
import android.provider.Settings;
import android.service.attention.AttentionService;
import android.util.Slog;
import android.util.StatsLog;
@@ -44,6 +48,8 @@ public class AttentionDetector {
    private static final String TAG = "AttentionDetector";
    private static final boolean DEBUG = false;

    private boolean mIsSettingEnabled;

    /**
     * Invoked whenever user attention is detected.
     */
@@ -128,15 +134,35 @@ public class AttentionDetector {
        mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE;
    }

    @VisibleForTesting
    void updateEnabledFromSettings(Context context) {
        mIsSettingEnabled = Settings.System.getIntForUser(context.getContentResolver(),
                Settings.System.ADAPTIVE_SLEEP, 0, UserHandle.USER_CURRENT) == 1;
    }

    public void systemReady(Context context) {
        updateEnabledFromSettings(context);
        mAttentionManager = LocalServices.getService(AttentionManagerInternal.class);
        mMaximumExtensionMillis = context.getResources().getInteger(
                com.android.internal.R.integer.config_attentionMaximumExtension);
        mMaxAttentionApiTimeoutMillis = context.getResources().getInteger(
                com.android.internal.R.integer.config_attentionApiTimeout);

        context.getContentResolver().registerContentObserver(Settings.System.getUriFor(
                Settings.System.ADAPTIVE_SLEEP),
                false, new ContentObserver(new Handler()) {
                    @Override
                    public void onChange(boolean selfChange) {
                        updateEnabledFromSettings(context);
                    }
                }, UserHandle.USER_ALL);
    }

    public long updateUserActivity(long nextScreenDimming) {
        if (!mIsSettingEnabled) {
            return nextScreenDimming;
        }

        if (!isAttentionServiceSupported()) {
            return nextScreenDimming;
        }
+28 −0
Original line number Diff line number Diff line
@@ -32,10 +32,13 @@ import android.attention.AttentionManagerInternal;
import android.os.PowerManager;
import android.os.PowerManagerInternal;
import android.os.SystemClock;
import android.os.UserHandle;
import android.provider.Settings;
import android.service.attention.AttentionService;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
@@ -49,6 +52,7 @@ public class AttentionDetectorTest extends AndroidTestCase {
    private TestableAttentionDetector mAttentionDetector;
    private long mAttentionTimeout;
    private long mNextDimming;
    private int mIsSettingEnabled;

    @Before
    public void setUp() {
@@ -59,6 +63,20 @@ public class AttentionDetectorTest extends AndroidTestCase {
        mAttentionDetector.onWakefulnessChangeStarted(PowerManagerInternal.WAKEFULNESS_AWAKE);
        mAttentionDetector.setAttentionServiceSupported(true);
        mNextDimming = SystemClock.uptimeMillis() + 3000L;

        // Save the existing state.
        mIsSettingEnabled = Settings.System.getIntForUser(getContext().getContentResolver(),
                Settings.System.ADAPTIVE_SLEEP, 0, UserHandle.USER_CURRENT);

        Settings.System.putIntForUser(getContext().getContentResolver(),
                Settings.System.ADAPTIVE_SLEEP, 1, UserHandle.USER_CURRENT);
        mAttentionDetector.updateEnabledFromSettings(getContext());
    }

    @After
    public void tearDown() {
        Settings.System.putIntForUser(getContext().getContentResolver(),
                Settings.System.ADAPTIVE_SLEEP, mIsSettingEnabled, UserHandle.USER_CURRENT);
    }

    @Test
@@ -68,6 +86,16 @@ public class AttentionDetectorTest extends AndroidTestCase {
        assertThat(when).isLessThan(mNextDimming);
    }

    @Test
    public void testOnUserActivity_doesntCheckIfNotEnabled() {
        Settings.System.putIntForUser(getContext().getContentResolver(),
                Settings.System.ADAPTIVE_SLEEP, 0, UserHandle.USER_CURRENT);
        mAttentionDetector.updateEnabledFromSettings(getContext());
        long when = registerAttention();
        verify(mAttentionManagerInternal, never()).checkAttention(anyInt(), anyLong(), any());
        assertThat(mNextDimming).isEqualTo(when);
    }

    @Test
    public void testOnUserActivity_doesntCheckIfNotSupported() {
        mAttentionDetector.setAttentionServiceSupported(false);