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.internal.telephony.metrics;
18 
19 import android.telephony.CellularIdentifierDisclosure;
20 
21 import com.android.internal.annotations.VisibleForTesting;
22 import com.android.internal.telephony.TelephonyStatsLog;
23 import com.android.telephony.Rlog;
24 
25 /**
26  * Facilitates writing stats relating to cellular transparency features. Delegates the actual
27  * writing of stats out to {@link TelephonyStatsLog}.
28  */
29 public class CellularSecurityTransparencyStats {
30 
31     private static final String LOG_TAG = "CellularSecurityTransparencyStats";
32     private static final String LOG_DESCRIPTOR_SIM_MCC = "SIM MCC";
33     private static final String LOG_DESCRIPTOR_SIM_MNC = "SIM MNC";
34     private static final String LOG_DESCRIPTOR_DISCLOSURE_MCC = "disclosure MCC";
35     private static final String LOG_DESCRIPTOR_DISCLOSURE_MNC = "disclosure MNC";
36     private static final int DEFAULT_PLMN_PART = -1;
37 
38     /**
39      * Log an identifier disclosure to be written out to {@link TelephonyStatsLog}
40      */
logIdentifierDisclosure(CellularIdentifierDisclosure disclosure, String simMcc, String simMnc, boolean notificationsEnabled)41     public void logIdentifierDisclosure(CellularIdentifierDisclosure disclosure, String simMcc,
42             String simMnc, boolean notificationsEnabled) {
43 
44         int mcc = parsePlmnPartOrDefault(simMcc, LOG_DESCRIPTOR_SIM_MCC);
45         int mnc = parsePlmnPartOrDefault(simMnc, LOG_DESCRIPTOR_SIM_MNC);
46 
47         int disclosureMcc = DEFAULT_PLMN_PART;
48         int disclosureMnc = DEFAULT_PLMN_PART;
49         String plmn = disclosure.getPlmn();
50         if (plmn != null) {
51             String[] plmnParts = plmn.split("-");
52             if (plmnParts.length == 2) {
53                 disclosureMcc = parsePlmnPartOrDefault(plmnParts[0], LOG_DESCRIPTOR_DISCLOSURE_MCC);
54                 disclosureMnc = parsePlmnPartOrDefault(plmnParts[1], LOG_DESCRIPTOR_DISCLOSURE_MNC);
55             }
56         }
57 
58         writeIdentifierDisclosure(mcc, mnc, disclosureMcc, disclosureMnc,
59                 disclosure.getCellularIdentifier(), disclosure.getNasProtocolMessage(),
60                 disclosure.isEmergency(), notificationsEnabled);
61 
62     }
63 
parsePlmnPartOrDefault(String input, String logDescriptor)64     private int parsePlmnPartOrDefault(String input, String logDescriptor) {
65         try {
66             return Integer.parseInt(input);
67         } catch (NumberFormatException e) {
68             Rlog.d(LOG_TAG, "Failed to parse " + logDescriptor + ": " + input);
69         }
70 
71         return DEFAULT_PLMN_PART;
72     }
73 
74     /**
75      * Write identifier disclosure data out to {@link TelephonyStatsLog}. This method is split
76      * out to enable testing, since {@link TelephonyStatsLog} is a final static class.
77      */
78     @VisibleForTesting
writeIdentifierDisclosure(int mcc, int mnc, int disclosureMcc, int disclosureMnc, int identifier, int protocolMessage, boolean isEmergency, boolean areNotificationsEnabled)79     public void writeIdentifierDisclosure(int mcc, int mnc, int disclosureMcc, int disclosureMnc,
80             int identifier, int protocolMessage, boolean isEmergency,
81             boolean areNotificationsEnabled) {
82         TelephonyStatsLog.write(TelephonyStatsLog.CELLULAR_IDENTIFIER_DISCLOSED, mcc, mnc,
83                 disclosureMcc, disclosureMnc, identifier, protocolMessage, isEmergency,
84                 areNotificationsEnabled);
85     }
86 }
87