Not use NEW_TASKS for Wallpaper & style's 2-pane UX

Refine launch mode to support 2-pane UX for Settings.

Bug: 197609643
Test: make RunSettingsRoboTests
Change-Id: I5212bbcf8c512fac571284379a29cf487f387c5a
diff --git a/src/com/android/settings/display/TopLevelWallpaperPreferenceController.java b/src/com/android/settings/display/TopLevelWallpaperPreferenceController.java
index c84a15e..2e59725 100644
--- a/src/com/android/settings/display/TopLevelWallpaperPreferenceController.java
+++ b/src/com/android/settings/display/TopLevelWallpaperPreferenceController.java
@@ -31,6 +31,7 @@
 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 @@
         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 6ad9974..62b34e2 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 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.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 @@
     }
 
     @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 0000000..ddd7c88
--- /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;
+    }
+}