Commit 057577ff authored by Kyle Milka's avatar Kyle Milka Committed by Commit Bot

[SharingHub] Start bottom row

Show 7 3P sharing options in the bottom row. For now these are just
the first 7 things returned by the package manager, no filtering or
sorting is applied.

Bug: 1009124
Change-Id: I6bae7e52e14a044192a8f8c9b9bd550525c300ba
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1995467
Commit-Queue: Kyle Milka <kmilka@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Reviewed-by: default avatarJeffrey Cohen <jeffreycohen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#732889}
parent 5b14716c
...@@ -1503,6 +1503,7 @@ chrome_java_sources = [ ...@@ -1503,6 +1503,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/share/ShareSheetBottomSheetContent.java", "java/src/org/chromium/chrome/browser/share/ShareSheetBottomSheetContent.java",
"java/src/org/chromium/chrome/browser/share/ShareSheetCoordinator.java", "java/src/org/chromium/chrome/browser/share/ShareSheetCoordinator.java",
"java/src/org/chromium/chrome/browser/share/ShareSheetItemViewProperties.java", "java/src/org/chromium/chrome/browser/share/ShareSheetItemViewProperties.java",
"java/src/org/chromium/chrome/browser/share/ShareSheetPropertyModelBuilder.java",
"java/src/org/chromium/chrome/browser/sharing/SharingAdapter.java", "java/src/org/chromium/chrome/browser/sharing/SharingAdapter.java",
"java/src/org/chromium/chrome/browser/sharing/SharingJNIBridge.java", "java/src/org/chromium/chrome/browser/sharing/SharingJNIBridge.java",
"java/src/org/chromium/chrome/browser/sharing/SharingNotificationUtil.java", "java/src/org/chromium/chrome/browser/sharing/SharingNotificationUtil.java",
......
...@@ -6,11 +6,7 @@ package org.chromium.chrome.browser.share; ...@@ -6,11 +6,7 @@ package org.chromium.chrome.browser.share;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
...@@ -18,7 +14,6 @@ import android.widget.ArrayAdapter; ...@@ -18,7 +14,6 @@ import android.widget.ArrayAdapter;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import java.util.List; import java.util.List;
...@@ -53,22 +48,7 @@ class ShareDialogAdapter extends ArrayAdapter<ResolveInfo> { ...@@ -53,22 +48,7 @@ class ShareDialogAdapter extends ArrayAdapter<ResolveInfo> {
ImageView icon = (ImageView) view.findViewById(R.id.icon); ImageView icon = (ImageView) view.findViewById(R.id.icon);
text.setText(getItem(position).loadLabel(mManager)); text.setText(getItem(position).loadLabel(mManager));
icon.setImageDrawable(loadIconForResolveInfo(getItem(position))); icon.setImageDrawable(ShareHelper.loadIconForResolveInfo(getItem(position), mManager));
return view; return view;
} }
private Drawable loadIconForResolveInfo(ResolveInfo info) {
try {
final int iconRes = info.getIconResource();
if (iconRes != 0) {
Resources res = mManager.getResourcesForApplication(info.activityInfo.packageName);
Drawable icon = ApiCompatibilityUtils.getDrawable(res, iconRes);
return icon;
}
} catch (NameNotFoundException | NotFoundException e) {
// Could not find the icon. loadIcon call below will return the default app icon.
}
return info.loadIcon(mManager);
}
} }
...@@ -21,6 +21,8 @@ import android.content.pm.ActivityInfo; ...@@ -21,6 +21,8 @@ import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
...@@ -812,4 +814,23 @@ public class ShareHelper { ...@@ -812,4 +814,23 @@ public class ShareHelper {
private static String getClassNameKey(@NonNull String sourcePackageName) { private static String getClassNameKey(@NonNull String sourcePackageName) {
return sourcePackageName + CLASS_NAME_KEY_SUFFIX; return sourcePackageName + CLASS_NAME_KEY_SUFFIX;
} }
/**
* Loads the icon for the provided ResolveInfo.
* @param info The ResolveInfo to load the icon for.
* @param manager The package manager to use to load the icon.
*/
static Drawable loadIconForResolveInfo(ResolveInfo info, PackageManager manager) {
try {
final int iconRes = info.getIconResource();
if (iconRes != 0) {
Resources res = manager.getResourcesForApplication(info.activityInfo.packageName);
Drawable icon = ApiCompatibilityUtils.getDrawable(res, iconRes);
return icon;
}
} catch (NameNotFoundException | NotFoundException e) {
// Could not find the icon. loadIcon call below will return the default app icon.
}
return info.loadIcon(manager);
}
} }
...@@ -8,9 +8,7 @@ import android.app.Activity; ...@@ -8,9 +8,7 @@ import android.app.Activity;
import android.content.ClipData; import android.content.ClipData;
import android.content.ClipboardManager; import android.content.ClipboardManager;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.content.res.AppCompatResources; import android.support.v7.content.res.AppCompatResources;
import android.view.View.OnClickListener;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.ActivityTabProvider; import org.chromium.chrome.browser.ActivityTabProvider;
...@@ -35,6 +33,7 @@ public class ShareSheetCoordinator { ...@@ -35,6 +33,7 @@ public class ShareSheetCoordinator {
private final BottomSheetController mBottomSheetController; private final BottomSheetController mBottomSheetController;
private final ActivityTabProvider mActivityTabProvider; private final ActivityTabProvider mActivityTabProvider;
private final TabCreatorManager.TabCreator mTabCreator; private final TabCreatorManager.TabCreator mTabCreator;
private final ShareSheetPropertyModelBuilder mPropertyModelBuilder;
/** /**
* Constructs a new ShareSheetCoordinator. * Constructs a new ShareSheetCoordinator.
...@@ -47,6 +46,7 @@ public class ShareSheetCoordinator { ...@@ -47,6 +46,7 @@ public class ShareSheetCoordinator {
mBottomSheetController = controller; mBottomSheetController = controller;
mActivityTabProvider = provider; mActivityTabProvider = provider;
mTabCreator = tabCreator; mTabCreator = tabCreator;
mPropertyModelBuilder = new ShareSheetPropertyModelBuilder(mBottomSheetController);
} }
protected void showShareSheet(ShareParams params) { protected void showShareSheet(ShareParams params) {
...@@ -70,8 +70,8 @@ public class ShareSheetCoordinator { ...@@ -70,8 +70,8 @@ public class ShareSheetCoordinator {
ShareSheetBottomSheetContent bottomSheet, Activity activity) { ShareSheetBottomSheetContent bottomSheet, Activity activity) {
ArrayList<PropertyModel> models = new ArrayList<>(); ArrayList<PropertyModel> models = new ArrayList<>();
// QR Codes // QR Codes
PropertyModel qrcodePropertyModel = PropertyModel qrcodePropertyModel = mPropertyModelBuilder.createPropertyModel(
createPropertyModel(AppCompatResources.getDrawable(activity, R.drawable.qr_code), AppCompatResources.getDrawable(activity, R.drawable.qr_code),
activity.getResources().getString(R.string.qr_code_share_icon_label), activity.getResources().getString(R.string.qr_code_share_icon_label),
(currentActivity) -> { (currentActivity) -> {
mBottomSheetController.hideContent(bottomSheet, true); mBottomSheetController.hideContent(bottomSheet, true);
...@@ -82,9 +82,9 @@ public class ShareSheetCoordinator { ...@@ -82,9 +82,9 @@ public class ShareSheetCoordinator {
models.add(qrcodePropertyModel); models.add(qrcodePropertyModel);
// Send Tab To Self // Send Tab To Self
PropertyModel PropertyModel sttsPropertyModel =
sttsPropertyModel = mPropertyModelBuilder
createPropertyModel( .createPropertyModel(
AppCompatResources.getDrawable(activity, R.drawable.send_tab), AppCompatResources.getDrawable(activity, R.drawable.send_tab),
activity.getResources().getString( activity.getResources().getString(
R.string.send_tab_to_self_share_activity_title), R.string.send_tab_to_self_share_activity_title),
...@@ -100,7 +100,7 @@ public class ShareSheetCoordinator { ...@@ -100,7 +100,7 @@ public class ShareSheetCoordinator {
models.add(sttsPropertyModel); models.add(sttsPropertyModel);
// Copy URL // Copy URL
PropertyModel copyPropertyModel = createPropertyModel( PropertyModel copyPropertyModel = mPropertyModelBuilder.createPropertyModel(
AppCompatResources.getDrawable(activity, R.drawable.ic_content_copy_black), AppCompatResources.getDrawable(activity, R.drawable.ic_content_copy_black),
activity.getResources().getString(R.string.sharing_copy_url), (params) -> { activity.getResources().getString(R.string.sharing_copy_url), (params) -> {
mBottomSheetController.hideContent(bottomSheet, true); mBottomSheetController.hideContent(bottomSheet, true);
...@@ -141,9 +141,10 @@ public class ShareSheetCoordinator { ...@@ -141,9 +141,10 @@ public class ShareSheetCoordinator {
private ArrayList<PropertyModel> createBottomRowPropertyModels( private ArrayList<PropertyModel> createBottomRowPropertyModels(
ShareSheetBottomSheetContent bottomSheet, Activity activity, ShareParams params) { ShareSheetBottomSheetContent bottomSheet, Activity activity, ShareParams params) {
ArrayList<PropertyModel> models = new ArrayList<>(); ArrayList<PropertyModel> models =
mPropertyModelBuilder.selectThirdPartyApps(activity, bottomSheet, params);
// More... // More...
PropertyModel morePropertyModel = createPropertyModel( PropertyModel morePropertyModel = mPropertyModelBuilder.createPropertyModel(
AppCompatResources.getDrawable(activity, R.drawable.sharing_more), AppCompatResources.getDrawable(activity, R.drawable.sharing_more),
activity.getResources().getString(R.string.sharing_more_icon_label), activity.getResources().getString(R.string.sharing_more_icon_label),
(shareParams) -> { (shareParams) -> {
...@@ -155,17 +156,6 @@ public class ShareSheetCoordinator { ...@@ -155,17 +156,6 @@ public class ShareSheetCoordinator {
return models; return models;
} }
private PropertyModel createPropertyModel(
Drawable icon, String label, OnClickListener listener) {
PropertyModel propertyModel =
new PropertyModel.Builder(ShareSheetItemViewProperties.ALL_KEYS)
.with(ShareSheetItemViewProperties.ICON, icon)
.with(ShareSheetItemViewProperties.LABEL, label)
.with(ShareSheetItemViewProperties.CLICK_LISTENER, listener)
.build();
return propertyModel;
}
private void createNewTab(String url) { private void createNewTab(String url) {
mTabCreator.createNewTab( mTabCreator.createNewTab(
new LoadUrlParams(url), TabLaunchType.FROM_LINK, mActivityTabProvider.get()); new LoadUrlParams(url), TabLaunchType.FROM_LINK, mActivityTabProvider.get());
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.share;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.view.View.OnClickListener;
import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetController;
import org.chromium.ui.modelutil.PropertyModel;
import java.util.ArrayList;
import java.util.List;
/**
* Handles displaying the share sheet. The version used depends on several
* conditions.
* Android K and below: custom share dialog
* Android L+: system share sheet
* #chrome-sharing-hub enabled: custom share sheet
*/
class ShareSheetPropertyModelBuilder {
private final BottomSheetController mBottomSheetController;
ShareSheetPropertyModelBuilder(BottomSheetController bottomSheetController) {
mBottomSheetController = bottomSheetController;
}
protected ArrayList<PropertyModel> selectThirdPartyApps(
Activity activity, ShareSheetBottomSheetContent bottomSheet, ShareParams params) {
Intent intent = ShareHelper.getShareLinkAppCompatibilityIntent();
final ShareHelper.TargetChosenCallback callback = params.getCallback();
PackageManager manager = activity.getPackageManager();
List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent, 0);
List<ResolveInfo> thirdPartyActivities = new ArrayList<>();
for (ResolveInfo res : resolveInfoList) {
if (!res.activityInfo.packageName.equals(activity.getPackageName())) {
thirdPartyActivities.add(res);
}
}
ArrayList<PropertyModel> models = new ArrayList<>();
for (int i = 0; i < 7 && i < thirdPartyActivities.size(); ++i) {
ResolveInfo info = thirdPartyActivities.get(i);
PropertyModel propertyModel =
createPropertyModel(ShareHelper.loadIconForResolveInfo(info, manager),
(String) info.loadLabel(manager), (shareParams) -> {
ActivityInfo ai = info.activityInfo;
ComponentName component =
new ComponentName(ai.applicationInfo.packageName, ai.name);
if (callback != null) {
callback.onTargetChosen(component);
}
if (params.saveLastUsed()) {
ShareHelper.setLastShareComponentName(
component, params.getSourcePackageName());
}
mBottomSheetController.hideContent(bottomSheet, true);
ShareHelper.makeIntentAndShare(params, component);
});
models.add(propertyModel);
}
return models;
}
protected PropertyModel createPropertyModel(
Drawable icon, String label, OnClickListener listener) {
PropertyModel propertyModel =
new PropertyModel.Builder(ShareSheetItemViewProperties.ALL_KEYS)
.with(ShareSheetItemViewProperties.ICON, icon)
.with(ShareSheetItemViewProperties.LABEL, label)
.with(ShareSheetItemViewProperties.CLICK_LISTENER, listener)
.build();
return propertyModel;
}
}
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