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.telephony.ims.feature.ImsFeature;
26 
27 import com.android.internal.telephony.ims.ImsResolver;
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     /**
40      * Return the package name of the ImsService that is implementing RCS features for the device.
41      * @param context calling context
42      * @param phone object from telephony
43      * @param intent that should match a CarrierSmsFilter
44      * @return the name of the ImsService implementing RCS features on the device.
45      */
46     @Nullable
getImsRcsPackageForIntent( Context context, Phone phone, Intent intent)47     public static String getImsRcsPackageForIntent(
48             Context context, Phone phone, Intent intent) {
49 
50         String carrierImsPackage = getImsRcsPackage(phone);
51         if (carrierImsPackage == null) {
52             if (VDBG) Rlog.v(TAG, "No ImsService found implementing RCS.");
53             return null;
54         }
55 
56         PackageManager packageManager = context.getPackageManager();
57         List<ResolveInfo> receivers = packageManager.queryIntentServices(intent, 0);
58         for (ResolveInfo info : receivers) {
59             if (info.serviceInfo == null) {
60                 Rlog.e(TAG, "Can't get service information from " + info);
61                 continue;
62             }
63 
64             if (carrierImsPackage.equals(info.serviceInfo.packageName)) {
65                 return carrierImsPackage;
66             }
67         }
68         return null;
69     }
70 
71     /**
72      * @return the package name of the ImsService that is configured to implement RCS, or null if
73      * there is none configured/available.
74      */
75     @Nullable
getImsRcsPackage(Phone phone)76     private static String getImsRcsPackage(Phone phone) {
77         ImsResolver resolver = ImsResolver.getInstance();
78         if (resolver == null) {
79             Rlog.i(TAG, "getImsRcsPackage: Device does not support IMS - skipping");
80             return null;
81         }
82 
83         final long identity = Binder.clearCallingIdentity();
84         try {
85             return resolver.getConfiguredImsServicePackageName(phone.getPhoneId(),
86                     ImsFeature.FEATURE_RCS);
87         } finally {
88             Binder.restoreCallingIdentity(identity);
89         }
90     }
91 
CarrierSmsUtils()92     private CarrierSmsUtils() {}
93 }
94