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 android.telecom.PhoneAccountHandle;
22 import com.android.voicemail.impl.VoicemailStatus;
23 import com.android.voicemail.impl.VvmLog;
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 retryLimit;
36   private final int retryDelayMillis;
37 
38   private BaseTask task;
39 
40   private int retryCount;
41   private boolean failed;
42 
43   private VoicemailStatus.DeferredEditor voicemailStatusEditor;
44 
RetryPolicy(int retryLimit, int retryDelayMillis)45   public RetryPolicy(int retryLimit, int retryDelayMillis) {
46     this.retryLimit = retryLimit;
47     this.retryDelayMillis = retryDelayMillis;
48   }
49 
hasMoreRetries()50   private boolean hasMoreRetries() {
51     return retryCount < retryLimit;
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 if
57    * the task is successful or there are no retries left.
58    */
getVoicemailStatusEditor()59   public VoicemailStatus.Editor getVoicemailStatusEditor() {
60     return voicemailStatusEditor;
61   }
62 
63   @Override
onCreate(BaseTask task, Bundle extras)64   public void onCreate(BaseTask task, Bundle extras) {
65     this.task = task;
66     retryCount = extras.getInt(EXTRA_RETRY_COUNT, 0);
67     if (retryCount > 0) {
68       VvmLog.i(
69           TAG,
70           "retry #"
71               + retryCount
72               + " for "
73               + this.task
74               + " queued, executing in "
75               + retryDelayMillis);
76       this.task.setExecutionTime(this.task.getTimeMillis() + retryDelayMillis);
77     }
78     PhoneAccountHandle phoneAccountHandle = task.getPhoneAccountHandle();
79     if (phoneAccountHandle == null) {
80       VvmLog.e(TAG, "null phone account for phoneAccountHandle " + task.getPhoneAccountHandle());
81       // This should never happen, but continue on if it does. The status write will be
82       // discarded.
83     }
84     voicemailStatusEditor = VoicemailStatus.deferredEdit(task.getContext(), phoneAccountHandle);
85   }
86 
87   @Override
onBeforeExecute()88   public void onBeforeExecute() {}
89 
90   @Override
onCompleted()91   public void onCompleted() {
92     if (!failed || !hasMoreRetries()) {
93       if (!failed) {
94         VvmLog.i(TAG, task + " completed successfully");
95       }
96       if (!hasMoreRetries()) {
97         VvmLog.i(TAG, "Retry limit for " + task + " reached");
98       }
99       VvmLog.i(TAG, "committing deferred status: " + voicemailStatusEditor.getValues());
100       voicemailStatusEditor.deferredApply();
101       return;
102     }
103     VvmLog.i(TAG, "discarding deferred status: " + voicemailStatusEditor.getValues());
104     Intent intent = task.createRestartIntent();
105     intent.putExtra(EXTRA_RETRY_COUNT, retryCount + 1);
106 
107     task.getContext().sendBroadcast(intent);
108   }
109 
110   @Override
onFail()111   public void onFail() {
112     failed = true;
113   }
114 
115   @Override
onDuplicatedTaskAdded()116   public void onDuplicatedTaskAdded() {}
117 }
118