summaryrefslogtreecommitdiffstats
path: root/java/com/android/voicemail/impl/VoicemailClientImpl.java
blob: 993594eb3acc555a646506740e5254996fa6a371 (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/**
 * 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.content.SharedPreferences;
import android.os.Build.VERSION_CODES;
import android.os.PersistableBundle;
import android.preference.PreferenceManager;
import android.provider.VoicemailContract.Status;
import android.provider.VoicemailContract.Voicemails;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.os.BuildCompat;
import android.telecom.PhoneAccountHandle;
import android.telephony.TelephonyManager;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
import com.android.dialer.configprovider.ConfigProviderBindings;
import com.android.voicemail.PinChanger;
import com.android.voicemail.VisualVoicemailTypeExtensions;
import com.android.voicemail.VoicemailClient;
import com.android.voicemail.VoicemailVersionConstants;
import com.android.voicemail.impl.configui.VoicemailSecretCodeActivity;
import com.android.voicemail.impl.settings.VisualVoicemailSettingsUtil;
import com.android.voicemail.impl.sync.VvmAccountManager;
import com.android.voicemail.impl.transcribe.TranscriptionBackfillService;
import com.android.voicemail.impl.transcribe.TranscriptionConfigProvider;
import java.util.List;
import javax.inject.Inject;

/**
 * {@link VoicemailClient} to be used when the voicemail module is activated. May only be used above
 * O.
 */
public class VoicemailClientImpl implements VoicemailClient {

  /**
   * List of legacy OMTP voicemail packages that should be ignored. It could never be the active VVM
   * package anymore. For example, voicemails in OC will no longer be handled by telephony, but
   * legacy voicemails might still exist in the database due to upgrading from NYC. Dialer will
   * fetch these voicemails again so it should be ignored.
   */
  private static final String[] OMTP_VOICEMAIL_BLACKLIST = {"com.android.phone"};

  // Flag name used for configuration
  private static final String ALLOW_VOICEMAIL_ARCHIVE = "allow_voicemail_archive";

  private static final String[] OMTP_VOICEMAIL_TYPE = {
    TelephonyManager.VVM_TYPE_OMTP,
    TelephonyManager.VVM_TYPE_CVVM,
    VisualVoicemailTypeExtensions.VVM_TYPE_VVM3
  };

  @Inject
  public VoicemailClientImpl() {
    Assert.checkArgument(BuildCompat.isAtLeastO());
  }

  @Override
  public boolean isVoicemailModuleEnabled() {
    return true;
  }

  @Override
  public boolean hasCarrierSupport(Context context, PhoneAccountHandle phoneAccountHandle) {
    OmtpVvmCarrierConfigHelper config = new OmtpVvmCarrierConfigHelper(context, phoneAccountHandle);
    return config.isValid() && !config.isCarrierAppInstalled();
  }

  @Override
  public boolean isVoicemailEnabled(Context context, PhoneAccountHandle phoneAccountHandle) {
    return VisualVoicemailSettingsUtil.isEnabled(context, phoneAccountHandle);
  }

  @Override
  public void setVoicemailEnabled(
      Context context, PhoneAccountHandle phoneAccountHandle, boolean enabled) {
    VisualVoicemailSettingsUtil.setEnabled(context, phoneAccountHandle, enabled);
  }

  @Override
  public boolean isVoicemailArchiveEnabled(Context context, PhoneAccountHandle phoneAccountHandle) {
    return VisualVoicemailSettingsUtil.isArchiveEnabled(context, phoneAccountHandle);
  }

  @Override
  public boolean isVoicemailArchiveAvailable(Context context) {
    if (!BuildCompat.isAtLeastO()) {
      LogUtil.i("VoicemailClientImpl.isVoicemailArchiveAllowed", "not running on O or later");
      return false;
    }

    if (!ConfigProviderBindings.get(context).getBoolean(ALLOW_VOICEMAIL_ARCHIVE, false)) {
      LogUtil.i(
          "VoicemailClientImpl.isVoicemailArchiveAllowed",
          "feature disabled by config: %s",
          ALLOW_VOICEMAIL_ARCHIVE);
      return false;
    }

    return true;
  }

  @Override
  public void setVoicemailArchiveEnabled(
      Context context, PhoneAccountHandle phoneAccountHandle, boolean value) {
    VisualVoicemailSettingsUtil.setArchiveEnabled(context, phoneAccountHandle, value);
  }

  @Override
  public boolean isVoicemailTranscriptionAvailable(Context context) {
    if (!BuildCompat.isAtLeastO()) {
      LogUtil.i(
          "VoicemailClientImpl.isVoicemailTranscriptionAvailable", "not running on O or later");
      return false;
    }

    TranscriptionConfigProvider provider = new TranscriptionConfigProvider(context);
    if (!provider.isVoicemailTranscriptionAvailable()) {
      LogUtil.i(
          "VoicemailClientImpl.isVoicemailTranscriptionAvailable", "feature disabled by config");
      return false;
    }

    return true;
  }

  @Override
  public boolean isVoicemailDonationAvailable(Context context) {
    if (!isVoicemailTranscriptionAvailable(context)) {
      LogUtil.i("VoicemailClientImpl.isVoicemailDonationAvailable", "transcription not available");
      return false;
    }

    TranscriptionConfigProvider provider = new TranscriptionConfigProvider(context);
    if (!provider.isVoicemailDonationAvailable()) {
      LogUtil.i("VoicemailClientImpl.isVoicemailDonationAvailable", "feature disabled by config");
      return false;
    }

    return true;
  }

  @Override
  public boolean isVoicemailDonationEnabled(Context context, PhoneAccountHandle account) {
    return isVoicemailTranscriptionAvailable(context)
        && VisualVoicemailSettingsUtil.isVoicemailDonationEnabled(context, account);
  }

  @Override
  public void setVoicemailDonationEnabled(
      Context context, PhoneAccountHandle phoneAccountHandle, boolean enabled) {
    VisualVoicemailSettingsUtil.setVoicemailDonationEnabled(context, phoneAccountHandle, enabled);
  }

  @Override
  public boolean isActivated(Context context, PhoneAccountHandle phoneAccountHandle) {
    return VvmAccountManager.isAccountActivated(context, phoneAccountHandle);
  }

  @Override
  public void showConfigUi(@NonNull Context context) {
    Intent intent = new Intent(context, VoicemailSecretCodeActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
  }

  @Override
  public PersistableBundle getConfig(Context context, PhoneAccountHandle phoneAccountHandle) {
    return new OmtpVvmCarrierConfigHelper(context, phoneAccountHandle).getConfig();
  }

  @Override
  @MainThread
  public void onBoot(@NonNull Context context) {
    OmtpService.onBoot(context);
    StatusCheckJobService.schedule(context);
  }

  @Override
  @MainThread
  public void onShutdown(@NonNull Context context) {
    OmtpService.onShutdown(context);
  }

  @Override
  public void addActivationStateListener(ActivationStateListener listener) {
    VvmAccountManager.addListener(listener);
  }

  @Override
  public void removeActivationStateListener(ActivationStateListener listener) {
    VvmAccountManager.removeListener(listener);
  }

  @Override
  public PinChanger createPinChanger(Context context, PhoneAccountHandle phoneAccountHandle) {
    return new PinChangerImpl(context, phoneAccountHandle);
  }

  @TargetApi(VERSION_CODES.O)
  @Override
  public void appendOmtpVoicemailSelectionClause(
      Context context, StringBuilder where, List<String> selectionArgs) {
    String omtpSource =
        context.getSystemService(TelephonyManager.class).getVisualVoicemailPackageName();
    if (where.length() != 0) {
      where.append(" AND ");
    }
    where.append("(");
    {
      where.append("(");
      {
        where.append(Voicemails.IS_OMTP_VOICEMAIL).append(" != 1");
        where.append(")");
      }
      where.append(" OR ");
      where.append("(");
      {
        where.append(Voicemails.SOURCE_PACKAGE).append(" = ?");
        selectionArgs.add(omtpSource);
        where.append(")");
      }
      where.append(")");
    }

    for (String blacklistedPackage : OMTP_VOICEMAIL_BLACKLIST) {
      where.append("AND (").append(Voicemails.SOURCE_PACKAGE).append("!= ?)");
      selectionArgs.add(blacklistedPackage);
    }
  }

  @TargetApi(VERSION_CODES.O)
  @Override
  public void appendOmtpVoicemailStatusSelectionClause(
      Context context, StringBuilder where, List<String> selectionArgs) {
    String omtpSource =
        context.getSystemService(TelephonyManager.class).getVisualVoicemailPackageName();
    if (where.length() != 0) {
      where.append(" AND ");
    }
    where.append("(");
    {
      where.append("(");
      {
        where.append(Status.SOURCE_PACKAGE).append(" = ? ");
        selectionArgs.add(omtpSource);
        where.append(")");
      }
      where.append(" OR NOT (");
      {
        for (int i = 0; i < OMTP_VOICEMAIL_TYPE.length; i++) {
          if (i != 0) {
            where.append(" OR ");
          }
          where.append(" (");
          {
            where.append(Status.SOURCE_TYPE).append(" IS ?");
            selectionArgs.add(OMTP_VOICEMAIL_TYPE[i]);
            where.append(")");
          }
        }
        where.append(")");
      }
      for (String blacklistedPackage : OMTP_VOICEMAIL_BLACKLIST) {
        where.append("AND (");
        {
          where.append(Voicemails.SOURCE_PACKAGE).append("!= ?");
          selectionArgs.add(blacklistedPackage);
          where.append(")");
        }
      }
      where.append(")");
    }
  }

  @Override
  public void onTosAccepted(Context context, PhoneAccountHandle account) {
    LogUtil.i("VoicemailClientImpl.onTosAccepted", "try backfilling voicemail transcriptions");
    TranscriptionBackfillService.scheduleTask(context, account);
  }

  @Override
  public boolean hasAcceptedTos(Context context, PhoneAccountHandle phoneAccountHandle) {
    SharedPreferences preferences =
        PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
    OmtpVvmCarrierConfigHelper helper = new OmtpVvmCarrierConfigHelper(context, phoneAccountHandle);
    boolean isVvm3 = VisualVoicemailTypeExtensions.VVM_TYPE_VVM3.equals(helper.getVvmType());
    if (isVvm3) {
      return preferences.getInt(VoicemailVersionConstants.PREF_VVM3_TOS_VERSION_ACCEPTED_KEY, 0)
          >= VoicemailVersionConstants.CURRENT_VVM3_TOS_VERSION;
    } else {
      return preferences.getInt(VoicemailVersionConstants.PREF_DIALER_TOS_VERSION_ACCEPTED_KEY, 0)
          >= VoicemailVersionConstants.CURRENT_DIALER_TOS_VERSION;
    }
  }

  @Override
  @Nullable
  public String getCarrierConfigString(Context context, PhoneAccountHandle account, String key) {
    OmtpVvmCarrierConfigHelper helper = new OmtpVvmCarrierConfigHelper(context, account);
    return helper.isValid() ? helper.getString(key) : null;
  }
}