Home | History | Annotate | Download | only in research
      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 /**
     28  * Service to invoke the uploader.
     29  *
     30  * Can be regularly invoked, invoked on boot, etc.
     31  */
     32 public final class UploaderService extends IntentService {
     33     private static final String TAG = UploaderService.class.getSimpleName();
     34     public static final long RUN_INTERVAL = AlarmManager.INTERVAL_HOUR;
     35     public static final String EXTRA_UPLOAD_UNCONDITIONALLY = UploaderService.class.getName()
     36             + ".extra.UPLOAD_UNCONDITIONALLY";
     37 
     38     public UploaderService() {
     39         super("Research Uploader Service");
     40     }
     41 
     42     @Override
     43     protected void onHandleIntent(final Intent intent) {
     44         // We may reach this point either because the alarm fired, or because the system explicitly
     45         // requested that an Upload occur.  In the latter case, we want to cancel the alarm in case
     46         // it's about to fire.
     47         cancelAndRescheduleUploadingService(this, false /* needsRescheduling */);
     48 
     49         final Uploader uploader = new Uploader(this);
     50         if (!uploader.isPossibleToUpload()) return;
     51         if (isUploadingUnconditionally(intent.getExtras()) || uploader.isConvenientToUpload()) {
     52             uploader.doUpload();
     53         }
     54         cancelAndRescheduleUploadingService(this, true /* needsRescheduling */);
     55     }
     56 
     57     private boolean isUploadingUnconditionally(final Bundle bundle) {
     58         if (bundle == null) return false;
     59         if (bundle.containsKey(EXTRA_UPLOAD_UNCONDITIONALLY)) {
     60             return bundle.getBoolean(EXTRA_UPLOAD_UNCONDITIONALLY);
     61         }
     62         return false;
     63     }
     64 
     65     /**
     66      * Arrange for the UploaderService to be run on a regular basis.
     67      *
     68      * Any existing scheduled invocation of UploaderService is removed and optionally rescheduled.
     69      * This may cause problems if this method is called so often that no scheduled invocation is
     70      * ever run.  But if the delay is short enough that it will go off when the user is sleeping,
     71      * then there should be no starvation.
     72      *
     73      * @param context {@link Context} object
     74      * @param needsRescheduling whether to schedule a future intent to be delivered to this service
     75      */
     76     public static void cancelAndRescheduleUploadingService(final Context context,
     77             final boolean needsRescheduling) {
     78         final Intent intent = new Intent(context, UploaderService.class);
     79         final PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
     80         final AlarmManager alarmManager = (AlarmManager) context.getSystemService(
     81                 Context.ALARM_SERVICE);
     82         alarmManager.cancel(pendingIntent);
     83         if (needsRescheduling) {
     84             alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()
     85                     + UploaderService.RUN_INTERVAL, pendingIntent);
     86         }
     87     }
     88 }
     89