diff --git a/res/values/strings.xml b/res/values/strings.xml index c01f4b987b6..1df5dad4fc0 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -14461,6 +14461,10 @@ Show additional information Display things like the time, weather, or other information on the screen saver + + Show home controls + + Show home controls button from the screen saver More settings diff --git a/res/xml/dream_fragment_overview.xml b/res/xml/dream_fragment_overview.xml index 3321fd1dda1..8377a0685c2 100644 --- a/res/xml/dream_fragment_overview.xml +++ b/res/xml/dream_fragment_overview.xml @@ -46,6 +46,12 @@ android:summary="@string/dream_complications_toggle_summary" settings:controller="com.android.settings.dream.DreamComplicationPreferenceController"/> + + diff --git a/src/com/android/settings/dream/DreamHomeControlsPreferenceController.java b/src/com/android/settings/dream/DreamHomeControlsPreferenceController.java new file mode 100644 index 00000000000..b39f3b1ace6 --- /dev/null +++ b/src/com/android/settings/dream/DreamHomeControlsPreferenceController.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.dream; + +import android.content.Context; + +import com.android.internal.annotations.VisibleForTesting; +import com.android.settings.R; +import com.android.settings.core.TogglePreferenceController; +import com.android.settingslib.dream.DreamBackend; + +/** + * Controller for the {@link androidx.preference.SwitchPreference} which controls if dream + * overlays should be enabled. + */ +public class DreamHomeControlsPreferenceController extends TogglePreferenceController { + private final DreamBackend mBackend; + + public DreamHomeControlsPreferenceController(Context context, String key) { + this(context, key, DreamBackend.getInstance(context)); + } + + @VisibleForTesting + public DreamHomeControlsPreferenceController(Context context, String key, + DreamBackend dreamBackend) { + super(context, key); + mBackend = dreamBackend; + } + + @Override + public int getAvailabilityStatus() { + final boolean supported = + mBackend.getSupportedComplications() + .contains(DreamBackend.COMPLICATION_TYPE_HOME_CONTROLS); + return supported ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; + } + + @Override + public boolean isChecked() { + return mBackend.getEnabledComplications().contains( + DreamBackend.COMPLICATION_TYPE_HOME_CONTROLS); + } + + @Override + public boolean setChecked(boolean isChecked) { + mBackend.setHomeControlsEnabled(isChecked); + return true; + } + + @Override + public int getSliceHighlightMenuRes() { + return R.string.menu_key_display; + } +} diff --git a/tests/robotests/src/com/android/settings/dream/DreamHomeControlsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/dream/DreamHomeControlsPreferenceControllerTest.java new file mode 100644 index 00000000000..452604c8012 --- /dev/null +++ b/tests/robotests/src/com/android/settings/dream/DreamHomeControlsPreferenceControllerTest.java @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.dream; + +import static com.android.settingslib.dream.DreamBackend.COMPLICATION_TYPE_HOME_CONTROLS; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.util.ArraySet; + +import androidx.preference.PreferenceScreen; +import androidx.preference.SwitchPreference; +import androidx.test.core.app.ApplicationProvider; + +import com.android.settingslib.dream.DreamBackend; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Answers; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.robolectric.shadow.api.Shadow; +import org.robolectric.shadows.ShadowContentResolver; +import org.robolectric.shadows.ShadowSettings; + +@RunWith(RobolectricTestRunner.class) +@Config(shadows = {ShadowSettings.ShadowSecure.class}) +public class DreamHomeControlsPreferenceControllerTest { + + private Context mContext; + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private PreferenceScreen mScreen; + private DreamHomeControlsPreferenceController mController; + private SwitchPreference mPreference; + private DreamBackend mBackend; + private ShadowContentResolver mShadowContentResolver; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = ApplicationProvider.getApplicationContext(); + mShadowContentResolver = Shadow.extract(mContext.getContentResolver()); + mBackend = new DreamBackend(mContext); + mController = new DreamHomeControlsPreferenceController(mContext, "key", mBackend); + mPreference = new SwitchPreference(mContext); + mPreference.setKey(mController.getPreferenceKey()); + when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); + mController.displayPreference(mScreen); + + // Make home controls supported by default + mBackend.setSupportedComplications( + new ArraySet<>(new Integer[]{COMPLICATION_TYPE_HOME_CONTROLS})); + } + + @After + public void tearDown() { + ShadowSettings.ShadowSecure.reset(); + } + + @Test + public void testSetChecked_setTrue_enablesSetting() { + mBackend.setHomeControlsEnabled(false); + assertThat(mBackend.getEnabledComplications()) + .doesNotContain(COMPLICATION_TYPE_HOME_CONTROLS); + + mController.setChecked(true); + assertThat(mBackend.getEnabledComplications()) + .contains(COMPLICATION_TYPE_HOME_CONTROLS); + } + + @Test + public void testSetChecked_setFalse_disablesSetting() { + mBackend.setHomeControlsEnabled(true); + assertThat(mBackend.getEnabledComplications()) + .contains(COMPLICATION_TYPE_HOME_CONTROLS); + + mController.setChecked(false); + assertThat(mBackend.getEnabledComplications()) + .doesNotContain(COMPLICATION_TYPE_HOME_CONTROLS); + } + + @Test + public void testIsChecked_returnsFalse() { + mBackend.setHomeControlsEnabled(false); + assertThat(mController.isChecked()).isFalse(); + } + + @Test + public void testIsChecked_returnsTrue() { + mBackend.setHomeControlsEnabled(true); + assertThat(mBackend.getEnabledComplications()) + .contains(COMPLICATION_TYPE_HOME_CONTROLS); + assertThat(mController.isChecked()).isTrue(); + } +}