P2p fixes

- NPE fixes
- Remove group on CTRL-EVENT-DISCONNECTED since the supplicant sometimes misses
the group removed event
- Fix the pattern match since we sometimes end up creating group without passphrase

Bug: 7119140
Bug: 7210558
Change-Id: I2cf0be073aaaaa9b931ea9a79b0bf4bd6a2b32ff
diff --git a/wifi/java/android/net/wifi/p2p/WifiP2pGroup.java b/wifi/java/android/net/wifi/p2p/WifiP2pGroup.java
index bc492b3..cf7604d 100644
--- a/wifi/java/android/net/wifi/p2p/WifiP2pGroup.java
+++ b/wifi/java/android/net/wifi/p2p/WifiP2pGroup.java
@@ -68,7 +68,7 @@
         "ssid=\"(.+)\" " +
         "freq=(\\d+) " +
         "(?:psk=)?([0-9a-fA-F]{64})?" +
-        "(?:passphrase=)?(?:\"(.{8,63})\")? " +
+        "(?:passphrase=)?(?:\"(.{0,63})\")? " +
         "go_dev_addr=((?:[0-9a-f]{2}:){5}[0-9a-f]{2})" +
         " ?(\\[PERSISTENT\\])?"
     );
diff --git a/wifi/java/android/net/wifi/p2p/WifiP2pService.java b/wifi/java/android/net/wifi/p2p/WifiP2pService.java
index 13abb79..b317f3f 100644
--- a/wifi/java/android/net/wifi/p2p/WifiP2pService.java
+++ b/wifi/java/android/net/wifi/p2p/WifiP2pService.java
@@ -527,6 +527,11 @@
                 case WifiMonitor.NETWORK_CONNECTION_EVENT:
                 case WifiMonitor.NETWORK_DISCONNECTION_EVENT:
                 case WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT:
+                case WifiMonitor.AUTHENTICATION_FAILURE_EVENT:
+                case WifiMonitor.WPS_SUCCESS_EVENT:
+                case WifiMonitor.WPS_FAIL_EVENT:
+                case WifiMonitor.WPS_OVERLAP_EVENT:
+                case WifiMonitor.WPS_TIMEOUT_EVENT:
                 case WifiMonitor.P2P_GROUP_REMOVED_EVENT:
                 case WifiMonitor.P2P_DEVICE_FOUND_EVENT:
                 case WifiMonitor.P2P_DEVICE_LOST_EVENT:
@@ -537,6 +542,9 @@
                 case PEER_CONNECTION_USER_ACCEPT:
                 case PEER_CONNECTION_USER_REJECT:
                 case GROUP_CREATING_TIMED_OUT:
+                case DhcpStateMachine.CMD_PRE_DHCP_ACTION:
+                case DhcpStateMachine.CMD_POST_DHCP_ACTION:
+                case DhcpStateMachine.CMD_ON_QUIT:
                     break;
                     /* unexpected group created, remove */
                 case WifiMonitor.P2P_GROUP_STARTED_EVENT:
@@ -1351,34 +1359,18 @@
                     if (mWifiNative.p2pGroupRemove(mGroup.getInterface())) {
                         replyToMessage(message, WifiP2pManager.REMOVE_GROUP_SUCCEEDED);
                     } else {
+                        handleGroupRemoved();
+                        transitionTo(mInactiveState);
                         replyToMessage(message, WifiP2pManager.REMOVE_GROUP_FAILED,
                                 WifiP2pManager.ERROR);
                     }
                     break;
+                /* The supplicant misses the group removed event at times and just
+                 * sends a network disconnect event */
+                case WifiMonitor.NETWORK_DISCONNECTION_EVENT:
                 case WifiMonitor.P2P_GROUP_REMOVED_EVENT:
                     if (DBG) logd(getName() + " group removed");
-                    Collection <WifiP2pDevice> devices = mGroup.getClientList();
-                    boolean changed = false;
-                    for (WifiP2pDevice d : mPeers.getDeviceList()) {
-                        if (devices.contains(d) || mGroup.getOwner().equals(d)) {
-                            d.status = WifiP2pDevice.AVAILABLE;
-                            changed = true;
-                        }
-                    }
-
-                    if (mGroup.isGroupOwner()) {
-                        stopDhcpServer(mGroup.getInterface());
-                    } else {
-                        if (DBG) logd("stop DHCP client");
-                        mDhcpStateMachine.sendMessage(DhcpStateMachine.CMD_STOP_DHCP);
-                        mDhcpStateMachine.doQuit();
-                        mDhcpStateMachine = null;
-                    }
-
-                    mGroup = null;
-                    mWifiNative.p2pFlush();
-                    mServiceDiscReqId = null;
-                    if (changed) sendP2pPeersChangedBroadcast();
+                    handleGroupRemoved();
                     transitionTo(mInactiveState);
                     break;
                 case WifiMonitor.P2P_DEVICE_LOST_EVENT:
@@ -1734,9 +1726,12 @@
      */
     private void updatePersistentNetworks() {
         String listStr = mWifiNative.listNetworks();
+        if (listStr == null) return;
 
         boolean isSaveRequired = false;
         String[] lines = listStr.split("\n");
+        if (lines == null) return;
+
         // Skip the first line, which is a header
         for (int i = 1; i < lines.length; i++) {
             String[] result = lines[i].split("\t");
@@ -2101,6 +2096,31 @@
         sendMessage(WifiP2pManager.DISCOVER_PEERS);
     }
 
+    private void handleGroupRemoved() {
+        Collection <WifiP2pDevice> devices = mGroup.getClientList();
+        boolean changed = false;
+        for (WifiP2pDevice d : mPeers.getDeviceList()) {
+            if (devices.contains(d) || mGroup.getOwner().equals(d)) {
+                d.status = WifiP2pDevice.AVAILABLE;
+                changed = true;
+            }
+        }
+
+        if (mGroup.isGroupOwner()) {
+            stopDhcpServer(mGroup.getInterface());
+        } else {
+            if (DBG) logd("stop DHCP client");
+            mDhcpStateMachine.sendMessage(DhcpStateMachine.CMD_STOP_DHCP);
+            mDhcpStateMachine.doQuit();
+            mDhcpStateMachine = null;
+        }
+
+        mGroup = null;
+        mWifiNative.p2pFlush();
+        mServiceDiscReqId = null;
+        if (changed) sendP2pPeersChangedBroadcast();
+    }
+
     //State machine initiated requests can have replyTo set to null indicating
     //there are no recipients, we ignore those reply actions
     private void replyToMessage(Message msg, int what) {