diff options
author | Jeff Sharkey <jsharkey@android.com> | 2013-01-12 15:58:51 -0800 |
---|---|---|
committer | Jeff Sharkey <jsharkey@android.com> | 2013-01-28 16:00:54 -0800 |
commit | 38648831a92295e9a11831e19e5a9dab4cbd939e (patch) | |
tree | feed2c5d167c42d3a548a6e029372b278e2660e1 /src/com/android/providers/downloads/DownloadNotifier.java | |
parent | e4b1f0ae43f169886d8d651a418e7f309e3e6f2f (diff) | |
download | android_packages_providers_DownloadProvider-38648831a92295e9a11831e19e5a9dab4cbd939e.tar.gz android_packages_providers_DownloadProvider-38648831a92295e9a11831e19e5a9dab4cbd939e.tar.bz2 android_packages_providers_DownloadProvider-38648831a92295e9a11831e19e5a9dab4cbd939e.zip |
Cleaner thread management, less global state.
Switch to using a ThreadPoolExecutor for handling downloads, which
gives us parallelism logic that is easier to reason about. Also
open the door to eventually waiting until the executor is drained
to stopSelf().
Removes DownloadHandler singleton, and gives explicit path for
publishing active download speeds to notifications.
Change-Id: I1836e7742bb8a84861d1ca6bd1e59b2040bd12f8
Diffstat (limited to 'src/com/android/providers/downloads/DownloadNotifier.java')
-rw-r--r-- | src/com/android/providers/downloads/DownloadNotifier.java | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/src/com/android/providers/downloads/DownloadNotifier.java b/src/com/android/providers/downloads/DownloadNotifier.java index daae7831..2dd9805a 100644 --- a/src/com/android/providers/downloads/DownloadNotifier.java +++ b/src/com/android/providers/downloads/DownloadNotifier.java @@ -32,6 +32,7 @@ import android.net.Uri; import android.provider.Downloads; import android.text.TextUtils; import android.text.format.DateUtils; +import android.util.LongSparseLongArray; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Maps; @@ -66,6 +67,13 @@ public class DownloadNotifier { @GuardedBy("mActiveNotifs") private final HashMap<String, Long> mActiveNotifs = Maps.newHashMap(); + /** + * Current speed of active downloads, mapped from {@link DownloadInfo#mId} + * to speed in bytes per second. + */ + @GuardedBy("mDownloadSpeed") + private final LongSparseLongArray mDownloadSpeed = new LongSparseLongArray(); + public DownloadNotifier(Context context) { mContext = context; mNotifManager = (NotificationManager) context.getSystemService( @@ -77,6 +85,20 @@ public class DownloadNotifier { } /** + * Notify the current speed of an active download, used for calcuating + * estimated remaining time. + */ + public void notifyDownloadSpeed(long id, long bytesPerSecond) { + synchronized (mDownloadSpeed) { + if (bytesPerSecond != 0) { + mDownloadSpeed.put(id, bytesPerSecond); + } else { + mDownloadSpeed.delete(id); + } + } + } + + /** * Update {@link NotificationManager} to reflect the given set of * {@link DownloadInfo}, adding, collapsing, and removing as needed. */ @@ -163,16 +185,16 @@ public class DownloadNotifier { String remainingText = null; String percentText = null; if (type == TYPE_ACTIVE) { - final DownloadHandler handler = DownloadHandler.getInstance(); - long current = 0; long total = 0; long speed = 0; - for (DownloadInfo info : cluster) { - if (info.mTotalBytes != -1) { - current += info.mCurrentBytes; - total += info.mTotalBytes; - speed += handler.getCurrentSpeed(info.mId); + synchronized (mDownloadSpeed) { + for (DownloadInfo info : cluster) { + if (info.mTotalBytes != -1) { + current += info.mCurrentBytes; + total += info.mTotalBytes; + speed += mDownloadSpeed.get(info.mId); + } } } |