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.phone.vvm.omtp.scheduling; 18 19 import android.content.Intent; 20 import android.telecom.PhoneAccountHandle; 21 import com.android.phone.VoicemailStatus; 22 import com.android.phone.vvm.omtp.VvmLog; 23 import com.android.phone.vvm.omtp.utils.PhoneAccountHandleConverter; 24 25 /** 26 * A task with this policy will automatically re-queue itself if {@link BaseTask#fail()} has been 27 * called during {@link BaseTask#onExecuteInBackgroundThread()}. A task will be retried at most 28 * <code>retryLimit</code> times and with a <code>retryDelayMillis</code> interval in between. 29 */ 30 public class RetryPolicy implements Policy { 31 32 private static final String TAG = "RetryPolicy"; 33 private static final String EXTRA_RETRY_COUNT = "extra_retry_count"; 34 35 private final int mRetryLimit; 36 private final int mRetryDelayMillis; 37 38 private BaseTask mTask; 39 40 private int mRetryCount; 41 private boolean mFailed; 42 43 private VoicemailStatus.DeferredEditor mVoicemailStatusEditor; 44 45 public RetryPolicy(int retryLimit, int retryDelayMillis) { 46 mRetryLimit = retryLimit; 47 mRetryDelayMillis = retryDelayMillis; 48 } 49 50 private boolean hasMoreRetries() { 51 return mRetryCount < mRetryLimit; 52 } 53 54 /** 55 * Error status should only be set if retries has exhausted or the task is successful. Status 56 * writes to this editor will be deferred until the task has ended, and will only be committed 57 * if the task is successful or there are no retries left. 58 */ 59 public VoicemailStatus.Editor getVoicemailStatusEditor() { 60 return mVoicemailStatusEditor; 61 } 62 63 @Override 64 public void onCreate(BaseTask task, Intent intent, int flags, int startId) { 65 mTask = task; 66 mRetryCount = intent.getIntExtra(EXTRA_RETRY_COUNT, 0); 67 if (mRetryCount > 0) { 68 VvmLog.d(TAG, "retry #" + mRetryCount + " for " + mTask + " queued, executing in " 69 + mRetryDelayMillis); 70 mTask.setExecutionTime(mTask.getTimeMillis() + mRetryDelayMillis); 71 } 72 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter 73 .fromSubId(task.getSubId()); 74 if (phoneAccountHandle == null) { 75 VvmLog.e(TAG, "null phone account for subId " + task.getSubId()); 76 // This should never happen, but continue on if it does. The status write will be 77 // discarded. 78 } 79 mVoicemailStatusEditor = VoicemailStatus 80 .deferredEdit(task.getContext(), phoneAccountHandle); 81 } 82 83 @Override 84 public void onBeforeExecute() { 85 86 } 87 88 @Override 89 public void onCompleted() { 90 if (!mFailed || !hasMoreRetries()) { 91 if (!mFailed) { 92 VvmLog.d(TAG, mTask.toString() + " completed successfully"); 93 } 94 if (!hasMoreRetries()) { 95 VvmLog.d(TAG, "Retry limit for " + mTask + " reached"); 96 } 97 VvmLog.i(TAG, "committing deferred status: " + mVoicemailStatusEditor.getValues()); 98 mVoicemailStatusEditor.deferredApply(); 99 return; 100 } 101 VvmLog.i(TAG, "discarding deferred status: " + mVoicemailStatusEditor.getValues()); 102 Intent intent = mTask.createRestartIntent(); 103 intent.putExtra(EXTRA_RETRY_COUNT, mRetryCount + 1); 104 105 mTask.getContext().startService(intent); 106 } 107 108 @Override 109 public void onFail() { 110 mFailed = true; 111 } 112 113 @Override 114 public void onDuplicatedTaskAdded() { 115 116 } 117 } 118