diff options
author | Steve Howard <showard@google.com> | 2010-07-01 16:35:03 -0700 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2010-07-01 16:35:03 -0700 |
commit | 9b3731f7e361deb346c5117c2ff79b5e39ac3bc2 (patch) | |
tree | a95d12e430ecc644c8122243d47ff91d83325bc1 /src | |
parent | 43204c874c6d6e7e8d90a362af1a6ada76aa6909 (diff) | |
parent | f674f3120a2e26dc3844211f0c9e1b5ad0478c5b (diff) | |
download | android_packages_providers_DownloadProvider-9b3731f7e361deb346c5117c2ff79b5e39ac3bc2.tar.gz android_packages_providers_DownloadProvider-9b3731f7e361deb346c5117c2ff79b5e39ac3bc2.tar.bz2 android_packages_providers_DownloadProvider-9b3731f7e361deb346c5117c2ff79b5e39ac3bc2.zip |
am f674f312: am 23357198: Stub out the system clock in the download manager, add tests
Merge commit 'f674f3120a2e26dc3844211f0c9e1b5ad0478c5b'
* commit 'f674f3120a2e26dc3844211f0c9e1b5ad0478c5b':
Stub out the system clock in the download manager, add tests
Diffstat (limited to 'src')
4 files changed, 38 insertions, 15 deletions
diff --git a/src/com/android/providers/downloads/DownloadService.java b/src/com/android/providers/downloads/DownloadService.java index 9e890ea0..f3bc9586 100644 --- a/src/com/android/providers/downloads/DownloadService.java +++ b/src/com/android/providers/downloads/DownloadService.java @@ -17,6 +17,7 @@ package com.android.providers.downloads; import com.google.android.collect.Lists; +import com.google.common.annotations.VisibleForTesting; import android.app.AlarmManager; import android.app.PendingIntent; @@ -62,7 +63,7 @@ public class DownloadService extends Service { /** Observer to get notified when the content observer's data changes */ private DownloadManagerContentObserver mObserver; - + /** Class to handle Notification Manager updates */ private DownloadNotification mNotifier; @@ -109,6 +110,9 @@ public class DownloadService extends Service { */ private CharArrayBuffer mNewChars; + @VisibleForTesting + SystemFacade mSystemFacade; + /* ------------ Inner Classes ------------ */ /** @@ -200,6 +204,10 @@ public class DownloadService extends Service { Log.v(Constants.TAG, "Service onCreate"); } + if (mSystemFacade == null) { + mSystemFacade = new RealSystemFacade(); + } + mDownloads = Lists.newArrayList(); mObserver = new DownloadManagerContentObserver(); @@ -209,7 +217,7 @@ public class DownloadService extends Service { mMediaScannerService = null; mMediaScannerConnecting = false; mMediaScannerConnection = new MediaScannerConnection(); - + mNotifier = new DownloadNotification(this); mNotifier.mNotificationMgr.cancelAll(); mNotifier.updateNotification(); @@ -259,10 +267,10 @@ public class DownloadService extends Service { public UpdateThread() { super("Download Service"); } - + public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); - + boolean keepService = false; // for each update from the database, remember which download is // supposed to get restarted soonest in the future @@ -292,7 +300,7 @@ public class DownloadService extends Service { DownloadReceiver.class.getName()); alarms.set( AlarmManager.RTC_WAKEUP, - System.currentTimeMillis() + wakeUp, + mSystemFacade.currentTimeMillis() + wakeUp, PendingIntent.getBroadcast(DownloadService.this, 0, intent, PendingIntent.FLAG_ONE_SHOT)); } @@ -305,7 +313,7 @@ public class DownloadService extends Service { } boolean networkAvailable = Helpers.isNetworkAvailable(DownloadService.this); boolean networkRoaming = Helpers.isNetworkRoaming(DownloadService.this); - long now = System.currentTimeMillis(); + long now = mSystemFacade.currentTimeMillis(); Cursor cursor = getContentResolver().query(Downloads.Impl.CONTENT_URI, null, null, null, Downloads.Impl._ID); @@ -666,7 +674,7 @@ public class DownloadService extends Service { ContentUris.withAppendedId(Downloads.Impl.CONTENT_URI, info.mId), values, null, null); } - DownloadThread downloader = new DownloadThread(this, info); + DownloadThread downloader = new DownloadThread(this, mSystemFacade, info); info.mHasActiveThread = true; downloader.start(); } @@ -757,7 +765,7 @@ public class DownloadService extends Service { getContentResolver().update( ContentUris.withAppendedId(Downloads.Impl.CONTENT_URI, info.mId), values, null, null); - DownloadThread downloader = new DownloadThread(this, info); + DownloadThread downloader = new DownloadThread(this, mSystemFacade, info); info.mHasActiveThread = true; downloader.start(); } diff --git a/src/com/android/providers/downloads/DownloadThread.java b/src/com/android/providers/downloads/DownloadThread.java index d2bd3220..f1296b8f 100644 --- a/src/com/android/providers/downloads/DownloadThread.java +++ b/src/com/android/providers/downloads/DownloadThread.java @@ -56,9 +56,11 @@ public class DownloadThread extends Thread { private Context mContext; private DownloadInfo mInfo; + private SystemFacade mSystemFacade; - public DownloadThread(Context context, DownloadInfo info) { + public DownloadThread(Context context, SystemFacade systemFacade, DownloadInfo info) { mContext = context; + mSystemFacade = systemFacade; mInfo = info; } @@ -131,7 +133,7 @@ public class DownloadThread extends Thread { // Tough luck, that's not a resumable download if (Config.LOGD) { Log.d(Constants.TAG, - "can't resume interrupted non-resumable download"); + "can't resume interrupted non-resumable download"); } f.delete(); finalStatus = Downloads.Impl.STATUS_PRECONDITION_FAILED; @@ -363,7 +365,7 @@ http_request_loop: if (mimeType == null) { header = response.getFirstHeader("Content-Type"); if (header != null) { - mimeType = sanitizeMimeType(header.getValue()); + mimeType = sanitizeMimeType(header.getValue()); } } header = response.getFirstHeader("ETag"); @@ -593,7 +595,7 @@ http_request_loop: } } bytesSoFar += bytesRead; - long now = System.currentTimeMillis(); + long now = mSystemFacade.currentTimeMillis(); if (bytesSoFar - bytesNotified > Constants.MIN_PROGRESS_STEP && now - timeLastNotification > Constants.MIN_PROGRESS_TIME) { @@ -678,7 +680,7 @@ http_request_loop: } else if (Downloads.Impl.isStatusSuccess(finalStatus) && DrmRawContent.DRM_MIMETYPE_MESSAGE_STRING .equalsIgnoreCase(mimeType)) { - // transfer the file to the DRM content provider + // transfer the file to the DRM content provider File file = new File(filename); Intent item = DrmStore.addDrmFile(mContext.getContentResolver(), file, null); if (item == null) { @@ -688,7 +690,7 @@ http_request_loop: filename = item.getDataString(); mimeType = item.getType(); } - + file.delete(); } else if (Downloads.Impl.isStatusSuccess(finalStatus)) { // make sure the file is readable @@ -748,7 +750,7 @@ http_request_loop: values.put(Downloads.Impl.COLUMN_URI, uri); } values.put(Downloads.Impl.COLUMN_MIME_TYPE, mimeType); - values.put(Downloads.Impl.COLUMN_LAST_MODIFICATION, System.currentTimeMillis()); + values.put(Downloads.Impl.COLUMN_LAST_MODIFICATION, mSystemFacade.currentTimeMillis()); values.put(Constants.RETRY_AFTER_X_REDIRECT_COUNT, retryAfter + (redirectCount << 28)); if (!countRetry) { values.put(Constants.FAILED_CONNECTIONS, 0); diff --git a/src/com/android/providers/downloads/RealSystemFacade.java b/src/com/android/providers/downloads/RealSystemFacade.java new file mode 100644 index 00000000..88f10d8c --- /dev/null +++ b/src/com/android/providers/downloads/RealSystemFacade.java @@ -0,0 +1,7 @@ +package com.android.providers.downloads; + +class RealSystemFacade implements SystemFacade { + public long currentTimeMillis() { + return System.currentTimeMillis(); + } +} diff --git a/src/com/android/providers/downloads/SystemFacade.java b/src/com/android/providers/downloads/SystemFacade.java new file mode 100644 index 00000000..4498877d --- /dev/null +++ b/src/com/android/providers/downloads/SystemFacade.java @@ -0,0 +1,6 @@ + +package com.android.providers.downloads; + +interface SystemFacade { + public long currentTimeMillis(); +} |