summaryrefslogtreecommitdiffstats
path: root/java/com/android/voicemail/impl/StatusCheckTask.java
blob: e59eb3b37ee038aa148975aa177f166f69c6bb44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
 * Copyright (C) 2017 The Android Open Source Project
 *
 * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 * <p>http://www.apache.org/licenses/LICENSE-2.0
 *
 * <p>Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License
 */
package com.android.voicemail.impl;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.telecom.PhoneAccountHandle;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import com.android.dialer.logging.DialerImpression;
import com.android.dialer.proguard.UsedByReflection;
import com.android.voicemail.impl.scheduling.BaseTask;
import com.android.voicemail.impl.sms.StatusMessage;
import com.android.voicemail.impl.sms.StatusSmsFetcher;
import com.android.voicemail.impl.sync.VvmAccountManager;
import com.android.voicemail.impl.utils.LoggerUtils;
import java.io.IOException;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

/**
 * Task to verify the account status is still correct. This task is only for book keeping so any
 * error is ignored and will not retry. If the provision status sent by the carrier is "ready" the
 * access credentials will be updated (although it is not expected to change without the carrier
 * actively sending out an STATUS SMS which will be handled by {@link
 * com.android.voicemail.impl.sms.OmtpMessageReceiver}). If the provisioning status is not ready an
 * {@link ActivationTask} will be launched to attempt to correct it.
 */
@TargetApi(VERSION_CODES.O)
@UsedByReflection(value = "Tasks.java")
public class StatusCheckTask extends BaseTask {

  public StatusCheckTask() {
    super(TASK_STATUS_CHECK);
  }

  public static void start(Context context, PhoneAccountHandle phoneAccountHandle) {
    Intent intent = BaseTask.createIntent(context, StatusCheckTask.class, phoneAccountHandle);
    context.sendBroadcast(intent);
  }

  @Override
  public void onExecuteInBackgroundThread() {
    TelephonyManager telephonyManager =
        getContext()
            .getSystemService(TelephonyManager.class)
            .createForPhoneAccountHandle(getPhoneAccountHandle());

    if (telephonyManager == null) {
      VvmLog.w(
          "StatusCheckTask.onExecuteInBackgroundThread",
          getPhoneAccountHandle() + " no longer valid");
      return;
    }
    if (telephonyManager.getServiceState().getState() != ServiceState.STATE_IN_SERVICE) {
      VvmLog.i(
          "StatusCheckTask.onExecuteInBackgroundThread",
          getPhoneAccountHandle() + " not in service");
      return;
    }
    OmtpVvmCarrierConfigHelper config =
        new OmtpVvmCarrierConfigHelper(getContext(), getPhoneAccountHandle());
    if (!config.isValid()) {
      VvmLog.e(
          "StatusCheckTask.onExecuteInBackgroundThread",
          "config no longer valid for " + getPhoneAccountHandle());
      VvmAccountManager.removeAccount(getContext(), getPhoneAccountHandle());
      return;
    }

    Bundle data;
    try (StatusSmsFetcher fetcher = new StatusSmsFetcher(getContext(), getPhoneAccountHandle())) {
      config.getProtocol().requestStatus(config, fetcher.getSentIntent());
      // Both the fetcher and OmtpMessageReceiver will be triggered, but
      // OmtpMessageReceiver will just route the SMS back to ActivationTask, which will be
      // rejected because the task is still running.
      data = fetcher.get();
    } catch (TimeoutException e) {
      VvmLog.e("StatusCheckTask.onExecuteInBackgroundThread", "timeout requesting status");
      return;
    } catch (CancellationException e) {
      VvmLog.e("StatusCheckTask.onExecuteInBackgroundThread", "Unable to send status request SMS");
      return;
    } catch (InterruptedException | ExecutionException | IOException e) {
      VvmLog.e("StatusCheckTask.onExecuteInBackgroundThread", "can't get future STATUS SMS", e);
      return;
    }

    StatusMessage message = new StatusMessage(data);
    VvmLog.i(
        "StatusCheckTask.onExecuteInBackgroundThread",
        "STATUS SMS received: st="
            + message.getProvisioningStatus()
            + ", rc="
            + message.getReturnCode());
    if (message.getProvisioningStatus().equals(OmtpConstants.SUBSCRIBER_READY)) {
      VvmLog.i(
          "StatusCheckTask.onExecuteInBackgroundThread",
          "subscriber ready, no activation required");
      LoggerUtils.logImpressionOnMainThread(
          getContext(), DialerImpression.Type.VVM_STATUS_CHECK_READY);
      VvmAccountManager.addAccount(getContext(), getPhoneAccountHandle(), message);
    } else {
      VvmLog.i(
          "StatusCheckTask.onExecuteInBackgroundThread",
          "subscriber not ready, attempting reactivation");
      VvmAccountManager.removeAccount(getContext(), getPhoneAccountHandle());
      LoggerUtils.logImpressionOnMainThread(
          getContext(), DialerImpression.Type.VVM_STATUS_CHECK_REACTIVATION);
      ActivationTask.start(getContext(), getPhoneAccountHandle(), data);
    }
  }
}