| 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 @VisibleForTesting static final int SECONDS_PER_DAY = 24 * 60 * 60; | 75 @VisibleForTesting static final int SECONDS_PER_DAY = 24 * 60 * 60; |
| 76 private final IBinder mBinder = new LocalBinder(); | 76 private final IBinder mBinder = new LocalBinder(); |
| 77 private final List<DownloadSharedPreferenceEntry> mDownloadSharedPreferenceE
ntries = | 77 private final List<DownloadSharedPreferenceEntry> mDownloadSharedPreferenceE
ntries = |
| 78 new ArrayList<DownloadSharedPreferenceEntry>(); | 78 new ArrayList<DownloadSharedPreferenceEntry>(); |
| 79 private final List<String> mDownloadsInProgress = new ArrayList<String>(); | 79 private final List<String> mDownloadsInProgress = new ArrayList<String>(); |
| 80 private NotificationManager mNotificationManager; | 80 private NotificationManager mNotificationManager; |
| 81 private SharedPreferences mSharedPrefs; | 81 private SharedPreferences mSharedPrefs; |
| 82 private Context mContext; | 82 private Context mContext; |
| 83 private int mNextNotificationId; | 83 private int mNextNotificationId; |
| 84 private int mNumAutoResumptionAttemptLeft; | 84 private int mNumAutoResumptionAttemptLeft; |
| 85 private boolean mStopPostingProgressNotifications; |
| 85 | 86 |
| 86 /** | 87 /** |
| 87 * Class for clients to access. | 88 * Class for clients to access. |
| 88 */ | 89 */ |
| 89 public class LocalBinder extends Binder { | 90 public class LocalBinder extends Binder { |
| 90 DownloadNotificationService getService() { | 91 DownloadNotificationService getService() { |
| 91 return DownloadNotificationService.this; | 92 return DownloadNotificationService.this; |
| 92 } | 93 } |
| 93 } | 94 } |
| 94 | 95 |
| 95 @Override | 96 @Override |
| 96 public void onTaskRemoved(Intent rootIntent) { | 97 public void onTaskRemoved(Intent rootIntent) { |
| 98 mStopPostingProgressNotifications = true; |
| 97 // This funcion is called when Chrome is swiped away from the recent app
s | 99 // This funcion is called when Chrome is swiped away from the recent app
s |
| 98 // drawer. So it doesn't catch all scenarios that chrome can get killed. | 100 // drawer. So it doesn't catch all scenarios that chrome can get killed. |
| 99 // This will only help Android 4.4.2. | 101 // This will only help Android 4.4.2. |
| 100 onBrowserKilled(); | 102 onBrowserKilled(); |
| 101 } | 103 } |
| 102 | 104 |
| 103 @Override | 105 @Override |
| 104 public void onCreate() { | 106 public void onCreate() { |
| 107 mStopPostingProgressNotifications = false; |
| 105 mContext = getApplicationContext(); | 108 mContext = getApplicationContext(); |
| 106 mNotificationManager = (NotificationManager) mContext.getSystemService( | 109 mNotificationManager = (NotificationManager) mContext.getSystemService( |
| 107 Context.NOTIFICATION_SERVICE); | 110 Context.NOTIFICATION_SERVICE); |
| 108 mSharedPrefs = ContextUtils.getAppSharedPreferences(); | 111 mSharedPrefs = ContextUtils.getAppSharedPreferences(); |
| 109 parseDownloadSharedPrefs(); | 112 parseDownloadSharedPrefs(); |
| 110 // Because this service is a started service and returns START_STICKY in | 113 // Because this service is a started service and returns START_STICKY in |
| 111 // onStartCommand(), it will be restarted as soon as resources are avail
able | 114 // onStartCommand(), it will be restarted as soon as resources are avail
able |
| 112 // after it is killed. As a result, onCreate() may be called after Chrom
e | 115 // after it is killed. As a result, onCreate() may be called after Chrom
e |
| 113 // gets killed and before user restarts chrome. In that case, | 116 // gets killed and before user restarts chrome. In that case, |
| 114 // DownloadManagerService.hasDownloadManagerService() will return false
as | 117 // DownloadManagerService.hasDownloadManagerService() will return false
as |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 * @param percentage Percentage completed. Value should be between 0 to 100
if | 202 * @param percentage Percentage completed. Value should be between 0 to 100
if |
| 200 * the percentage can be determined, or -1 if it is unknown. | 203 * the percentage can be determined, or -1 if it is unknown. |
| 201 * @param timeRemainingInMillis Remaining download time in milliseconds. | 204 * @param timeRemainingInMillis Remaining download time in milliseconds. |
| 202 * @param startTime Time when download started. | 205 * @param startTime Time when download started. |
| 203 * @param isOffTheRecord Whether the download is off the record. | 206 * @param isOffTheRecord Whether the download is off the record. |
| 204 * @param canDownloadWhileMetered Whether the download can happen in metered
network. | 207 * @param canDownloadWhileMetered Whether the download can happen in metered
network. |
| 205 */ | 208 */ |
| 206 public void notifyDownloadProgress(String downloadGuid, String fileName, int
percentage, | 209 public void notifyDownloadProgress(String downloadGuid, String fileName, int
percentage, |
| 207 long timeRemainingInMillis, long startTime, boolean isOffTheRecord, | 210 long timeRemainingInMillis, long startTime, boolean isOffTheRecord, |
| 208 boolean canDownloadWhileMetered, boolean isOfflinePage) { | 211 boolean canDownloadWhileMetered, boolean isOfflinePage) { |
| 212 if (mStopPostingProgressNotifications) return; |
| 209 boolean indeterminate = percentage == INVALID_DOWNLOAD_PERCENTAGE; | 213 boolean indeterminate = percentage == INVALID_DOWNLOAD_PERCENTAGE; |
| 210 NotificationCompat.Builder builder = buildNotification( | 214 NotificationCompat.Builder builder = buildNotification( |
| 211 android.R.drawable.stat_sys_download, fileName, null); | 215 android.R.drawable.stat_sys_download, fileName, null); |
| 212 builder.setOngoing(true).setProgress(100, percentage, indeterminate); | 216 builder.setOngoing(true).setProgress(100, percentage, indeterminate); |
| 213 builder.setPriority(Notification.PRIORITY_HIGH); | 217 builder.setPriority(Notification.PRIORITY_HIGH); |
| 214 if (!indeterminate) { | 218 if (!indeterminate) { |
| 215 NumberFormat formatter = NumberFormat.getPercentInstance(Locale.getD
efault()); | 219 NumberFormat formatter = NumberFormat.getPercentInstance(Locale.getD
efault()); |
| 216 String percentText = formatter.format(percentage / 100.0); | 220 String percentText = formatter.format(percentage / 100.0); |
| 217 String duration = formatRemainingTime(mContext, timeRemainingInMilli
s); | 221 String duration = formatRemainingTime(mContext, timeRemainingInMilli
s); |
| 218 builder.setContentText(duration); | 222 builder.setContentText(duration); |
| (...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 776 return context.getString(R.string.remaining_duration_minutes, minute
s); | 780 return context.getString(R.string.remaining_duration_minutes, minute
s); |
| 777 } else if (minutes > 0) { | 781 } else if (minutes > 0) { |
| 778 return context.getString(R.string.remaining_duration_one_minute); | 782 return context.getString(R.string.remaining_duration_one_minute); |
| 779 } else if (seconds == 1) { | 783 } else if (seconds == 1) { |
| 780 return context.getString(R.string.remaining_duration_one_second); | 784 return context.getString(R.string.remaining_duration_one_second); |
| 781 } else { | 785 } else { |
| 782 return context.getString(R.string.remaining_duration_seconds, second
s); | 786 return context.getString(R.string.remaining_duration_seconds, second
s); |
| 783 } | 787 } |
| 784 } | 788 } |
| 785 } | 789 } |
| OLD | NEW |