Add control to disable suspend optimizations

Add an advanced setting that allows user to turn off power savings at screen off.

Bug: 5885175
Change-Id: I2dd013b86d7500a2ad1f9ec75d86551808f05543
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index e1ef67c..9ec4744 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -3288,6 +3288,14 @@
                 "wifi_watchdog_poor_network_test_enabled";
 
         /**
+         * Setting to turn on suspend optimizations at screen off on Wi-Fi. Enabled by default and
+         * needs to be set to 0 to disable it.
+         * @hide
+         */
+        public static final String WIFI_SUSPEND_OPTIMIZATIONS_ENABLED =
+                "wifi_suspend_optimizations_enabled";
+
+        /**
          * Setting to turn off walled garden test on Wi-Fi. Feature is enabled by default and
          * the setting needs to be set to 0 to disable it.
          * @hide
diff --git a/wifi/java/android/net/wifi/WifiStateMachine.java b/wifi/java/android/net/wifi/WifiStateMachine.java
index c480759..a8975ff 100644
--- a/wifi/java/android/net/wifi/WifiStateMachine.java
+++ b/wifi/java/android/net/wifi/WifiStateMachine.java
@@ -41,6 +41,7 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.PackageManager;
+import android.database.ContentObserver;
 import android.net.ConnectivityManager;
 import android.net.DhcpInfo;
 import android.net.DhcpInfoInternal;
@@ -138,6 +139,8 @@
     private boolean mSetScanActive = false;
     /* High perf mode is true if an app has held a high perf Wifi Lock */
     private boolean mHighPerfMode = false;
+    /* Tracks if user has disabled suspend optimizations through settings */
+    private AtomicBoolean mSuspendOptEnabled = new AtomicBoolean(true);
 
     private boolean mBluetoothConnectionActive = false;
 
@@ -591,6 +594,9 @@
         mPrimaryDeviceType = mContext.getResources().getString(
                 com.android.internal.R.string.config_wifi_p2p_device_type);
 
+        mSuspendOptEnabled.set(Settings.Secure.getInt(mContext.getContentResolver(),
+                    Settings.Secure.WIFI_SUSPEND_OPTIMIZATIONS_ENABLED, 1) == 1);
+
         mContext.registerReceiver(
             new BroadcastReceiver() {
                 @Override
@@ -626,18 +632,25 @@
                         enableBackgroundScanCommand(false);
                     }
                     enableAllNetworks();
-                    sendMessage(CMD_CLEAR_SUSPEND_OPTIMIZATIONS);
+                    if (mSuspendOptEnabled.get()) {
+                        if (DBG) log("Clear suspend optimizations");
+                        sendMessage(CMD_CLEAR_SUSPEND_OPTIMIZATIONS);
+                    }
                 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
                     enableRssiPolling(false);
                     if (mBackgroundScanSupported) {
                         enableBackgroundScanCommand(true);
                     }
-                    //Allow 2s for suspend optimizations to be set
-                    mSuspendWakeLock.acquire(2000);
-                    sendMessage(CMD_SET_SUSPEND_OPTIMIZATIONS);
+                    if (mSuspendOptEnabled.get()) {
+                        if (DBG) log("Enable suspend optimizations");
+                        //Allow 2s for suspend optimizations to be set
+                        mSuspendWakeLock.acquire(2000);
+                        sendMessage(CMD_SET_SUSPEND_OPTIMIZATIONS);
+                    }
                 }
             }
         };
+
         mContext.registerReceiver(
                 new BroadcastReceiver() {
                     @Override
@@ -648,6 +661,16 @@
                 },
                 new IntentFilter(ACTION_DELAYED_DRIVER_STOP));
 
+        mContext.getContentResolver().registerContentObserver(Settings.Secure.getUriFor(
+                Settings.Secure.WIFI_SUSPEND_OPTIMIZATIONS_ENABLED), false,
+                new ContentObserver(getHandler()) {
+                    @Override
+                    public void onChange(boolean selfChange) {
+                        mSuspendOptEnabled.set(Settings.Secure.getInt(mContext.getContentResolver(),
+                                Settings.Secure.WIFI_SUSPEND_OPTIMIZATIONS_ENABLED, 1) == 1);
+                    }
+                });
+
         mScanResultCache = new LruCache<String, ScanResult>(SCAN_RESULT_CACHE_SIZE);
 
         PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
@@ -1133,6 +1156,8 @@
         sb.append("mLastNetworkId ").append(mLastNetworkId).append(LS);
         sb.append("mReconnectCount ").append(mReconnectCount).append(LS);
         sb.append("mIsScanMode ").append(mIsScanMode).append(LS);
+        sb.append("mHighPerfMode").append(mHighPerfMode).append(LS);
+        sb.append("mSuspendOptEnabled").append(mSuspendOptEnabled).append(LS);
         sb.append("Supplicant status").append(LS)
                 .append(mWifiNative.status()).append(LS).append(LS);