ConfigUpdater metrics

Bug: 330256940
Test: atest MetricsCollectorTest, atest PersistantAtomsStorageTest,
atest SatelliteStatsTest
Manual Test: b/339865721#comment2

Change-Id: Ibf9f15dccbf3e4111ef28aba27a14b2c30ac8a48
Merged-In: Ibf9f15dccbf3e4111ef28aba27a14b2c30ac8a48
diff --git a/src/com/android/phone/satellite/accesscontrol/SatelliteAccessController.java b/src/com/android/phone/satellite/accesscontrol/SatelliteAccessController.java
index 886451d..5fa73c9 100644
--- a/src/com/android/phone/satellite/accesscontrol/SatelliteAccessController.java
+++ b/src/com/android/phone/satellite/accesscontrol/SatelliteAccessController.java
@@ -63,7 +63,9 @@
 import com.android.internal.telephony.TelephonyCountryDetector;
 import com.android.internal.telephony.flags.FeatureFlags;
 import com.android.internal.telephony.satellite.SatelliteConfig;
+import com.android.internal.telephony.satellite.SatelliteConstants;
 import com.android.internal.telephony.satellite.SatelliteController;
+import com.android.internal.telephony.satellite.metrics.ConfigUpdaterMetricsStats;
 import com.android.internal.telephony.util.TelephonyUtils;
 import com.android.phone.PhoneGlobals;
 
@@ -194,6 +196,7 @@
     private static final String CONFIG_UPDATER_SATELLITE_IS_ALLOW_ACCESS_CONTROL_KEY =
             "config_updater_satellite_is_allow_access_control";
     private SharedPreferences mSharedPreferences;
+    private final ConfigUpdaterMetricsStats mConfigUpdaterMetricsStats;
 
     /**
      * Map key: binder of the callback, value: callback to receive the satellite communication
@@ -245,12 +248,16 @@
                 handleIsSatelliteSupportedResult(resultCode, resultData);
             }
         };
+
+        mConfigUpdaterMetricsStats = ConfigUpdaterMetricsStats.getOrCreateInstance();
+
         mInternalSatelliteProvisionedResultReceiver = new ResultReceiver(this) {
             @Override
             protected void onReceiveResult(int resultCode, Bundle resultData) {
                 handleIsSatelliteProvisionedResult(resultCode, resultData);
             }
         };
+
         // Init the SatelliteOnDeviceAccessController so that the S2 level can be cached
         initSatelliteOnDeviceAccessController();
     }
@@ -548,41 +555,55 @@
         SatelliteConfig satelliteConfig = mSatelliteController.getSatelliteConfig();
         if (satelliteConfig == null) {
             loge("satelliteConfig is null");
+            mConfigUpdaterMetricsStats.reportOemAndCarrierConfigError(
+                    SatelliteConstants.CONFIG_UPDATE_RESULT_NO_SATELLITE_DATA);
             return;
         }
 
         List<String> satelliteCountryCodes = satelliteConfig.getDeviceSatelliteCountryCodes();
         if (!isValidCountryCodes(satelliteCountryCodes)) {
             logd("country codes is invalid");
+            mConfigUpdaterMetricsStats.reportOemConfigError(
+                    SatelliteConstants.CONFIG_UPDATE_RESULT_DEVICE_DATA_INVALID_COUNTRY_CODE);
             return;
         }
 
         Boolean isSatelliteDataForAllowedRegion = satelliteConfig.isSatelliteDataForAllowedRegion();
         if (isSatelliteDataForAllowedRegion == null) {
             loge("Satellite allowed is not configured with country codes");
+            mConfigUpdaterMetricsStats.reportOemConfigError(
+                    SatelliteConstants.CONFIG_UPDATE_RESULT_DEVICE_DATA_INVALID_S2_CELL_FILE);
             return;
         }
 
         File configUpdaterS2CellFile = satelliteConfig.getSatelliteS2CellFile(context);
         if (configUpdaterS2CellFile == null || !configUpdaterS2CellFile.exists()) {
             logd("No S2 cell file configured or the file does not exist");
+            mConfigUpdaterMetricsStats.reportOemConfigError(
+                    SatelliteConstants.CONFIG_UPDATE_RESULT_DEVICE_DATA_INVALID_S2_CELL_FILE);
             return;
         }
 
         if (!isS2CellFileValid(configUpdaterS2CellFile)) {
             loge("The configured S2 cell file is not valid");
+            mConfigUpdaterMetricsStats.reportOemConfigError(
+                    SatelliteConstants.CONFIG_UPDATE_RESULT_DEVICE_DATA_INVALID_S2_CELL_FILE);
             return;
         }
 
         File localS2CellFile = copySatS2FileToLocalDirectory(configUpdaterS2CellFile);
         if (localS2CellFile == null || !localS2CellFile.exists()) {
             loge("Fail to copy S2 cell file to local directory");
+            mConfigUpdaterMetricsStats.reportOemConfigError(
+                    SatelliteConstants.CONFIG_UPDATE_RESULT_IO_ERROR);
             return;
         }
 
         if (!updateSharedPreferencesCountryCodes(context, satelliteCountryCodes)) {
             loge("Fail to copy country coeds into shared preferences");
             localS2CellFile.delete();
+            mConfigUpdaterMetricsStats.reportOemConfigError(
+                    SatelliteConstants.CONFIG_UPDATE_RESULT_IO_ERROR);
             return;
         }
 
@@ -590,6 +611,8 @@
                 context, isSatelliteDataForAllowedRegion.booleanValue())) {
             loge("Fail to copy allow access control into shared preferences");
             localS2CellFile.delete();
+            mConfigUpdaterMetricsStats.reportOemConfigError(
+                    SatelliteConstants.CONFIG_UPDATE_RESULT_IO_ERROR);
             return;
         }
 
@@ -609,6 +632,8 @@
             logd("clear mCachedAccessRestrictionMap");
             mCachedAccessRestrictionMap.clear();
         }
+
+        mConfigUpdaterMetricsStats.reportConfigUpdateSuccess();
     }
 
     private void loadOverlayConfigs(@NonNull Context context) {
diff --git a/src/com/android/phone/satellite/entitlement/SatelliteEntitlementController.java b/src/com/android/phone/satellite/entitlement/SatelliteEntitlementController.java
index 8c2693b..d686974 100644
--- a/src/com/android/phone/satellite/entitlement/SatelliteEntitlementController.java
+++ b/src/com/android/phone/satellite/entitlement/SatelliteEntitlementController.java
@@ -16,8 +16,6 @@
 
 package com.android.phone.satellite.entitlement;
 
-import static com.android.phone.satellite.entitlement.SatelliteEntitlementResult.SATELLITE_ENTITLEMENT_STATUS_ENABLED;
-
 import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME;
 import static java.time.temporal.ChronoUnit.SECONDS;
 
@@ -44,7 +42,9 @@
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.telephony.ExponentialBackoff;
 import com.android.internal.telephony.flags.FeatureFlags;
+import com.android.internal.telephony.satellite.SatelliteConstants;
 import com.android.internal.telephony.satellite.SatelliteController;
+import com.android.internal.telephony.satellite.metrics.EntitlementMetricsStats;
 import com.android.internal.telephony.subscription.SubscriptionManagerService;
 import com.android.libraries.entitlement.ServiceEntitlementException;
 
@@ -109,6 +109,7 @@
     /** Map key : slotId, value : The last used subId. */
     @GuardedBy("mLock")
     private Map<Integer, Integer> mSubIdPerSlot = new HashMap<>();
+    @NonNull private final EntitlementMetricsStats mEntitlementMetricsStats;
 
     /**
      * Create the SatelliteEntitlementController singleton instance.
@@ -159,6 +160,7 @@
         IntentFilter intentFilter = new IntentFilter();
         intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
         context.registerReceiver(mReceiver, intentFilter);
+        mEntitlementMetricsStats = EntitlementMetricsStats.getOrCreateInstance();
     }
 
     @Override
@@ -255,11 +257,15 @@
             try {
                 synchronized (mLock) {
                     mIsEntitlementInProgressPerSub.put(subId, true);
-                    mSatelliteEntitlementResultPerSub.put(subId, getSatelliteEntitlementApi(
-                            subId).checkEntitlementStatus());
+                    SatelliteEntitlementResult entitlementResult =  getSatelliteEntitlementApi(
+                            subId).checkEntitlementStatus();
+                    mSatelliteEntitlementResultPerSub.put(subId, entitlementResult);
+                    mEntitlementMetricsStats.reportSuccess(subId,
+                            getEntitlementStatus(entitlementResult), false);
                 }
             } catch (ServiceEntitlementException e) {
                 loge(e.toString());
+                mEntitlementMetricsStats.reportError(subId, e.getErrorCode(), false);
                 if (!isInternetConnected()) {
                     logd("StartQuery: disconnected. " + e);
                     synchronized (mLock) {
@@ -319,11 +325,15 @@
                 int currentRetryCount = getRetryCount(subId);
                 mRetryCountPerSub.put(subId, currentRetryCount + 1);
                 logd("[" + subId + "] retry cnt:" + getRetryCount(subId));
-                mSatelliteEntitlementResultPerSub.put(subId, getSatelliteEntitlementApi(
-                        subId).checkEntitlementStatus());
+                SatelliteEntitlementResult entitlementResult =  getSatelliteEntitlementApi(
+                        subId).checkEntitlementStatus();
+                mSatelliteEntitlementResultPerSub.put(subId, entitlementResult);
+                mEntitlementMetricsStats.reportSuccess(subId,
+                        getEntitlementStatus(entitlementResult), true);
             }
         } catch (ServiceEntitlementException e) {
             loge(e.toString());
+            mEntitlementMetricsStats.reportError(subId, e.getErrorCode(), true);
             if (!isRetryAvailable(subId)) {
                 logd("retryQuery: unavailable.");
                 queryCompleted(subId);
@@ -464,8 +474,8 @@
         sendMessageDelayed(message, TimeUnit.DAYS.toMillis(
                 getSatelliteEntitlementStatusRefreshDays(subId)));
         logd("queryCompleted: updateSatelliteEntitlementStatus");
-        updateSatelliteEntitlementStatus(subId,
-                entitlementResult.getEntitlementStatus() == SATELLITE_ENTITLEMENT_STATUS_ENABLED,
+        updateSatelliteEntitlementStatus(subId, entitlementResult.getEntitlementStatus() ==
+                        SatelliteEntitlementResult.SATELLITE_ENTITLEMENT_STATUS_ENABLED,
                 entitlementResult.getAllowedPLMNList(), entitlementResult.getBarredPLMNList());
     }
 
@@ -511,12 +521,13 @@
     private void resetSatelliteEntitlementRestrictedReason(int subId) {
         SatelliteEntitlementResult previousResult;
         SatelliteEntitlementResult enabledResult = new SatelliteEntitlementResult(
-                SATELLITE_ENTITLEMENT_STATUS_ENABLED, new ArrayList<>(), new ArrayList<>());
+                SatelliteEntitlementResult.SATELLITE_ENTITLEMENT_STATUS_ENABLED,
+                new ArrayList<>(), new ArrayList<>());
         synchronized (mLock) {
             previousResult = mSatelliteEntitlementResultPerSub.get(subId);
         }
         if (previousResult != null && previousResult.getEntitlementStatus()
-                != SATELLITE_ENTITLEMENT_STATUS_ENABLED) {
+                != SatelliteEntitlementResult.SATELLITE_ENTITLEMENT_STATUS_ENABLED) {
             logd("set enabled status for removing satellite entitlement restricted reason");
             synchronized (mLock) {
                 mSatelliteEntitlementResultPerSub.put(subId, enabledResult);
@@ -633,6 +644,22 @@
                 plmnAllowedList, plmnBarredList, null);
     }
 
+    private @SatelliteConstants.SatelliteEntitlementStatus int getEntitlementStatus(
+            SatelliteEntitlementResult entitlementResult) {
+        switch (entitlementResult.getEntitlementStatus()) {
+            case SatelliteEntitlementResult.SATELLITE_ENTITLEMENT_STATUS_DISABLED:
+                return SatelliteConstants.SATELLITE_ENTITLEMENT_STATUS_DISABLED;
+            case SatelliteEntitlementResult.SATELLITE_ENTITLEMENT_STATUS_ENABLED:
+                return SatelliteConstants.SATELLITE_ENTITLEMENT_STATUS_ENABLED;
+            case SatelliteEntitlementResult.SATELLITE_ENTITLEMENT_STATUS_INCOMPATIBLE:
+                return SatelliteConstants.SATELLITE_ENTITLEMENT_STATUS_INCOMPATIBLE;
+            case SatelliteEntitlementResult.SATELLITE_ENTITLEMENT_STATUS_PROVISIONING:
+                return SatelliteConstants.SATELLITE_ENTITLEMENT_STATUS_PROVISIONING;
+            default:
+                return SatelliteConstants.SATELLITE_ENTITLEMENT_STATUS_UNKNOWN;
+        }
+    }
+
     private static void logd(String log) {
         Rlog.d(TAG, log);
     }