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

Commit 7933af02 authored by Yi Jiang's avatar Yi Jiang Committed by Android (Google) Code Review
Browse files

Merge "Don't show suggestion slice if 'Screen attention' is already on" into qt-qpr1-dev

parents bde8c0a6 811908f5
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;

import androidx.core.graphics.drawable.IconCompat;
import androidx.slice.Slice;
@@ -72,11 +73,12 @@ public class ContextualAdaptiveSleepSlice implements CustomSliceable {
            return null;
        }

        // Display the contextual card only if all the following 3 conditions hold:
        // 1. The Screen Attention is enabled in Settings.
        // Display the contextual card only if all the following 4 conditions hold:
        // 1. The Screen Attention is available in Settings.
        // 2. The device is not recently set up.
        // 3. Current user hasn't opened Screen Attention's settings page before.
        if (isSettingsAvailable() && !isUserInteracted() && !isRecentlySetup()) {
        // 4. Screen Attention is off.
        if (isSettingsAvailable() && !isUserInteracted() && !isRecentlySetup() && !isTurnedOn()) {
            final IconCompat icon = IconCompat.createWithResource(mContext,
                    R.drawable.ic_settings_adaptive_sleep);
            final CharSequence title = mContext.getText(R.string.adaptive_sleep_title);
@@ -122,6 +124,14 @@ public class ContextualAdaptiveSleepSlice implements CustomSliceable {
        return PendingIntent.getActivity(mContext, 0  /* requestCode */, intent, 0  /* flags */);
    }

    /**
     * @return {@code true} if the feature is turned on for the device, otherwise {@code false}
     */
    private boolean isTurnedOn() {
        return Settings.System.getInt(
                mContext.getContentResolver(), Settings.System.ADAPTIVE_SLEEP, 0) != 0;
    }

    /**
     * @return {@code true} if the current user has opened the Screen Attention settings page
     * before, otherwise {@code false}.
+18 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;

import androidx.slice.Slice;
import androidx.slice.SliceProvider;
@@ -65,6 +66,7 @@ public class ContextualAdaptiveSleepSliceTest {
        mContext = spy(RuntimeEnvironment.application);
        mContextualAdaptiveSleepSlice = spy(new ContextualAdaptiveSleepSlice(mContext));

        Settings.System.putInt(mContext.getContentResolver(), Settings.System.ADAPTIVE_SLEEP, 0);
        doReturn(mPackageManager).when(mContext).getPackageManager();
        doReturn(mSharedPreferences).when(mContext).getSharedPreferences(eq(PREF), anyInt());
        doReturn(true).when(mContextualAdaptiveSleepSlice).isSettingsAvailable();
@@ -112,4 +114,20 @@ public class ContextualAdaptiveSleepSliceTest {

        assertThat(slice).isNull();
    }

    @Test
    public void getSlice_ShowIfNotTurnedOn() {
        final Slice slice = mContextualAdaptiveSleepSlice.getSlice();

        assertThat(slice).isNotNull();
    }

    @Test
    public void getSlice_DoNotShowIfTurnedOn() {
        Settings.System.putInt(mContext.getContentResolver(), Settings.System.ADAPTIVE_SLEEP, 1);

        final Slice slice = mContextualAdaptiveSleepSlice.getSlice();

        assertThat(slice).isNull();
    }
}