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.metrics; 18 19 import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; 20 21 import static com.android.internal.telephony.TelephonyStatsLog.MODEM_RESTART; 22 23 import android.os.Build; 24 25 import com.android.internal.telephony.Phone; 26 import com.android.internal.telephony.PhoneFactory; 27 import com.android.internal.telephony.TelephonyStatsLog; 28 import com.android.telephony.Rlog; 29 30 /** Metrics for the modem restarts. */ 31 public class ModemRestartStats { 32 private static final String TAG = ModemRestartStats.class.getSimpleName(); 33 34 /* Maximum length of the baseband version. */ 35 private static final int MAX_BASEBAND_LEN = 100; 36 37 /* Maximum length of the modem restart reason. */ 38 private static final int MAX_REASON_LEN = 100; 39 ModemRestartStats()40 private ModemRestartStats() { } 41 42 /** Generate metrics when modem restart occurs. */ onModemRestart(String reason)43 public static void onModemRestart(String reason) { 44 reason = truncateString(reason, MAX_REASON_LEN); 45 String basebandVersion = truncateString(Build.getRadioVersion(), MAX_BASEBAND_LEN); 46 int carrierId = getCarrierId(); 47 48 Rlog.d(TAG, "Modem restart (carrier=" + carrierId + "): " + reason); 49 TelephonyStatsLog.write(MODEM_RESTART, basebandVersion, reason, carrierId); 50 } 51 truncateString(String string, int maxLen)52 private static String truncateString(String string, int maxLen) { 53 string = nullToEmpty(string); 54 if (string.length() > maxLen) { 55 string = string.substring(0, maxLen); 56 } 57 return string; 58 } 59 nullToEmpty(String string)60 private static String nullToEmpty(String string) { 61 return string != null ? string : ""; 62 } 63 64 /** Returns the carrier ID of the first SIM card for which carrier ID is available. */ getCarrierId()65 private static int getCarrierId() { 66 int carrierId = INVALID_SUBSCRIPTION_ID; 67 try { 68 for (Phone phone : PhoneFactory.getPhones()) { 69 carrierId = phone.getCarrierId(); 70 if (carrierId != INVALID_SUBSCRIPTION_ID) { 71 break; 72 } 73 } 74 } catch (IllegalStateException e) { 75 // Nothing to do here. 76 } 77 return carrierId; 78 } 79 } 80