MinimalIntervalPolicy.java revision d5e47f6da5b08b13ecdfa7f1edc7e12aeb83fab9
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.voicemail.impl.scheduling;
18
19import android.content.Intent;
20import com.android.voicemail.impl.scheduling.Task.TaskId;
21
22/**
23 * If a task with this policy succeeds, a {@link BlockerTask} with the same {@link TaskId} of the
24 * task will be queued immediately, preventing the same task from running for a certain amount of
25 * time.
26 */
27public class MinimalIntervalPolicy implements Policy {
28
29  BaseTask mTask;
30  TaskId mId;
31  int mBlockForMillis;
32
33  public MinimalIntervalPolicy(int blockForMillis) {
34    mBlockForMillis = blockForMillis;
35  }
36
37  @Override
38  public void onCreate(BaseTask task, Intent intent, int flags, int startId) {
39    mTask = task;
40    mId = mTask.getId();
41  }
42
43  @Override
44  public void onBeforeExecute() {}
45
46  @Override
47  public void onCompleted() {
48    if (!mTask.hasFailed()) {
49      Intent intent =
50          mTask.createIntent(mTask.getContext(), BlockerTask.class, mId.phoneAccountHandle);
51      intent.putExtra(BlockerTask.EXTRA_TASK_ID, mId.id);
52      intent.putExtra(BlockerTask.EXTRA_BLOCK_FOR_MILLIS, mBlockForMillis);
53      mTask.getContext().startService(intent);
54    }
55  }
56
57  @Override
58  public void onFail() {}
59
60  @Override
61  public void onDuplicatedTaskAdded() {}
62}
63