auto import from //branches/cupcake/...@130745
diff --git a/services/java/com/android/server/WifiService.java b/services/java/com/android/server/WifiService.java
index 2d61b1e..c009fac 100644
--- a/services/java/com/android/server/WifiService.java
+++ b/services/java/com/android/server/WifiService.java
@@ -22,6 +22,7 @@
 import static android.net.wifi.WifiManager.WIFI_STATE_ENABLING;
 import static android.net.wifi.WifiManager.WIFI_STATE_UNKNOWN;
 
+import android.app.ActivityManagerNative;
 import android.app.AlarmManager;
 import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
@@ -204,8 +205,6 @@
                         mWifiHandler.removeMessages(MESSAGE_RELEASE_WAKELOCK);
                         synchronized (sDriverStopWakeLock) {
                             if (sDriverStopWakeLock.isHeld()) {
-                                if (DBG) Log.d(TAG, "Releasing driver-stop wakelock " +
-                                        sDriverStopWakeLock);
                                 sDriverStopWakeLock.release();
                             }
                         }
@@ -213,9 +212,18 @@
                 }
         );
 
-        Log.d(TAG, "WifiService starting up with Wi-Fi " +
-            (wifiEnabled ? "enabled" : "disabled"));
-        registerForBroadcasts();
+        Log.i(TAG, "WifiService starting up with Wi-Fi " +
+                (wifiEnabled ? "enabled" : "disabled"));
+
+        mContext.registerReceiver(
+                new BroadcastReceiver() {
+                    @Override
+                    public void onReceive(Context context, Intent intent) {
+                        updateWifiState();
+                    }
+                },
+                new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
+
         setWifiEnabledBlocking(wifiEnabled, false);
     }
 
@@ -293,7 +301,7 @@
                         decrementHiddentNetworkPresentCounter();
                     }
                 }
-                mIsHiddenNetworkPresent.put(netId, new Boolean(present));
+                mIsHiddenNetworkPresent.put(netId, present);
             }
         } else {
             Log.e(TAG, "addOrUpdateHiddenNetwork(): Invalid (negative) network id!");
@@ -422,6 +430,7 @@
 
     /**
      * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
+     * @param enable {@code true} to enable, {@code false} to disable.
      * @return {@code true} if the enable/disable operation was
      *         started or is already in the queue.
      */
@@ -429,10 +438,6 @@
         enforceChangePermission();
         if (mWifiHandler == null) return false;
 
-        /*
-         * Remove any enable/disable Wi-Fi messages we may have in the queue
-         * before adding a new one
-         */
         synchronized (mWifiHandler) {
             sWakeLock.acquire();
             sendEnableMessage(enable, true);
@@ -458,40 +463,42 @@
             return false;
         }
 
-        updateWifiState(enable ? WIFI_STATE_ENABLING : WIFI_STATE_DISABLING);
+        setWifiEnabledState(enable ? WIFI_STATE_ENABLING : WIFI_STATE_DISABLING);
 
         if (enable) {
             if (!WifiNative.loadDriver()) {
                 Log.e(TAG, "Failed to load Wi-Fi driver.");
-                updateWifiState(WIFI_STATE_UNKNOWN);
+                setWifiEnabledState(WIFI_STATE_UNKNOWN);
                 return false;
             }
             if (!WifiNative.startSupplicant()) {
                 WifiNative.unloadDriver();
                 Log.e(TAG, "Failed to start supplicant daemon.");
-                updateWifiState(WIFI_STATE_UNKNOWN);
+                setWifiEnabledState(WIFI_STATE_UNKNOWN);
                 return false;
             }
+            registerForBroadcasts();
             mWifiStateTracker.startEventLoop();
         } else {
 
-            // Remove notification (it will no-op if it isn't visible)
+            mContext.unregisterReceiver(mReceiver);
+           // Remove notification (it will no-op if it isn't visible)
             mWifiStateTracker.setNotificationVisible(false, 0, false, 0);
 
             boolean failedToStopSupplicantOrUnloadDriver = false;
             if (!WifiNative.stopSupplicant()) {
                 Log.e(TAG, "Failed to stop supplicant daemon.");
-                updateWifiState(WIFI_STATE_UNKNOWN);
+                setWifiEnabledState(WIFI_STATE_UNKNOWN);
                 failedToStopSupplicantOrUnloadDriver = true;
             }
-            
+
             // We must reset the interface before we unload the driver
             mWifiStateTracker.resetInterface();
-            
+
             if (!WifiNative.unloadDriver()) {
                 Log.e(TAG, "Failed to unload Wi-Fi driver.");
                 if (!failedToStopSupplicantOrUnloadDriver) {
-                    updateWifiState(WIFI_STATE_UNKNOWN);
+                    setWifiEnabledState(WIFI_STATE_UNKNOWN);
                     failedToStopSupplicantOrUnloadDriver = true;
                 }
             }
@@ -505,7 +512,7 @@
         if (persist) {
             persistWifiEnabled(enable);
         }
-        updateWifiState(eventualWifiState);
+        setWifiEnabledState(eventualWifiState);
 
         /*
          * Initialize the hidden networks state and the number of allowed
@@ -519,7 +526,7 @@
         return true;
     }
 
-    private void updateWifiState(int wifiState) {
+    private void setWifiEnabledState(int wifiState) {
         final int previousWifiState = mWifiState;
 
         // Update state
@@ -527,6 +534,7 @@
 
         // Broadcast
         final Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION);
+        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
         intent.putExtra(WifiManager.EXTRA_WIFI_STATE, wifiState);
         intent.putExtra(WifiManager.EXTRA_PREVIOUS_WIFI_STATE, previousWifiState);
         mContext.sendStickyBroadcast(intent);
@@ -551,7 +559,7 @@
      *         {@link WifiManager#WIFI_STATE_ENABLING},
      *         {@link WifiManager#WIFI_STATE_UNKNOWN}
      */
-    public int getWifiState() {
+    public int getWifiEnabledState() {
         enforceAccessPermission();
         return mWifiState;
     }
@@ -1082,9 +1090,7 @@
          */
         removeNetworkIfHidden(netId);
 
-        synchronized (mWifiStateTracker) {
-            return WifiNative.removeNetworkCommand(netId);
-        }
+        return mWifiStateTracker.removeNetwork(netId);
     }
 
     /**
@@ -1221,7 +1227,6 @@
                             if (scanResult != null) {
                               scanResult.level = -scanResultLevel;
                               scanList.add(scanResult);
-                              //if (DBG) Log.d(TAG, "ScanResult: " + scanResult);
                             }
                         } else if (DBG) {
                             Log.w(TAG,
@@ -1344,7 +1349,7 @@
             if (result && mNeedReconfig) {
                 mNeedReconfig = false;
                 result = WifiNative.reloadConfigCommand();
-                
+
                 if (result) {
                     Intent intent = new Intent(WifiManager.NETWORK_IDS_CHANGED_ACTION);
                     mContext.sendBroadcast(intent);
@@ -1446,9 +1451,7 @@
             int stayAwakeConditions =
                     Settings.System.getInt(mContext.getContentResolver(),
                                            Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0);
-            if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
-                /* do nothing, we'll check isAirplaneModeOn later. */
-            } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
+            if (action.equals(Intent.ACTION_SCREEN_ON)) {
                 mAlarmManager.cancel(mIdleIntent);
                 mDeviceIdle = false;
                 mScreenOff = false;
@@ -1543,7 +1546,6 @@
     }
 
     private void sendStartMessage(boolean scanOnlyMode) {
-        if (DBG) Log.d(TAG, "sendStartMessage(" + scanOnlyMode + ")");
         Message.obtain(mWifiHandler, MESSAGE_START_WIFI, scanOnlyMode ? 1 : 0, 0).sendToTarget();
     }
 
@@ -1561,6 +1563,9 @@
         }
 
         synchronized (mWifiHandler) {
+            if (mWifiState == WIFI_STATE_ENABLING && !airplaneMode) {
+                return;
+            }
             if (wifiShouldBeEnabled) {
                 if (wifiShouldBeStarted) {
                     sWakeLock.acquire();
@@ -1573,6 +1578,15 @@
                                     mContext.getContentResolver(),
                                     Settings.Secure.WIFI_MOBILE_DATA_TRANSITION_WAKELOCK_TIMEOUT_MS,
                                     DEFAULT_WAKELOCK_TIMEOUT);
+                    /*
+                     * The following wakelock is held in order to ensure
+                     * that the connectivity manager has time to fail over
+                     * to the mobile data network. The connectivity manager
+                     * releases it once mobile data connectivity has been
+                     * established. If connectivity cannot be established,
+                     * the wakelock is released after wakeLockTimeout
+                     * milliseconds have elapsed.
+                     */
                     sDriverStopWakeLock.acquire();
                     mWifiHandler.sendEmptyMessage(MESSAGE_STOP_WIFI);
                     mWifiHandler.sendEmptyMessageDelayed(MESSAGE_RELEASE_WAKELOCK, wakeLockTimeout);
@@ -1585,18 +1599,15 @@
     }
 
     private void registerForBroadcasts() {
+        IntentFilter intentFilter = new IntentFilter();
         if (isAirplaneSensitive()) {
-            mContext.registerReceiver(mReceiver,
-                new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
+            intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
         }
-        mContext.registerReceiver(mReceiver,
-                new IntentFilter(Intent.ACTION_SCREEN_ON));
-        mContext.registerReceiver(mReceiver,
-                new IntentFilter(Intent.ACTION_SCREEN_OFF));
-        mContext.registerReceiver(mReceiver,
-                new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
-        mContext.registerReceiver(mReceiver,
-                new IntentFilter(ACTION_DEVICE_IDLE));
+        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
+        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
+        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
+        intentFilter.addAction(ACTION_DEVICE_IDLE);
+        mContext.registerReceiver(mReceiver, intentFilter);
     }
     
     private boolean isAirplaneSensitive() {
@@ -1664,7 +1675,7 @@
 
     @Override
     protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
-        if (mContext.checkCallingPermission(android.Manifest.permission.DUMP)
+        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
                 != PackageManager.PERMISSION_GRANTED) {
             pw.println("Permission Denial: can't dump WifiService from from pid="
                     + Binder.getCallingPid()
@@ -1672,11 +1683,14 @@
             return;
         }
         pw.println("Wi-Fi is " + stateName(mWifiState));
-        pw.println("stay-awake conditions: " +
+        pw.println("Stay-awake conditions: " +
                 Settings.System.getInt(mContext.getContentResolver(),
                                        Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0));
         pw.println();
 
+        pw.println("Internal state:");
+        pw.println(mWifiStateTracker);
+        pw.println();
         pw.println("Latest scan results:");
         List<ScanResult> scanResults = mWifiStateTracker.getScanResultsList();
         if (scanResults != null && scanResults.size() != 0) {
@@ -1786,13 +1800,6 @@
             return -1;
         }
 
-        private synchronized void clear() {
-            if (!mList.isEmpty()) {
-                mList.clear();
-                updateWifiState();
-            }
-        }
-
         private void dump(PrintWriter pw) {
             for (WifiLock l : mList) {
                 pw.print("    ");