Bring up L2capNetwork for client network request

This change brings up an L2capNetwork when there is a client network
request and the BluetoothSocket connects successfully.

Any error on that network will mark all associated NetworkRequests as
unfulfillable triggering an onUnavailable() callback on them. This may
or may not be expected, because the network might have come up before
the onUnavailable() is sent.

Fixing this should go hand-in-hand with instituting some sort of retry
logic. Without retries, if these callbacks were to just tear down the
network (triggering onLost()), an app might not be able to trigger a
retry using the same PSM and remote MAC parameters, because a second
request for the same parameters might exist (so the request is never
fully removed). This means, that without any sort of retry logic,
tearing down all active requests for this network is the best option.

Test: TH
Change-Id: I70196aa4505aa0a8cbf815764de8d977c1bc70cc
diff --git a/service/src/com/android/server/L2capNetworkProvider.java b/service/src/com/android/server/L2capNetworkProvider.java
index 6948e58..e3becd6 100644
--- a/service/src/com/android/server/L2capNetworkProvider.java
+++ b/service/src/com/android/server/L2capNetworkProvider.java
@@ -437,6 +437,8 @@
             public final List<NetworkRequest> requests = new ArrayList<>();
             // TODO: add support for retries.
             public final ConnectThread connectThread;
+            @Nullable
+            public L2capNetwork network;
 
             public ClientRequestInfo(NetworkRequest request, ConnectThread connectThread) {
                 this.specifier = (L2capNetworkSpecifier) request.getNetworkSpecifier();
@@ -494,8 +496,29 @@
             final ClientRequestInfo cri = mClientNetworkRequests.get(specifier);
             if (cri == null) return false;
 
-            // TODO: implement createClientNetwork
-            return false;
+            final NetworkCapabilities caps = new NetworkCapabilities.Builder(CAPABILITIES)
+                    .setNetworkSpecifier(specifier)
+                    .build();
+
+            final L2capNetwork network = createL2capNetwork(socket, caps,
+                    new L2capNetwork.ICallback() {
+                // TODO: do not send onUnavailable() after the network has become available. The
+                // right thing to do here is to tearDown the network (if it still exists, because
+                // note that the request might have already been removed in the meantime, so
+                // `network` cannot be used directly.
+                @Override
+                public void onError(L2capNetwork network) {
+                    declareAllNetworkRequestsUnfulfillable(specifier);
+                }
+                @Override
+                public void onNetworkUnwanted(L2capNetwork network) {
+                    declareAllNetworkRequestsUnfulfillable(specifier);
+                }
+            });
+            if (network == null) return false;
+
+            cri.network = network;
+            return true;
         }
 
         private boolean isValidL2capSpecifier(@Nullable NetworkSpecifier spec) {
@@ -613,6 +636,10 @@
                 // #abort().
                 cri.connectThread.abort();
             }
+
+            if (cri.network != null) {
+                cri.network.tearDown();
+            }
         }
 
         private void declareAllNetworkRequestsUnfulfillable(L2capNetworkSpecifier specifier) {