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.dialer.app.voicemail.error;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.database.Cursor;
22import android.os.Build.VERSION;
23import android.os.Build.VERSION_CODES;
24import android.provider.VoicemailContract.Status;
25import android.telecom.PhoneAccountHandle;
26import android.telephony.TelephonyManager;
27import com.android.contacts.common.compat.TelephonyManagerCompat;
28import com.android.dialer.common.Assert;
29import com.android.dialer.common.LogUtil;
30import com.android.dialer.configprovider.ConfigProviderBindings;
31import com.android.dialer.logging.DialerImpression;
32import com.android.dialer.logging.Logger;
33
34/**
35 * This class will detect the corruption in the voicemail status and log it so we can track how many
36 * users are affected.
37 */
38public class VoicemailStatusCorruptionHandler {
39
40  /** Where the check is made so logging can be done. */
41  public enum Source {
42    Activity,
43    Notification
44  }
45
46  private static final String CONFIG_VVM_STATUS_FIX_DISABLED = "vvm_status_fix_disabled";
47
48  public static void maybeFixVoicemailStatus(Context context, Cursor statusCursor, Source source) {
49
50    if (ConfigProviderBindings.get(context).getBoolean(CONFIG_VVM_STATUS_FIX_DISABLED, false)) {
51      return;
52    }
53
54    if (VERSION.SDK_INT != VERSION_CODES.N_MR1) {
55      // This issue is specific to N MR1, it is fixed in future SDK.
56      return;
57    }
58
59    if (statusCursor.getCount() == 0) {
60      return;
61    }
62
63    statusCursor.moveToFirst();
64    VoicemailStatus status = new VoicemailStatus(context, statusCursor);
65    PhoneAccountHandle phoneAccountHandle =
66        new PhoneAccountHandle(
67            ComponentName.unflattenFromString(status.phoneAccountComponentName),
68            status.phoneAccountId);
69
70    TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class);
71
72    boolean visualVoicemailEnabled =
73        TelephonyManagerCompat.isVisualVoicemailEnabled(telephonyManager, phoneAccountHandle);
74    LogUtil.i(
75        "VoicemailStatusCorruptionHandler.maybeFixVoicemailStatus",
76        "Source="
77            + source
78            + ", CONFIGURATION_STAIE="
79            + status.configurationState
80            + ", visualVoicemailEnabled="
81            + visualVoicemailEnabled);
82
83    // If visual voicemail is enabled, the CONFIGURATION_STATE should be either OK, PIN_NOT_SET,
84    // or other failure code. CONFIGURATION_STATE_NOT_CONFIGURED means that the client has been
85    // shut down improperly (b/32371710). The client should be reset or the VVM tab will be
86    // missing.
87    if (Status.CONFIGURATION_STATE_NOT_CONFIGURED == status.configurationState
88        && visualVoicemailEnabled) {
89      LogUtil.e(
90          "VoicemailStatusCorruptionHandler.maybeFixVoicemailStatus",
91          "VVM3 voicemail status corrupted");
92
93      switch (source) {
94        case Activity:
95          Logger.get(context)
96              .logImpression(
97                  DialerImpression.Type
98                      .VOICEMAIL_CONFIGURATION_STATE_CORRUPTION_DETECTED_FROM_ACTIVITY);
99          break;
100        case Notification:
101          Logger.get(context)
102              .logImpression(
103                  DialerImpression.Type
104                      .VOICEMAIL_CONFIGURATION_STATE_CORRUPTION_DETECTED_FROM_NOTIFICATION);
105          break;
106        default:
107          Assert.fail("this should never happen");
108          break;
109      }
110      // At this point we could attempt to work around the issue by disabling and re-enabling
111      // voicemail. Unfortunately this work around is buggy so we'll do nothing for now.
112    }
113  }
114}
115