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 mTask; 31 TaskId mId; 32 int mBlockForMillis; 33 34 public MinimalIntervalPolicy(int blockForMillis) { 35 mBlockForMillis = blockForMillis; 36 } 37 38 @Override 39 public void onCreate(BaseTask task, Bundle extras) { 40 mTask = task; 41 mId = mTask.getId(); 42 } 43 44 @Override 45 public void onBeforeExecute() {} 46 47 @Override 48 public void onCompleted() { 49 if (!mTask.hasFailed()) { 50 Intent intent = 51 BaseTask.createIntent(mTask.getContext(), BlockerTask.class, mId.phoneAccountHandle); 52 intent.putExtra(BlockerTask.EXTRA_TASK_ID, mId.id); 53 intent.putExtra(BlockerTask.EXTRA_BLOCK_FOR_MILLIS, mBlockForMillis); 54 mTask.getContext().sendBroadcast(intent); 55 } 56 } 57 58 @Override 59 public void onFail() {} 60 61 @Override 62 public void onDuplicatedTaskAdded() {} 63 } 64