/** * Creates an Intent to open the file in another app by firing an Intent to Android. * * @param fileUri Uri pointing to the file. * @param mimeType MIME type for the file. * @return Intent that can be used to start an Activity for the file. */ public static Intent createViewIntentForDownloadItem(Uri fileUri, String mimeType) { Intent fileIntent = new Intent(Intent.ACTION_VIEW); String normalizedMimeType = Intent.normalizeMimeType(mimeType); if (TextUtils.isEmpty(normalizedMimeType)) { fileIntent.setData(fileUri); } else { fileIntent.setDataAndType(fileUri, normalizedMimeType); } fileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); fileIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); return fileIntent; }
/** * Creates an Intent to share {@code items} with another app by firing an Intent to Android. * * <p>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.getFile())); } 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) { // Sharing a DownloadItem. shareIntent.putExtra(Intent.EXTRA_STREAM, getUriForItem(items.get(0).getFile())); } 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; }