1 /*
2  * Copyright (C) 2024 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.phone.security;
18 
19 import static android.safetycenter.SafetyCenterManager.ACTION_REFRESH_SAFETY_SOURCES;
20 import static android.safetycenter.SafetyCenterManager.EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID;
21 
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.PackageManager;
26 
27 import com.android.internal.annotations.VisibleForTesting;
28 import com.android.internal.telephony.Phone;
29 import com.android.internal.telephony.flags.Flags;
30 import com.android.phone.PhoneGlobals;
31 import com.android.telephony.Rlog;
32 
33 public class SafetySourceReceiver extends BroadcastReceiver {
34     private static final String TAG = "TelephonySafetySourceReceiver";
35     @Override
onReceive(Context context, Intent intent)36     public void onReceive(Context context, Intent intent) {
37 
38         // If none of the features that depend on this receiver are enabled, there's no reason
39         // to progress.
40         if (!Flags.enableIdentifierDisclosureTransparencyUnsolEvents()
41                 || !Flags.enableModemCipherTransparencyUnsolEvents()) {
42             return;
43         }
44 
45         String action = intent.getAction();
46         if (!ACTION_REFRESH_SAFETY_SOURCES.equals(action)) {
47             return;
48         }
49 
50         String refreshBroadcastId =
51                 intent.getStringExtra(EXTRA_REFRESH_SAFETY_SOURCES_BROADCAST_ID);
52         if (refreshBroadcastId == null) {
53             return;
54         }
55 
56         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
57             if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
58                 refreshSafetySources(refreshBroadcastId);
59             }
60         } else {
61             refreshSafetySources(refreshBroadcastId);
62         }
63     }
64 
refreshSafetySources(String refreshBroadcastId)65     private void refreshSafetySources(String refreshBroadcastId) {
66         Phone phone = getDefaultPhone();
67         // It's possible that phones have not been created yet. Safety center may send a refresh
68         // broadcast very early on.
69         if (phone != null) {
70             phone.refreshSafetySources(refreshBroadcastId);
71         }
72 
73     }
74 
75     @VisibleForTesting
getDefaultPhone()76     public Phone getDefaultPhone() {
77         try {
78             return PhoneGlobals.getPhone();
79         } catch (IllegalStateException e) {
80             Rlog.i(TAG, "Unable to get phone. Skipping safety source refresh: " + e.getMessage());
81         }
82         return null;
83     }
84 }
85