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

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

Issue 2469303002: 📰Open Downloads suggestions externally when appropriate (Closed)
Patch Set: whitespace Created 4 years, 1 month 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 06cdf90ed6fd03f8841206482395d2be9cc8ec7d..d4521adef594ee3e0113832f7470baf70bbf0967 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
@@ -426,6 +426,45 @@ public static Uri getUriForItem(File file) {
return uri;
}
+ /**
+ * Opens a file in Chrome or in another app if appropriate.
+ * @param file path to the file to open.
+ * @param mimeType mime type of the file.
+ * @param isOffTheRecord whether we are in an off the record context.
+ * @return whether the file could successfully be opened.
+ */
+ public static boolean openFile(File file, String mimeType, boolean isOffTheRecord) {
+ Context context = ContextUtils.getApplicationContext();
+ Intent viewIntent = createViewIntentForDownloadItem(Uri.fromFile(file), mimeType);
+ DownloadManagerService service = DownloadManagerService.getDownloadManagerService(context);
+
+ // Check if Chrome should open the file itself.
+ if (service.isDownloadOpenableInBrowser(isOffTheRecord, mimeType)) {
+ // Share URIs use the content:// scheme when able, which looks bad when displayed
+ // in the URL bar.
+ Uri fileUri = Uri.fromFile(file);
+ Uri shareUri = getUriForItem(file);
+ String normalizedMimeType = Intent.normalizeMimeType(mimeType);
+
+ Intent intent =
+ getMediaViewerIntentForDownloadItem(fileUri, shareUri, normalizedMimeType);
+ IntentHandler.startActivityForTrustedIntent(intent, context);
+ return true;
+ }
+
+ // Check if any apps can open the file.
+ try {
+ context.startActivity(viewIntent);
+ return true;
+ } catch (ActivityNotFoundException e) {
+ // Can't launch the Intent.
+ Toast.makeText(context, context.getString(R.string.download_cant_open_file),
+ Toast.LENGTH_SHORT)
+ .show();
+ return false;
+ }
+ }
+
private static void recordShareHistograms(int count, int filterType) {
RecordHistogram.recordEnumeratedHistogram("Android.DownloadManager.Share.FileTypes",
filterType, DownloadFilter.FILTER_BOUNDARY);

Powered by Google App Engine
This is Rietveld 408576698