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

Commit e6af4e13 authored by Yi Jiang's avatar Yi Jiang Committed by android-build-merger
Browse files

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

am: 7933af02

Change-Id: Iecc58066521d19bb58514eca667a5f4daecbe0de
parents 5776e726 7933af02
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();
    }
}