Home | History | Annotate | Download | only in scheduling
      1 /*
      2  * Copyright (C) 2016 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.voicemail.impl.scheduling;
     18 
     19 import android.os.Bundle;
     20 import com.android.voicemail.impl.VvmLog;
     21 
     22 /**
     23  * A task with Postpone policy will not be executed immediately. It will wait for a while and if a
     24  * duplicated task is queued during the duration, the task will be postponed further. The task will
     25  * only be executed if no new task was added in postponeMillis. Useful to batch small tasks in quick
     26  * succession together.
     27  */
     28 public class PostponePolicy implements Policy {
     29 
     30   private static final String TAG = "PostponePolicy";
     31 
     32   private final int mPostponeMillis;
     33   private BaseTask mTask;
     34 
     35   public PostponePolicy(int postponeMillis) {
     36     mPostponeMillis = postponeMillis;
     37   }
     38 
     39   @Override
     40   public void onCreate(BaseTask task, Bundle extras) {
     41     mTask = task;
     42     mTask.setExecutionTime(mTask.getTimeMillis() + mPostponeMillis);
     43   }
     44 
     45   @Override
     46   public void onBeforeExecute() {
     47     // Do nothing
     48   }
     49 
     50   @Override
     51   public void onCompleted() {
     52     // Do nothing
     53   }
     54 
     55   @Override
     56   public void onFail() {
     57     // Do nothing
     58   }
     59 
     60   @Override
     61   public void onDuplicatedTaskAdded() {
     62     if (mTask.hasStarted()) {
     63       return;
     64     }
     65     VvmLog.i(TAG, "postponing " + mTask);
     66     mTask.setExecutionTime(mTask.getTimeMillis() + mPostponeMillis);
     67   }
     68 }
     69