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 android.os.Bundle;
21import android.telecom.PhoneAccountHandle;
22import com.android.voicemail.impl.VoicemailStatus;
23import 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 */
30public 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 if
57   * 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, Bundle extras) {
65    mTask = task;
66    mRetryCount = extras.getInt(EXTRA_RETRY_COUNT, 0);
67    if (mRetryCount > 0) {
68      VvmLog.i(
69          TAG,
70          "retry #" + mRetryCount + " for " + mTask + " queued, executing in " + mRetryDelayMillis);
71      mTask.setExecutionTime(mTask.getTimeMillis() + mRetryDelayMillis);
72    }
73    PhoneAccountHandle phoneAccountHandle = task.getPhoneAccountHandle();
74    if (phoneAccountHandle == null) {
75      VvmLog.e(TAG, "null phone account for phoneAccountHandle " + task.getPhoneAccountHandle());
76      // This should never happen, but continue on if it does. The status write will be
77      // discarded.
78    }
79    mVoicemailStatusEditor = VoicemailStatus.deferredEdit(task.getContext(), phoneAccountHandle);
80  }
81
82  @Override
83  public void onBeforeExecute() {}
84
85  @Override
86  public void onCompleted() {
87    if (!mFailed || !hasMoreRetries()) {
88      if (!mFailed) {
89        VvmLog.i(TAG, mTask + " completed successfully");
90      }
91      if (!hasMoreRetries()) {
92        VvmLog.i(TAG, "Retry limit for " + mTask + " reached");
93      }
94      VvmLog.i(TAG, "committing deferred status: " + mVoicemailStatusEditor.getValues());
95      mVoicemailStatusEditor.deferredApply();
96      return;
97    }
98    VvmLog.i(TAG, "discarding deferred status: " + mVoicemailStatusEditor.getValues());
99    Intent intent = mTask.createRestartIntent();
100    intent.putExtra(EXTRA_RETRY_COUNT, mRetryCount + 1);
101
102    mTask.getContext().sendBroadcast(intent);
103  }
104
105  @Override
106  public void onFail() {
107    mFailed = true;
108  }
109
110  @Override
111  public void onDuplicatedTaskAdded() {}
112}
113