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
This commit is contained in:
Kunhung Li
2021-09-30 14:19:18 +08:00
parent 0a942ab338
commit c692af2a76
3 changed files with 66 additions and 8 deletions

View File

@@ -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);

View File

@@ -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();
}
}

View File

@@ -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;
}
}