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.sync;
18
19import android.content.Context;
20import android.content.Intent;
21import android.os.Bundle;
22import android.telecom.PhoneAccountHandle;
23import com.android.dialer.logging.DialerImpression;
24import com.android.dialer.proguard.UsedByReflection;
25import com.android.voicemail.impl.Voicemail;
26import com.android.voicemail.impl.VoicemailStatus;
27import com.android.voicemail.impl.scheduling.BaseTask;
28import com.android.voicemail.impl.scheduling.RetryPolicy;
29import com.android.voicemail.impl.utils.LoggerUtils;
30
31/**
32 * Task to download a single voicemail from the server. This task is initiated by a SMS notifying
33 * the new voicemail arrival, and ignores the duplicated tasks constraint.
34 */
35@UsedByReflection(value = "Tasks.java")
36public class SyncOneTask extends BaseTask {
37
38  private static final int RETRY_TIMES = 2;
39  private static final int RETRY_INTERVAL_MILLIS = 5_000;
40
41  private static final String EXTRA_PHONE_ACCOUNT_HANDLE = "extra_phone_account_handle";
42  private static final String EXTRA_SYNC_TYPE = "extra_sync_type";
43  private static final String EXTRA_VOICEMAIL = "extra_voicemail";
44
45  private PhoneAccountHandle mPhone;
46  private String mSyncType;
47  private Voicemail mVoicemail;
48
49  public static void start(Context context, PhoneAccountHandle phone, Voicemail voicemail) {
50    Intent intent = BaseTask.createIntent(context, SyncOneTask.class, phone);
51    intent.putExtra(EXTRA_PHONE_ACCOUNT_HANDLE, phone);
52    intent.putExtra(EXTRA_SYNC_TYPE, OmtpVvmSyncService.SYNC_DOWNLOAD_ONE_TRANSCRIPTION);
53    intent.putExtra(EXTRA_VOICEMAIL, voicemail);
54    context.sendBroadcast(intent);
55  }
56
57  public SyncOneTask() {
58    super(TASK_ALLOW_DUPLICATES);
59    addPolicy(new RetryPolicy(RETRY_TIMES, RETRY_INTERVAL_MILLIS));
60  }
61
62  @Override
63  public void onCreate(Context context, Bundle extras) {
64    super.onCreate(context, extras);
65    mPhone = extras.getParcelable(EXTRA_PHONE_ACCOUNT_HANDLE);
66    mSyncType = extras.getString(EXTRA_SYNC_TYPE);
67    mVoicemail = extras.getParcelable(EXTRA_VOICEMAIL);
68  }
69
70  @Override
71  public void onExecuteInBackgroundThread() {
72    OmtpVvmSyncService service = new OmtpVvmSyncService(getContext());
73    service.sync(this, mSyncType, mPhone, mVoicemail, VoicemailStatus.edit(getContext(), mPhone));
74  }
75
76  @Override
77  public Intent createRestartIntent() {
78    LoggerUtils.logImpressionOnMainThread(getContext(), DialerImpression.Type.VVM_AUTO_RETRY_SYNC);
79    Intent intent = super.createRestartIntent();
80    intent.putExtra(EXTRA_PHONE_ACCOUNT_HANDLE, mPhone);
81    intent.putExtra(EXTRA_SYNC_TYPE, mSyncType);
82    intent.putExtra(EXTRA_VOICEMAIL, mVoicemail);
83    return intent;
84  }
85}
86