aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/research/UploaderService.java
diff options
context:
space:
mode:
authorKurt Partridge <kep@google.com>2013-04-09 19:08:39 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2013-04-09 19:08:40 +0000
commit09ab6495a2ef109043e6d92f5d75304fcce472a3 (patch)
tree5201c1aac120b306b45d24096fa9daad0bf065e0 /java/src/com/android/inputmethod/research/UploaderService.java
parent19dcd154932b8aef07f371bdda7f879a70c3c3c8 (diff)
parent6d71d238e2e072802cb36a011a52f38f3efd9c40 (diff)
downloadlatinime-09ab6495a2ef109043e6d92f5d75304fcce472a3.tar.gz
latinime-09ab6495a2ef109043e6d92f5d75304fcce472a3.tar.xz
latinime-09ab6495a2ef109043e6d92f5d75304fcce472a3.zip
Merge "[FileEncap18] Clean up uploading scheduling"
Diffstat (limited to 'java/src/com/android/inputmethod/research/UploaderService.java')
-rw-r--r--java/src/com/android/inputmethod/research/UploaderService.java46
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);
+ }
}