DO NOT MERGE Wifi: Disable PNO (if enabled) only when the scan is allowed to trigger

CPed from https://partner-android-review.googlesource.com/#/c/216165

In WiFi's disconnected state and when a PNO is enabled, a scan
request through an alarm source shall disable the PNO, though
the scan fails to trigger (if cancelled due to cancelDelayedScan).
This commit ensures to disable the PNO only before the scan request
succeeds to trigger.

Change-Id: Ie462011d99644e83314c55e8135f8cbf2b8de5c7
(cherry picked from commit 4c94fc6a884fc17424c76d791a42c3c263b390b2)
diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java
index e2a58ea..ca05b96 100644
--- a/service/java/com/android/server/wifi/WifiStateMachine.java
+++ b/service/java/com/android/server/wifi/WifiStateMachine.java
@@ -7989,10 +7989,6 @@
                         messageHandlingStatus = MESSAGE_HANDLING_STATUS_REFUSED;
                         return HANDLED;
                     }
-                    /* Disable background scan temporarily during a regular scan */
-                    if (mEnableBackgroundScan) {
-                        enableBackgroundScan(false);
-                    }
                     if (message.arg1 == SCAN_ALARM_SOURCE) {
                         // Check if the CMD_START_SCAN message is obsolete (and thus if it should
                         // not be processed) and restart the scan
@@ -8011,9 +8007,28 @@
                                     + " -> obsolete");
                             return HANDLED;
                         }
+                        /* Disable background scan temporarily during a regular scan */
+                        if (mEnableBackgroundScan) {
+                            enableBackgroundScan(false);
+                        }
                         handleScanRequest(WifiNative.SCAN_WITHOUT_CONNECTION_SETUP, message);
                         ret = HANDLED;
                     } else {
+
+                        /*
+                         * The SCAN request is not handled in this state and
+                         * would eventually might/will get handled in the
+                         * parent's state. The PNO, if already enabled had to
+                         * get disabled before the SCAN trigger. Hence, stop
+                         * the PNO if already enabled in this state, though the
+                         * SCAN request is not handled(PNO disable before the
+                         * SCAN trigger in any other state is not the right
+                         * place to issue).
+                         */
+
+                        if (mEnableBackgroundScan) {
+                            enableBackgroundScan(false);
+                        }
                         ret = NOT_HANDLED;
                     }
                     break;