various fixes:

- allow for making use of more aggressive initial autojoin attempt rssi thresholds
- create a blacklist due to missing identiy reason
- make autojoin parameters configurable

Change-Id: I385db83aab1da7918ff6935eb59a209b253d42e6
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index 3907fc5..7599bf4 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -339,6 +339,22 @@
          capability can provide power savings when wifi needs to be always kept on. -->
     <bool translatable="false" name="config_wifi_background_scan_support">false</bool>
 
+    <!-- Boolean indicating we re-try re-associating once upon disconnection and RSSI is high failure  -->
+    <bool translatable="true" name="config_wifi_enable_disconnection_debounce">true</bool>
+
+    <!-- Boolean indicating autojoin will prefer 5GHz and choose 5GHz BSSIDs -->
+    <bool translatable="true" name="config_wifi_enable_5GHz_preference">true</bool>
+
+    <!-- Integer specifying the basic autojoin parameters -->
+    <integer translatable="false" name="config_wifi_framework_5GHz_preference_boost_threshold">-65</integer>
+    <integer translatable="false" name="config_wifi_framework_5GHz_preference_boost_factor">5</integer>
+    <integer translatable="false" name="config_wifi_framework_current_association_hysteresis_high">16</integer>
+    <integer translatable="false" name="config_wifi_framework_current_association_hysteresis_low">10</integer>
+    <integer translatable="false" name="config_wifi_framework_5GHz_preference_penalty_threshold">-75</integer>
+    <integer translatable="false" name="config_wifi_framework_5GHz_preference_penalty_factor">2</integer>
+
+
+
     <!-- Integer indicating wpa_supplicant scan interval in milliseconds -->
     <integer translatable="false" name="config_wifi_supplicant_scan_interval">15000</integer>
 
@@ -351,9 +367,24 @@
          point on the move. A value of 0 means no periodic scans will be used in the framework. -->
     <integer translatable="false" name="config_wifi_framework_scan_interval">300000</integer>
 
-    <!-- Integer indicating associated scan interval in milliseconds -->
+    <!-- Integer indicating associated partial scan interval in milliseconds -->
     <integer translatable="false" name="config_wifi_framework_associated_scan_interval">10000</integer>
 
+    <!-- Integer indicating associated full scan backoff, representing a fraction: xx/8 -->
+    <integer translatable="false" name="config_wifi_framework_associated_full_scan_backoff">12</integer>
+
+    <!-- Integer indicating associated full scan max interval in milliseconds -->
+    <integer translatable="false" name="config_wifi_framework_associated_full_scan_max_interval">300000</integer>
+
+    <!-- Integer indicating associated full scan max total dwell time in milliseconds -->
+    <integer translatable="false" name="config_wifi_framework_associated_full_scan_max_total_dwell_time">500</integer>
+
+    <!-- Integer indicating associated full scan max num active channels -->
+    <integer translatable="false" name="config_wifi_framework_associated_partial_scan_max_num_active_channels">6</integer>
+
+    <!-- Integer indicating associated full scan max num passive channels -->
+    <integer translatable="false" name="config_wifi_framework_associated_partial_scan_max_num_passive_channels">3</integer>
+
     <!-- Boolean indicating associated scan are allowed -->
     <bool translatable="false" name="config_wifi_framework_enable_associated_autojoin_scan">true</bool>
 
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index 7fc522f..ba564a0 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -297,6 +297,21 @@
   <java-symbol type="bool" name="config_wifi_framework_enable_associated_autojoin_scan" />
   <java-symbol type="bool" name="config_wifi_framework_enable_associated_network_selection" />
   <java-symbol type="bool" name="config_wifi_only_link_same_credential_configurations" />
+  <java-symbol type="bool" name="config_wifi_enable_disconnection_debounce" />
+  <java-symbol type="bool" name="config_wifi_enable_5GHz_preference" />
+  <java-symbol type="integer" name="config_wifi_framework_5GHz_preference_boost_threshold" />
+  <java-symbol type="integer" name="config_wifi_framework_5GHz_preference_boost_factor" />
+  <java-symbol type="integer" name="config_wifi_framework_5GHz_preference_penalty_threshold" />
+  <java-symbol type="integer" name="config_wifi_framework_current_association_hysteresis_high" />
+  <java-symbol type="integer" name="config_wifi_framework_current_association_hysteresis_low" />
+  <java-symbol type="integer" name="config_wifi_framework_5GHz_preference_penalty_threshold" />
+  <java-symbol type="integer" name="config_wifi_framework_5GHz_preference_penalty_factor" />
+  <java-symbol type="integer" name="config_wifi_framework_associated_scan_interval" />
+  <java-symbol type="integer" name="config_wifi_framework_associated_full_scan_backoff" />
+  <java-symbol type="integer" name="config_wifi_framework_associated_full_scan_max_interval" />
+  <java-symbol type="integer" name="config_wifi_framework_associated_full_scan_max_total_dwell_time" />
+  <java-symbol type="integer" name="config_wifi_framework_associated_partial_scan_max_num_active_channels" />
+  <java-symbol type="integer" name="config_wifi_framework_associated_partial_scan_max_num_passive_channels" />
   <java-symbol type="integer" name="config_bluetooth_max_advertisers" />
   <java-symbol type="integer" name="config_bluetooth_max_scan_filters" />
   <java-symbol type="integer" name="config_cursorWindowSize" />
@@ -315,7 +330,6 @@
   <java-symbol type="integer" name="config_shortPressOnPowerBehavior" />
   <java-symbol type="integer" name="config_toastDefaultGravity" />
   <java-symbol type="integer" name="config_wifi_framework_scan_interval" />
-  <java-symbol type="integer" name="config_wifi_framework_associated_scan_interval" />
   <java-symbol type="integer" name="config_wifi_supplicant_scan_interval" />
   <java-symbol type="integer" name="config_wifi_scan_interval_p2p_connected" />
   <java-symbol type="integer" name="db_connection_pool_size" />
diff --git a/wifi/java/android/net/wifi/WifiConfiguration.java b/wifi/java/android/net/wifi/WifiConfiguration.java
index aeaff71..9ece434 100644
--- a/wifi/java/android/net/wifi/WifiConfiguration.java
+++ b/wifi/java/android/net/wifi/WifiConfiguration.java
@@ -186,6 +186,8 @@
     public static final int DISABLED_AUTH_FAILURE                           = 3;
     /** @hide */
     public static final int DISABLED_ASSOCIATION_REJECT                     = 4;
+    /** @hide */
+    public static final int DISABLED_BY_WIFI_MANAGER                        = 5;
 
     /**
      * The ID number that the supplicant uses to identify this
@@ -438,11 +440,7 @@
 
     /** @hide
      * 5GHz band is prefered low over 2.4 if the 5GHz RSSI is higher than this threshold */
-    public static int A_BAND_PREFERENCE_RSSI_THRESHOLD_LOW = -65;
-
-    /** @hide
-     * 5GHz band is prefered hard over 2.4 if the 5GHz RSSI is higher than this threshold */
-    public static int A_BAND_PREFERENCE_RSSI_THRESHOLD = -55;
+    public static int A_BAND_PREFERENCE_RSSI_THRESHOLD = -65;
 
     /** @hide
      * 5GHz band is penalized if the 5GHz RSSI is lower than this threshold **/
@@ -457,6 +455,12 @@
      ***/
     public static int HOME_NETWORK_RSSI_BOOST = 5;
 
+    /** @hide
+     * RSSI boost for configuration which use autoJoinUseAggressiveJoinAttemptThreshold
+     * To be more aggressive when initially attempting to auto join
+     */
+    public static int MAX_INITIAL_AUTO_JOIN_RSSI_BOOST = 8;
+
     /**
      * @hide
      * A summary of the RSSI and Band status for that configuration
@@ -594,6 +598,11 @@
     /** @hide */
     public static final int AUTO_JOIN_DISABLED_ON_AUTH_FAILURE  = 128;
     /** @hide */
+    public static final int AUTO_JOIN_DISABLED_NO_CREDENTIALS = 160;
+    /** @hide */
+    public static final int AUTO_JOIN_DISABLED_USER_ACTION = 161;
+
+    /** @hide */
     public static final int AUTO_JOIN_DELETED  = 200;
 
     /**
@@ -664,6 +673,18 @@
 
     /**
      * @hide
+     * Indicate that we didn't auto-join because rssi was too low
+     */
+    public boolean autoJoinBailedDueToLowRssi;
+
+    /**
+     * @hide
+     * AutoJoin even though RSSI is 10dB below threshold
+     */
+    public int autoJoinUseAggressiveJoinAttemptThreshold;
+
+    /**
+     * @hide
      * Number of time the scorer overrode a the priority based choice, when comparing two
      * WifiConfigurations, note that since comparing WifiConfiguration happens very often
      * potentially at every scan, this number might become very large, even on an idle
@@ -881,11 +902,10 @@
         if (this.autoJoinStatus > 0) {
             sbuf.append(" autoJoinStatus ").append(this.numConnectionFailures).append("\n");
         }
-        if (this.didSelfAdd || this.selfAdded) {
-            if (this.didSelfAdd) sbuf.append(" didSelfAdd");
-            if (this.selfAdded) sbuf.append(" selfAdded");
-            if (this.noInternetAccess) sbuf.append(" noInternetAccess");
-
+        if (this.didSelfAdd) sbuf.append(" didSelfAdd");
+        if (this.selfAdded) sbuf.append(" selfAdded");
+        if (this.noInternetAccess) sbuf.append(" noInternetAccess");
+        if (this.didSelfAdd || this.selfAdded || this.noInternetAccess) {
             sbuf.append("\n");
         }
         sbuf.append(" KeyMgmt:");
@@ -950,21 +970,41 @@
         if (this.preSharedKey != null) {
             sbuf.append('*');
         }
-
+        sbuf.append("\nEnterprise config:\n");
         sbuf.append(enterpriseConfig);
-        sbuf.append('\n');
 
+        sbuf.append("IP config:\n");
         sbuf.append(mIpConfiguration.toString());
 
-        if (this.creatorUid != 0)  sbuf.append("uid=" + Integer.toString(creatorUid));
-        if (this.autoJoinBSSID != null) sbuf.append("autoJoinBSSID=" + autoJoinBSSID);
+        if (this.creatorUid != 0)  sbuf.append(" uid=" + Integer.toString(creatorUid));
+        if (this.autoJoinBSSID != null) sbuf.append(" autoJoinBSSID=" + autoJoinBSSID);
+        long now_ms = System.currentTimeMillis();
         if (this.blackListTimestamp != 0) {
-            long now_ms = System.currentTimeMillis();
+            sbuf.append('\n');
             long diff = now_ms - this.blackListTimestamp;
             if (diff <= 0) {
-                sbuf.append("blackListed since <incorrect>");
+                sbuf.append(" blackListed since <incorrect>");
             } else {
-                sbuf.append("blackListed since ").append(Long.toString(diff/1000)).append( "sec");
+                sbuf.append(" blackListed: ").append(Long.toString(diff/1000)).append( "sec");
+            }
+        }
+        if (this.lastConnected != 0) {
+            sbuf.append('\n');
+            long diff = now_ms - this.lastConnected;
+            if (diff <= 0) {
+                sbuf.append("lastConnected since <incorrect>");
+            } else {
+                sbuf.append("lastConnected: ").append(Long.toString(diff/1000)).append( "sec");
+            }
+        }
+        if (this.lastConnectionFailure != 0) {
+            sbuf.append('\n');
+            long diff = now_ms - this.lastConnectionFailure;
+            if (diff <= 0) {
+                sbuf.append("lastConnectionFailure since <incorrect>");
+            } else {
+                sbuf.append("lastConnectionFailure: ").append(Long.toString(diff/1000));
+                sbuf.append( "sec");
             }
         }
         sbuf.append('\n');
@@ -984,15 +1024,29 @@
                 }
             }
         }
-        sbuf.append(" triggeredLow: ").append(numUserTriggeredWifiDisableLowRSSI);
-        sbuf.append(" triggeredBad: ").append(numUserTriggeredWifiDisableBadRSSI);
-        sbuf.append(" triggeredNotHigh: ").append(numUserTriggeredWifiDisableNotHighRSSI);
+        if (this.scanResultCache != null) {
+            sbuf.append("scan cache:  ");
+            for(ScanResult result : this.scanResultCache.values()) {
+                sbuf.append("{").append(result.BSSID).append(",").append(result.frequency);
+                sbuf.append(",").append(result.level).append(",st=");
+                sbuf.append(result.autoJoinStatus).append("} ");
+            }
+            sbuf.append('\n');
+        }
+        sbuf.append("triggeredLow: ").append(this.numUserTriggeredWifiDisableLowRSSI);
+        sbuf.append(" triggeredBad: ").append(this.numUserTriggeredWifiDisableBadRSSI);
+        sbuf.append(" triggeredNotHigh: ").append(this.numUserTriggeredWifiDisableNotHighRSSI);
         sbuf.append('\n');
-        sbuf.append(" ticksLow: ").append(numTicksAtLowRSSI);
-        sbuf.append(" ticksBad: ").append(numTicksAtBadRSSI);
-        sbuf.append(" ticksNotHigh: ").append(numTicksAtNotHighRSSI);
+        sbuf.append("ticksLow: ").append(this.numTicksAtLowRSSI);
+        sbuf.append(" ticksBad: ").append(this.numTicksAtBadRSSI);
+        sbuf.append(" ticksNotHigh: ").append(this.numTicksAtNotHighRSSI);
         sbuf.append('\n');
-        sbuf.append(" triggeredJoin: ").append(numUserTriggeredJoinAttempts);
+        sbuf.append("triggeredJoin: ").append(this.numUserTriggeredJoinAttempts);
+        sbuf.append('\n');
+        sbuf.append("autoJoinBailedDueToLowRssi: ").append(this.autoJoinBailedDueToLowRssi);
+        sbuf.append('\n');
+        sbuf.append("autoJoinUseAggressiveJoinAttemptThreshold: ");
+        sbuf.append(this.autoJoinUseAggressiveJoinAttemptThreshold);
         sbuf.append('\n');
 
         return sbuf.toString();
@@ -1310,6 +1364,9 @@
             numTicksAtNotHighRSSI = source.numTicksAtNotHighRSSI;
             numUserTriggeredJoinAttempts = source.numUserTriggeredJoinAttempts;
             autoJoinBSSID = source.autoJoinBSSID;
+            autoJoinUseAggressiveJoinAttemptThreshold
+                    = source.autoJoinUseAggressiveJoinAttemptThreshold;
+            autoJoinBailedDueToLowRssi = source.autoJoinBailedDueToLowRssi;
         }
     }
 
@@ -1370,7 +1427,8 @@
         dest.writeInt(numTicksAtBadRSSI);
         dest.writeInt(numTicksAtNotHighRSSI);
         dest.writeInt(numUserTriggeredJoinAttempts);
-
+        dest.writeInt(autoJoinUseAggressiveJoinAttemptThreshold);
+        dest.writeInt(autoJoinBailedDueToLowRssi ? 1 : 0);
     }
 
     /** Implement the Parcelable interface {@hide} */
@@ -1427,6 +1485,8 @@
                 config.numTicksAtBadRSSI = in.readInt();
                 config.numTicksAtNotHighRSSI = in.readInt();
                 config.numUserTriggeredJoinAttempts = in.readInt();
+                config.autoJoinUseAggressiveJoinAttemptThreshold = in.readInt();
+                config.autoJoinBailedDueToLowRssi = in.readInt() != 0;
                 return config;
             }