summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/app/voicemail/error/VoicemailStatus.java
blob: c429d6dcc8788fa25aabed22054dc3479daba87f (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * 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.net.Uri;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.provider.Settings;
import android.provider.Settings.Global;
import android.provider.VoicemailContract.Status;
import android.support.annotation.Nullable;
import android.telecom.PhoneAccountHandle;
import android.telephony.TelephonyManager;
import com.android.dialer.database.VoicemailStatusQuery;

/** Structured data from {@link android.provider.VoicemailContract.Status} */
public class VoicemailStatus {

  public final String sourcePackage;
  public final String type;

  public final String phoneAccountComponentName;
  public final String phoneAccountId;

  @Nullable public final Uri settingsUri;
  @Nullable public final Uri voicemailAccessUri;

  public final int configurationState;
  public final int dataChannelState;
  public final int notificationChannelState;

  public final int quotaOccupied;
  public final int quotaTotal;

  // System status

  public final boolean isAirplaneMode;

  /** Wraps the row currently pointed by <code>statusCursor</code> */
  public VoicemailStatus(Context context, Cursor statusCursor) {
    sourcePackage = getString(statusCursor, VoicemailStatusQuery.SOURCE_PACKAGE_INDEX, "");

    settingsUri = getUri(statusCursor, VoicemailStatusQuery.SETTINGS_URI_INDEX);
    voicemailAccessUri = getUri(statusCursor, VoicemailStatusQuery.VOICEMAIL_ACCESS_URI_INDEX);

    configurationState =
        getInt(
            statusCursor,
            VoicemailStatusQuery.CONFIGURATION_STATE_INDEX,
            Status.CONFIGURATION_STATE_NOT_CONFIGURED);
    dataChannelState =
        getInt(
            statusCursor,
            VoicemailStatusQuery.DATA_CHANNEL_STATE_INDEX,
            Status.DATA_CHANNEL_STATE_NO_CONNECTION);
    notificationChannelState =
        getInt(
            statusCursor,
            VoicemailStatusQuery.NOTIFICATION_CHANNEL_STATE_INDEX,
            Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION);

    isAirplaneMode =
        Settings.System.getInt(context.getContentResolver(), Global.AIRPLANE_MODE_ON, 0) != 0;

    if (VERSION.SDK_INT >= VERSION_CODES.N) {
      quotaOccupied =
          getInt(statusCursor, VoicemailStatusQuery.QUOTA_OCCUPIED_INDEX, Status.QUOTA_UNAVAILABLE);
      quotaTotal =
          getInt(statusCursor, VoicemailStatusQuery.QUOTA_TOTAL_INDEX, Status.QUOTA_UNAVAILABLE);
    } else {
      quotaOccupied = Status.QUOTA_UNAVAILABLE;
      quotaTotal = Status.QUOTA_UNAVAILABLE;
    }

    if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
      type =
          getString(
              statusCursor, VoicemailStatusQuery.SOURCE_TYPE_INDEX, TelephonyManager.VVM_TYPE_OMTP);
      phoneAccountComponentName =
          getString(statusCursor, VoicemailStatusQuery.PHONE_ACCOUNT_COMPONENT_NAME, "");
      phoneAccountId = getString(statusCursor, VoicemailStatusQuery.PHONE_ACCOUNT_ID, "");
    } else {
      type = TelephonyManager.VVM_TYPE_OMTP;
      phoneAccountComponentName = "";
      phoneAccountId = "";
    }
  }

  private VoicemailStatus(Builder builder) {
    sourcePackage = builder.sourcePackage;
    phoneAccountComponentName = builder.phoneAccountComponentName;
    phoneAccountId = builder.phoneAccountId;
    type = builder.type;
    settingsUri = builder.settingsUri;
    voicemailAccessUri = builder.voicemailAccessUri;
    configurationState = builder.configurationState;
    dataChannelState = builder.dataChannelState;
    notificationChannelState = builder.notificationChannelState;
    quotaOccupied = builder.quotaOccupied;
    quotaTotal = builder.quotaTotal;
    isAirplaneMode = builder.isAirplaneMode;
  }

  static class Builder {

    private String sourcePackage = "";
    private String type = TelephonyManager.VVM_TYPE_OMTP;
    private String phoneAccountComponentName = "";
    private String phoneAccountId = "";

    @Nullable private Uri settingsUri;
    @Nullable private Uri voicemailAccessUri;

    private int configurationState = Status.CONFIGURATION_STATE_NOT_CONFIGURED;
    private int dataChannelState = Status.DATA_CHANNEL_STATE_NO_CONNECTION;
    private int notificationChannelState = Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION;

    private int quotaOccupied = Status.QUOTA_UNAVAILABLE;
    private int quotaTotal = Status.QUOTA_UNAVAILABLE;

    private boolean isAirplaneMode;

    public VoicemailStatus build() {
      return new VoicemailStatus(this);
    }

    public Builder setSourcePackage(String sourcePackage) {
      this.sourcePackage = sourcePackage;
      return this;
    }

    public Builder setType(String type) {
      this.type = type;
      return this;
    }

    public Builder setPhoneAccountComponentName(String name) {
      this.phoneAccountComponentName = name;
      return this;
    }

    public Builder setPhoneAccountId(String id) {
      this.phoneAccountId = id;
      return this;
    }

    public Builder setSettingsUri(Uri settingsUri) {
      this.settingsUri = settingsUri;
      return this;
    }

    public Builder setVoicemailAccessUri(Uri voicemailAccessUri) {
      this.voicemailAccessUri = voicemailAccessUri;
      return this;
    }

    public Builder setConfigurationState(int configurationState) {
      this.configurationState = configurationState;
      return this;
    }

    public Builder setDataChannelState(int dataChannelState) {
      this.dataChannelState = dataChannelState;
      return this;
    }

    public Builder setNotificationChannelState(int notificationChannelState) {
      this.notificationChannelState = notificationChannelState;
      return this;
    }

    public Builder setQuotaOccupied(int quotaOccupied) {
      this.quotaOccupied = quotaOccupied;
      return this;
    }

    public Builder setQuotaTotal(int quotaTotal) {
      this.quotaTotal = quotaTotal;
      return this;
    }

    public Builder setAirplaneMode(boolean isAirplaneMode) {
      this.isAirplaneMode = isAirplaneMode;
      return this;
    }
  }

  public boolean isActive() {
    switch (configurationState) {
      case Status.CONFIGURATION_STATE_NOT_CONFIGURED:
      case Status.CONFIGURATION_STATE_DISABLED:
        return false;
      default:
        return true;
    }
  }

  @Override
  public String toString() {
    return "VoicemailStatus["
        + "sourcePackage: "
        + sourcePackage
        + ", type:"
        + type
        + ", settingsUri: "
        + settingsUri
        + ", voicemailAccessUri: "
        + voicemailAccessUri
        + ", configurationState: "
        + configurationState
        + ", dataChannelState: "
        + dataChannelState
        + ", notificationChannelState: "
        + notificationChannelState
        + ", quotaOccupied: "
        + quotaOccupied
        + ", quotaTotal: "
        + quotaTotal
        + ", isAirplaneMode: "
        + isAirplaneMode
        + "]";
  }

  @Nullable
  private static Uri getUri(Cursor cursor, int index) {
    if (cursor.getString(index) != null) {
      return Uri.parse(cursor.getString(index));
    }
    return null;
  }

  private static int getInt(Cursor cursor, int index, int defaultValue) {
    if (cursor.isNull(index)) {
      return defaultValue;
    }
    return cursor.getInt(index);
  }

  private static String getString(Cursor cursor, int index, String defaultValue) {
    if (cursor.isNull(index)) {
      return defaultValue;
    }
    return cursor.getString(index);
  }

  public PhoneAccountHandle getPhoneAccountHandle() {
    return new PhoneAccountHandle(
        ComponentName.unflattenFromString(phoneAccountComponentName), phoneAccountId);
  }
}