diff --git a/src/com/android/settings/display/TopLevelWallpaperPreferenceController.java b/src/com/android/settings/display/TopLevelWallpaperPreferenceController.java index c84a15e1e86..2e59725cdc1 100644 --- a/src/com/android/settings/display/TopLevelWallpaperPreferenceController.java +++ b/src/com/android/settings/display/TopLevelWallpaperPreferenceController.java @@ -31,6 +31,7 @@ import androidx.preference.Preference; import androidx.preference.PreferenceScreen; import com.android.settings.R; +import com.android.settings.activityembedding.ActivityEmbeddingUtils; import com.android.settings.core.BasePreferenceController; import com.android.settingslib.RestrictedLockUtilsInternal; import com.android.settingslib.RestrictedTopLevelPreference; @@ -97,7 +98,8 @@ public class TopLevelWallpaperPreferenceController extends BasePreferenceControl if (getPreferenceKey().equals(preference.getKey())) { final Intent intent = new Intent().setComponent( getComponentName()).putExtra(mWallpaperLaunchExtra, LAUNCHED_SETTINGS); - if (areStylesAvailable()) { + if (areStylesAvailable() && !ActivityEmbeddingUtils.isEmbeddingActivityEnabled( + mContext)) { intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); } preference.getContext().startActivity(intent); diff --git a/tests/robotests/src/com/android/settings/display/TopLevelWallpaperPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/TopLevelWallpaperPreferenceControllerTest.java index 6ad99745748..62b34e26546 100644 --- a/tests/robotests/src/com/android/settings/display/TopLevelWallpaperPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/display/TopLevelWallpaperPreferenceControllerTest.java @@ -29,6 +29,7 @@ import androidx.preference.Preference; import com.android.settings.R; import com.android.settings.testutils.shadow.SettingsShadowResources; +import com.android.settings.testutils.shadow.ShadowActivityEmbeddingUtils; import com.google.common.collect.Lists; @@ -43,7 +44,7 @@ import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowPackageManager; @RunWith(RobolectricTestRunner.class) -@Config(shadows = {SettingsShadowResources.class}) +@Config(shadows = {SettingsShadowResources.class, ShadowActivityEmbeddingUtils.class}) public class TopLevelWallpaperPreferenceControllerTest { private static final String TEST_KEY = "test_key"; @@ -204,18 +205,32 @@ public class TopLevelWallpaperPreferenceControllerTest { } @Test - public void handlePreferenceTreeClick_launchClearTask() { - mShadowPackageManager.setResolveInfosForIntent( - mWallpaperIntent, Lists.newArrayList()); + public void handlePreferenceTreeClick_embeddingActivityDisabled_launchWithTaskFlag() { + ShadowActivityEmbeddingUtils.setIsEmbeddingActivityEnabled(false); mShadowPackageManager.setResolveInfosForIntent( mStylesAndWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class))); - Preference preference = new Preference(mContext); preference.setKey(TEST_KEY); mController.handlePreferenceTreeClick(preference); - assertThat((Shadows.shadowOf(mContext).getNextStartedActivityForResult() - .intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_TASK) != 0).isTrue(); + int flags = Shadows.shadowOf(mContext).getNextStartedActivityForResult().intent.getFlags(); + assertThat((flags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0).isTrue(); + assertThat((flags & Intent.FLAG_ACTIVITY_CLEAR_TASK) != 0).isTrue(); + } + + @Test + public void handlePreferenceTreeClick_embeddingActivityEnabled_launchWithoutTaskFlag() { + ShadowActivityEmbeddingUtils.setIsEmbeddingActivityEnabled(true); + mShadowPackageManager.setResolveInfosForIntent( + mStylesAndWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class))); + Preference preference = new Preference(mContext); + preference.setKey(TEST_KEY); + + mController.handlePreferenceTreeClick(preference); + + int flags = Shadows.shadowOf(mContext).getNextStartedActivityForResult().intent.getFlags(); + assertThat((flags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0).isFalse(); + assertThat((flags & Intent.FLAG_ACTIVITY_CLEAR_TASK) != 0).isFalse(); } } diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowActivityEmbeddingUtils.java b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowActivityEmbeddingUtils.java new file mode 100644 index 00000000000..ddd7c8821ae --- /dev/null +++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowActivityEmbeddingUtils.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2021 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.testutils.shadow; + +import android.content.Context; + +import com.android.settings.activityembedding.ActivityEmbeddingUtils; + +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; + +/** + * Shadow class for {@link ActivityEmbeddingUtils} to test embedding activity features. + */ +@Implements(ActivityEmbeddingUtils.class) +public class ShadowActivityEmbeddingUtils { + private static boolean sIsEmbeddingActivityEnabled; + + @Implementation + public static boolean isEmbeddingActivityEnabled(Context context) { + return sIsEmbeddingActivityEnabled; + } + + public static void setIsEmbeddingActivityEnabled(boolean isEmbeddingActivityEnabled) { + sIsEmbeddingActivityEnabled = isEmbeddingActivityEnabled; + } +}