summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/app/voicemail/error/VoicemailStatusCorruptionHandler.java
blob: 3a169e41c6875e68fb6781004296e80b37b72cfe (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
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * 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
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.dialer.app.voicemail.error;

import android.content.ComponentName;
import android.content.Context;
import android.database.Cursor;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.provider.VoicemailContract.Status;
import android.telecom.PhoneAccountHandle;
import android.telephony.TelephonyManager;
import com.android.contacts.common.compat.TelephonyManagerCompat;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
import com.android.dialer.configprovider.ConfigProviderBindings;
import com.android.dialer.logging.DialerImpression;
import com.android.dialer.logging.Logger;

/**
 * This class will detect the corruption in the voicemail status and log it so we can track how many
 * users are affected.
 */
public class VoicemailStatusCorruptionHandler {

  /** Where the check is made so logging can be done. */
  public enum Source {
    Activity,
    Notification
  }

  private static final String CONFIG_VVM_STATUS_FIX_DISABLED = "vvm_status_fix_disabled";

  public static void maybeFixVoicemailStatus(Context context, Cursor statusCursor, Source source) {

    if (ConfigProviderBindings.get(context).getBoolean(CONFIG_VVM_STATUS_FIX_DISABLED, false)) {
      return;
    }

    if (VERSION.SDK_INT != VERSION_CODES.N_MR1) {
      // This issue is specific to N MR1, it is fixed in future SDK.
      return;
    }

    if (statusCursor.getCount() == 0) {
      return;
    }

    statusCursor.moveToFirst();
    VoicemailStatus status = new VoicemailStatus(context, statusCursor);
    PhoneAccountHandle phoneAccountHandle =
        new PhoneAccountHandle(
            ComponentName.unflattenFromString(status.phoneAccountComponentName),
            status.phoneAccountId);

    TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class);

    boolean visualVoicemailEnabled =
        TelephonyManagerCompat.isVisualVoicemailEnabled(telephonyManager, phoneAccountHandle);
    LogUtil.i(
        "VoicemailStatusCorruptionHandler.maybeFixVoicemailStatus",
        "Source="
            + source
            + ", CONFIGURATION_STAIE="
            + status.configurationState
            + ", visualVoicemailEnabled="
            + visualVoicemailEnabled);

    // If visual voicemail is enabled, the CONFIGURATION_STATE should be either OK, PIN_NOT_SET,
    // or other failure code. CONFIGURATION_STATE_NOT_CONFIGURED means that the client has been
    // shut down improperly (b/32371710). The client should be reset or the VVM tab will be
    // missing.
    if (Status.CONFIGURATION_STATE_NOT_CONFIGURED == status.configurationState
        && visualVoicemailEnabled) {
      LogUtil.e(
          "VoicemailStatusCorruptionHandler.maybeFixVoicemailStatus",
          "VVM3 voicemail status corrupted");

      switch (source) {
        case Activity:
          Logger.get(context)
              .logImpression(
                  DialerImpression.Type
                      .VOICEMAIL_CONFIGURATION_STATE_CORRUPTION_DETECTED_FROM_ACTIVITY);
          break;
        case Notification:
          Logger.get(context)
              .logImpression(
                  DialerImpression.Type
                      .VOICEMAIL_CONFIGURATION_STATE_CORRUPTION_DETECTED_FROM_NOTIFICATION);
          break;
        default:
          Assert.fail("this should never happen");
          break;
      }
      // At this point we could attempt to work around the issue by disabling and re-enabling
      // voicemail. Unfortunately this work around is buggy so we'll do nothing for now.
    }
  }
}