Snap for 10338099 from 83934035f5e9bb486c4c80c67aeaf8aa18eb9f6f to udc-release

Change-Id: I670574cf4f6ba7dfdaf4d5f8cd9f7b96cf2b903b
diff --git a/service/java/com/android/server/wifi/SupplicantStaIfaceCallbackAidlImpl.java b/service/java/com/android/server/wifi/SupplicantStaIfaceCallbackAidlImpl.java
index ffcb82d..af82aa0 100644
--- a/service/java/com/android/server/wifi/SupplicantStaIfaceCallbackAidlImpl.java
+++ b/service/java/com/android/server/wifi/SupplicantStaIfaceCallbackAidlImpl.java
@@ -428,7 +428,8 @@
             if (curConfiguration != null
                     && (WifiConfigurationUtil.isConfigForPskNetwork(curConfiguration)
                     || WifiConfigurationUtil.isConfigForWapiPskNetwork(curConfiguration))
-                    && !curConfiguration.getNetworkSelectionStatus().hasEverConnected()) {
+                    && !curConfiguration.getNetworkSelectionStatus().hasEverConnected()
+                    && mStateBeforeDisconnect == StaIfaceCallbackState.FOURWAY_HANDSHAKE) {
                 // Some AP implementations doesn't send de-authentication or dis-association
                 // frame after EAPOL failure. They keep retry EAPOL M1 frames. This leads to
                 // authentication timeout in supplicant. If this network was not connected before,
diff --git a/service/java/com/android/server/wifi/SupplicantStaIfaceCallbackHidlImpl.java b/service/java/com/android/server/wifi/SupplicantStaIfaceCallbackHidlImpl.java
index 214eac2..3673d7b 100644
--- a/service/java/com/android/server/wifi/SupplicantStaIfaceCallbackHidlImpl.java
+++ b/service/java/com/android/server/wifi/SupplicantStaIfaceCallbackHidlImpl.java
@@ -396,7 +396,8 @@
             if (curConfiguration != null
                     && (WifiConfigurationUtil.isConfigForPskNetwork(curConfiguration)
                     || WifiConfigurationUtil.isConfigForWapiPskNetwork(curConfiguration))
-                    && !curConfiguration.getNetworkSelectionStatus().hasEverConnected()) {
+                    && !curConfiguration.getNetworkSelectionStatus().hasEverConnected()
+                    && mStateBeforeDisconnect == State.FOURWAY_HANDSHAKE) {
                 // Some AP implementations doesn't send de-authentication or dis-association
                 // frame after EAPOL failure. They keep retry EAPOL M1 frames. This leads to
                 // authentication timeout in supplicant. If this network was not connected before,
diff --git a/service/java/com/android/server/wifi/WifiMetrics.java b/service/java/com/android/server/wifi/WifiMetrics.java
index 38561e1..7a543f8 100644
--- a/service/java/com/android/server/wifi/WifiMetrics.java
+++ b/service/java/com/android/server/wifi/WifiMetrics.java
@@ -1537,11 +1537,15 @@
         mCurrentDeviceMobilityStateStartMs = mClock.getElapsedSinceBootMillis();
         mCurrentDeviceMobilityStatePnoScanStartMs = -1;
         mOnWifiUsabilityListeners = new RemoteCallbackList<>();
+        mScanMetrics = new ScanMetrics(context, clock);
+    }
 
+    /** Begin listening to broadcasts */
+    public void start() {
         IntentFilter filter = new IntentFilter();
         filter.addAction(Intent.ACTION_SCREEN_ON);
         filter.addAction(Intent.ACTION_SCREEN_OFF);
-        context.registerReceiver(
+        mContext.registerReceiver(
                 new BroadcastReceiver() {
                     @Override
                     public void onReceive(Context context, Intent intent) {
@@ -1553,9 +1557,7 @@
                         }
                     }
                 }, filter, null, mHandler);
-        setScreenState(context.getSystemService(PowerManager.class).isInteractive());
-
-        mScanMetrics = new ScanMetrics(context, clock);
+        setScreenState(mContext.getSystemService(PowerManager.class).isInteractive());
     }
 
     /** Sets internal ScoringParams member */
diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java
index 66dceca..83d737d 100644
--- a/service/java/com/android/server/wifi/WifiServiceImpl.java
+++ b/service/java/com/android/server/wifi/WifiServiceImpl.java
@@ -579,6 +579,7 @@
 
             mWifiInjector.getWifiScanAlwaysAvailableSettingsCompatibility().initialize();
             mWifiInjector.getWifiNotificationManager().createNotificationChannels();
+            mWifiMetrics.start();
             mContext.registerReceiver(
                     new BroadcastReceiver() {
                         @Override
diff --git a/service/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalAidlImplTest.java b/service/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalAidlImplTest.java
index 087acb2..e3f0695 100644
--- a/service/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalAidlImplTest.java
+++ b/service/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalAidlImplTest.java
@@ -3069,6 +3069,11 @@
                 NativeUtil.macAddressToByteArray(BSSID),
                 SUPPLICANT_NETWORK_ID,
                 NativeUtil.byteArrayFromArrayList(NativeUtil.decodeSsid(SUPPLICANT_SSID)), false);
+        mISupplicantStaIfaceCallback.onStateChanged(
+                StaIfaceCallbackState.FOURWAY_HANDSHAKE,
+                NativeUtil.macAddressToByteArray(BSSID),
+                SUPPLICANT_NETWORK_ID,
+                NativeUtil.byteArrayFromArrayList(NativeUtil.decodeSsid(SUPPLICANT_SSID)), false);
         mISupplicantStaIfaceCallback.onAuthenticationTimeout(
                 NativeUtil.macAddressToByteArray(BSSID));
         verify(mWifiMonitor).broadcastAuthenticationFailureEvent(
diff --git a/service/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalHidlImplTest.java b/service/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalHidlImplTest.java
index 5836c9b..7f01d7b 100644
--- a/service/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalHidlImplTest.java
+++ b/service/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalHidlImplTest.java
@@ -3834,6 +3834,10 @@
                 ISupplicantStaIfaceCallback.State.ASSOCIATING,
                 NativeUtil.macAddressToByteArray(BSSID), SUPPLICANT_NETWORK_ID,
                 NativeUtil.decodeSsid(SUPPLICANT_SSID));
+        mISupplicantStaIfaceCallback.onStateChanged(
+                ISupplicantStaIfaceCallback.State.FOURWAY_HANDSHAKE,
+                NativeUtil.macAddressToByteArray(BSSID), SUPPLICANT_NETWORK_ID,
+                NativeUtil.decodeSsid(SUPPLICANT_SSID));
         mISupplicantStaIfaceCallback.onAuthenticationTimeout(
                 NativeUtil.macAddressToByteArray(BSSID));
         verify(mWifiMonitor).broadcastAuthenticationFailureEvent(
diff --git a/service/tests/wifitests/src/com/android/server/wifi/WifiMetricsTest.java b/service/tests/wifitests/src/com/android/server/wifi/WifiMetricsTest.java
index f58c6a1..6f1f534 100644
--- a/service/tests/wifitests/src/com/android/server/wifi/WifiMetricsTest.java
+++ b/service/tests/wifitests/src/com/android/server/wifi/WifiMetricsTest.java
@@ -217,6 +217,7 @@
         mWifiMetrics = new WifiMetrics(mContext, mFacade, mClock, mTestLooper.getLooper(),
                 new WifiAwareMetrics(mClock), new RttMetrics(mClock), mWifiPowerMetrics,
                 mWifiP2pMetrics, mDppMetrics, mWifiMonitor);
+        mWifiMetrics.start();
         mWifiMetrics.setWifiConfigManager(mWcm);
         mWifiMetrics.setWifiBlocklistMonitor(mWifiBlocklistMonitor);
         mWifiMetrics.setPasspointManager(mPpm);
diff --git a/service/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/service/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java
index 8e42464..c06ae93 100644
--- a/service/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java
+++ b/service/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java
@@ -771,6 +771,7 @@
     public void testWifiMetricsDump() {
         mWifiServiceImpl.checkAndStartWifi();
         mLooper.dispatchAll();
+        verify(mWifiMetrics).start();
         mWifiServiceImpl.dump(new FileDescriptor(), new PrintWriter(new StringWriter()),
                 new String[]{mWifiMetrics.PROTO_DUMP_ARG});
         mLooper.dispatchAll();
@@ -1888,6 +1889,7 @@
                 anyInt(), anyInt())).thenReturn(PackageManager.PERMISSION_GRANTED);
         mWifiServiceImpl.checkAndStartWifi();
         mLooper.dispatchAll();
+        verify(mWifiMetrics).start();
         verify(mWifiConfigManager).loadFromStore();
         verify(mActiveModeWarden).start();
     }