1 /*
2  * Copyright (C) 2020 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.content.ComponentName;
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.provider.Telephony;
25 import android.text.TextUtils;
26 import android.util.Log;
27 
28 /**
29  * This class provides utility functions related to CellBroadcast.
30  */
31 public class CellBroadcastUtils {
32     private static final String TAG = "CellBroadcastUtils";
33     private static final boolean VDBG = false;
34 
35     /**
36      * Utility method to query the default CBR's package name.
37      */
getDefaultCellBroadcastReceiverPackageName(Context context)38     public static String getDefaultCellBroadcastReceiverPackageName(Context context) {
39         PackageManager packageManager = context.getPackageManager();
40         ResolveInfo resolveInfo = packageManager.resolveActivity(
41                 new Intent(Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION),
42                 PackageManager.MATCH_SYSTEM_ONLY);
43         String packageName;
44 
45         if (resolveInfo == null) {
46             Log.e(TAG, "getDefaultCellBroadcastReceiverPackageName: no package found");
47             return null;
48         }
49 
50         packageName = resolveInfo.activityInfo.applicationInfo.packageName;
51 
52         if (VDBG) {
53             Log.d(TAG, "getDefaultCellBroadcastReceiverPackageName: found package: " + packageName);
54         }
55 
56         if (TextUtils.isEmpty(packageName) || packageManager.checkPermission(
57                 android.Manifest.permission.READ_CELL_BROADCASTS, packageName)
58                 == PackageManager.PERMISSION_DENIED) {
59             Log.e(TAG, "getDefaultCellBroadcastReceiverPackageName: returning null; "
60                     + "permission check failed for : " + packageName);
61             return null;
62         }
63 
64         return packageName;
65     }
66 
67     /**
68      * Utility method to get cellbroadcast alert dialog component name
69      */
getDefaultCellBroadcastAlertDialogComponent(Context context)70     public static ComponentName getDefaultCellBroadcastAlertDialogComponent(Context context) {
71         String cellBroadcastReceiverPackageName =
72                 getDefaultCellBroadcastReceiverPackageName(context);
73         if (TextUtils.isEmpty(cellBroadcastReceiverPackageName)) {
74             return null;
75         }
76         return ComponentName.createRelative(cellBroadcastReceiverPackageName,
77                 "com.android.cellbroadcastreceiver.CellBroadcastAlertDialog");
78     }
79 }
80