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