Commit a4311850 authored by Kyle Milka's avatar Kyle Milka Committed by Commit Bot

Close sharing hub when switching apps.

Make ShareSheetCoordinator implement WindowAndroid.ActivityStateObserver
this allows it to close the share sheet when switching apps.

Bug: 1087463
Change-Id: I6de44ea0e5c02de0c287f84bbbd991f331d2714d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2253139Reviewed-by: default avatarTanya Gupta <tgupta@chromium.org>
Commit-Queue: Kyle Milka <kmilka@chromium.org>
Cr-Commit-Position: refs/heads/master@{#780061}
parent 5c489b84
......@@ -20,6 +20,7 @@ import org.chromium.chrome.browser.share.ShareHelper;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetController;
import org.chromium.components.browser_ui.share.ShareParams;
import org.chromium.ui.base.WindowAndroid.ActivityStateObserver;
import org.chromium.ui.modelutil.PropertyModel;
import java.util.ArrayList;
......@@ -30,7 +31,7 @@ import java.util.Set;
* Coordinator for displaying the share sheet.
*/
// TODO(crbug/1022172): Should be package-protected once modularization is complete.
public class ShareSheetCoordinator {
public class ShareSheetCoordinator implements ActivityStateObserver {
private final BottomSheetController mBottomSheetController;
private final Supplier<Tab> mTabProvider;
private final ShareSheetPropertyModelBuilder mPropertyModelBuilder;
......@@ -38,6 +39,7 @@ public class ShareSheetCoordinator {
private final Callback<Tab> mPrintTabCallback;
private long mShareStartTime;
private boolean mExcludeFirstParty;
private ShareSheetBottomSheetContent mBottomSheet;
/**
* Constructs a new ShareSheetCoordinator.
......@@ -67,20 +69,21 @@ public class ShareSheetCoordinator {
if (activity == null) {
return;
}
params.getWindow().addActivityStateObserver(this);
ShareSheetBottomSheetContent bottomSheet = new ShareSheetBottomSheetContent(activity);
mBottomSheet = new ShareSheetBottomSheetContent(activity);
mShareStartTime = shareStartTime;
Set<Integer> contentTypes = ShareSheetPropertyModelBuilder.getContentTypes(
params, chromeShareExtras.isUrlOfVisiblePage());
List<PropertyModel> chromeFeatures =
createTopRowPropertyModels(bottomSheet, activity, params, contentTypes);
createTopRowPropertyModels(activity, params, contentTypes);
List<PropertyModel> thirdPartyApps = createBottomRowPropertyModels(
bottomSheet, activity, params, contentTypes, chromeShareExtras.saveLastUsed());
activity, params, contentTypes, chromeShareExtras.saveLastUsed());
bottomSheet.createRecyclerViews(chromeFeatures, thirdPartyApps);
mBottomSheet.createRecyclerViews(chromeFeatures, thirdPartyApps);
boolean shown = mBottomSheetController.requestShowContent(bottomSheet, true);
boolean shown = mBottomSheetController.requestShowContent(mBottomSheet, true);
if (shown) {
long delta = System.currentTimeMillis() - shareStartTime;
RecordHistogram.recordMediumTimesHistogram(
......@@ -95,25 +98,24 @@ public class ShareSheetCoordinator {
showShareSheet(params, chromeShareExtras, shareStartTime);
}
List<PropertyModel> createTopRowPropertyModels(ShareSheetBottomSheetContent bottomSheet,
List<PropertyModel> createTopRowPropertyModels(
Activity activity, ShareParams shareParams, Set<Integer> contentTypes) {
if (mExcludeFirstParty) {
return new ArrayList<>();
}
ChromeProvidedSharingOptionsProvider chromeProvidedSharingOptionsProvider =
new ChromeProvidedSharingOptionsProvider(activity, mTabProvider,
mBottomSheetController, bottomSheet, mPrefServiceBridge, shareParams,
mBottomSheetController, mBottomSheet, mPrefServiceBridge, shareParams,
mPrintTabCallback, mShareStartTime);
return chromeProvidedSharingOptionsProvider.getPropertyModels(contentTypes);
}
@VisibleForTesting
List<PropertyModel> createBottomRowPropertyModels(ShareSheetBottomSheetContent bottomSheet,
Activity activity, ShareParams params, Set<Integer> contentTypes,
boolean saveLastUsed) {
List<PropertyModel> createBottomRowPropertyModels(Activity activity, ShareParams params,
Set<Integer> contentTypes, boolean saveLastUsed) {
List<PropertyModel> models = mPropertyModelBuilder.selectThirdPartyApps(
bottomSheet, contentTypes, params, saveLastUsed, mShareStartTime);
mBottomSheet, contentTypes, params, saveLastUsed, mShareStartTime);
// More...
PropertyModel morePropertyModel = ShareSheetPropertyModelBuilder.createPropertyModel(
AppCompatResources.getDrawable(activity, R.drawable.sharing_more),
......@@ -121,7 +123,7 @@ public class ShareSheetCoordinator {
(shareParams)
-> {
RecordUserAction.record("SharingHubAndroid.MoreSelected");
mBottomSheetController.hideContent(bottomSheet, true);
mBottomSheetController.hideContent(mBottomSheet, true);
ShareHelper.showDefaultShareUi(params, saveLastUsed);
},
/*isFirstParty=*/true);
......@@ -134,4 +136,15 @@ public class ShareSheetCoordinator {
protected void disableFirstPartyFeaturesForTesting() {
mExcludeFirstParty = true;
}
// ActivityStateObserver
@Override
public void onActivityResumed() {}
@Override
public void onActivityPaused() {
if (mBottomSheet != null) {
mBottomSheetController.hideContent(mBottomSheet, true);
}
}
}
......@@ -90,10 +90,8 @@ public final class ShareSheetCoordinatorTest {
mShareSheetCoordinator.disableFirstPartyFeaturesForTesting();
Activity activity = mActivityTestRule.getActivity();
ShareSheetBottomSheetContent bottomSheet = new ShareSheetBottomSheetContent(activity);
List<PropertyModel> propertyModels =
mShareSheetCoordinator.createTopRowPropertyModels(bottomSheet, activity,
mShareSheetCoordinator.createTopRowPropertyModels(activity,
/*shareParams=*/null, ShareSheetPropertyModelBuilder.ALL_CONTENT_TYPES);
assertEquals("Property model list should be empty.", 0, propertyModels.size());
}
......@@ -102,11 +100,10 @@ public final class ShareSheetCoordinatorTest {
@MediumTest
public void testCreateBottomRowPropertyModels() {
Activity activity = mActivityTestRule.getActivity();
ShareSheetBottomSheetContent bottomSheet = new ShareSheetBottomSheetContent(activity);
List<PropertyModel> propertyModels = mShareSheetCoordinator.createBottomRowPropertyModels(
bottomSheet, activity, /*shareParams=*/null,
ShareSheetPropertyModelBuilder.ALL_CONTENT_TYPES, /*saveLastUsed=*/false);
List<PropertyModel> propertyModels =
mShareSheetCoordinator.createBottomRowPropertyModels(activity, /*shareParams=*/null,
ShareSheetPropertyModelBuilder.ALL_CONTENT_TYPES, /*saveLastUsed=*/false);
assertEquals("Incorrect number of property models.", 3, propertyModels.size());
assertEquals("First property model isn't testModel1.", "testModel1",
propertyModels.get(0).get(ShareSheetItemViewProperties.LABEL));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment