| Index: chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadHistoryItemWrapper.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadHistoryItemWrapper.java b/chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadHistoryItemWrapper.java
|
| index cf82eb29ebce394911464cc1b6a1d7b1f15808c6..f37d936c2b6888380dff7dfd9fd9dc8b6f8b1942 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadHistoryItemWrapper.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadHistoryItemWrapper.java
|
| @@ -4,22 +4,35 @@
|
|
|
| package org.chromium.chrome.browser.download.ui;
|
|
|
| +import android.app.PendingIntent;
|
| import android.content.ActivityNotFoundException;
|
| import android.content.ComponentName;
|
| import android.content.Context;
|
| import android.content.Intent;
|
| +import android.graphics.Bitmap;
|
| +import android.graphics.BitmapFactory;
|
| +import android.graphics.Color;
|
| import android.net.Uri;
|
| +import android.provider.Browser;
|
| +import android.support.customtabs.CustomTabsIntent;
|
| import android.text.TextUtils;
|
|
|
| import org.chromium.base.ContextUtils;
|
| import org.chromium.base.metrics.RecordHistogram;
|
| import org.chromium.chrome.R;
|
| +import org.chromium.chrome.browser.IntentHandler;
|
| +import org.chromium.chrome.browser.UrlConstants;
|
| +import org.chromium.chrome.browser.customtabs.CustomTabActivity;
|
| +import org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider;
|
| import org.chromium.chrome.browser.download.DownloadItem;
|
| +import org.chromium.chrome.browser.download.DownloadUtils;
|
| import org.chromium.chrome.browser.offlinepages.downloads.OfflinePageDownloadItem;
|
| import org.chromium.chrome.browser.widget.DateDividedAdapter.TimedItem;
|
| import org.chromium.ui.widget.Toast;
|
|
|
| import java.io.File;
|
| +import java.util.ArrayList;
|
| +import java.util.List;
|
| import java.util.Locale;
|
|
|
| /** Wraps different classes that contain information about downloads. */
|
| @@ -27,10 +40,12 @@ public abstract class DownloadHistoryItemWrapper implements TimedItem {
|
| private static final String TAG = "download_ui";
|
|
|
| protected final BackendProvider mBackendProvider;
|
| + protected final ComponentName mComponentName;
|
| private Long mStableId;
|
|
|
| - private DownloadHistoryItemWrapper(BackendProvider provider) {
|
| + private DownloadHistoryItemWrapper(BackendProvider provider, ComponentName component) {
|
| mBackendProvider = provider;
|
| + mComponentName = component;
|
| }
|
|
|
| @Override
|
| @@ -53,7 +68,7 @@ public abstract class DownloadHistoryItemWrapper implements TimedItem {
|
| abstract String getFilePath();
|
|
|
| /** @return The file where the download resides. */
|
| - abstract File getFile();
|
| + public abstract File getFile();
|
|
|
| /** @return String to display for the file. */
|
| abstract String getDisplayFileName();
|
| @@ -62,13 +77,13 @@ public abstract class DownloadHistoryItemWrapper implements TimedItem {
|
| abstract long getFileSize();
|
|
|
| /** @return URL the file was downloaded from. */
|
| - abstract String getUrl();
|
| + public abstract String getUrl();
|
|
|
| /** @return {@link DownloadFilter} that represents the file type. */
|
| - abstract int getFilterType();
|
| + public abstract int getFilterType();
|
|
|
| /** @return The mime type or null if the item doesn't have one. */
|
| - abstract String getMimeType();
|
| + public abstract String getMimeType();
|
|
|
| /** Called when the user wants to open the file. */
|
| abstract void open();
|
| @@ -107,8 +122,9 @@ public abstract class DownloadHistoryItemWrapper implements TimedItem {
|
| private final boolean mIsOffTheRecord;
|
| private File mFile;
|
|
|
| - DownloadItemWrapper(DownloadItem item, boolean isOffTheRecord, BackendProvider provider) {
|
| - super(provider);
|
| + DownloadItemWrapper(DownloadItem item, boolean isOffTheRecord, BackendProvider provider,
|
| + ComponentName component) {
|
| + super(provider, component);
|
| mItem = item;
|
| mIsOffTheRecord = isOffTheRecord;
|
| }
|
| @@ -167,6 +183,7 @@ public abstract class DownloadHistoryItemWrapper implements TimedItem {
|
| @Override
|
| public void open() {
|
| Context context = ContextUtils.getApplicationContext();
|
| + Intent viewIntent = DownloadUtils.createViewIntentForDownloadItem(mItem, getFile());
|
|
|
| if (mItem.hasBeenExternallyRemoved()) {
|
| Toast.makeText(context, context.getString(R.string.download_cant_open_file),
|
| @@ -174,21 +191,45 @@ public abstract class DownloadHistoryItemWrapper implements TimedItem {
|
| return;
|
| }
|
|
|
| - String mimeType = Intent.normalizeMimeType(mItem.getDownloadInfo().getMimeType());
|
| - Uri fileUri = Uri.fromFile(getFile());
|
| -
|
| - // Check if any apps can open the file.
|
| - Intent fileIntent = new Intent();
|
| - fileIntent.setAction(Intent.ACTION_VIEW);
|
| - if (TextUtils.isEmpty(mimeType)) {
|
| - fileIntent.setData(fileUri);
|
| - } else {
|
| - fileIntent.setDataAndType(fileUri, mimeType);
|
| + // Check if Chrome should open the file itself.
|
| + if (mBackendProvider.getDownloadDelegate().isDownloadOpenableInBrowser(
|
| + mItem.getId(), mIsOffTheRecord)) {
|
| + CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
|
| + builder.setToolbarColor(Color.BLACK);
|
| + builder.setShowTitle(true);
|
| +
|
| + // Create a PendingIntent that can be used to view the file externally.
|
| + PendingIntent pendingViewIntent =
|
| + PendingIntent.getActivity(context, 0, viewIntent, 0);
|
| + builder.addMenuItem(context.getString(R.string.download_manager_view_externally),
|
| + pendingViewIntent);
|
| +
|
| + // Create a PendingIntent that shares the file with external apps.
|
| + List<DownloadHistoryItemWrapper> items = new ArrayList<>();
|
| + items.add(this);
|
| + PendingIntent pendingShareIntent = PendingIntent.getActivity(
|
| + context, 0, DownloadUtils.createShareIntent(items), 0);
|
| + Bitmap bitmap = BitmapFactory.decodeResource(
|
| + context.getResources(), R.drawable.ic_share_white_24dp);
|
| + builder.setActionButton(
|
| + bitmap, context.getString(R.string.share), pendingShareIntent, true);
|
| +
|
| + // Build up the Intent further.
|
| + Intent intent = builder.build().intent;
|
| + intent.setAction(Intent.ACTION_VIEW);
|
| + intent.setData(Uri.parse(UrlConstants.FILE_SCHEME + getFilePath()));
|
| + intent.putExtra(CustomTabIntentDataProvider.EXTRA_IS_MEDIA_VIEWER, true);
|
| + intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
|
| +
|
| + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
| + intent.setClass(context, CustomTabActivity.class);
|
| + IntentHandler.startActivityForTrustedIntent(intent, context);
|
| + return;
|
| }
|
| - fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
| + // Check if any apps can open the file.
|
| try {
|
| - context.startActivity(fileIntent);
|
| + context.startActivity(viewIntent);
|
| recordOpenSuccess();
|
| } catch (ActivityNotFoundException e) {
|
| // Can't launch the Intent.
|
| @@ -232,16 +273,14 @@ public abstract class DownloadHistoryItemWrapper implements TimedItem {
|
| }
|
|
|
| /** Wraps a {@link OfflinePageDownloadItem}. */
|
| - static class OfflinePageItemWrapper extends DownloadHistoryItemWrapper {
|
| + public static class OfflinePageItemWrapper extends DownloadHistoryItemWrapper {
|
| private final OfflinePageDownloadItem mItem;
|
| - private final ComponentName mComponent;
|
| private File mFile;
|
|
|
| OfflinePageItemWrapper(OfflinePageDownloadItem item, BackendProvider provider,
|
| ComponentName component) {
|
| - super(provider);
|
| + super(provider, component);
|
| mItem = item;
|
| - mComponent = component;
|
| }
|
|
|
| @Override
|
| @@ -303,7 +342,7 @@ public abstract class DownloadHistoryItemWrapper implements TimedItem {
|
|
|
| @Override
|
| public void open() {
|
| - mBackendProvider.getOfflinePageBridge().openItem(getId(), mComponent);
|
| + mBackendProvider.getOfflinePageBridge().openItem(getId(), mComponentName);
|
| recordOpenSuccess();
|
| }
|
|
|
|
|