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 postponeMillis;
33   private BaseTask task;
34 
PostponePolicy(int postponeMillis)35   public PostponePolicy(int postponeMillis) {
36     this.postponeMillis = postponeMillis;
37   }
38 
39   @Override
onCreate(BaseTask task, Bundle extras)40   public void onCreate(BaseTask task, Bundle extras) {
41     this.task = task;
42     this.task.setExecutionTime(this.task.getTimeMillis() + postponeMillis);
43   }
44 
45   @Override
onBeforeExecute()46   public void onBeforeExecute() {
47     // Do nothing
48   }
49 
50   @Override
onCompleted()51   public void onCompleted() {
52     // Do nothing
53   }
54 
55   @Override
onFail()56   public void onFail() {
57     // Do nothing
58   }
59 
60   @Override
onDuplicatedTaskAdded()61   public void onDuplicatedTaskAdded() {
62     if (task.hasStarted()) {
63       return;
64     }
65     VvmLog.i(TAG, "postponing " + task);
66     task.setExecutionTime(task.getTimeMillis() + postponeMillis);
67   }
68 }
69