Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.offlinepages; | 5 package org.chromium.chrome.browser.offlinepages; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.content.Context; | 8 import android.content.Context; |
| 9 import android.net.Uri; | 9 import android.net.Uri; |
| 10 import android.os.AsyncTask; | 10 import android.os.AsyncTask; |
| 11 import android.os.Environment; | 11 import android.os.Environment; |
| 12 | 12 |
| 13 import org.chromium.base.ActivityState; | 13 import org.chromium.base.ActivityState; |
| 14 import org.chromium.base.ApplicationStatus; | 14 import org.chromium.base.ApplicationStatus; |
| 15 import org.chromium.base.Callback; | 15 import org.chromium.base.Callback; |
| 16 import org.chromium.base.FileUtils; | 16 import org.chromium.base.FileUtils; |
| 17 import org.chromium.base.Log; | 17 import org.chromium.base.Log; |
| 18 import org.chromium.base.StreamUtil; | 18 import org.chromium.base.StreamUtil; |
| 19 import org.chromium.base.VisibleForTesting; | 19 import org.chromium.base.VisibleForTesting; |
| 20 import org.chromium.base.metrics.RecordHistogram; | 20 import org.chromium.base.metrics.RecordHistogram; |
| 21 import org.chromium.chrome.R; | 21 import org.chromium.chrome.R; |
| 22 import org.chromium.chrome.browser.ChromeActivity; | 22 import org.chromium.chrome.browser.ChromeActivity; |
| 23 import org.chromium.chrome.browser.UrlConstants; | 23 import org.chromium.chrome.browser.UrlConstants; |
| 24 import org.chromium.chrome.browser.profiles.Profile; | 24 import org.chromium.chrome.browser.profiles.Profile; |
| 25 import org.chromium.chrome.browser.share.ShareHelper; | 25 import org.chromium.chrome.browser.share.ShareHelper; |
| 26 import org.chromium.chrome.browser.share.ShareParams; | |
| 26 import org.chromium.chrome.browser.snackbar.Snackbar; | 27 import org.chromium.chrome.browser.snackbar.Snackbar; |
| 27 import org.chromium.chrome.browser.snackbar.SnackbarManager; | 28 import org.chromium.chrome.browser.snackbar.SnackbarManager; |
| 28 import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController; | 29 import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController; |
| 29 import org.chromium.chrome.browser.tab.EmptyTabObserver; | 30 import org.chromium.chrome.browser.tab.EmptyTabObserver; |
| 30 import org.chromium.chrome.browser.tab.Tab; | 31 import org.chromium.chrome.browser.tab.Tab; |
| 31 import org.chromium.chrome.browser.tabmodel.TabModel; | 32 import org.chromium.chrome.browser.tabmodel.TabModel; |
| 32 import org.chromium.chrome.browser.tabmodel.TabModelSelector; | 33 import org.chromium.chrome.browser.tabmodel.TabModelSelector; |
| 33 import org.chromium.chrome.browser.tabmodel.TabModelSelectorTabModelObserver; | 34 import org.chromium.chrome.browser.tabmodel.TabModelSelectorTabModelObserver; |
| 34 import org.chromium.components.bookmarks.BookmarkId; | 35 import org.chromium.components.bookmarks.BookmarkId; |
| 35 import org.chromium.components.offlinepages.SavePageResult; | 36 import org.chromium.components.offlinepages.SavePageResult; |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 328 return; | 329 return; |
| 329 } | 330 } |
| 330 RecordHistogram.recordLongTimesHistogram( | 331 RecordHistogram.recordLongTimesHistogram( |
| 331 "OfflinePages.Wakeup.DelayTime", | 332 "OfflinePages.Wakeup.DelayTime", |
| 332 delayInMilliseconds, | 333 delayInMilliseconds, |
| 333 TimeUnit.MILLISECONDS); | 334 TimeUnit.MILLISECONDS); |
| 334 } | 335 } |
| 335 | 336 |
| 336 /** | 337 /** |
| 337 * Share an offline copy of the current page. | 338 * Share an offline copy of the current page. |
| 338 * @param shareDirectly Whether it should share directly with the activity t hat was most | 339 * @param builder The builder to construct {@link ShareParams} for holding s hare parameters. |
| 339 * recently used to share. | |
| 340 * @param saveLastUsed Whether to save the chosen activity for future direct sharing. | |
| 341 * @param mainActivity Activity that is used to access package manager. | |
| 342 * @param text Text to be shared. If both |text| and |url| are supplied, the y are concatenated | |
| 343 * with a space. | |
| 344 * @param screenshotUri Screenshot of the page to be shared. | |
| 345 * @param callback Optional callback to be called when user makes a choice. Will not be called | |
| 346 * if receiving a response when the user makes a choice is n ot supported (see | |
| 347 * TargetChosenReceiver#isSupported()). | |
| 348 * @param currentTab The current tab for which sharing is being done. | 340 * @param currentTab The current tab for which sharing is being done. |
| 349 */ | 341 */ |
| 350 public static void shareOfflinePage(final boolean shareDirectly, final boole an saveLastUsed, | 342 public static void shareOfflinePage(ShareParams.Builder builder, final Tab c urrentTab) { |
| 351 final Activity mainActivity, final String text, final Uri screenshot Uri, | |
| 352 final ShareHelper.TargetChosenCallback callback, final Tab currentTa b) { | |
| 353 final String url = currentTab.getUrl(); | |
| 354 final String title = currentTab.getTitle(); | |
| 355 final OfflinePageBridge offlinePageBridge = | 343 final OfflinePageBridge offlinePageBridge = |
| 356 OfflinePageBridge.getForProfile(currentTab.getProfile()); | 344 OfflinePageBridge.getForProfile(currentTab.getProfile()); |
| 357 | 345 |
| 358 if (offlinePageBridge == null) { | 346 if (offlinePageBridge == null) { |
| 359 Log.e(TAG, "Unable to perform sharing on current tab."); | 347 Log.e(TAG, "Unable to perform sharing on current tab."); |
| 360 return; | 348 return; |
| 361 } | 349 } |
| 362 | 350 |
| 363 OfflinePageItem offlinePage = offlinePageBridge.getOfflinePage(currentTa b.getWebContents()); | 351 OfflinePageItem offlinePage = offlinePageBridge.getOfflinePage(currentTa b.getWebContents()); |
| 364 if (offlinePage != null) { | 352 if (offlinePage != null) { |
| 365 // If we're currently on offline page get the saved file directly. | 353 // If we're currently on offline page get the saved file directly. |
| 366 prepareFileAndShare(shareDirectly, saveLastUsed, mainActivity, title , text, | 354 prepareFileAndShare(builder, offlinePage.getFilePath()); |
| 367 url, screenshotUri, callback, offlinePage.getFil ePath()); | |
| 368 return; | 355 return; |
| 369 } | 356 } |
| 370 | 357 |
| 371 // If this is an online page, share the offline copy of it. | 358 // If this is an online page, share the offline copy of it. |
| 372 Callback<OfflinePageItem> prepareForSharing = onGotOfflinePageItemToShar e(shareDirectly, | 359 Callback<OfflinePageItem> prepareForSharing = onGotOfflinePageItemToShar e(builder); |
| 373 saveLastUsed, mainActivity, title, text, url, screenshotUri, cal lback); | 360 offlinePageBridge.selectPageForOnlineUrl(currentTab.getUrl(), currentTab .getId(), |
| 374 offlinePageBridge.selectPageForOnlineUrl(url, currentTab.getId(), | 361 selectPageForOnlineUrlCallback( |
| 375 selectPageForOnlineUrlCallback(currentTab.getWebContents(), offl inePageBridge, | 362 currentTab.getWebContents(), offlinePageBridge, prepareF orSharing)); |
| 376 prepareForSharing)); | |
| 377 } | 363 } |
| 378 | 364 |
| 379 /** | 365 /** |
| 380 * Callback for receiving the OfflinePageItem and use it to call prepareForS haring. | 366 * Callback for receiving the OfflinePageItem and use it to call prepareForS haring. |
| 381 * @param shareDirectly Whether it should share directly with the activity t hat was most | 367 * @param builder The builder to construct {@link ShareParams} for holding s hare parameters. |
| 382 * recently used to share. | |
| 383 * @param mainActivity Activity that is used to access package manager | |
| 384 * @param title Title of the page. | |
| 385 * @param onlineUrl Online URL associated with the offline page that is used to access the | |
| 386 * offline page file path. | |
| 387 * @param screenshotUri Screenshot of the page to be shared. | |
| 388 * @param mContext The application context. | |
| 389 * @return a callback of OfflinePageItem | |
| 390 */ | 368 */ |
| 391 private static Callback<OfflinePageItem> onGotOfflinePageItemToShare( | 369 private static Callback<OfflinePageItem> onGotOfflinePageItemToShare( |
| 392 final boolean shareDirectly, final boolean saveLastUsed, final Activ ity mainActivity, | 370 final ShareParams.Builder builder) { |
| 393 final String title, final String text, final String onlineUrl, final Uri screenshotUri, | |
| 394 final ShareHelper.TargetChosenCallback callback) { | |
| 395 return new Callback<OfflinePageItem>() { | 371 return new Callback<OfflinePageItem>() { |
| 396 @Override | 372 @Override |
| 397 public void onResult(OfflinePageItem item) { | 373 public void onResult(OfflinePageItem item) { |
| 398 String offlineFilePath = (item == null) ? null : item.getFilePat h(); | 374 String offlineFilePath = (item == null) ? null : item.getFilePat h(); |
| 399 prepareFileAndShare(shareDirectly, saveLastUsed, mainActivity, t itle, text, | 375 prepareFileAndShare(builder, offlineFilePath); |
| 400 onlineUrl, screenshotUri, callback, offlineFilePath); | |
| 401 } | 376 } |
| 402 }; | 377 }; |
| 403 } | 378 } |
| 404 | 379 |
| 405 /** | 380 /** |
| 406 * Takes the offline page item from selectPageForOnlineURL. If it exists, in vokes | 381 * Takes the offline page item from selectPageForOnlineURL. If it exists, in vokes |
| 407 * |prepareForSharing| with it. Otherwise, saves a page for the online URL and invokes | 382 * |prepareForSharing| with it. Otherwise, saves a page for the online URL and invokes |
| 408 * |prepareForSharing| with the result when it's ready. | 383 * |prepareForSharing| with the result when it's ready. |
| 409 * @param webContents Contents of the page to save. | 384 * @param webContents Contents of the page to save. |
| 410 * @param offlinePageBridge A static copy of the offlinePageBridge. | 385 * @param offlinePageBridge A static copy of the offlinePageBridge. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 454 } | 429 } |
| 455 | 430 |
| 456 offlinePageBridge.getPageByOfflineId(offlineId, prepareForSharin g); | 431 offlinePageBridge.getPageByOfflineId(offlineId, prepareForSharin g); |
| 457 } | 432 } |
| 458 }; | 433 }; |
| 459 } | 434 } |
| 460 | 435 |
| 461 /** | 436 /** |
| 462 * If file path of offline page is not null, do file operations needed for t he page to be | 437 * If file path of offline page is not null, do file operations needed for t he page to be |
| 463 * shared. Otherwise, only share the online url. | 438 * shared. Otherwise, only share the online url. |
| 464 * @param shareDirectly Whether it should share directly with the activity t hat was most | 439 * @param builder The builder to construct {@link ShareParams} for holding s hare parameters. |
| 465 * recently used to share. | |
| 466 * @param saveLastUsed Whether to save the chosen activity for future direct sharing. | |
| 467 * @param activity Activity that is used to access package manager | |
| 468 * @param title Title of the page. | |
| 469 * @param text Text to be shared. If both |text| and |url| are supplied, the y are concatenated | |
| 470 * with a space. | |
| 471 * @param onlineUrl Online URL associated with the offline page that is used to access the | |
| 472 * offline page file path. | |
| 473 * @param screenshotUri Screenshot of the page to be shared. | |
| 474 * @param callback Optional callback to be called when user makes a choice. Will not be called | |
| 475 * if receiving a response when the user makes a choice is n ot supported (on | |
| 476 * older Android versions). | |
| 477 * @param filePath File path of the offline page. | 440 * @param filePath File path of the offline page. |
| 478 */ | 441 */ |
| 479 private static void prepareFileAndShare(final boolean shareDirectly, final b oolean saveLastUsed, | 442 private static void prepareFileAndShare( |
| 480 final Activity activity, final String title, final String text, fina l String onlineUrl, | 443 final ShareParams.Builder builder, final String filePath) { |
| 481 final Uri screenshotUri, final ShareHelper.TargetChosenCallback call back, | |
| 482 final String filePath) { | |
| 483 new AsyncTask<Void, Void, File>() { | 444 new AsyncTask<Void, Void, File>() { |
| 484 @Override | 445 @Override |
| 485 protected File doInBackground(Void... params) { | 446 protected File doInBackground(Void... params) { |
| 486 if (filePath == null) return null; | 447 if (filePath == null) return null; |
| 487 | 448 |
| 488 File offlinePageOriginal = new File(filePath); | 449 File offlinePageOriginal = new File(filePath); |
| 489 File shareableDir = getDirectoryForOfflineSharing(activity); | 450 File shareableDir = getDirectoryForOfflineSharing(builder.getAct ivity()); |
|
Ted C
2017/06/01 20:39:02
this method should just be able to use the applica
ltian
2017/06/01 23:27:06
Done.
| |
| 490 | 451 |
| 491 if (shareableDir == null) { | 452 if (shareableDir == null) { |
| 492 Log.e(TAG, "Unable to create subdirectory in shareable direc tory"); | 453 Log.e(TAG, "Unable to create subdirectory in shareable direc tory"); |
| 493 return null; | 454 return null; |
| 494 } | 455 } |
| 495 | 456 |
| 496 String fileName = rewriteOfflineFileName(offlinePageOriginal.get Name()); | 457 String fileName = rewriteOfflineFileName(offlinePageOriginal.get Name()); |
| 497 File offlinePageShareable = new File(shareableDir, fileName); | 458 File offlinePageShareable = new File(shareableDir, fileName); |
| 498 | 459 |
| 499 if (offlinePageShareable.exists()) { | 460 if (offlinePageShareable.exists()) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 515 | 476 |
| 516 return null; | 477 return null; |
| 517 } | 478 } |
| 518 | 479 |
| 519 @Override | 480 @Override |
| 520 protected void onPostExecute(File offlinePageShareable) { | 481 protected void onPostExecute(File offlinePageShareable) { |
| 521 Uri offlineUri = null; | 482 Uri offlineUri = null; |
| 522 if (offlinePageShareable != null) { | 483 if (offlinePageShareable != null) { |
| 523 offlineUri = Uri.fromFile(offlinePageShareable); | 484 offlineUri = Uri.fromFile(offlinePageShareable); |
| 524 } | 485 } |
| 525 ShareHelper.share(shareDirectly, saveLastUsed, activity, title, text, onlineUrl, | 486 builder.setOfflineUri(offlineUri); |
| 526 offlineUri, screenshotUri, callback); | 487 ShareHelper.share(builder.build()); |
| 527 } | 488 } |
| 528 }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR); | 489 }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR); |
| 529 } | 490 } |
| 530 | 491 |
| 531 /** | 492 /** |
| 532 * Copies the file from internal storage to a sharable directory. | 493 * Copies the file from internal storage to a sharable directory. |
| 533 * @param src The original file to be copied. | 494 * @param src The original file to be copied. |
| 534 * @param dst The destination file. | 495 * @param dst The destination file. |
| 535 */ | 496 */ |
| 536 @VisibleForTesting | 497 @VisibleForTesting |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 861 @VisibleForTesting | 822 @VisibleForTesting |
| 862 static void setInstanceForTesting(Internal instance) { | 823 static void setInstanceForTesting(Internal instance) { |
| 863 sInstance = instance; | 824 sInstance = instance; |
| 864 } | 825 } |
| 865 | 826 |
| 866 @VisibleForTesting | 827 @VisibleForTesting |
| 867 public static void setSnackbarDurationForTesting(int durationMs) { | 828 public static void setSnackbarDurationForTesting(int durationMs) { |
| 868 sSnackbarDurationMs = durationMs; | 829 sSnackbarDurationMs = durationMs; |
| 869 } | 830 } |
| 870 } | 831 } |
| OLD | NEW |