1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.research; 18 19 import android.app.AlarmManager; 20 import android.app.IntentService; 21 import android.app.PendingIntent; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.os.Bundle; 25 import android.os.SystemClock; 26 27 import com.android.inputmethod.latin.define.ProductionFlag; 28 29 /** 30 * Service to invoke the uploader. 31 * 32 * Can be regularly invoked, invoked on boot, etc. 33 */ 34 public final class UploaderService extends IntentService { 35 private static final String TAG = UploaderService.class.getSimpleName(); 36 private static final boolean DEBUG = false 37 && ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS_DEBUG; 38 public static final long RUN_INTERVAL = AlarmManager.INTERVAL_HOUR; 39 public static final String EXTRA_UPLOAD_UNCONDITIONALLY = UploaderService.class.getName() 40 + ".extra.UPLOAD_UNCONDITIONALLY"; 41 protected static final int TIMEOUT_IN_MS = 1000 * 4; 42 43 public UploaderService() { 44 super("Research Uploader Service"); 45 } 46 47 @Override 48 protected void onHandleIntent(final Intent intent) { 49 // We may reach this point either because the alarm fired, or because the system explicitly 50 // requested that an Upload occur. In the latter case, we want to cancel the alarm in case 51 // it's about to fire. 52 cancelAndRescheduleUploadingService(this, false /* needsRescheduling */); 53 54 final Uploader uploader = new Uploader(this); 55 if (!uploader.isPossibleToUpload()) return; 56 if (isUploadingUnconditionally(intent.getExtras()) || uploader.isConvenientToUpload()) { 57 uploader.doUpload(); 58 } 59 cancelAndRescheduleUploadingService(this, true /* needsRescheduling */); 60 } 61 62 private boolean isUploadingUnconditionally(final Bundle bundle) { 63 if (bundle == null) return false; 64 if (bundle.containsKey(EXTRA_UPLOAD_UNCONDITIONALLY)) { 65 return bundle.getBoolean(EXTRA_UPLOAD_UNCONDITIONALLY); 66 } 67 return false; 68 } 69 70 /** 71 * Arrange for the UploaderService to be run on a regular basis. 72 * 73 * Any existing scheduled invocation of UploaderService is removed and optionally rescheduled. 74 * This may cause problems if this method is called so often that no scheduled invocation is 75 * ever run. But if the delay is short enough that it will go off when the user is sleeping, 76 * then there should be no starvation. 77 * 78 * @param context {@link Context} object 79 * @param needsRescheduling whether to schedule a future intent to be delivered to this service 80 */ 81 public static void cancelAndRescheduleUploadingService(final Context context, 82 final boolean needsRescheduling) { 83 final Intent intent = new Intent(context, UploaderService.class); 84 final PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); 85 final AlarmManager alarmManager = (AlarmManager) context.getSystemService( 86 Context.ALARM_SERVICE); 87 alarmManager.cancel(pendingIntent); 88 if (needsRescheduling) { 89 alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() 90 + UploaderService.RUN_INTERVAL, pendingIntent); 91 } 92 } 93 } 94