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