diff options
author | Jeff Sharkey <jsharkey@android.com> | 2012-11-12 16:50:17 -0800 |
---|---|---|
committer | Jeff Sharkey <jsharkey@android.com> | 2012-11-14 16:21:54 -0800 |
commit | 52b703c5d0c4cff72bafdec0e2229368d3cc20d0 (patch) | |
tree | 0f5674a22b1113f32927f1585a8dc1adc9d2ea7b /src/com/android/providers/downloads/DownloadThread.java | |
parent | a40a349c0107660bfb4004467550725a3ca3ec97 (diff) | |
download | android_packages_providers_DownloadProvider-52b703c5d0c4cff72bafdec0e2229368d3cc20d0.tar.gz android_packages_providers_DownloadProvider-52b703c5d0c4cff72bafdec0e2229368d3cc20d0.tar.bz2 android_packages_providers_DownloadProvider-52b703c5d0c4cff72bafdec0e2229368d3cc20d0.zip |
Show remaining time in download notifications.
Calculate speed of in-progress downloads and estimate time remaining
until completion. Uses a moving average that is weighted 1:1 with
the most recent 500ms sample. Funnels timing data to notifications
through DownloadHandler.
Bug: 6777872
Change-Id: I9155f2979aa330bd1172f63bbfca1d053815cee5
Diffstat (limited to 'src/com/android/providers/downloads/DownloadThread.java')
-rw-r--r-- | src/com/android/providers/downloads/DownloadThread.java | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/src/com/android/providers/downloads/DownloadThread.java b/src/com/android/providers/downloads/DownloadThread.java index e74d5c72..2bd3d362 100644 --- a/src/com/android/providers/downloads/DownloadThread.java +++ b/src/com/android/providers/downloads/DownloadThread.java @@ -29,6 +29,7 @@ import android.net.http.AndroidHttpClient; import android.os.FileUtils; import android.os.PowerManager; import android.os.Process; +import android.os.SystemClock; import android.provider.Downloads; import android.text.TextUtils; import android.util.Log; @@ -100,6 +101,15 @@ public class DownloadThread extends Thread { public long mBytesNotified = 0; public long mTimeLastNotification = 0; + /** Historical bytes/second speed of this download. */ + public long mSpeed; + /** Time when current sample started. */ + public long mSpeedSampleStart; + /** Bytes transferred since current sample started. */ + public long mSpeedSampleBytes; + /** Estimated time until finished. */ + public long mRemainingMillis; + public State(DownloadInfo info) { mMimeType = Intent.normalizeMimeType(info.mMimeType); mRequestUri = info.mUri; @@ -423,7 +433,32 @@ public class DownloadThread extends Thread { * Report download progress through the database if necessary. */ private void reportProgress(State state, InnerState innerState) { - long now = mSystemFacade.currentTimeMillis(); + final long now = SystemClock.elapsedRealtime(); + + final long sampleDelta = now - state.mSpeedSampleStart; + if (sampleDelta > 500) { + final long sampleSpeed = ((state.mCurrentBytes - state.mSpeedSampleBytes) * 1000) + / sampleDelta; + + if (state.mSpeed == 0) { + state.mSpeed = sampleSpeed; + } else { + state.mSpeed = (state.mSpeed + sampleSpeed) / 2; + } + + state.mSpeedSampleStart = now; + state.mSpeedSampleBytes = state.mCurrentBytes; + + if (state.mSpeed != 0) { + state.mRemainingMillis = ((state.mTotalBytes - state.mCurrentBytes) * 1000) + / state.mSpeed; + } else { + state.mRemainingMillis = -1; + } + + DownloadHandler.getInstance().setRemainingMillis(mInfo.mId, state.mRemainingMillis); + } + if (state.mCurrentBytes - state.mBytesNotified > Constants.MIN_PROGRESS_STEP && now - state.mTimeLastNotification > Constants.MIN_PROGRESS_TIME) { ContentValues values = new ContentValues(); |