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 = [
"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/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/SharingJNIBridge.java",
"java/src/org/chromium/chrome/browser/sharing/SharingNotificationUtil.java",
......
......@@ -6,11 +6,7 @@ package org.chromium.chrome.browser.share;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
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.View;
import android.view.ViewGroup;
......@@ -18,7 +14,6 @@ import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.chrome.R;
import java.util.List;
......@@ -53,22 +48,7 @@ class ShareDialogAdapter extends ArrayAdapter<ResolveInfo> {
ImageView icon = (ImageView) view.findViewById(R.id.icon);
text.setText(getItem(position).loadLabel(mManager));
icon.setImageDrawable(loadIconForResolveInfo(getItem(position)));
icon.setImageDrawable(ShareHelper.loadIconForResolveInfo(getItem(position), mManager));
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);
}
}
\ No newline at end of file
}
......@@ -21,6 +21,8 @@ import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
......@@ -812,4 +814,23 @@ public class ShareHelper {
private static String getClassNameKey(@NonNull String sourcePackageName) {
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;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.content.res.AppCompatResources;
import android.view.View.OnClickListener;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ActivityTabProvider;
......@@ -35,6 +33,7 @@ public class ShareSheetCoordinator {
private final BottomSheetController mBottomSheetController;
private final ActivityTabProvider mActivityTabProvider;
private final TabCreatorManager.TabCreator mTabCreator;
private final ShareSheetPropertyModelBuilder mPropertyModelBuilder;
/**
* Constructs a new ShareSheetCoordinator.
......@@ -47,6 +46,7 @@ public class ShareSheetCoordinator {
mBottomSheetController = controller;
mActivityTabProvider = provider;
mTabCreator = tabCreator;
mPropertyModelBuilder = new ShareSheetPropertyModelBuilder(mBottomSheetController);
}
protected void showShareSheet(ShareParams params) {
......@@ -70,21 +70,21 @@ public class ShareSheetCoordinator {
ShareSheetBottomSheetContent bottomSheet, Activity activity) {
ArrayList<PropertyModel> models = new ArrayList<>();
// QR Codes
PropertyModel qrcodePropertyModel =
createPropertyModel(AppCompatResources.getDrawable(activity, R.drawable.qr_code),
activity.getResources().getString(R.string.qr_code_share_icon_label),
(currentActivity) -> {
mBottomSheetController.hideContent(bottomSheet, true);
QrCodeCoordinator qrCodeCoordinator =
new QrCodeCoordinator(activity, this::createNewTab);
qrCodeCoordinator.show();
});
PropertyModel qrcodePropertyModel = mPropertyModelBuilder.createPropertyModel(
AppCompatResources.getDrawable(activity, R.drawable.qr_code),
activity.getResources().getString(R.string.qr_code_share_icon_label),
(currentActivity) -> {
mBottomSheetController.hideContent(bottomSheet, true);
QrCodeCoordinator qrCodeCoordinator =
new QrCodeCoordinator(activity, this::createNewTab);
qrCodeCoordinator.show();
});
models.add(qrcodePropertyModel);
// Send Tab To Self
PropertyModel
sttsPropertyModel =
createPropertyModel(
PropertyModel sttsPropertyModel =
mPropertyModelBuilder
.createPropertyModel(
AppCompatResources.getDrawable(activity, R.drawable.send_tab),
activity.getResources().getString(
R.string.send_tab_to_self_share_activity_title),
......@@ -100,7 +100,7 @@ public class ShareSheetCoordinator {
models.add(sttsPropertyModel);
// Copy URL
PropertyModel copyPropertyModel = createPropertyModel(
PropertyModel copyPropertyModel = mPropertyModelBuilder.createPropertyModel(
AppCompatResources.getDrawable(activity, R.drawable.ic_content_copy_black),
activity.getResources().getString(R.string.sharing_copy_url), (params) -> {
mBottomSheetController.hideContent(bottomSheet, true);
......@@ -141,9 +141,10 @@ public class ShareSheetCoordinator {
private ArrayList<PropertyModel> createBottomRowPropertyModels(
ShareSheetBottomSheetContent bottomSheet, Activity activity, ShareParams params) {
ArrayList<PropertyModel> models = new ArrayList<>();
ArrayList<PropertyModel> models =
mPropertyModelBuilder.selectThirdPartyApps(activity, bottomSheet, params);
// More...
PropertyModel morePropertyModel = createPropertyModel(
PropertyModel morePropertyModel = mPropertyModelBuilder.createPropertyModel(
AppCompatResources.getDrawable(activity, R.drawable.sharing_more),
activity.getResources().getString(R.string.sharing_more_icon_label),
(shareParams) -> {
......@@ -155,17 +156,6 @@ public class ShareSheetCoordinator {
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) {
mTabCreator.createNewTab(
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