Loading src/com/android/settings/homepage/contextualcards/slices/ContextualAdaptiveSleepSlice.java +13 −3 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); Loading Loading @@ -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}. Loading tests/robotests/src/com/android/settings/homepage/contextualcards/slices/ContextualAdaptiveSleepSliceTest.java +18 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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(); Loading Loading @@ -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(); } } Loading
src/com/android/settings/homepage/contextualcards/slices/ContextualAdaptiveSleepSlice.java +13 −3 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); Loading Loading @@ -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}. Loading
tests/robotests/src/com/android/settings/homepage/contextualcards/slices/ContextualAdaptiveSleepSliceTest.java +18 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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(); Loading Loading @@ -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(); } }