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.download; | 5 package org.chromium.chrome.browser.download; |
| 6 | 6 |
| 7 import android.app.DownloadManager; | 7 import android.app.DownloadManager; |
| 8 import android.app.Notification; | 8 import android.app.Notification; |
| 9 import android.app.NotificationManager; | 9 import android.app.NotificationManager; |
| 10 import android.app.PendingIntent; | 10 import android.app.PendingIntent; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 | 21 |
| 22 import org.chromium.base.ContextUtils; | 22 import org.chromium.base.ContextUtils; |
| 23 import org.chromium.base.Log; | 23 import org.chromium.base.Log; |
| 24 import org.chromium.base.VisibleForTesting; | 24 import org.chromium.base.VisibleForTesting; |
| 25 import org.chromium.base.library_loader.ProcessInitException; | 25 import org.chromium.base.library_loader.ProcessInitException; |
| 26 import org.chromium.chrome.R; | 26 import org.chromium.chrome.R; |
| 27 import org.chromium.chrome.browser.ChromeApplication; | 27 import org.chromium.chrome.browser.ChromeApplication; |
| 28 import org.chromium.chrome.browser.init.BrowserParts; | 28 import org.chromium.chrome.browser.init.BrowserParts; |
| 29 import org.chromium.chrome.browser.init.ChromeBrowserInitializer; | 29 import org.chromium.chrome.browser.init.ChromeBrowserInitializer; |
| 30 import org.chromium.chrome.browser.init.EmptyBrowserParts; | 30 import org.chromium.chrome.browser.init.EmptyBrowserParts; |
| 31 import org.chromium.chrome.browser.offlinepages.downloads.OfflinePageDownloadBri dge; | |
| 31 import org.chromium.chrome.browser.util.IntentUtils; | 32 import org.chromium.chrome.browser.util.IntentUtils; |
| 32 | 33 |
| 33 import java.text.NumberFormat; | 34 import java.text.NumberFormat; |
| 34 import java.util.ArrayList; | 35 import java.util.ArrayList; |
| 35 import java.util.HashSet; | 36 import java.util.HashSet; |
| 36 import java.util.Iterator; | 37 import java.util.Iterator; |
| 37 import java.util.List; | 38 import java.util.List; |
| 38 import java.util.Locale; | 39 import java.util.Locale; |
| 39 import java.util.Set; | 40 import java.util.Set; |
| 40 | 41 |
| 41 /** | 42 /** |
| 42 * Service responsible for creating and updating download notifications even aft er | 43 * Service responsible for creating and updating download notifications even aft er |
| 43 * Chrome gets killed. | 44 * Chrome gets killed. |
| 44 */ | 45 */ |
| 45 public class DownloadNotificationService extends Service { | 46 public class DownloadNotificationService extends Service { |
| 46 static final String EXTRA_DOWNLOAD_NOTIFICATION_ID = "DownloadNotificationId "; | 47 static final String EXTRA_DOWNLOAD_NOTIFICATION_ID = "DownloadNotificationId "; |
| 47 static final String EXTRA_DOWNLOAD_GUID = "DownloadGuid"; | 48 static final String EXTRA_DOWNLOAD_GUID = "DownloadGuid"; |
| 48 static final String EXTRA_DOWNLOAD_FILE_NAME = "DownloadFileName"; | 49 static final String EXTRA_DOWNLOAD_FILE_NAME = "DownloadFileName"; |
| 49 static final String EXTRA_NOTIFICATION_DISMISSED = "NotificationDismissed"; | 50 static final String EXTRA_NOTIFICATION_DISMISSED = "NotificationDismissed"; |
| 50 static final String EXTRA_DOWNLOAD_IS_OFF_THE_RECORD = "DownloadIsOffTheReco rd"; | 51 static final String EXTRA_DOWNLOAD_IS_OFF_THE_RECORD = "DownloadIsOffTheReco rd"; |
| 51 static final String ACTION_DOWNLOAD_CANCEL = | 52 static final String ACTION_DOWNLOAD_CANCEL = |
| 52 "org.chromium.chrome.browser.download.DOWNLOAD_CANCEL"; | 53 "org.chromium.chrome.browser.download.DOWNLOAD_CANCEL"; |
| 53 static final String ACTION_DOWNLOAD_PAUSE = | 54 static final String ACTION_DOWNLOAD_PAUSE = |
| 54 "org.chromium.chrome.browser.download.DOWNLOAD_PAUSE"; | 55 "org.chromium.chrome.browser.download.DOWNLOAD_PAUSE"; |
| 55 static final String ACTION_DOWNLOAD_RESUME = | 56 static final String ACTION_DOWNLOAD_RESUME = |
| 56 "org.chromium.chrome.browser.download.DOWNLOAD_RESUME"; | 57 "org.chromium.chrome.browser.download.DOWNLOAD_RESUME"; |
| 57 public static final String ACTION_DOWNLOAD_RESUME_ALL = | 58 public static final String ACTION_DOWNLOAD_RESUME_ALL = |
| 58 "org.chromium.chrome.browser.download.DOWNLOAD_RESUME_ALL"; | 59 "org.chromium.chrome.browser.download.DOWNLOAD_RESUME_ALL"; |
| 60 public static final String ACTION_DOWNLOAD_OPEN = | |
| 61 "org.chromium.chrome.browser.download.DOWNLOAD_OPEN"; | |
| 59 static final int INVALID_DOWNLOAD_PERCENTAGE = -1; | 62 static final int INVALID_DOWNLOAD_PERCENTAGE = -1; |
| 60 @VisibleForTesting | 63 @VisibleForTesting |
| 61 static final String PENDING_DOWNLOAD_NOTIFICATIONS = "PendingDownloadNotific ations"; | 64 static final String PENDING_DOWNLOAD_NOTIFICATIONS = "PendingDownloadNotific ations"; |
| 62 static final String NOTIFICATION_NAMESPACE = "DownloadNotificationService"; | 65 static final String NOTIFICATION_NAMESPACE = "DownloadNotificationService"; |
| 63 private static final String TAG = "DownloadNotification"; | 66 private static final String TAG = "DownloadNotification"; |
| 64 private static final String NEXT_DOWNLOAD_NOTIFICATION_ID = "NextDownloadNot ificationId"; | 67 private static final String NEXT_DOWNLOAD_NOTIFICATION_ID = "NextDownloadNot ificationId"; |
| 65 // Notification Id starting value, to avoid conflicts from IDs used in prior versions. | 68 // Notification Id starting value, to avoid conflicts from IDs used in prior versions. |
| 66 private static final int STARTING_NOTIFICATION_ID = 1000000; | 69 private static final int STARTING_NOTIFICATION_ID = 1000000; |
| 67 private static final String AUTO_RESUMPTION_ATTEMPT_LEFT = "ResumptionAttemp tLeft"; | 70 private static final String AUTO_RESUMPTION_ATTEMPT_LEFT = "ResumptionAttemp tLeft"; |
| 68 private static final int MAX_RESUMPTION_ATTEMPT_LEFT = 5; | 71 private static final int MAX_RESUMPTION_ATTEMPT_LEFT = 5; |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 * @param fileName File name of the download. | 197 * @param fileName File name of the download. |
| 195 * @param percentage Percentage completed. Value should be between 0 to 100 if | 198 * @param percentage Percentage completed. Value should be between 0 to 100 if |
| 196 * the percentage can be determined, or -1 if it is unknown. | 199 * the percentage can be determined, or -1 if it is unknown. |
| 197 * @param timeRemainingInMillis Remaining download time in milliseconds. | 200 * @param timeRemainingInMillis Remaining download time in milliseconds. |
| 198 * @param startTime Time when download started. | 201 * @param startTime Time when download started. |
| 199 * @param isOffTheRecord Whether the download is off the record. | 202 * @param isOffTheRecord Whether the download is off the record. |
| 200 * @param canDownloadWhileMetered Whether the download can happen in metered network. | 203 * @param canDownloadWhileMetered Whether the download can happen in metered network. |
| 201 */ | 204 */ |
| 202 public void notifyDownloadProgress(String downloadGuid, String fileName, int percentage, | 205 public void notifyDownloadProgress(String downloadGuid, String fileName, int percentage, |
| 203 long timeRemainingInMillis, long startTime, boolean isOffTheRecord, | 206 long timeRemainingInMillis, long startTime, boolean isOffTheRecord, |
| 204 boolean canDownloadWhileMetered) { | 207 boolean canDownloadWhileMetered, boolean isOfflinePage) { |
| 205 boolean indeterminate = percentage == INVALID_DOWNLOAD_PERCENTAGE; | 208 boolean indeterminate = percentage == INVALID_DOWNLOAD_PERCENTAGE; |
| 206 NotificationCompat.Builder builder = buildNotification( | 209 NotificationCompat.Builder builder = buildNotification( |
| 207 android.R.drawable.stat_sys_download, fileName, null); | 210 android.R.drawable.stat_sys_download, fileName, null); |
| 208 builder.setOngoing(true).setProgress(100, percentage, indeterminate); | 211 builder.setOngoing(true).setProgress(100, percentage, indeterminate); |
| 209 builder.setPriority(Notification.PRIORITY_HIGH); | 212 builder.setPriority(Notification.PRIORITY_HIGH); |
| 210 if (!indeterminate) { | 213 if (!indeterminate) { |
| 211 NumberFormat formatter = NumberFormat.getPercentInstance(Locale.getD efault()); | 214 NumberFormat formatter = NumberFormat.getPercentInstance(Locale.getD efault()); |
| 212 String percentText = formatter.format(percentage / 100.0); | 215 String percentText = formatter.format(percentage / 100.0); |
| 213 String duration = formatRemainingTime(mContext, timeRemainingInMilli s); | 216 String duration = formatRemainingTime(mContext, timeRemainingInMilli s); |
| 214 builder.setContentText(duration); | 217 builder.setContentText(duration); |
| 215 if (Build.VERSION.CODENAME.equals("N") | 218 if (Build.VERSION.CODENAME.equals("N") |
| 216 || Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { | 219 || Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { |
| 217 builder.setSubText(percentText); | 220 builder.setSubText(percentText); |
| 218 } else { | 221 } else { |
| 219 builder.setContentInfo(percentText); | 222 builder.setContentInfo(percentText); |
| 220 } | 223 } |
| 221 } | 224 } |
| 222 int notificationId = getNotificationId(downloadGuid); | 225 int notificationId = getNotificationId(downloadGuid); |
| 226 int itemType = isOfflinePage ? DownloadSharedPreferenceEntry.ITEM_TYPE_O FFLINE_PAGE | |
| 227 : DownloadSharedPreferenceEntry.ITEM_TYPE_D OWNLOAD; | |
| 223 addOrReplaceSharedPreferenceEntry(new DownloadSharedPreferenceEntry(noti ficationId, | 228 addOrReplaceSharedPreferenceEntry(new DownloadSharedPreferenceEntry(noti ficationId, |
| 224 isOffTheRecord, canDownloadWhileMetered, downloadGuid, fileName, | 229 isOffTheRecord, canDownloadWhileMetered, downloadGuid, fileName, itemType)); |
| 225 DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOAD)); | |
| 226 if (startTime > 0) builder.setWhen(startTime); | 230 if (startTime > 0) builder.setWhen(startTime); |
| 227 Intent cancelIntent = buildActionIntent( | 231 Intent cancelIntent = buildActionIntent( |
| 228 ACTION_DOWNLOAD_CANCEL, notificationId, downloadGuid, fileName); | 232 ACTION_DOWNLOAD_CANCEL, notificationId, downloadGuid, fileName); |
| 229 builder.addAction(R.drawable.btn_close_white, | 233 builder.addAction(R.drawable.btn_close_white, |
| 230 mContext.getResources().getString(R.string.download_notification _cancel_button), | 234 mContext.getResources().getString(R.string.download_notification _cancel_button), |
| 231 buildPendingIntent(cancelIntent, notificationId)); | 235 buildPendingIntent(cancelIntent, notificationId)); |
| 232 Intent pauseIntent = buildActionIntent( | 236 Intent pauseIntent = buildActionIntent( |
| 233 ACTION_DOWNLOAD_PAUSE, notificationId, downloadGuid, fileName); | 237 ACTION_DOWNLOAD_PAUSE, notificationId, downloadGuid, fileName); |
| 234 builder.addAction(R.drawable.ic_vidcontrol_pause, | 238 builder.addAction(R.drawable.ic_vidcontrol_pause, |
| 235 mContext.getResources().getString(R.string.download_notification _pause_button), | 239 mContext.getResources().getString(R.string.download_notification _pause_button), |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 304 } | 308 } |
| 305 | 309 |
| 306 /** | 310 /** |
| 307 * Add a download successful notification. | 311 * Add a download successful notification. |
| 308 * @param downloadGuid GUID of the download. | 312 * @param downloadGuid GUID of the download. |
| 309 * @param fileName GUID of the download. | 313 * @param fileName GUID of the download. |
| 310 * @param systemDownloadId Download ID assigned by system DownloadManager. | 314 * @param systemDownloadId Download ID assigned by system DownloadManager. |
| 311 * @return ID of the successful download notification. Used for removing the notification when | 315 * @return ID of the successful download notification. Used for removing the notification when |
| 312 * user click on the snackbar. | 316 * user click on the snackbar. |
| 313 */ | 317 */ |
| 314 public int notifyDownloadSuccessful(String downloadGuid, String fileName, | 318 public int notifyDownloadSuccessful( |
| 315 long systemDownloadId) { | 319 String downloadGuid, String fileName, long systemDownloadId, boolean isOfflinePage) { |
| 316 int notificationId = getNotificationId(downloadGuid); | 320 int notificationId = getNotificationId(downloadGuid); |
| 317 NotificationCompat.Builder builder = buildNotification( | 321 NotificationCompat.Builder builder = buildNotification( |
| 318 android.R.drawable.stat_sys_download_done, fileName, | 322 android.R.drawable.stat_sys_download_done, fileName, |
| 319 mContext.getResources().getString(R.string.download_notification _completed)); | 323 mContext.getResources().getString(R.string.download_notification _completed)); |
| 320 ComponentName component = new ComponentName( | 324 ComponentName component = new ComponentName( |
| 321 mContext.getPackageName(), DownloadBroadcastReceiver.class.getNa me()); | 325 mContext.getPackageName(), DownloadBroadcastReceiver.class.getNa me()); |
| 322 Intent intent = new Intent(DownloadManager.ACTION_NOTIFICATION_CLICKED); | 326 Intent intent; |
| 327 if (isOfflinePage) { | |
| 328 intent = | |
| 329 buildActionIntent(ACTION_DOWNLOAD_OPEN, notificationId, down loadGuid, fileName); | |
| 330 } else { | |
| 331 intent = new Intent(DownloadManager.ACTION_NOTIFICATION_CLICKED); | |
| 332 long[] idArray = {systemDownloadId}; | |
| 333 intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_ID S, idArray); | |
| 334 } | |
| 323 intent.setComponent(component); | 335 intent.setComponent(component); |
| 324 long[] idArray = {systemDownloadId}; | |
| 325 intent.putExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS, i dArray); | |
| 326 builder.setContentIntent(PendingIntent.getBroadcast( | 336 builder.setContentIntent(PendingIntent.getBroadcast( |
| 327 mContext, notificationId, intent, PendingIntent.FLAG_UPDATE_CURR ENT)); | 337 mContext, notificationId, intent, PendingIntent.FLAG_UPDATE_CURR ENT)); |
| 328 updateNotification(notificationId, builder.build()); | 338 updateNotification(notificationId, builder.build()); |
| 329 removeSharedPreferenceEntry(downloadGuid); | 339 removeSharedPreferenceEntry(downloadGuid); |
| 330 mDownloadsInProgress.remove(downloadGuid); | 340 mDownloadsInProgress.remove(downloadGuid); |
| 331 return notificationId; | 341 return notificationId; |
| 332 } | 342 } |
| 333 | 343 |
| 334 /** | 344 /** |
| 335 * Add a download failed notification. | 345 * Add a download failed notification. |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 463 // If user manually resumes a download, update the network type if it | 473 // If user manually resumes a download, update the network type if it |
| 464 // is not metered previously. | 474 // is not metered previously. |
| 465 entry.canDownloadWhileMetered = metered; | 475 entry.canDownloadWhileMetered = metered; |
| 466 } | 476 } |
| 467 // Update the SharedPreference entry. | 477 // Update the SharedPreference entry. |
| 468 addOrReplaceSharedPreferenceEntry(entry); | 478 addOrReplaceSharedPreferenceEntry(entry); |
| 469 } else if (intent.getAction() == ACTION_DOWNLOAD_RESUME_ALL | 479 } else if (intent.getAction() == ACTION_DOWNLOAD_RESUME_ALL |
| 470 && (mDownloadSharedPreferenceEntries.isEmpty() | 480 && (mDownloadSharedPreferenceEntries.isEmpty() |
| 471 || DownloadManagerService.hasDownloadManagerService())) { | 481 || DownloadManagerService.hasDownloadManagerService())) { |
| 472 return; | 482 return; |
| 483 } else if (intent.getAction() == ACTION_DOWNLOAD_OPEN) { | |
| 484 // TODO(fgorski): Do we even need to do anything special here, befor e we launch Chrome? | |
| 473 } | 485 } |
| 474 | 486 |
| 475 BrowserParts parts = new EmptyBrowserParts() { | 487 BrowserParts parts = new EmptyBrowserParts() { |
| 476 @Override | 488 @Override |
| 477 public boolean shouldStartGpuProcess() { | 489 public boolean shouldStartGpuProcess() { |
| 478 return false; | 490 return false; |
| 479 } | 491 } |
| 480 | 492 |
| 481 @Override | 493 @Override |
| 482 public void finishNativeInitialization() { | 494 public void finishNativeInitialization() { |
| 483 DownloadServiceDelegate downloadServiceDelegate = getDelegateFor Entry(entry); | 495 DownloadServiceDelegate downloadServiceDelegate = |
| 496 getServiceDelegate(intent.getAction() == ACTION_DOWNLOAD _OPEN | |
| 497 ? DownloadSharedPreferenceEntry.ITEM_TYP E_OFFLINE_PAGE | |
|
qinmin
2016/08/18 23:46:59
fix indentation
fgorski
2016/08/19 17:03:24
Done. Problem here is that this is the product of
| |
| 498 : entry.itemType); | |
| 484 switch (intent.getAction()) { | 499 switch (intent.getAction()) { |
| 485 case ACTION_DOWNLOAD_CANCEL: | 500 case ACTION_DOWNLOAD_CANCEL: |
| 486 // TODO(qinmin): Alternatively, we can delete the downlo aded content on | 501 // TODO(qinmin): Alternatively, we can delete the downlo aded content on |
| 487 // SD card, and remove the download ID from the SharedPr eferences so we | 502 // SD card, and remove the download ID from the SharedPr eferences so we |
| 488 // don't need to restart the browser process. http://crb ug.com/579643. | 503 // don't need to restart the browser process. http://crb ug.com/579643. |
| 489 cancelNotification(entry.notificationId, entry.downloadG uid); | 504 cancelNotification(entry.notificationId, entry.downloadG uid); |
| 490 downloadServiceDelegate.cancelDownload(entry.downloadGui d, | 505 downloadServiceDelegate.cancelDownload(entry.downloadGui d, |
| 491 entry.isOffTheRecord, IntentUtils.safeGetBoolean Extra( | 506 entry.isOffTheRecord, IntentUtils.safeGetBoolean Extra( |
| 492 intent, EXTRA_NOTIFICATION_DISMISSED, fa lse)); | 507 intent, EXTRA_NOTIFICATION_DISMISSED, fa lse)); |
| 493 break; | 508 break; |
| 494 case ACTION_DOWNLOAD_PAUSE: | 509 case ACTION_DOWNLOAD_PAUSE: |
| 495 downloadServiceDelegate.pauseDownload(entry.downloadGuid , | 510 downloadServiceDelegate.pauseDownload(entry.downloadGuid , |
| 496 entry.isOffTheRecord); | 511 entry.isOffTheRecord); |
| 497 break; | 512 break; |
| 498 case ACTION_DOWNLOAD_RESUME: | 513 case ACTION_DOWNLOAD_RESUME: |
| 499 notifyDownloadProgress(entry.downloadGuid, entry.fileNam e, | 514 notifyDownloadProgress(entry.downloadGuid, entry.fileNam e, |
| 500 INVALID_DOWNLOAD_PERCENTAGE, 0, 0, entry.isOffTh eRecord, | 515 INVALID_DOWNLOAD_PERCENTAGE, 0, 0, entry.isOffTh eRecord, |
| 501 entry.canDownloadWhileMetered); | 516 entry.canDownloadWhileMetered, isOfflinePage(ent ry)); |
| 502 downloadServiceDelegate.resumeDownload(entry.buildDownlo adItem(), true); | 517 downloadServiceDelegate.resumeDownload(entry.buildDownlo adItem(), true); |
| 503 break; | 518 break; |
| 504 case ACTION_DOWNLOAD_RESUME_ALL: | 519 case ACTION_DOWNLOAD_RESUME_ALL: |
| 505 assert entry == null; | 520 assert entry == null; |
| 506 resumeAllPendingDownloads(); | 521 resumeAllPendingDownloads(); |
| 507 break; | 522 break; |
| 523 case ACTION_DOWNLOAD_OPEN: | |
| 524 assert entry == null; | |
| 525 String guid = IntentUtils.safeGetStringExtra(intent, EXT RA_DOWNLOAD_GUID); | |
| 526 downloadServiceDelegate.openItem(guid); | |
| 527 break; | |
| 508 default: | 528 default: |
| 509 Log.e(TAG, "Unrecognized intent action.", intent); | 529 Log.e(TAG, "Unrecognized intent action.", intent); |
| 510 break; | 530 break; |
| 511 } | 531 } |
| 532 downloadServiceDelegate.destroyServiceDelegate(); | |
| 512 } | 533 } |
| 513 }; | 534 }; |
| 514 try { | 535 try { |
| 515 ChromeBrowserInitializer.getInstance(mContext).handlePreNativeStartu p(parts); | 536 ChromeBrowserInitializer.getInstance(mContext).handlePreNativeStartu p(parts); |
| 516 ChromeBrowserInitializer.getInstance(mContext).handlePostNativeStart up(true, parts); | 537 ChromeBrowserInitializer.getInstance(mContext).handlePostNativeStart up(true, parts); |
| 517 } catch (ProcessInitException e) { | 538 } catch (ProcessInitException e) { |
| 518 Log.e(TAG, "Unable to load native library.", e); | 539 Log.e(TAG, "Unable to load native library.", e); |
| 519 ChromeApplication.reportStartupErrorAndExit(e); | 540 ChromeApplication.reportStartupErrorAndExit(e); |
| 520 } | 541 } |
| 521 } | 542 } |
| 522 | 543 |
| 523 /** | 544 /** |
| 524 * Gets appropriate download delegate that can handle interactions with down load item referred | 545 * Gets appropriate download delegate that can handle interactions with down load item referred |
| 525 * to by the entry. | 546 * to by the entry. |
| 526 * @param entry download entry for which the delegate is provided | 547 * @param forOfflinePage Whether the service should deal with offline pages or downloads. |
| 527 * @return delegate for interactions with the entry | 548 * @return delegate for interactions with the entry |
| 528 */ | 549 */ |
| 529 DownloadServiceDelegate getDelegateForEntry(DownloadSharedPreferenceEntry en try) { | 550 DownloadServiceDelegate getServiceDelegate(int downloadItemType) { |
| 530 // TODO(fgorski): handle cases where entry is an offline page once imple mented. | 551 if (downloadItemType == DownloadSharedPreferenceEntry.ITEM_TYPE_OFFLINE_ PAGE) { |
| 552 return OfflinePageDownloadBridge.getDownloadServiceDelegate(); | |
| 553 } | |
| 554 if (downloadItemType != DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOAD ) { | |
| 555 Log.e(TAG, "Unrecognized intent type.", downloadItemType); | |
| 556 } | |
| 531 return DownloadManagerService.getDownloadManagerService(getApplicationCo ntext()); | 557 return DownloadManagerService.getDownloadManagerService(getApplicationCo ntext()); |
| 532 } | 558 } |
| 533 | 559 |
| 534 /** | 560 /** |
| 535 * Update the notification with id. | 561 * Update the notification with id. |
| 536 * @param id Id of the notification that has to be updated. | 562 * @param id Id of the notification that has to be updated. |
| 537 * @param notification the notification object that needs to be updated. | 563 * @param notification the notification object that needs to be updated. |
| 538 */ | 564 */ |
| 539 @VisibleForTesting | 565 @VisibleForTesting |
| 540 void updateNotification(int id, Notification notification) { | 566 void updateNotification(int id, Notification notification) { |
| 541 mNotificationManager.notify(NOTIFICATION_NAMESPACE, id, notification); | 567 mNotificationManager.notify(NOTIFICATION_NAMESPACE, id, notification); |
| 542 } | 568 } |
| 543 | 569 |
| 544 /** | 570 /** |
| 545 * Checks if an intent requires operations on a download. | 571 * Checks if an intent requires operations on a download. |
| 546 * @param intent An intent to validate. | 572 * @param intent An intent to validate. |
| 547 * @return true if the intent requires actions, or false otherwise. | 573 * @return true if the intent requires actions, or false otherwise. |
| 548 */ | 574 */ |
| 549 static boolean isDownloadOperationIntent(Intent intent) { | 575 static boolean isDownloadOperationIntent(Intent intent) { |
| 550 if (intent == null) return false; | 576 if (intent == null) return false; |
| 551 if (ACTION_DOWNLOAD_RESUME_ALL.equals(intent.getAction())) return true; | 577 if (ACTION_DOWNLOAD_RESUME_ALL.equals(intent.getAction())) return true; |
| 552 if (!ACTION_DOWNLOAD_CANCEL.equals(intent.getAction()) | 578 if (!ACTION_DOWNLOAD_CANCEL.equals(intent.getAction()) |
| 553 && !ACTION_DOWNLOAD_RESUME.equals(intent.getAction()) | 579 && !ACTION_DOWNLOAD_RESUME.equals(intent.getAction()) |
| 554 && !ACTION_DOWNLOAD_PAUSE.equals(intent.getAction())) { | 580 && !ACTION_DOWNLOAD_PAUSE.equals(intent.getAction()) |
| 581 && !ACTION_DOWNLOAD_OPEN.equals(intent.getAction())) { | |
| 555 return false; | 582 return false; |
| 556 } | 583 } |
| 557 if (!intent.hasExtra(EXTRA_DOWNLOAD_NOTIFICATION_ID) | 584 if (!intent.hasExtra(EXTRA_DOWNLOAD_NOTIFICATION_ID) |
| 558 || !intent.hasExtra(EXTRA_DOWNLOAD_FILE_NAME) | 585 || !intent.hasExtra(EXTRA_DOWNLOAD_FILE_NAME) |
| 559 || !intent.hasExtra(EXTRA_DOWNLOAD_GUID)) { | 586 || !intent.hasExtra(EXTRA_DOWNLOAD_GUID)) { |
| 560 return false; | 587 return false; |
| 561 } | 588 } |
| 562 final int notificationId = | 589 final int notificationId = |
| 563 IntentUtils.safeGetIntExtra(intent, EXTRA_DOWNLOAD_NOTIFICATION_ ID, -1); | 590 IntentUtils.safeGetIntExtra(intent, EXTRA_DOWNLOAD_NOTIFICATION_ ID, -1); |
| 564 if (notificationId == -1) return false; | 591 if (notificationId == -1) return false; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 614 * Resumes all pending downloads from |mDownloadSharedPreferenceEntries|. If a download is | 641 * Resumes all pending downloads from |mDownloadSharedPreferenceEntries|. If a download is |
| 615 * already in progress, do nothing. | 642 * already in progress, do nothing. |
| 616 */ | 643 */ |
| 617 public void resumeAllPendingDownloads() { | 644 public void resumeAllPendingDownloads() { |
| 618 boolean isNetworkMetered = DownloadManagerService.isActiveNetworkMetered (mContext); | 645 boolean isNetworkMetered = DownloadManagerService.isActiveNetworkMetered (mContext); |
| 619 if (!DownloadManagerService.hasDownloadManagerService()) return; | 646 if (!DownloadManagerService.hasDownloadManagerService()) return; |
| 620 for (int i = 0; i < mDownloadSharedPreferenceEntries.size(); ++i) { | 647 for (int i = 0; i < mDownloadSharedPreferenceEntries.size(); ++i) { |
| 621 DownloadSharedPreferenceEntry entry = mDownloadSharedPreferenceEntri es.get(i); | 648 DownloadSharedPreferenceEntry entry = mDownloadSharedPreferenceEntri es.get(i); |
| 622 if (mDownloadsInProgress.contains(entry.downloadGuid)) continue; | 649 if (mDownloadsInProgress.contains(entry.downloadGuid)) continue; |
| 623 if (!entry.canDownloadWhileMetered && isNetworkMetered) continue; | 650 if (!entry.canDownloadWhileMetered && isNetworkMetered) continue; |
| 624 notifyDownloadProgress(entry.downloadGuid, entry.fileName, | 651 notifyDownloadProgress(entry.downloadGuid, entry.fileName, INVALID_D OWNLOAD_PERCENTAGE, |
| 625 INVALID_DOWNLOAD_PERCENTAGE, 0, 0, false, entry.canDownloadW hileMetered); | 652 0, 0, false, entry.canDownloadWhileMetered, isOfflinePage(en try)); |
| 626 getDelegateForEntry(entry).resumeDownload(entry.buildDownloadItem(), false); | 653 DownloadServiceDelegate downloadServiceDelegate = getServiceDelegate (entry.itemType); |
| 654 downloadServiceDelegate.resumeDownload(entry.buildDownloadItem(), fa lse); | |
| 655 downloadServiceDelegate.destroyServiceDelegate(); | |
| 627 } | 656 } |
| 628 } | 657 } |
| 629 | 658 |
| 630 /** | 659 /** |
| 631 * Parse a list of the DownloadSharedPreferenceEntry and the number of auto resumption attempt | 660 * Parse a list of the DownloadSharedPreferenceEntry and the number of auto resumption attempt |
| 632 * left from the shared preference. | 661 * left from the shared preference. |
| 633 */ | 662 */ |
| 634 void parseDownloadSharedPrefs() { | 663 void parseDownloadSharedPrefs() { |
| 635 mNumAutoResumptionAttemptLeft = mSharedPrefs.getInt(AUTO_RESUMPTION_ATTE MPT_LEFT, | 664 mNumAutoResumptionAttemptLeft = mSharedPrefs.getInt(AUTO_RESUMPTION_ATTE MPT_LEFT, |
| 636 MAX_RESUMPTION_ATTEMPT_LEFT); | 665 MAX_RESUMPTION_ATTEMPT_LEFT); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 682 if (entry != null) return entry.notificationId; | 711 if (entry != null) return entry.notificationId; |
| 683 int notificationId = mNextNotificationId; | 712 int notificationId = mNextNotificationId; |
| 684 mNextNotificationId = mNextNotificationId == Integer.MAX_VALUE | 713 mNextNotificationId = mNextNotificationId == Integer.MAX_VALUE |
| 685 ? STARTING_NOTIFICATION_ID : mNextNotificationId + 1; | 714 ? STARTING_NOTIFICATION_ID : mNextNotificationId + 1; |
| 686 SharedPreferences.Editor editor = mSharedPrefs.edit(); | 715 SharedPreferences.Editor editor = mSharedPrefs.edit(); |
| 687 editor.putInt(NEXT_DOWNLOAD_NOTIFICATION_ID, mNextNotificationId); | 716 editor.putInt(NEXT_DOWNLOAD_NOTIFICATION_ID, mNextNotificationId); |
| 688 editor.apply(); | 717 editor.apply(); |
| 689 return notificationId; | 718 return notificationId; |
| 690 } | 719 } |
| 691 | 720 |
| 721 private boolean isOfflinePage(DownloadSharedPreferenceEntry entry) { | |
| 722 return entry.itemType == DownloadSharedPreferenceEntry.ITEM_TYPE_OFFLINE _PAGE; | |
| 723 } | |
| 724 | |
| 692 /** | 725 /** |
| 693 * Format remaining time for the given millis, in the following format: | 726 * Format remaining time for the given millis, in the following format: |
| 694 * 5 hours; will include 1 unit, can go down to seconds precision. | 727 * 5 hours; will include 1 unit, can go down to seconds precision. |
| 695 * This is similar to what android.java.text.Formatter.formatShortElapsedTim e() does. Don't use | 728 * This is similar to what android.java.text.Formatter.formatShortElapsedTim e() does. Don't use |
| 696 * ui::TimeFormat::Simple() as it is very expensive. | 729 * ui::TimeFormat::Simple() as it is very expensive. |
| 697 * | 730 * |
| 698 * @param context the application context. | 731 * @param context the application context. |
| 699 * @param millis the remaining time in milli seconds. | 732 * @param millis the remaining time in milli seconds. |
| 700 * @return the formatted remaining time. | 733 * @return the formatted remaining time. |
| 701 */ | 734 */ |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 734 return context.getString(R.string.remaining_duration_minutes, minute s); | 767 return context.getString(R.string.remaining_duration_minutes, minute s); |
| 735 } else if (minutes > 0) { | 768 } else if (minutes > 0) { |
| 736 return context.getString(R.string.remaining_duration_one_minute); | 769 return context.getString(R.string.remaining_duration_one_minute); |
| 737 } else if (seconds == 1) { | 770 } else if (seconds == 1) { |
| 738 return context.getString(R.string.remaining_duration_one_second); | 771 return context.getString(R.string.remaining_duration_one_second); |
| 739 } else { | 772 } else { |
| 740 return context.getString(R.string.remaining_duration_seconds, second s); | 773 return context.getString(R.string.remaining_duration_seconds, second s); |
| 741 } | 774 } |
| 742 } | 775 } |
| 743 } | 776 } |
| OLD | NEW |