diff options
author | Christopher Tate <ctate@google.com> | 2014-06-16 17:13:09 -0700 |
---|---|---|
committer | Christopher Tate <ctate@google.com> | 2014-06-16 17:28:42 -0700 |
commit | be3aa64469e8d1806250318808f79f41fb4c7cbd (patch) | |
tree | 477a9455b857b3578af8ec0bb2a31c746e4fed34 /src/com/android/providers/downloads/DownloadService.java | |
parent | 261a12d452e20f2cbe028613d9741e7643a41653 (diff) | |
download | android_packages_providers_DownloadProvider-be3aa64469e8d1806250318808f79f41fb4c7cbd.tar.gz android_packages_providers_DownloadProvider-be3aa64469e8d1806250318808f79f41fb4c7cbd.tar.bz2 android_packages_providers_DownloadProvider-be3aa64469e8d1806250318808f79f41fb4c7cbd.zip |
Transition the Download Service's cleanup work to a scheduled Job
...preparatory to finally removing the scratchpad "idle maintenance"
infrastructure from the product.
Bug 14993295
Change-Id: I1e84247de19e616910db1781b2c399a8b15a805c
Diffstat (limited to 'src/com/android/providers/downloads/DownloadService.java')
-rw-r--r-- | src/com/android/providers/downloads/DownloadService.java | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/com/android/providers/downloads/DownloadService.java b/src/com/android/providers/downloads/DownloadService.java index 084a359c..58cf380c 100644 --- a/src/com/android/providers/downloads/DownloadService.java +++ b/src/com/android/providers/downloads/DownloadService.java @@ -23,6 +23,9 @@ import android.app.AlarmManager; import android.app.DownloadManager; import android.app.PendingIntent; import android.app.Service; +import android.app.job.JobInfo; +import android.app.job.JobScheduler; +import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; @@ -88,6 +91,15 @@ public class DownloadService extends Service { /** Class to handle Notification Manager updates */ private DownloadNotifier mNotifier; + /** Scheduling of the periodic cleanup job */ + private JobInfo mCleanupJob; + + private static final int CLEANUP_JOB_ID = 1; + private static final long CLEANUP_JOB_PERIOD = 1000 * 60 * 60 * 24; // one day + private static ComponentName sCleanupServiceName = new ComponentName( + DownloadIdleService.class.getPackage().getName(), + DownloadIdleService.class.getName()); + /** * The Service's view of the list of downloads, mapping download IDs to the corresponding info * object. This is kept independently from the content provider, and the Service only initiates @@ -193,6 +205,28 @@ public class DownloadService extends Service { mObserver = new DownloadManagerContentObserver(); getContentResolver().registerContentObserver(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, true, mObserver); + + JobScheduler js = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); + if (needToScheduleCleanup(js)) { + final JobInfo job = new JobInfo.Builder(CLEANUP_JOB_ID, sCleanupServiceName) + .setPeriodic(CLEANUP_JOB_PERIOD) + .setRequiresCharging(true) + .setRequiresDeviceIdle(true) + .build(); + js.schedule(job); + } + } + + private boolean needToScheduleCleanup(JobScheduler js) { + List<JobInfo> myJobs = js.getAllPendingJobs(); + final int N = myJobs.size(); + for (int i = 0; i < N; i++) { + if (myJobs.get(i).getId() == CLEANUP_JOB_ID) { + // It's already been (persistently) scheduled; no need to do it again + return false; + } + } + return true; } @Override |