1/*
2 * Copyright (C) 2017 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.voicemail.impl;
18
19import android.annotation.SuppressLint;
20import android.annotation.TargetApi;
21import android.content.Context;
22import android.os.Build.VERSION_CODES;
23import android.telecom.PhoneAccountHandle;
24import android.telecom.TelecomManager;
25import com.android.voicemail.impl.settings.VisualVoicemailSettingsUtil;
26
27/**
28 * When a new package is installed, check if it matches any of the vvm carrier apps of the currently
29 * enabled dialer VVM sources. The dialer VVM client will be disabled upon carrier VVM app
30 * installation, unless it was explicitly enabled by the user.
31 *
32 * <p>The ACTION_PACKAGE_ADDED broadcast can no longer be received. (see
33 * https://developer.android.com/preview/features/background.html#broadcasts) New apps are scanned
34 * when a VVM SMS is received instead, as it can be a result of the carrier VVM app trying to run
35 * activation.
36 */
37@SuppressLint("AndroidApiChecker") // forEach
38@TargetApi(VERSION_CODES.O)
39public final class VvmPackageInstallHandler {
40
41  /**
42   * Iterates through all phone account and disable VVM on a account if {@code packageName} is
43   * listed as a carrier VVM package.
44   */
45  public static void handlePackageInstalled(Context context, String packageName) {
46    // This get called every time an app is installed and will be noisy. Don't log until the app
47    // is identified as a carrier VVM app.
48    for (PhoneAccountHandle phoneAccount :
49        context.getSystemService(TelecomManager.class).getCallCapablePhoneAccounts()) {
50      OmtpVvmCarrierConfigHelper carrierConfigHelper =
51          new OmtpVvmCarrierConfigHelper(context, phoneAccount);
52      if (!carrierConfigHelper.isValid()) {
53        continue;
54      }
55      if (carrierConfigHelper.getCarrierVvmPackageNames() == null) {
56        continue;
57      }
58      if (!carrierConfigHelper.getCarrierVvmPackageNames().contains(packageName)) {
59        continue;
60      }
61
62      VvmLog.i("VvmPackageInstallHandler.handlePackageInstalled", "Carrier app installed");
63      if (VisualVoicemailSettingsUtil.isEnabledUserSet(context, phoneAccount)) {
64        // Skip the check if this voicemail source's setting is overridden by the user.
65        VvmLog.i(
66            "VvmPackageInstallHandler.handlePackageInstalled",
67            "VVM enabled by user, not disabling");
68        continue;
69      }
70
71      // Force deactivate the client. The user can re-enable it in the settings.
72      // There is no need to update the settings for deactivation. At this point, if the
73      // default value is used it should be false because a carrier package is present.
74      VvmLog.i(
75          "VvmPackageInstallHandler.handlePackageInstalled",
76          "Carrier VVM package installed, disabling system VVM client");
77      VisualVoicemailSettingsUtil.setEnabled(context, phoneAccount, false);
78    }
79  }
80}
81