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