1/*
2 * Copyright (C) 2015 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 */
16package com.android.voicemail.impl;
17
18import android.content.BroadcastReceiver;
19import android.content.Context;
20import android.content.Intent;
21import android.telecom.PhoneAccountHandle;
22import android.telecom.TelecomManager;
23import com.android.voicemail.VoicemailComponent;
24import com.android.voicemail.impl.settings.VisualVoicemailSettingsUtil;
25
26/**
27 * When a new package is installed, check if it matches any of the vvm carrier apps of the currently
28 * enabled dialer VVM sources. The dialer VVM client will be disabled upon carrier VVM app
29 * installation, unless it was explicitly enabled by the user.
30 */
31public class VvmPackageInstallReceiver extends BroadcastReceiver {
32
33  private static final String TAG = "VvmPkgInstallReceiver";
34
35  @Override
36  public void onReceive(Context context, Intent intent) {
37    if (!VoicemailComponent.get(context).getVoicemailClient().isVoicemailModuleEnabled()) {
38      return;
39    }
40
41    if (intent.getData() == null) {
42      return;
43    }
44
45    String packageName = intent.getData().getSchemeSpecificPart();
46    if (packageName == null) {
47      return;
48    }
49
50    // This get called every time an app is installed and will be noisy. Don't log until the app
51    // is identified as a carrier VVM app.
52    for (PhoneAccountHandle phoneAccount :
53        context.getSystemService(TelecomManager.class).getCallCapablePhoneAccounts()) {
54      OmtpVvmCarrierConfigHelper carrierConfigHelper =
55          new OmtpVvmCarrierConfigHelper(context, phoneAccount);
56      if (!carrierConfigHelper.isValid()) {
57        continue;
58      }
59      if (carrierConfigHelper.getCarrierVvmPackageNames() == null) {
60        continue;
61      }
62      if (!carrierConfigHelper.getCarrierVvmPackageNames().contains(packageName)) {
63        continue;
64      }
65
66      VvmLog.i(TAG, "Carrier app installed");
67      if (VisualVoicemailSettingsUtil.isEnabledUserSet(context, phoneAccount)) {
68        // Skip the check if this voicemail source's setting is overridden by the user.
69        VvmLog.i(TAG, "VVM enabled by user, not disabling");
70        continue;
71      }
72
73      // Force deactivate the client. The user can re-enable it in the settings.
74      // There is no need to update the settings for deactivation. At this point, if the
75      // default value is used it should be false because a carrier package is present.
76      VvmLog.i(TAG, "Carrier VVM package installed, disabling system VVM client");
77      VisualVoicemailSettingsUtil.setEnabled(context, phoneAccount, false);
78    }
79  }
80}
81