Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(679)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/download/DownloadUtils.java

Issue 2343363005: [Download Home] Open supported files in Custom Tabs (Closed)
Patch Set: Comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/download/DownloadUtils.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadUtils.java b/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadUtils.java
index cef55b3c0f0d678ce4fadba72e5556de1b0ec77d..274745156e4f962cdbc826e0571aa988fec3733b 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadUtils.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadUtils.java
@@ -7,7 +7,13 @@ package org.chromium.chrome.browser.download;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
+import android.net.Uri;
+import android.os.StrictMode;
+import android.text.TextUtils;
+import org.chromium.base.ContentUriUtils;
+import org.chromium.base.ContextUtils;
+import org.chromium.base.Log;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.metrics.RecordUserAction;
import org.chromium.chrome.R;
@@ -16,6 +22,8 @@ import org.chromium.chrome.browser.IntentHandler;
import org.chromium.chrome.browser.UrlConstants;
import org.chromium.chrome.browser.download.ui.BackendProvider;
import org.chromium.chrome.browser.download.ui.BackendProvider.DownloadDelegate;
+import org.chromium.chrome.browser.download.ui.DownloadFilter;
+import org.chromium.chrome.browser.download.ui.DownloadHistoryItemWrapper;
import org.chromium.chrome.browser.offlinepages.downloads.OfflinePageDownloadBridge;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType;
@@ -25,11 +33,20 @@ import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.ui.base.DeviceFormFactor;
import org.chromium.ui.widget.Toast;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* A class containing some utility static methods.
*/
public class DownloadUtils {
+ private static final String TAG = "download";
+
+ private static final String DEFAULT_MIME_TYPE = "*/*";
+ private static final String MIME_TYPE_DELIMITER = "/";
+
private static final String EXTRA_IS_OFF_THE_RECORD =
"org.chromium.chrome.browser.download.IS_OFF_THE_RECORD";
@@ -135,4 +152,154 @@ public class DownloadUtils {
return true;
}
+
+ /**
+ * Creates an Intent to open the file in another app by firing an Intent to Android.
+ * @param item Item to open.
+ * @param file File pointing at the DownloadItem.
+ * @return Intent that can be used to start an Activity for the DownloadItem.
+ */
+ public static Intent createViewIntentForDownloadItem(DownloadItem item, File file) {
+ String mimeType = Intent.normalizeMimeType(item.getDownloadInfo().getMimeType());
+ Uri fileUri = Uri.fromFile(file);
+ Intent fileIntent = new Intent();
+ fileIntent.setAction(Intent.ACTION_VIEW);
+ if (TextUtils.isEmpty(mimeType)) {
+ fileIntent.setData(fileUri);
+ } else {
+ fileIntent.setDataAndType(fileUri, mimeType);
+ }
+ fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ return fileIntent;
+ }
+
+ /**
+ * Creates an Intent to share {@code items} with another app by firing an Intent to Android.
+ *
+ * Sharing a DownloadItem shares the file itself, while sharing an OfflinePageItem shares the
+ * URL.
+ *
+ * @param items Items to share.
+ * @return Intent that can be used to share the items.
+ */
+ public static Intent createShareIntent(List<DownloadHistoryItemWrapper> items) {
+ Intent shareIntent = new Intent();
+ String intentAction;
+ ArrayList<Uri> itemUris = new ArrayList<Uri>();
+ StringBuilder offlinePagesString = new StringBuilder();
+ int selectedItemsFilterType = items.get(0).getFilterType();
+
+ String intentMimeType = "";
+ String[] intentMimeParts = {"", ""};
+
+ for (int i = 0; i < items.size(); i++) {
+ DownloadHistoryItemWrapper wrappedItem = items.get(i);
+
+ if (wrappedItem instanceof DownloadHistoryItemWrapper.OfflinePageItemWrapper) {
+ if (offlinePagesString.length() != 0) {
+ offlinePagesString.append("\n");
+ }
+ offlinePagesString.append(wrappedItem.getUrl());
+ } else {
+ itemUris.add(getUriForItem(wrappedItem));
+ }
+
+ if (selectedItemsFilterType != wrappedItem.getFilterType()) {
+ selectedItemsFilterType = DownloadFilter.FILTER_ALL;
+ }
+
+ String mimeType = Intent.normalizeMimeType(wrappedItem.getMimeType());
+
+ // If a mime type was not retrieved from the backend or could not be normalized,
+ // set the mime type to the default.
+ if (TextUtils.isEmpty(mimeType)) {
+ intentMimeType = DEFAULT_MIME_TYPE;
+ continue;
+ }
+
+ // If the intent mime type has not been set yet, set it to the mime type for this item.
+ if (TextUtils.isEmpty(intentMimeType)) {
+ intentMimeType = mimeType;
+ if (!TextUtils.isEmpty(intentMimeType)) {
+ intentMimeParts = intentMimeType.split(MIME_TYPE_DELIMITER);
+ // Guard against invalid mime types.
+ if (intentMimeParts.length != 2) intentMimeType = DEFAULT_MIME_TYPE;
+ }
+ continue;
+ }
+
+ // Either the mime type is already the default or it matches the current item's mime
+ // type. In either case, intentMimeType is already the correct value.
+ if (TextUtils.equals(intentMimeType, DEFAULT_MIME_TYPE)
+ || TextUtils.equals(intentMimeType, mimeType)) {
+ continue;
+ }
+
+ String[] mimeParts = mimeType.split(MIME_TYPE_DELIMITER);
+ if (!TextUtils.equals(intentMimeParts[0], mimeParts[0])) {
+ // The top-level types don't match; fallback to the default mime type.
+ intentMimeType = DEFAULT_MIME_TYPE;
+ } else {
+ // The mime type should be {top-level type}/*
+ intentMimeType = intentMimeParts[0] + MIME_TYPE_DELIMITER + "*";
+ }
+ }
+
+ // Use Action_SEND if there is only one downloaded item or only text to share.
+ if (itemUris.size() == 0 || (itemUris.size() == 1 && offlinePagesString.length() == 0)) {
+ intentAction = Intent.ACTION_SEND;
+ } else {
+ intentAction = Intent.ACTION_SEND_MULTIPLE;
+ }
+
+ if (itemUris.size() == 1 && offlinePagesString.length() == 0) {
+ shareIntent.putExtra(Intent.EXTRA_STREAM, getUriForItem(items.get(0)));
+ } else {
+ shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, itemUris);
+ }
+
+ if (offlinePagesString.length() != 0) {
+ shareIntent.putExtra(Intent.EXTRA_TEXT, offlinePagesString.toString());
+ }
+
+ shareIntent.setAction(intentAction);
+ shareIntent.setType(intentMimeType);
+ shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+
+ recordShareHistograms(items.size(), selectedItemsFilterType);
+
+ return shareIntent;
+ }
+
+ private static Uri getUriForItem(DownloadHistoryItemWrapper itemWrapper) {
+ Uri uri = null;
+
+ // #getContentUriFromFile causes a disk read when it calls into FileProvider#getUriForFile.
+ // Obtaining a content URI is on the critical path for creating a share intent after the
+ // user taps on the share button, so even if we were to run this method on a background
+ // thread we would have to wait. As it depends on user-selected items, we cannot
+ // know/preload which URIs we need until the user presses share.
+ StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
+ try {
+ // Try to obtain a content:// URI, which is preferred to a file:/// URI so that
+ // receiving apps don't attempt to determine the file's mime type (which often fails).
+ uri = ContentUriUtils.getContentUriFromFile(ContextUtils.getApplicationContext(),
+ itemWrapper.getFile());
+ } catch (IllegalArgumentException e) {
+ Log.e(TAG, "Could not create content uri: " + e);
+ }
+ StrictMode.setThreadPolicy(oldPolicy);
+
+ if (uri == null) uri = Uri.fromFile(itemWrapper.getFile());
+
+ return uri;
+ }
+
+ private static void recordShareHistograms(int count, int filterType) {
+ RecordHistogram.recordEnumeratedHistogram("Android.DownloadManager.Share.FileTypes",
+ filterType, DownloadFilter.FILTER_BOUNDARY);
+
+ RecordHistogram.recordLinearCountHistogram("Android.DownloadManager.Share.Count",
+ count, 1, 20, 20);
+ }
}

Powered by Google App Engine
This is Rietveld 408576698