1 /*
2  * Copyright (C) 2022 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.devicelockcontroller.policy;
18 
19 import static android.Manifest.permission.RECEIVE_EMERGENCY_BROADCAST;
20 
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.content.pm.ResolveInfo;
25 import android.provider.Telephony;
26 import android.text.TextUtils;
27 
28 import androidx.annotation.Nullable;
29 
30 import com.android.devicelockcontroller.util.LogUtil;
31 
32 import java.util.Locale;
33 
34 /** This class provides utility functions related to CellBroadcast. */
35 final class CellBroadcastUtils {
36     private static final String TAG = "CellBroadcastUtils";
37 
CellBroadcastUtils()38     private CellBroadcastUtils() {}
39 
40     /** Utility method to query the default CBR's package name. */
41     @Nullable
getDefaultCellBroadcastReceiverPackageName(Context context)42     static String getDefaultCellBroadcastReceiverPackageName(Context context) {
43         final PackageManager packageManager = context.getPackageManager();
44         final ResolveInfo resolveInfo =
45                 packageManager.resolveActivity(
46                         new Intent(Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION),
47                         PackageManager.MATCH_SYSTEM_ONLY);
48 
49         if (resolveInfo == null) {
50             LogUtil.e(TAG, "getDefaultCellBroadcastReceiverPackageName: no package found");
51 
52             return null;
53         }
54 
55         final String packageName = resolveInfo.activityInfo.applicationInfo.packageName;
56         LogUtil.d(TAG, String.format(Locale.US,
57                 "getDefaultCellBroadcastReceiverPackageName: found package: %s", packageName));
58 
59         if (TextUtils.isEmpty(packageName)
60                 || packageManager.checkPermission(RECEIVE_EMERGENCY_BROADCAST, packageName)
61                 == PackageManager.PERMISSION_DENIED) {
62             LogUtil.e(TAG, String.format(Locale.US, "getDefaultCellBroadcastReceiverPackageName: "
63                         + "returning null; permission check failed for : %s", packageName));
64 
65             return null;
66         }
67 
68         return packageName;
69     }
70 }
71