Commit a1cbc486 authored by Tanya Gupta's avatar Tanya Gupta Committed by Commit Bot

Refactored ChromeProvidedOptionsProvider to remove redundancy and

simplify the addition of a new feature.

Change-Id: Ie0fdde88c4e48af19d4679d19eb3c11807382a6d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2343218
Commit-Queue: Tanya Gupta <tgupta@chromium.org>
Reviewed-by: default avatarKyle Milka <kmilka@chromium.org>
Cr-Commit-Position: refs/heads/master@{#800333}
parent d35f5dac
...@@ -9,6 +9,7 @@ import android.content.ClipData; ...@@ -9,6 +9,7 @@ import android.content.ClipData;
import android.content.ClipboardManager; import android.content.ClipboardManager;
import android.content.Context; import android.content.Context;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.View;
import androidx.appcompat.content.res.AppCompatResources; import androidx.appcompat.content.res.AppCompatResources;
...@@ -96,21 +97,60 @@ class ChromeProvidedSharingOptionsProvider { ...@@ -96,21 +97,60 @@ class ChromeProvidedSharingOptionsProvider {
/** /**
* Encapsulates a {@link PropertyModel} and the {@link ContentType}s it should be shown for. * Encapsulates a {@link PropertyModel} and the {@link ContentType}s it should be shown for.
*/ */
private static class FirstPartyOption { private class FirstPartyOption {
private final PropertyModel mPropertyModel; final Collection<Integer> mContentTypes;
private final Collection<Integer> mContentTypes; final PropertyModel mPropertyModel;
FirstPartyOption(PropertyModel propertyModel, Collection<Integer> contentTypes) { /**
mPropertyModel = propertyModel; * Should only be used when the default property model constructed in the builder does not
* fit the feature's needs. This should be rare.
*
* @param model Property model for the first party option.
* @param contentTypes Content types to trigger for.
*/
FirstPartyOption(PropertyModel model, Collection<Integer> contentTypes) {
mPropertyModel = model;
mContentTypes = contentTypes; mContentTypes = contentTypes;
} }
}
private class FirstPartyOptionBuilder {
private int mIcon;
private int mIconLabel;
private String mFeatureNameForMetrics;
private Callback<View> mOnClickCallback;
private final Integer[] mContentTypesInBuilder;
FirstPartyOptionBuilder(Integer... contentTypes) {
mContentTypesInBuilder = contentTypes;
}
FirstPartyOptionBuilder setIcon(int icon, int iconLabel) {
mIcon = icon;
mIconLabel = iconLabel;
return this;
}
FirstPartyOptionBuilder setFeatureNameForMetrics(String featureName) {
mFeatureNameForMetrics = featureName;
return this;
}
PropertyModel getPropertyModel() { FirstPartyOptionBuilder setOnClickCallback(Callback<View> onClickCallback) {
return mPropertyModel; mOnClickCallback = onClickCallback;
return this;
} }
Collection<Integer> getContentTypes() { FirstPartyOption build() {
return mContentTypes; PropertyModel model = ShareSheetPropertyModelBuilder.createPropertyModel(
AppCompatResources.getDrawable(mActivity, mIcon),
mActivity.getResources().getString(mIconLabel), (view) -> {
RecordUserAction.record(mFeatureNameForMetrics);
recordTimeToShare(mShareStartTime);
mBottomSheetController.hideContent(mBottomSheetContent, true);
mOnClickCallback.onResult(view);
});
return new FirstPartyOption(model, Arrays.asList(mContentTypesInBuilder));
} }
} }
...@@ -124,8 +164,8 @@ class ChromeProvidedSharingOptionsProvider { ...@@ -124,8 +164,8 @@ class ChromeProvidedSharingOptionsProvider {
List<PropertyModel> getPropertyModels(Set<Integer> contentTypes) { List<PropertyModel> getPropertyModels(Set<Integer> contentTypes) {
List<PropertyModel> propertyModels = new ArrayList<>(); List<PropertyModel> propertyModels = new ArrayList<>();
for (FirstPartyOption firstPartyOption : mOrderedFirstPartyOptions) { for (FirstPartyOption firstPartyOption : mOrderedFirstPartyOptions) {
if (!Collections.disjoint(contentTypes, firstPartyOption.getContentTypes())) { if (!Collections.disjoint(contentTypes, firstPartyOption.mContentTypes)) {
propertyModels.add(firstPartyOption.getPropertyModel()); propertyModels.add(firstPartyOption.mPropertyModel);
} }
} }
return propertyModels; return propertyModels;
...@@ -195,62 +235,53 @@ class ChromeProvidedSharingOptionsProvider { ...@@ -195,62 +235,53 @@ class ChromeProvidedSharingOptionsProvider {
} }
private FirstPartyOption createCopyLinkFirstPartyOption() { private FirstPartyOption createCopyLinkFirstPartyOption() {
PropertyModel propertyModel = ShareSheetPropertyModelBuilder.createPropertyModel( return new FirstPartyOptionBuilder(
AppCompatResources.getDrawable(mActivity, R.drawable.ic_content_copy_black), ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE)
mActivity.getResources().getString(R.string.sharing_copy_url), (view) -> { .setIcon(R.drawable.ic_content_copy_black, R.string.sharing_copy_url)
RecordUserAction.record("SharingHubAndroid.CopyURLSelected"); .setFeatureNameForMetrics("SharingHubAndroid.CopyURLSelected")
recordTimeToShare(mShareStartTime); .setOnClickCallback((view) -> {
mBottomSheetController.hideContent(mBottomSheetContent, true);
ClipboardManager clipboard = (ClipboardManager) mActivity.getSystemService( ClipboardManager clipboard = (ClipboardManager) mActivity.getSystemService(
Context.CLIPBOARD_SERVICE); Context.CLIPBOARD_SERVICE);
clipboard.setPrimaryClip( clipboard.setPrimaryClip(
ClipData.newPlainText(mShareParams.getTitle(), mShareParams.getUrl())); ClipData.newPlainText(mShareParams.getTitle(), mShareParams.getUrl()));
Toast.makeText(mActivity, R.string.link_copied, Toast.LENGTH_SHORT).show(); Toast.makeText(mActivity, R.string.link_copied, Toast.LENGTH_SHORT).show();
}); })
return new FirstPartyOption(propertyModel, .build();
Arrays.asList(ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE));
} }
private FirstPartyOption createCopyImageFirstPartyOption() { private FirstPartyOption createCopyImageFirstPartyOption() {
PropertyModel propertyModel = ShareSheetPropertyModelBuilder.createPropertyModel( return new FirstPartyOptionBuilder(ContentType.IMAGE)
AppCompatResources.getDrawable(mActivity, R.drawable.ic_content_copy_black), .setIcon(R.drawable.ic_content_copy_black, R.string.sharing_copy_image)
mActivity.getResources().getString(R.string.sharing_copy_image), (view) -> { .setFeatureNameForMetrics("SharingHubAndroid.CopyImageSelected")
RecordUserAction.record("SharingHubAndroid.CopyImageSelected"); .setOnClickCallback((view) -> {
recordTimeToShare(mShareStartTime);
mBottomSheetController.hideContent(mBottomSheetContent, true);
if (!mShareParams.getFileUris().isEmpty()) { if (!mShareParams.getFileUris().isEmpty()) {
Clipboard.getInstance().setImageUri(mShareParams.getFileUris().get(0)); Clipboard.getInstance().setImageUri(mShareParams.getFileUris().get(0));
Toast.makeText(mActivity, R.string.image_copied, Toast.LENGTH_SHORT).show(); Toast.makeText(mActivity, R.string.image_copied, Toast.LENGTH_SHORT).show();
} }
}); })
return new FirstPartyOption(propertyModel, Arrays.asList(ContentType.IMAGE)); .build();
} }
private FirstPartyOption createCopyTextFirstPartyOption() { private FirstPartyOption createCopyTextFirstPartyOption() {
PropertyModel propertyModel = ShareSheetPropertyModelBuilder.createPropertyModel( return new FirstPartyOptionBuilder(ContentType.TEXT, ContentType.HIGHLIGHTED_TEXT)
AppCompatResources.getDrawable(mActivity, R.drawable.ic_content_copy_black), .setIcon(R.drawable.ic_content_copy_black, R.string.sharing_copy_text)
mActivity.getResources().getString(R.string.sharing_copy_text), (view) -> { .setFeatureNameForMetrics("SharingHubAndroid.CopyTextSelected")
RecordUserAction.record("SharingHubAndroid.CopyTextSelected"); .setOnClickCallback((view) -> {
recordTimeToShare(mShareStartTime);
mBottomSheetController.hideContent(mBottomSheetContent, true);
ClipboardManager clipboard = (ClipboardManager) mActivity.getSystemService( ClipboardManager clipboard = (ClipboardManager) mActivity.getSystemService(
Context.CLIPBOARD_SERVICE); Context.CLIPBOARD_SERVICE);
clipboard.setPrimaryClip( clipboard.setPrimaryClip(
ClipData.newPlainText(mShareParams.getTitle(), mShareParams.getText())); ClipData.newPlainText(mShareParams.getTitle(), mShareParams.getText()));
Toast.makeText(mActivity, R.string.text_copied, Toast.LENGTH_SHORT).show(); Toast.makeText(mActivity, R.string.text_copied, Toast.LENGTH_SHORT).show();
}); })
return new FirstPartyOption( .build();
propertyModel, Arrays.asList(ContentType.TEXT, ContentType.HIGHLIGHTED_TEXT));
} }
private FirstPartyOption createSendTabToSelfFirstPartyOption() { private FirstPartyOption createSendTabToSelfFirstPartyOption() {
PropertyModel propertyModel = ShareSheetPropertyModelBuilder.createPropertyModel( return new FirstPartyOptionBuilder(
AppCompatResources.getDrawable(mActivity, R.drawable.send_tab), ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE, ContentType.IMAGE)
mActivity.getResources().getString(R.string.send_tab_to_self_share_activity_title), .setIcon(R.drawable.send_tab, R.string.send_tab_to_self_share_activity_title)
(view) -> { .setFeatureNameForMetrics("SharingHubAndroid.SendTabToSelfSelected")
RecordUserAction.record("SharingHubAndroid.SendTabToSelfSelected"); .setOnClickCallback((view) -> {
recordTimeToShare(mShareStartTime);
mBottomSheetController.hideContent(mBottomSheetContent, true);
SendTabToSelfShareActivity.actionHandler(mActivity, mUrl, SendTabToSelfShareActivity.actionHandler(mActivity, mUrl,
mShareParams.getTitle(), mShareParams.getTitle(),
mTabProvider.get() mTabProvider.get()
...@@ -259,54 +290,41 @@ class ChromeProvidedSharingOptionsProvider { ...@@ -259,54 +290,41 @@ class ChromeProvidedSharingOptionsProvider {
.getVisibleEntry() .getVisibleEntry()
.getTimestamp(), .getTimestamp(),
mBottomSheetController); mBottomSheetController);
}); })
return new FirstPartyOption(propertyModel, .build();
Arrays.asList(ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE,
ContentType.IMAGE));
} }
private FirstPartyOption createQrCodeFirstPartyOption() { private FirstPartyOption createQrCodeFirstPartyOption() {
PropertyModel propertyModel = ShareSheetPropertyModelBuilder.createPropertyModel( return new FirstPartyOptionBuilder(
AppCompatResources.getDrawable(mActivity, R.drawable.qr_code), ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE, ContentType.IMAGE)
mActivity.getResources().getString(R.string.qr_code_share_icon_label), (view) -> { .setIcon(R.drawable.qr_code, R.string.qr_code_share_icon_label)
RecordUserAction.record("SharingHubAndroid.QRCodeSelected"); .setFeatureNameForMetrics("SharingHubAndroid.QRCodeSelected")
recordTimeToShare(mShareStartTime); .setOnClickCallback((view) -> {
mBottomSheetController.hideContent(mBottomSheetContent, true);
QrCodeCoordinator qrCodeCoordinator = new QrCodeCoordinator(mActivity, mUrl); QrCodeCoordinator qrCodeCoordinator = new QrCodeCoordinator(mActivity, mUrl);
qrCodeCoordinator.show(); qrCodeCoordinator.show();
}); })
return new FirstPartyOption(propertyModel, .build();
Arrays.asList(ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE,
ContentType.IMAGE));
} }
private FirstPartyOption createPrintingFirstPartyOption() { private FirstPartyOption createPrintingFirstPartyOption() {
PropertyModel propertyModel = ShareSheetPropertyModelBuilder.createPropertyModel( return new FirstPartyOptionBuilder(ContentType.LINK_PAGE_VISIBLE)
AppCompatResources.getDrawable(mActivity, R.drawable.sharing_print), .setIcon(R.drawable.sharing_print, R.string.print_share_activity_title)
mActivity.getResources().getString(R.string.print_share_activity_title), (view) -> { .setFeatureNameForMetrics("SharingHubAndroid.PrintSelected")
RecordUserAction.record("SharingHubAndroid.PrintSelected"); .setOnClickCallback((view) -> { mPrintTabCallback.onResult(mTabProvider.get()); })
recordTimeToShare(mShareStartTime); .build();
mBottomSheetController.hideContent(mBottomSheetContent, true);
mPrintTabCallback.onResult(mTabProvider.get());
});
return new FirstPartyOption(
propertyModel, Collections.singleton(ContentType.LINK_PAGE_VISIBLE));
} }
private FirstPartyOption createHighlightsFirstPartyOption() { private FirstPartyOption createHighlightsFirstPartyOption() {
PropertyModel propertyModel = ShareSheetPropertyModelBuilder.createPropertyModel( return new FirstPartyOptionBuilder(ContentType.HIGHLIGHTED_TEXT)
AppCompatResources.getDrawable(mActivity, R.drawable.link), .setIcon(R.drawable.link, R.string.sharing_highlights)
mActivity.getResources().getString(R.string.sharing_highlights), .setFeatureNameForMetrics("SharingHubAndroid.LinkToTextSelected")
(currentActivity) -> { .setOnClickCallback((view) -> {
RecordUserAction.record("SharingHubAndroid.LinkToTextSelected");
recordTimeToShare(mShareStartTime);
LinkToTextCoordinator linkToTextCoordinator = new LinkToTextCoordinator( LinkToTextCoordinator linkToTextCoordinator = new LinkToTextCoordinator(
mActivity, mTabProvider.get().getWindowAndroid(), mActivity, mTabProvider.get().getWindowAndroid(),
mChromeOptionShareCallback, mUrl, mShareParams.getText()); mChromeOptionShareCallback, mShareParams.getUrl(),
mBottomSheetController.hideContent(mBottomSheetContent, true); mShareParams.getText());
}); })
return new FirstPartyOption( .build();
propertyModel, Collections.singleton(ContentType.HIGHLIGHTED_TEXT));
} }
/** /**
......
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