OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.download; | 5 package org.chromium.chrome.browser.download; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.app.PendingIntent; | 8 import android.app.PendingIntent; |
9 import android.content.ActivityNotFoundException; | 9 import android.content.ActivityNotFoundException; |
10 import android.content.Context; | 10 import android.content.Context; |
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 | 446 |
447 // #getContentUriFromFile causes a disk read when it calls into FileProv
ider#getUriForFile. | 447 // #getContentUriFromFile causes a disk read when it calls into FileProv
ider#getUriForFile. |
448 // Obtaining a content URI is on the critical path for creating a share
intent after the | 448 // Obtaining a content URI is on the critical path for creating a share
intent after the |
449 // user taps on the share button, so even if we were to run this method
on a background | 449 // user taps on the share button, so even if we were to run this method
on a background |
450 // thread we would have to wait. As it depends on user-selected items, w
e cannot | 450 // thread we would have to wait. As it depends on user-selected items, w
e cannot |
451 // know/preload which URIs we need until the user presses share. | 451 // know/preload which URIs we need until the user presses share. |
452 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); | 452 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
453 try { | 453 try { |
454 // Try to obtain a content:// URI, which is preferred to a file:// U
RI so that | 454 // Try to obtain a content:// URI, which is preferred to a file:// U
RI so that |
455 // receiving apps don't attempt to determine the file's mime type (w
hich often fails). | 455 // receiving apps don't attempt to determine the file's mime type (w
hich often fails). |
456 uri = ContentUriUtils.getContentUriFromFile(ContextUtils.getApplicat
ionContext(), file); | 456 uri = ContentUriUtils.getContentUriFromFile(file); |
457 } catch (IllegalArgumentException e) { | 457 } catch (IllegalArgumentException e) { |
458 Log.e(TAG, "Could not create content uri: " + e); | 458 Log.e(TAG, "Could not create content uri: " + e); |
459 } | 459 } |
460 StrictMode.setThreadPolicy(oldPolicy); | 460 StrictMode.setThreadPolicy(oldPolicy); |
461 | 461 |
462 if (uri == null) uri = Uri.fromFile(file); | 462 if (uri == null) uri = Uri.fromFile(file); |
463 | 463 |
464 return uri; | 464 return uri; |
465 } | 465 } |
466 | 466 |
467 /** | 467 /** |
468 * Opens a file in Chrome or in another app if appropriate. | 468 * Opens a file in Chrome or in another app if appropriate. |
469 * @param file path to the file to open. | 469 * @param file path to the file to open. |
470 * @param mimeType mime type of the file. | 470 * @param mimeType mime type of the file. |
471 * @param downloadGuid The associated download GUID. | 471 * @param downloadGuid The associated download GUID. |
472 * @param isOffTheRecord whether we are in an off the record context. | 472 * @param isOffTheRecord whether we are in an off the record context. |
473 * @return whether the file could successfully be opened. | 473 * @return whether the file could successfully be opened. |
474 */ | 474 */ |
475 public static boolean openFile( | 475 public static boolean openFile( |
476 File file, String mimeType, String downloadGuid, boolean isOffTheRec
ord) { | 476 File file, String mimeType, String downloadGuid, boolean isOffTheRec
ord) { |
477 Context context = ContextUtils.getApplicationContext(); | 477 Context context = ContextUtils.getApplicationContext(); |
478 DownloadManagerService service = DownloadManagerService.getDownloadManag
erService(context); | 478 DownloadManagerService service = DownloadManagerService.getDownloadManag
erService(); |
479 | 479 |
480 // Check if Chrome should open the file itself. | 480 // Check if Chrome should open the file itself. |
481 if (service.isDownloadOpenableInBrowser(isOffTheRecord, mimeType)) { | 481 if (service.isDownloadOpenableInBrowser(isOffTheRecord, mimeType)) { |
482 // Share URIs use the content:// scheme when able, which looks bad w
hen displayed | 482 // Share URIs use the content:// scheme when able, which looks bad w
hen displayed |
483 // in the URL bar. | 483 // in the URL bar. |
484 Uri fileUri = Uri.fromFile(file); | 484 Uri fileUri = Uri.fromFile(file); |
485 Uri contentUri = getUriForItem(file); | 485 Uri contentUri = getUriForItem(file); |
486 String normalizedMimeType = Intent.normalizeMimeType(mimeType); | 486 String normalizedMimeType = Intent.normalizeMimeType(mimeType); |
487 | 487 |
488 Intent intent = | 488 Intent intent = |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
743 public static Date getDateAtMidnight(long timestamp) { | 743 public static Date getDateAtMidnight(long timestamp) { |
744 Calendar cal = Calendar.getInstance(); | 744 Calendar cal = Calendar.getInstance(); |
745 cal.setTimeInMillis(timestamp); | 745 cal.setTimeInMillis(timestamp); |
746 cal.set(Calendar.HOUR_OF_DAY, 0); | 746 cal.set(Calendar.HOUR_OF_DAY, 0); |
747 cal.set(Calendar.MINUTE, 0); | 747 cal.set(Calendar.MINUTE, 0); |
748 cal.set(Calendar.SECOND, 0); | 748 cal.set(Calendar.SECOND, 0); |
749 cal.set(Calendar.MILLISECOND, 0); | 749 cal.set(Calendar.MILLISECOND, 0); |
750 return cal.getTime(); | 750 return cal.getTime(); |
751 } | 751 } |
752 } | 752 } |
OLD | NEW |