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   private static final String EXTRA_SYNC_TYPE = "extra_sync_type";
41 
42   private final RetryPolicy mRetryPolicy;
43 
44   private PhoneAccountHandle mPhone;
45   private String mSyncType;
46 
start(Context context, PhoneAccountHandle phone, String syncType)47   public static void start(Context context, PhoneAccountHandle phone, String syncType) {
48     Intent intent = BaseTask.createIntent(context, SyncTask.class, phone);
49     intent.putExtra(EXTRA_PHONE_ACCOUNT_HANDLE, phone);
50     intent.putExtra(EXTRA_SYNC_TYPE, syncType);
51     context.sendBroadcast(intent);
52   }
53 
SyncTask()54   public SyncTask() {
55     super(TASK_SYNC);
56     mRetryPolicy = new RetryPolicy(RETRY_TIMES, RETRY_INTERVAL_MILLIS);
57     addPolicy(mRetryPolicy);
58     addPolicy(new MinimalIntervalPolicy(MINIMAL_INTERVAL_MILLIS));
59   }
60 
61   @Override
onCreate(Context context, Bundle extras)62   public void onCreate(Context context, Bundle extras) {
63     super.onCreate(context, extras);
64     mPhone = extras.getParcelable(EXTRA_PHONE_ACCOUNT_HANDLE);
65     mSyncType = extras.getString(EXTRA_SYNC_TYPE);
66   }
67 
68   @Override
onExecuteInBackgroundThread()69   public void onExecuteInBackgroundThread() {
70     OmtpVvmSyncService service = new OmtpVvmSyncService(getContext());
71     service.sync(this, mSyncType, mPhone, null, mRetryPolicy.getVoicemailStatusEditor());
72   }
73 
74   @Override
createRestartIntent()75   public Intent createRestartIntent() {
76     LoggerUtils.logImpressionOnMainThread(getContext(), DialerImpression.Type.VVM_AUTO_RETRY_SYNC);
77     Intent intent = super.createRestartIntent();
78     intent.putExtra(EXTRA_PHONE_ACCOUNT_HANDLE, mPhone);
79     intent.putExtra(EXTRA_SYNC_TYPE, mSyncType);
80     return intent;
81   }
82 }
83