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.internal.telephony;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.os.Binder;
25 import android.os.PersistableBundle;
26 import android.telephony.CarrierConfigManager;
27 
28 import com.android.telephony.Rlog;
29 
30 import java.util.List;
31 
32 /**
33  * This is a basic utility class for common Carrier SMS Functions
34  */
35 public class CarrierSmsUtils {
36     protected static final boolean VDBG = false;
37     protected static final String TAG = CarrierSmsUtils.class.getSimpleName();
38 
39     private static final String CARRIER_IMS_PACKAGE_KEY =
40             CarrierConfigManager.KEY_CONFIG_IMS_PACKAGE_OVERRIDE_STRING;
41 
42     /** Return a Carrier-overridden IMS package, if it exists and is a CarrierSmsFilter
43      *
44      * @param context calling context
45      * @param phone object from telephony
46      * @param intent that should match a CarrierSmsFilter
47      * @return the name of the IMS CarrierService package
48      */
49     @Nullable
getCarrierImsPackageForIntent( Context context, Phone phone, Intent intent)50     public static String getCarrierImsPackageForIntent(
51             Context context, Phone phone, Intent intent) {
52 
53         String carrierImsPackage = getCarrierImsPackage(context, phone);
54         if (carrierImsPackage == null) {
55             if (VDBG) Rlog.v(TAG, "No CarrierImsPackage override found");
56             return null;
57         }
58 
59         PackageManager packageManager = context.getPackageManager();
60         List<ResolveInfo> receivers = packageManager.queryIntentServices(intent, 0);
61         for (ResolveInfo info : receivers) {
62             if (info.serviceInfo == null) {
63                 Rlog.e(TAG, "Can't get service information from " + info);
64                 continue;
65             }
66 
67             if (carrierImsPackage.equals(info.serviceInfo.packageName)) {
68                 return carrierImsPackage;
69             }
70         }
71         return null;
72     }
73 
74     @Nullable
getCarrierImsPackage(Context context, Phone phone)75     private static String getCarrierImsPackage(Context context, Phone phone) {
76         CarrierConfigManager cm = (CarrierConfigManager) context.getSystemService(
77                 Context.CARRIER_CONFIG_SERVICE);
78         if (cm == null) {
79             Rlog.e(TAG, "Failed to retrieve CarrierConfigManager");
80             return null;
81         }
82 
83         final long identity = Binder.clearCallingIdentity();
84         try {
85             PersistableBundle config = cm.getConfigForSubId(phone.getSubId());
86             if (config == null) {
87                 if (VDBG) Rlog.v(TAG, "No CarrierConfig for subId:" + phone.getSubId());
88                 return null;
89             }
90             return config.getString(CARRIER_IMS_PACKAGE_KEY, null);
91         } finally {
92             Binder.restoreCallingIdentity(identity);
93         }
94     }
95 
CarrierSmsUtils()96     private CarrierSmsUtils() {}
97 }
98