Merge "Add metrics for WifiNetworkSuggestion Api App usage" into rvc-dev
diff --git a/service/java/com/android/server/wifi/WifiMetrics.java b/service/java/com/android/server/wifi/WifiMetrics.java
index 03c8249..7257bc2 100644
--- a/service/java/com/android/server/wifi/WifiMetrics.java
+++ b/service/java/com/android/server/wifi/WifiMetrics.java
@@ -83,6 +83,7 @@
 import com.android.server.wifi.proto.nano.WifiMetricsProto.WifiLockStats;
 import com.android.server.wifi.proto.nano.WifiMetricsProto.WifiNetworkRequestApiLog;
 import com.android.server.wifi.proto.nano.WifiMetricsProto.WifiNetworkSuggestionApiLog;
+import com.android.server.wifi.proto.nano.WifiMetricsProto.WifiNetworkSuggestionApiLog.SuggestionAppCount;
 import com.android.server.wifi.proto.nano.WifiMetricsProto.WifiToggleStats;
 import com.android.server.wifi.proto.nano.WifiMetricsProto.WifiUsabilityStats;
 import com.android.server.wifi.proto.nano.WifiMetricsProto.WifiUsabilityStatsEntry;
@@ -410,6 +411,7 @@
             {5, 20, 50, 100, 500};
     private final IntHistogram mWifiNetworkSuggestionApiListSizeHistogram =
             new IntHistogram(NETWORK_SUGGESTION_API_LIST_SIZE_HISTOGRAM_BUCKETS);
+    private final IntCounter mWifiNetworkSuggestionApiAppTypeCounter = new IntCounter();
     private final WifiLockStats mWifiLockStats = new WifiLockStats();
     private static final int[] WIFI_LOCK_SESSION_DURATION_HISTOGRAM_BUCKETS =
             {1, 10, 60, 600, 3600};
@@ -3626,7 +3628,9 @@
                         + mWifiNetworkRequestApiMatchSizeHistogram);
                 pw.println("mWifiNetworkSuggestionApiLog:\n" + mWifiNetworkSuggestionApiLog);
                 pw.println("mWifiNetworkSuggestionApiMatchSizeHistogram:\n"
-                        + mWifiNetworkRequestApiMatchSizeHistogram);
+                        + mWifiNetworkSuggestionApiListSizeHistogram);
+                pw.println("mWifiNetworkSuggestionApiAppTypeCounter:\n"
+                        + mWifiNetworkSuggestionApiAppTypeCounter);
                 pw.println("mNetworkIdToNominatorId:\n" + mNetworkIdToNominatorId);
                 pw.println("mWifiLockStats:\n" + mWifiLockStats);
                 pw.println("mWifiLockHighPerfAcqDurationSecHistogram:\n"
@@ -4243,6 +4247,14 @@
 
             mWifiNetworkSuggestionApiLog.networkListSizeHistogram =
                     mWifiNetworkSuggestionApiListSizeHistogram.toProto();
+            mWifiNetworkSuggestionApiLog.appCountPerType =
+                    mWifiNetworkSuggestionApiAppTypeCounter.toProto(SuggestionAppCount.class,
+                            (key, count) -> {
+                                SuggestionAppCount entry = new SuggestionAppCount();
+                                entry.appType = key;
+                                entry.count = count;
+                                return entry;
+                            });
             mWifiLogProto.wifiNetworkSuggestionApiLog = mWifiNetworkSuggestionApiLog;
 
             mWifiLockStats.highPerfLockAcqDurationSecHistogram =
@@ -4507,6 +4519,7 @@
             mWifiNetworkSuggestionApiLog.clear();
             mWifiNetworkRequestApiMatchSizeHistogram.clear();
             mWifiNetworkSuggestionApiListSizeHistogram.clear();
+            mWifiNetworkSuggestionApiAppTypeCounter.clear();
             mWifiLockHighPerfAcqDurationSecHistogram.clear();
             mWifiLockLowLatencyAcqDurationSecHistogram.clear();
             mWifiLockHighPerfActiveSessionDurationSecHistogram.clear();
@@ -5863,6 +5876,27 @@
         }
     }
 
+    /** Increment number of app add suggestion with different privilege */
+    public void incrementNetworkSuggestionApiUsageNumOfAppInType(int appType) {
+        int typeCode;
+        synchronized (mLock) {
+            switch (appType) {
+                case WifiNetworkSuggestionsManager.APP_TYPE_CARRIER_PRIVILEGED:
+                    typeCode = WifiNetworkSuggestionApiLog.TYPE_CARRIER_PRIVILEGED;
+                    break;
+                case WifiNetworkSuggestionsManager.APP_TYPE_NETWORK_PROVISIONING:
+                    typeCode = WifiNetworkSuggestionApiLog.TYPE_NETWORK_PROVISIONING;
+                    break;
+                case WifiNetworkSuggestionsManager.APP_TYPE_NON_PRIVILEGED:
+                    typeCode = WifiNetworkSuggestionApiLog.TYPE_NON_PRIVILEGED;
+                    break;
+                default:
+                    typeCode = WifiNetworkSuggestionApiLog.TYPE_UNKNOWN;
+            }
+        }
+        mWifiNetworkSuggestionApiAppTypeCounter.increment(typeCode);
+    }
+
     /**
      * Sets the nominator for a network (i.e. which entity made the suggestion to connect)
      * @param networkId the ID of the network, from its {@link WifiConfiguration}
diff --git a/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java b/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java
index 28cf627..685df07 100644
--- a/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java
+++ b/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java
@@ -105,6 +105,10 @@
     public static final String EXTRA_UID =
             "com.android.server.wifi.extra.NetworkSuggestion.UID";
 
+    public static final int APP_TYPE_CARRIER_PRIVILEGED = 1;
+    public static final int APP_TYPE_NETWORK_PROVISIONING = 2;
+    public static final int APP_TYPE_NON_PRIVILEGED = 3;
+
     /**
      * Limit number of hidden networks attach to scan
      */
@@ -824,19 +828,27 @@
             if (mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(uid)) {
                 Log.i(TAG, "Setting the carrier provisioning app approved");
                 perAppInfo.hasUserApproved = true;
+                mWifiMetrics.incrementNetworkSuggestionApiUsageNumOfAppInType(
+                        APP_TYPE_NETWORK_PROVISIONING);
             } else if (carrierId != TelephonyManager.UNKNOWN_CARRIER_ID) {
                 Log.i(TAG, "Setting the carrier privileged app approved");
                 perAppInfo.carrierId = carrierId;
+                mWifiMetrics.incrementNetworkSuggestionApiUsageNumOfAppInType(
+                        APP_TYPE_CARRIER_PRIVILEGED);
             } else if (perAppInfo.packageName.equals(activeScorerPackage)) {
                 Log.i(TAG, "Exempting the active scorer app");
                 // nothing more to do, user approval related checks are done at network selection
                 // time (which also takes care of any dynamic changes in active scorer).
+                mWifiMetrics.incrementNetworkSuggestionApiUsageNumOfAppInType(
+                        APP_TYPE_NON_PRIVILEGED);
             } else {
                 if (isSuggestionFromForegroundApp(packageName)) {
                     sendUserApprovalDialog(packageName, uid);
                 } else {
                     sendUserApprovalNotification(packageName, uid);
                 }
+                mWifiMetrics.incrementNetworkSuggestionApiUsageNumOfAppInType(
+                        APP_TYPE_NON_PRIVILEGED);
             }
         }
         // If PerAppInfo is upgrade from pre-R, uid may not be set.
diff --git a/service/proto/src/metrics.proto b/service/proto/src/metrics.proto
index d1dd39e..627c245 100644
--- a/service/proto/src/metrics.proto
+++ b/service/proto/src/metrics.proto
@@ -2897,6 +2897,24 @@
 
 // NetworkSuggestion API metrics.
 message WifiNetworkSuggestionApiLog {
+  enum SuggestionAppTypeCode {
+    // UNKNOWN type suggestion app.
+    TYPE_UNKNOWN = 0;
+    // Suggestion app with carrier privilege.
+    TYPE_CARRIER_PRIVILEGED = 1;
+    // Suggestion app with network provisioning permission(NETWORK_CARRIER_PROVISIONING).
+    TYPE_NETWORK_PROVISIONING = 2;
+    // Suggestion app without special privilege.
+    TYPE_NON_PRIVILEGED = 3;
+  }
+
+  message SuggestionAppCount {
+    // Suggestion app type.
+    optional SuggestionAppTypeCode app_type = 1;
+    // Num of app in this type.
+    optional int32 count = 2;
+  }
+
   // Number of modifications to their suggestions by apps.
   optional int32 num_modification = 1;
 
@@ -2908,6 +2926,9 @@
 
   // Histogram for size of the network lists provided by various apps on the device.
   repeated HistogramBucketInt32 network_list_size_histogram = 4;
+
+  // Count of app using suggestion API with different privilege.
+  repeated SuggestionAppCount app_count_per_type = 5;
 }
 
 // WifiLock metrics
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiMetricsTest.java b/tests/wifitests/src/com/android/server/wifi/WifiMetricsTest.java
index 5b5cc7d..191f81c 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiMetricsTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiMetricsTest.java
@@ -4186,6 +4186,20 @@
 
         mWifiMetrics.incrementNetworkSuggestionApiNumConnectFailure();
 
+        mWifiMetrics.incrementNetworkSuggestionApiUsageNumOfAppInType(
+                WifiNetworkSuggestionsManager.APP_TYPE_NON_PRIVILEGED);
+        mWifiMetrics.incrementNetworkSuggestionApiUsageNumOfAppInType(
+                WifiNetworkSuggestionsManager.APP_TYPE_NON_PRIVILEGED);
+        mWifiMetrics.incrementNetworkSuggestionApiUsageNumOfAppInType(
+                WifiNetworkSuggestionsManager.APP_TYPE_NON_PRIVILEGED);
+        mWifiMetrics.incrementNetworkSuggestionApiUsageNumOfAppInType(
+                WifiNetworkSuggestionsManager.APP_TYPE_CARRIER_PRIVILEGED);
+        mWifiMetrics.incrementNetworkSuggestionApiUsageNumOfAppInType(
+                WifiNetworkSuggestionsManager.APP_TYPE_CARRIER_PRIVILEGED);
+        mWifiMetrics.incrementNetworkSuggestionApiUsageNumOfAppInType(
+                WifiNetworkSuggestionsManager.APP_TYPE_NETWORK_PROVISIONING);
+
+
         mWifiMetrics.noteNetworkSuggestionApiListSizeHistogram(new ArrayList<Integer>() {{
                 add(5);
                 add(100);
@@ -4214,6 +4228,17 @@
         };
         assertHistogramBucketsEqual(expectedNetworkListSizeHistogram,
                 mDecodedProto.wifiNetworkSuggestionApiLog.networkListSizeHistogram);
+
+        assertEquals(3, mDecodedProto.wifiNetworkSuggestionApiLog.appCountPerType.length);
+        assertEquals(WifiMetricsProto.WifiNetworkSuggestionApiLog.TYPE_CARRIER_PRIVILEGED,
+                mDecodedProto.wifiNetworkSuggestionApiLog.appCountPerType[0].appType);
+        assertEquals(2, mDecodedProto.wifiNetworkSuggestionApiLog.appCountPerType[0].count);
+        assertEquals(WifiMetricsProto.WifiNetworkSuggestionApiLog.TYPE_NETWORK_PROVISIONING,
+                mDecodedProto.wifiNetworkSuggestionApiLog.appCountPerType[1].appType);
+        assertEquals(1, mDecodedProto.wifiNetworkSuggestionApiLog.appCountPerType[1].count);
+        assertEquals(WifiMetricsProto.WifiNetworkSuggestionApiLog.TYPE_NON_PRIVILEGED,
+                mDecodedProto.wifiNetworkSuggestionApiLog.appCountPerType[2].appType);
+        assertEquals(3, mDecodedProto.wifiNetworkSuggestionApiLog.appCountPerType[2].count);
     }
 
     private NetworkSelectionExperimentDecisions findUniqueNetworkSelectionExperimentDecisions(
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java
index 7db13a1..8fa2fa1 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java
@@ -301,7 +301,9 @@
                         TEST_PACKAGE_2, TEST_FEATURE));
         verify(mPasspointManager).addOrUpdateProvider(
                 passpointConfiguration, TEST_UID_2, TEST_PACKAGE_2, true, true);
-
+        verify(mWifiMetrics, times(2))
+                .incrementNetworkSuggestionApiUsageNumOfAppInType(
+                        WifiNetworkSuggestionsManager.APP_TYPE_NON_PRIVILEGED);
         Set<WifiNetworkSuggestion> allNetworkSuggestions =
                 mWifiNetworkSuggestionsManager.getAllNetworkSuggestions();
         Set<WifiNetworkSuggestion> expectedAllNetworkSuggestions =
@@ -541,11 +543,12 @@
         assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS,
                 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1,
                         TEST_PACKAGE_1, TEST_FEATURE));
-
         // Assert that the original config was not metered.
         assertEquals(WifiConfiguration.METERED_OVERRIDE_NONE,
                 networkSuggestion.wifiConfiguration.meteredOverride);
-
+        verify(mWifiMetrics).incrementNetworkSuggestionApiUsageNumOfAppInType(
+                WifiNetworkSuggestionsManager.APP_TYPE_NON_PRIVILEGED);
+        reset(mWifiMetrics);
         // Store the original WifiConfiguration from WifiConfigManager.
         WifiConfiguration configInWcm =
                 new WifiConfiguration(networkSuggestion.wifiConfiguration);
@@ -567,7 +570,7 @@
         assertEquals(WifiConfiguration.METERED_OVERRIDE_METERED,
                 mWifiNetworkSuggestionsManager
                         .get(TEST_PACKAGE_1).get(0).wifiConfiguration.meteredOverride);
-
+        verify(mWifiMetrics, never()).incrementNetworkSuggestionApiUsageNumOfAppInType(anyInt());
         // Verify we did update config in WCM.
         ArgumentCaptor<WifiConfiguration> configCaptor =
                 ArgumentCaptor.forClass(WifiConfiguration.class);
@@ -674,7 +677,8 @@
         assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS,
                 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1,
                         TEST_PACKAGE_1, TEST_FEATURE));
-
+        verify(mWifiMetrics).incrementNetworkSuggestionApiUsageNumOfAppInType(
+                WifiNetworkSuggestionsManager.APP_TYPE_NETWORK_PROVISIONING);
         ScanDetail scanDetail = createScanDetailForNetwork(networkSuggestion.wifiConfiguration);
 
         Set<ExtendedWifiNetworkSuggestion> matchingExtNetworkSuggestions =
@@ -2912,7 +2916,8 @@
                 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE);
 
         assertEquals(status, WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS);
-
+        verify(mWifiMetrics).incrementNetworkSuggestionApiUsageNumOfAppInType(
+                WifiNetworkSuggestionsManager.APP_TYPE_NETWORK_PROVISIONING);
     }
 
     @Test
@@ -3168,6 +3173,7 @@
         assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_NOT_ALLOWED, status);
         verify(mNotificationManger, never()).notify(anyInt(), any());
         assertEquals(0, mWifiNetworkSuggestionsManager.get(TEST_PACKAGE_1).size());
+        verify(mWifiMetrics, never()).incrementNetworkSuggestionApiUsageNumOfAppInType(anyInt());
     }
 
     /**
@@ -3191,6 +3197,8 @@
         assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, status);
         verify(mNotificationManger, never()).notify(anyInt(), any());
         assertEquals(1,  mWifiNetworkSuggestionsManager.get(TEST_PACKAGE_1).size());
+        verify(mWifiMetrics).incrementNetworkSuggestionApiUsageNumOfAppInType(
+                WifiNetworkSuggestionsManager.APP_TYPE_CARRIER_PRIVILEGED);
     }
 
     /**