diff options
author | 2013-02-27 17:27:12 -0800 | |
---|---|---|
committer | 2013-03-25 17:50:26 -0700 | |
commit | 6d71d238e2e072802cb36a011a52f38f3efd9c40 (patch) | |
tree | ed9160b8651586e356c4b6f206fdfde19542d4f7 /java/src/com/android/inputmethod/research/UploaderService.java | |
parent | fb658d6c531de8cfd55e4d36bffc9ccabc401f94 (diff) | |
download | latinime-6d71d238e2e072802cb36a011a52f38f3efd9c40.tar.gz latinime-6d71d238e2e072802cb36a011a52f38f3efd9c40.tar.xz latinime-6d71d238e2e072802cb36a011a52f38f3efd9c40.zip |
[FileEncap18] Clean up uploading scheduling
- Move scheduling logic from ResearchLogger.java to
UploaderService.java
- Switch to a one-shot timer. Previously the uploader was scheduled
on an inexact repeating schedule. It's better to reschedule the
next upload after the current one is finished to reduce the chances
of multiple uploads happening at the same time.
- Avoid double-execution
- Previously a scheduled upload might run right after an explicit
one if they occured at the same time. This change reduces the
chances of this.
- Some method extraction and naming
Change-Id: I9efda11be77d334c7f61bd40a36d65f0421ebde4
Diffstat (limited to 'java/src/com/android/inputmethod/research/UploaderService.java')
-rw-r--r-- | java/src/com/android/inputmethod/research/UploaderService.java | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/research/UploaderService.java b/java/src/com/android/inputmethod/research/UploaderService.java index 6a9f5c1f4..6a9717b7c 100644 --- a/java/src/com/android/inputmethod/research/UploaderService.java +++ b/java/src/com/android/inputmethod/research/UploaderService.java @@ -18,6 +18,8 @@ package com.android.inputmethod.research; import android.app.AlarmManager; import android.app.IntentService; +import android.app.PendingIntent; +import android.content.Context; import android.content.Intent; import android.os.Bundle; @@ -43,11 +45,17 @@ public final class UploaderService extends IntentService { @Override protected void onHandleIntent(final Intent intent) { + // We may reach this point either because the alarm fired, or because the system explicitly + // requested that an Upload occur. In the latter case, we want to cancel the alarm in case + // it's about to fire. + cancelAndRescheduleUploadingService(this, false /* needsRescheduling */); + final Uploader uploader = new Uploader(this); if (!uploader.isPossibleToUpload()) return; if (isUploadingUnconditionally(intent.getExtras()) || uploader.isConvenientToUpload()) { uploader.doUpload(); } + cancelAndRescheduleUploadingService(this, true /* needsRescheduling */); } private boolean isUploadingUnconditionally(final Bundle bundle) { @@ -57,4 +65,42 @@ public final class UploaderService extends IntentService { } return false; } + + /** + * Arrange for the UploaderService to be run on a regular basis. + * + * Any existing scheduled invocation of UploaderService is removed and optionally rescheduled. + * This may cause problems if this method is called so often that no scheduled invocation is + * ever run. But if the delay is short enough that it will go off when the user is sleeping, + * then there should be no starvation. + * + * @param context {@link Context} object + * @param needsRescheduling whether to schedule a future intent to be delivered to this service + */ + public static void cancelAndRescheduleUploadingService(final Context context, + final boolean needsRescheduling) { + final PendingIntent pendingIntent = getPendingIntentForService(context); + final AlarmManager alarmManager = (AlarmManager) context.getSystemService( + Context.ALARM_SERVICE); + cancelAnyScheduledServiceAlarm(alarmManager, pendingIntent); + if (needsRescheduling) { + scheduleServiceAlarm(alarmManager, pendingIntent); + } + } + + private static PendingIntent getPendingIntentForService(final Context context) { + final Intent intent = new Intent(context, UploaderService.class); + return PendingIntent.getService(context, 0, intent, 0); + } + + private static void cancelAnyScheduledServiceAlarm(final AlarmManager alarmManager, + final PendingIntent pendingIntent) { + alarmManager.cancel(pendingIntent); + } + + private static void scheduleServiceAlarm(final AlarmManager alarmManager, + final PendingIntent pendingIntent) { + alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, UploaderService.RUN_INTERVAL, + pendingIntent); + } } |