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.sync;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.telecom.PhoneAccountHandle;
23 import com.android.dialer.logging.DialerImpression;
24 import com.android.dialer.proguard.UsedByReflection;
25 import com.android.voicemail.impl.scheduling.BaseTask;
26 import com.android.voicemail.impl.scheduling.MinimalIntervalPolicy;
27 import com.android.voicemail.impl.scheduling.RetryPolicy;
28 import com.android.voicemail.impl.utils.LoggerUtils;
29 
30 /** System initiated sync request. */
31 @UsedByReflection(value = "Tasks.java")
32 public class SyncTask extends BaseTask {
33 
34   // Try sync for a total of 5 times, should take around 5 minutes before finally giving up.
35   private static final int RETRY_TIMES = 4;
36   private static final int RETRY_INTERVAL_MILLIS = 5_000;
37   private static final int MINIMAL_INTERVAL_MILLIS = 60_000;
38 
39   private static final String EXTRA_PHONE_ACCOUNT_HANDLE = "extra_phone_account_handle";
40 
41   private final RetryPolicy retryPolicy;
42 
43   private PhoneAccountHandle phone;
44 
start(Context context, PhoneAccountHandle phone)45   public static void start(Context context, PhoneAccountHandle phone) {
46     Intent intent = BaseTask.createIntent(context, SyncTask.class, phone);
47     intent.putExtra(EXTRA_PHONE_ACCOUNT_HANDLE, phone);
48     context.sendBroadcast(intent);
49   }
50 
SyncTask()51   public SyncTask() {
52     super(TASK_SYNC);
53     retryPolicy = new RetryPolicy(RETRY_TIMES, RETRY_INTERVAL_MILLIS);
54     addPolicy(retryPolicy);
55     addPolicy(new MinimalIntervalPolicy(MINIMAL_INTERVAL_MILLIS));
56   }
57 
58   @Override
onCreate(Context context, Bundle extras)59   public void onCreate(Context context, Bundle extras) {
60     super.onCreate(context, extras);
61     phone = extras.getParcelable(EXTRA_PHONE_ACCOUNT_HANDLE);
62   }
63 
64   @Override
onExecuteInBackgroundThread()65   public void onExecuteInBackgroundThread() {
66     OmtpVvmSyncService service = new OmtpVvmSyncService(getContext());
67     service.sync(this, phone, null, retryPolicy.getVoicemailStatusEditor());
68   }
69 
70   @Override
createRestartIntent()71   public Intent createRestartIntent() {
72     LoggerUtils.logImpressionOnMainThread(getContext(), DialerImpression.Type.VVM_AUTO_RETRY_SYNC);
73     Intent intent = super.createRestartIntent();
74     intent.putExtra(EXTRA_PHONE_ACCOUNT_HANDLE, phone);
75     return intent;
76   }
77 }
78