1/**
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * <p>http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * <p>Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied. See the License for the specific language governing permissions and
12 * limitations under the License
13 */
14package com.android.voicemail.impl;
15
16import android.annotation.TargetApi;
17import android.content.Context;
18import android.content.Intent;
19import android.os.Build.VERSION_CODES;
20import android.provider.VoicemailContract.Status;
21import android.provider.VoicemailContract.Voicemails;
22import android.support.annotation.Nullable;
23import android.support.v4.os.BuildCompat;
24import android.telecom.PhoneAccountHandle;
25import android.telephony.TelephonyManager;
26import com.android.dialer.common.Assert;
27import com.android.dialer.common.ConfigProviderBindings;
28import com.android.dialer.common.LogUtil;
29import com.android.voicemail.VisualVoicemailTypeExtensions;
30import com.android.voicemail.VoicemailClient;
31import com.android.voicemail.impl.settings.VisualVoicemailSettingsUtil;
32import com.android.voicemail.impl.settings.VoicemailChangePinActivity;
33import com.android.voicemail.impl.settings.VoicemailSettingsFragment;
34import com.android.voicemail.impl.sync.VvmAccountManager;
35import java.util.List;
36import javax.inject.Inject;
37
38/**
39 * {@link VoicemailClient} to be used when the voicemail module is activated. May only be used above
40 * O.
41 */
42public class VoicemailClientImpl implements VoicemailClient {
43
44  /**
45   * List of legacy OMTP voicemail packages that should be ignored. It could never be the active VVM
46   * package anymore. For example, voicemails in OC will no longer be handled by telephony, but
47   * legacy voicemails might still exist in the database due to upgrading from NYC. Dialer will
48   * fetch these voicemails again so it should be ignored.
49   */
50  private static final String[] OMTP_VOICEMAIL_BLACKLIST = {"com.android.phone"};
51
52  // Flag name used for configuration
53  private static final String ALLOW_VOICEMAIL_ARCHIVE = "allow_voicemail_archive";
54
55  private static final String[] OMTP_VOICEMAIL_TYPE = {
56    TelephonyManager.VVM_TYPE_OMTP,
57    TelephonyManager.VVM_TYPE_CVVM,
58    VisualVoicemailTypeExtensions.VVM_TYPE_VVM3
59  };
60
61  @Inject
62  public VoicemailClientImpl() {
63    Assert.checkArgument(BuildCompat.isAtLeastO());
64  }
65
66  @Override
67  public boolean isVoicemailModuleEnabled() {
68    return true;
69  }
70
71  @Override
72  public boolean isVoicemailEnabled(Context context, PhoneAccountHandle phoneAccountHandle) {
73    return VisualVoicemailSettingsUtil.isEnabled(context, phoneAccountHandle);
74  }
75
76  @Override
77  public void setVoicemailEnabled(
78      Context context, PhoneAccountHandle phoneAccountHandle, boolean enabled) {
79    VisualVoicemailSettingsUtil.setEnabled(context, phoneAccountHandle, enabled);
80  }
81
82  @Nullable
83  @Override
84  public String getSettingsFragment() {
85    return VoicemailSettingsFragment.class.getName();
86  }
87
88  @Override
89  public boolean isVoicemailArchiveEnabled(Context context, PhoneAccountHandle phoneAccountHandle) {
90    return VisualVoicemailSettingsUtil.isArchiveEnabled(context, phoneAccountHandle);
91  }
92
93  @Override
94  public boolean isVoicemailArchiveAvailable(Context context) {
95    if (!BuildCompat.isAtLeastO()) {
96      LogUtil.i("VoicemailClientImpl.isVoicemailArchiveAllowed", "not running on O or later");
97      return false;
98    }
99
100    if (!ConfigProviderBindings.get(context).getBoolean(ALLOW_VOICEMAIL_ARCHIVE, false)) {
101      LogUtil.i(
102          "VoicemailClientImpl.isVoicemailArchiveAllowed",
103          "feature disabled by config: %s",
104          ALLOW_VOICEMAIL_ARCHIVE);
105      return false;
106    }
107
108    return true;
109  }
110
111  @Override
112  public void setVoicemailArchiveEnabled(
113      Context context, PhoneAccountHandle phoneAccountHandle, boolean value) {
114    VisualVoicemailSettingsUtil.setArchiveEnabled(context, phoneAccountHandle, value);
115  }
116
117  @Override
118  public Intent getSetPinIntent(Context context, PhoneAccountHandle phoneAccountHandle) {
119    Intent intent = new Intent(context, VoicemailChangePinActivity.class);
120    intent.putExtra(VoicemailChangePinActivity.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
121    return intent;
122  }
123
124  @Override
125  public boolean isActivated(Context context, PhoneAccountHandle phoneAccountHandle) {
126    return VvmAccountManager.isAccountActivated(context, phoneAccountHandle);
127  }
128
129  @TargetApi(VERSION_CODES.O)
130  @Override
131  public void appendOmtpVoicemailSelectionClause(
132      Context context, StringBuilder where, List<String> selectionArgs) {
133    String omtpSource =
134        context.getSystemService(TelephonyManager.class).getVisualVoicemailPackageName();
135    if (where.length() != 0) {
136      where.append(" AND ");
137    }
138    where.append("(");
139    {
140      where.append("(");
141      {
142        where.append(Voicemails.IS_OMTP_VOICEMAIL).append(" != 1");
143        where.append(")");
144      }
145      where.append(" OR ");
146      where.append("(");
147      {
148        where.append(Voicemails.SOURCE_PACKAGE).append(" = ?");
149        selectionArgs.add(omtpSource);
150        where.append(")");
151      }
152      where.append(")");
153    }
154
155    for (String blacklistedPackage : OMTP_VOICEMAIL_BLACKLIST) {
156      where.append("AND (").append(Voicemails.SOURCE_PACKAGE).append("!= ?)");
157      selectionArgs.add(blacklistedPackage);
158    }
159  }
160
161  @TargetApi(VERSION_CODES.O)
162  @Override
163  public void appendOmtpVoicemailStatusSelectionClause(
164      Context context, StringBuilder where, List<String> selectionArgs) {
165    String omtpSource =
166        context.getSystemService(TelephonyManager.class).getVisualVoicemailPackageName();
167    if (where.length() != 0) {
168      where.append(" AND ");
169    }
170    where.append("(");
171    {
172      where.append("(");
173      {
174        where.append(Status.SOURCE_PACKAGE).append(" = ? ");
175        selectionArgs.add(omtpSource);
176        where.append(")");
177      }
178      where.append(" OR NOT (");
179      {
180        for (int i = 0; i < OMTP_VOICEMAIL_TYPE.length; i++) {
181          if (i != 0) {
182            where.append(" OR ");
183          }
184          where.append(" (");
185          {
186            where.append(Status.SOURCE_TYPE).append(" IS ?");
187            selectionArgs.add(OMTP_VOICEMAIL_TYPE[i]);
188            where.append(")");
189          }
190        }
191        where.append(")");
192      }
193      for (String blacklistedPackage : OMTP_VOICEMAIL_BLACKLIST) {
194        where.append("AND (");
195        {
196          where.append(Voicemails.SOURCE_PACKAGE).append("!= ?");
197          selectionArgs.add(blacklistedPackage);
198          where.append(")");
199        }
200      }
201      where.append(")");
202    }
203  }
204}
205