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 
17 package com.android.phone.vvm;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.os.PersistableBundle;
24 import android.telecom.PhoneAccountHandle;
25 import android.telecom.TelecomManager;
26 import android.telephony.CarrierConfigManager;
27 import android.telephony.TelephonyManager;
28 import android.telephony.VisualVoicemailService;
29 import android.text.TextUtils;
30 import android.util.ArraySet;
31 
32 import java.util.Collections;
33 import java.util.Set;
34 
35 /**
36  * Receives {@link Intent#ACTION_PACKAGE_ADDED} for the system dialer to inform it a carrier visual
37  * voicemail app has been installed. ACTION_PACKAGE_ADDED requires the receiver process to be
38  * running so the system dialer cannot receive it itself.
39  *
40  * Carrier VVM apps are usually regular apps, not a
41  * {@link VisualVoicemailService} nor {@link android.service.carrier.CarrierMessagingService} so it
42  * will not take precedence over the system dialer. The system dialer should disable VVM it self
43  * to let the carrier app take over since the installation is an explicit user interaction. Carrier
44  * customer support might also ask the user to switch to their app if they believe there's any
45  * issue in the system dialer so this transition should not require more user interaction.
46  *
47  * @see CarrierConfigManager#KEY_CARRIER_VVM_PACKAGE_NAME_STRING_ARRAY
48  */
49 public class CarrierVvmPackageInstalledReceiver extends BroadcastReceiver {
50 
51     private static final String TAG = "VvmPkgInstalledRcvr";
52 
53     /**
54      * Hidden broadcast to the system dialer
55      */
56     private static final String ACTION_CARRIER_VVM_PACKAGE_INSTALLED =
57             "com.android.internal.telephony.CARRIER_VVM_PACKAGE_INSTALLED";
58 
register(Context context)59     public void register(Context context) {
60         IntentFilter intentFilter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
61         intentFilter.addDataScheme("package");
62         context.registerReceiver(this, intentFilter);
63     }
64 
65     @Override
onReceive(Context context, Intent intent)66     public void onReceive(Context context, Intent intent) {
67         if (intent.getData() == null) {
68             return;
69         }
70         String packageName = intent.getData().getSchemeSpecificPart();
71         if (packageName == null) {
72             return;
73         }
74 
75         TelecomManager telecomManager = context.getSystemService(TelecomManager.class);
76         String systemDialer = telecomManager.getSystemDialerPackage();
77         TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class);
78         for (PhoneAccountHandle phoneAccountHandle : telecomManager.getCallCapablePhoneAccounts()) {
79             TelephonyManager pinnedTelephonyManager = telephonyManager
80                     .createForPhoneAccountHandle(phoneAccountHandle);
81 
82             if (pinnedTelephonyManager == null) {
83                 VvmLog.e(TAG, "cannot create TelephonyManager from " + phoneAccountHandle);
84                 continue;
85             }
86 
87             if (!getCarrierVvmPackages(telephonyManager).contains(packageName)) {
88                 continue;
89             }
90 
91             VvmLog.i(TAG, "Carrier VVM app " + packageName + " installed");
92 
93             String vvmPackage = pinnedTelephonyManager.getVisualVoicemailPackageName();
94             if (!TextUtils.equals(vvmPackage, systemDialer)) {
95                 // Non system dialer do not need to prioritize carrier vvm app.
96                 VvmLog.i(TAG, "non system dialer " + vvmPackage + " ignored");
97                 continue;
98             }
99 
100             VvmLog.i(TAG, "sending broadcast to " + vvmPackage);
101             Intent broadcast = new Intent(ACTION_CARRIER_VVM_PACKAGE_INSTALLED);
102             broadcast.putExtra(Intent.EXTRA_PACKAGE_NAME, packageName);
103             broadcast.setPackage(vvmPackage);
104             context.sendBroadcast(broadcast);
105         }
106     }
107 
getCarrierVvmPackages(TelephonyManager pinnedTelephonyManager)108     private static Set<String> getCarrierVvmPackages(TelephonyManager pinnedTelephonyManager) {
109         Set<String> carrierPackages = new ArraySet<>();
110 
111         PersistableBundle config = pinnedTelephonyManager.getCarrierConfig();
112         String singlePackage = config
113                 .getString(CarrierConfigManager.KEY_CARRIER_VVM_PACKAGE_NAME_STRING);
114         if (!TextUtils.isEmpty(singlePackage)) {
115             carrierPackages.add(singlePackage);
116         }
117         String[] arrayPackages = config
118                 .getStringArray(CarrierConfigManager.KEY_CARRIER_VVM_PACKAGE_NAME_STRING_ARRAY);
119         if (arrayPackages != null) {
120             Collections.addAll(carrierPackages, arrayPackages);
121         }
122 
123         return carrierPackages;
124     }
125 }
126