Merge "Add wake lock that is acquired during full wifi scans" into mnc-dev
diff --git a/service/java/com/android/server/wifi/WifiConfigStore.java b/service/java/com/android/server/wifi/WifiConfigStore.java
index dde3020..d219531 100644
--- a/service/java/com/android/server/wifi/WifiConfigStore.java
+++ b/service/java/com/android/server/wifi/WifiConfigStore.java
@@ -4174,16 +4174,11 @@
         WifiConfiguration config = mConfiguredNetworks.get(netId);
         if (config != null) {
             if (enabled) {
-                loge("SSID re-enabled for  " + config.configKey() +
+                loge("Ignoring SSID re-enabled from supplicant:  " + config.configKey() +
                         " had autoJoinStatus=" + Integer.toString(config.autoJoinStatus)
                         + " self added " + config.selfAdded + " ephemeral " + config.ephemeral);
-                //TODO: http://b/16381983 Fix Wifi Network Blacklisting
-                //TODO: really I don't know if re-enabling is right but we
-                //TODO: should err on the side of trying to connect
-                //TODO: even if the attempt will fail
-                if (config.autoJoinStatus == WifiConfiguration.AUTO_JOIN_DISABLED_ON_AUTH_FAILURE) {
-                    config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
-                }
+                //We should not re-enable the BSSID based on Supplicant reanable.
+                // Framework will re-enable it after its own blacklist timer expires
             } else {
                 loge("SSID temp disabled for  " + config.configKey() +
                         " had autoJoinStatus=" + Integer.toString(config.autoJoinStatus)
diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java
index 84bb38e..3734a67 100644
--- a/service/java/com/android/server/wifi/WifiServiceImpl.java
+++ b/service/java/com/android/server/wifi/WifiServiceImpl.java
@@ -966,7 +966,7 @@
                 return new ArrayList<ScanResult>();
             }
             if (!canReadPeerMacAddresses && !isActiveNetworkScorer
-                    && !checkCallerHasLocationPermission(callingPackage, uid)) {
+                    && !checkCallerCanAccessScanResults(callingPackage, uid)) {
                 return new ArrayList<ScanResult>();
             }
             if (mAppOps.noteOp(AppOpsManager.OP_WIFI_SCAN, uid, callingPackage)
@@ -2035,7 +2035,7 @@
      * Checks that calling process has android.Manifest.permission.ACCESS_COARSE_LOCATION or
      * android.Manifest.permission.ACCESS_FINE_LOCATION and a corresponding app op is allowed
      */
-    private boolean checkCallerHasLocationPermission(String callingPackage, int uid) {
+    private boolean checkCallerCanAccessScanResults(String callingPackage, int uid) {
         if (ActivityManager.checkUidPermission(Manifest.permission.ACCESS_FINE_LOCATION, uid)
                 == PackageManager.PERMISSION_GRANTED
                 && isAppOppAllowed(AppOpsManager.OP_FINE_LOCATION, callingPackage, uid)) {
@@ -2058,10 +2058,13 @@
         if (enforceLocationPermission) {
             throw new SecurityException("Need ACCESS_COARSE_LOCATION or "
                     + "ACCESS_FINE_LOCATION permission to get scan results");
-        } else {
-            Log.e(TAG, "Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION "
-                    + "permission to get scan results");
         }
+        // Pre-M apps running in the foreground should continue getting scan results
+        if (isForegroundApp(callingPackage)) {
+            return true;
+        }
+        Log.e(TAG, "Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION "
+                + "permission to get scan results");
         return false;
     }
 
@@ -2069,4 +2072,15 @@
         return mAppOps.noteOp(op, uid, callingPackage) == AppOpsManager.MODE_ALLOWED;
     }
 
+    /**
+     * Return true if the specified package name is a foreground app.
+     *
+     * @param pkgName application package name.
+     */
+    private boolean isForegroundApp(String pkgName) {
+        ActivityManager am = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
+        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
+        return !tasks.isEmpty() && pkgName.equals(tasks.get(0).topActivity.getPackageName());
+    }
+
 }
diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java
index 2468573..2c543b7 100644
--- a/service/java/com/android/server/wifi/WifiStateMachine.java
+++ b/service/java/com/android/server/wifi/WifiStateMachine.java
@@ -3619,9 +3619,19 @@
     private void setFrequencyBand() {
         int band = Settings.Global.getInt(mContext.getContentResolver(),
                 Settings.Global.WIFI_FREQUENCY_BAND, WifiManager.WIFI_FREQUENCY_BAND_AUTO);
-        setFrequencyBand(band, false);
+
+        if (mWifiNative.setBand(band)) {
+            mFrequencyBand.set(band);
+            if (PDBG) {
+                logd("done set frequency band " + band);
+            }
+        } else {
+            loge("Failed to set frequency band " + band);
+        }
     }
 
+
+
     private void setSuspendOptimizationsNative(int reason, boolean enabled) {
         if (DBG) {
             log("setSuspendOptimizationsNative: " + reason + " " + enabled
@@ -5769,6 +5779,8 @@
                     mLastSignalLevel = -1;
 
                     mWifiInfo.setMacAddress(mWifiNative.getMacAddress());
+                    /* set frequency band of operation */
+                    setFrequencyBand();
                     mWifiNative.enableSaveConfig();
                     mWifiConfigStore.loadAndEnableAllNetworks();
                     if (mWifiConfigStore.enableVerboseLogging.get() > 0) {
@@ -6107,8 +6119,6 @@
              * driver are changed to reduce interference with bluetooth
              */
             mWifiNative.setBluetoothCoexistenceScanMode(mBluetoothConnectionActive);
-            /* set frequency band of operation */
-            setFrequencyBand();
             /* initialize network state */
             setNetworkDetailedState(DetailedState.DISCONNECTED);