From 4d2fd6ee09b465c9ef9d71590217283f7e5f9c05 Mon Sep 17 00:00:00 2001 From: Fabian Kozynski Date: Wed, 22 Mar 2023 10:35:01 -0400 Subject: [PATCH] Add screenSize and smallestScreenSize to manifest We need to be aware of these as an orientation change could cause change in these (so we were not restarting for orientation but got restarted by other changes). UiControllerImpl already was set up in a previous CL to properly re-inflate what's necessary if the screen size/orientation changes. Test: atest ControlsActivityTest Test: atest ControlsUiControllerImplTest Test: manual: open a detailed view in panel and rotate device Fixes: 270583496 Change-Id: I4c0caee6dfe76e5434f1545f750418816baf0189 --- packages/SystemUI/AndroidManifest.xml | 2 +- .../systemui/controls/ui/ControlsActivity.kt | 10 +- .../controls/ui/ControlsUiController.kt | 2 +- .../controls/ui/ControlsUiControllerImpl.kt | 2 +- packages/SystemUI/tests/AndroidManifest.xml | 5 + .../controls/ui/ControlsActivityTest.kt | 125 ++++++++++++++++++ .../ui/ControlsUiControllerImplTest.kt | 2 +- .../controls/ui/TestableControlsActivity.kt | 40 ++++++ 8 files changed, 181 insertions(+), 7 deletions(-) create mode 100644 packages/SystemUI/tests/src/com/android/systemui/controls/ui/ControlsActivityTest.kt create mode 100644 packages/SystemUI/tests/src/com/android/systemui/controls/ui/TestableControlsActivity.kt diff --git a/packages/SystemUI/AndroidManifest.xml b/packages/SystemUI/AndroidManifest.xml index aadc14061a61d..cacf13326178b 100644 --- a/packages/SystemUI/AndroidManifest.xml +++ b/packages/SystemUI/AndroidManifest.xml @@ -878,7 +878,7 @@ android:showForAllUsers="true" android:finishOnTaskLaunch="true" android:launchMode="singleInstance" - android:configChanges="screenLayout|keyboard|keyboardHidden|orientation" + android:configChanges="screenSize|smallestScreenSize|screenLayout|keyboard|keyboardHidden|orientation" android:visibleToInstantApps="true"> diff --git a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsActivity.kt b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsActivity.kt index 224eb1ca409a6..c964b9654955f 100644 --- a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsActivity.kt +++ b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsActivity.kt @@ -47,7 +47,8 @@ import javax.inject.Inject * destroyed on SCREEN_OFF events, due to issues with occluded activities over lockscreen as well as * user expectations for the activity to not continue running. */ -class ControlsActivity @Inject constructor( +// Open for testing +open class ControlsActivity @Inject constructor( private val uiController: ControlsUiController, private val broadcastDispatcher: BroadcastDispatcher, private val dreamManager: IDreamManager, @@ -98,8 +99,11 @@ class ControlsActivity @Inject constructor( override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) - if (lastConfiguration.diff(newConfig) and ActivityInfo.CONFIG_ORIENTATION != 0 ) { - uiController.onOrientationChange() + val interestingFlags = ActivityInfo.CONFIG_ORIENTATION or + ActivityInfo.CONFIG_SCREEN_SIZE or + ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE + if (lastConfiguration.diff(newConfig) and interestingFlags != 0 ) { + uiController.onSizeChange() } lastConfiguration = newConfig } diff --git a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiController.kt b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiController.kt index 3ecf4236656d0..0cc4683baef1b 100644 --- a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiController.kt +++ b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiController.kt @@ -65,7 +65,7 @@ interface ControlsUiController { */ fun getPreferredSelectedItem(structures: List): SelectedItem - fun onOrientationChange() + fun onSizeChange() } sealed class SelectedItem { diff --git a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiControllerImpl.kt b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiControllerImpl.kt index d2833796d2a59..5543916495486 100644 --- a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiControllerImpl.kt +++ b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiControllerImpl.kt @@ -809,7 +809,7 @@ class ControlsUiControllerImpl @Inject constructor ( } } - override fun onOrientationChange() { + override fun onSizeChange() { selectionItem?.let { when (selectedItem) { is SelectedItem.StructureItem -> createListView(it) diff --git a/packages/SystemUI/tests/AndroidManifest.xml b/packages/SystemUI/tests/AndroidManifest.xml index d4e06bc2a5c83..690f63c456c70 100644 --- a/packages/SystemUI/tests/AndroidManifest.xml +++ b/packages/SystemUI/tests/AndroidManifest.xml @@ -108,6 +108,11 @@ android:excludeFromRecents="true" /> + + diff --git a/packages/SystemUI/tests/src/com/android/systemui/controls/ui/ControlsActivityTest.kt b/packages/SystemUI/tests/src/com/android/systemui/controls/ui/ControlsActivityTest.kt new file mode 100644 index 0000000000000..0f62b24bdb068 --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/controls/ui/ControlsActivityTest.kt @@ -0,0 +1,125 @@ +/* + * 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.systemui.controls.ui + +import android.content.Intent +import android.content.res.Configuration +import android.service.dreams.IDreamManager +import android.testing.AndroidTestingRunner +import android.testing.TestableLooper +import androidx.test.filters.SmallTest +import androidx.test.rule.ActivityTestRule +import androidx.test.runner.intercepting.SingleActivityFactory +import com.android.systemui.SysuiTestCase +import com.android.systemui.broadcast.BroadcastDispatcher +import com.android.systemui.controls.settings.ControlsSettingsDialogManager +import com.android.systemui.flags.FeatureFlags +import com.android.systemui.statusbar.policy.KeyguardStateController +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mock +import org.mockito.Mockito.verify +import org.mockito.MockitoAnnotations + +@SmallTest +@RunWith(AndroidTestingRunner::class) +@TestableLooper.RunWithLooper +class ControlsActivityTest : SysuiTestCase() { + @Mock private lateinit var uiController: ControlsUiController + @Mock private lateinit var broadcastDispatcher: BroadcastDispatcher + @Mock private lateinit var dreamManager: IDreamManager + @Mock private lateinit var featureFlags: FeatureFlags + @Mock private lateinit var controlsSettingsDialogManager: ControlsSettingsDialogManager + @Mock private lateinit var keyguardStateController: KeyguardStateController + + @Rule + @JvmField + var activityRule = + ActivityTestRule( + object : + SingleActivityFactory( + TestableControlsActivity::class.java + ) { + override fun create(intent: Intent?): TestableControlsActivity { + return TestableControlsActivity( + uiController, + broadcastDispatcher, + dreamManager, + featureFlags, + controlsSettingsDialogManager, + keyguardStateController, + ) + } + }, + false, + false + ) + + @Before + fun setup() { + MockitoAnnotations.initMocks(this) + activityRule.launchActivity(Intent()) + } + + @Test + fun testOrientationChangeForwardsToUiController() { + val currentConfig = activityRule.activity.resources.configuration + val newConfig = Configuration(currentConfig) + newConfig.orientation = switchOrientation(currentConfig.orientation) + activityRule.runOnUiThread { activityRule.activity.onConfigurationChanged(newConfig) } + + verify(uiController).onSizeChange() + } + + @Test + fun testScreenChangeForwardsToUiController() { + val currentConfig = activityRule.activity.resources.configuration + val newConfig = Configuration(currentConfig) + swapHeightWidth(newConfig) + activityRule.runOnUiThread { activityRule.activity.onConfigurationChanged(newConfig) } + + verify(uiController).onSizeChange() + } + + @Test + fun testChangeSmallestScreenSizeForwardsToUiController() { + val currentConfig = activityRule.activity.resources.configuration + val newConfig = Configuration(currentConfig) + newConfig.smallestScreenWidthDp *= 2 + newConfig.screenWidthDp *= 2 + activityRule.runOnUiThread { activityRule.activity.onConfigurationChanged(newConfig) } + + verify(uiController).onSizeChange() + } + + private fun switchOrientation(orientation: Int): Int { + return if (orientation == Configuration.ORIENTATION_LANDSCAPE) { + Configuration.ORIENTATION_PORTRAIT + } else { + Configuration.ORIENTATION_LANDSCAPE + } + } + + private fun swapHeightWidth(configuration: Configuration) { + val oldHeight = configuration.screenHeightDp + val oldWidth = configuration.screenWidthDp + configuration.screenHeightDp = oldWidth + configuration.screenWidthDp = oldHeight + } +} diff --git a/packages/SystemUI/tests/src/com/android/systemui/controls/ui/ControlsUiControllerImplTest.kt b/packages/SystemUI/tests/src/com/android/systemui/controls/ui/ControlsUiControllerImplTest.kt index 6ca4dcccfca0a..91f279c1bf73d 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/controls/ui/ControlsUiControllerImplTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/controls/ui/ControlsUiControllerImplTest.kt @@ -450,7 +450,7 @@ class ControlsUiControllerImplTest : SysuiTestCase() { taskViewConsumerCaptor.value.accept(taskView) - underTest.onOrientationChange() + underTest.onSizeChange() verify(taskView).onLocationChanged() } diff --git a/packages/SystemUI/tests/src/com/android/systemui/controls/ui/TestableControlsActivity.kt b/packages/SystemUI/tests/src/com/android/systemui/controls/ui/TestableControlsActivity.kt new file mode 100644 index 0000000000000..f0b473210630a --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/controls/ui/TestableControlsActivity.kt @@ -0,0 +1,40 @@ +/* + * 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.systemui.controls.ui + +import android.service.dreams.IDreamManager +import com.android.systemui.broadcast.BroadcastDispatcher +import com.android.systemui.controls.settings.ControlsSettingsDialogManager +import com.android.systemui.flags.FeatureFlags +import com.android.systemui.statusbar.policy.KeyguardStateController + +class TestableControlsActivity( + uiController: ControlsUiController, + broadcastDispatcher: BroadcastDispatcher, + dreamManager: IDreamManager, + featureFlags: FeatureFlags, + controlsSettingsDialogManager: ControlsSettingsDialogManager, + keyguardStateController: KeyguardStateController +) : + ControlsActivity( + uiController, + broadcastDispatcher, + dreamManager, + featureFlags, + controlsSettingsDialogManager, + keyguardStateController + )