Resist SIM logical channel leaking when clients crash

When apps crashed with a logical channel opened, the channel
can not be reused by other apps, causing a leak.

With monitoring liveness of the cliets who open logical
channels. Telephony is able to close the channels proactively
when detecting clients die.

Bug: 197658986
Test: atest IccOpenLogicalChannelRequestTest CarrierApiTest
Change-Id: I278169759f50b5f92af79910f77c9df575dc5d26
Merged-In: Ib511f2d05094f58661f32f652a87034df174d21d
(cherry picked from commit 3b77c4c849aa7c6b26f2a3a206c448aa0b098254)
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 37dde95..003b639 100755
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -749,6 +749,12 @@
                         }
                         openChannelResp = new IccOpenLogicalChannelResponse(channelId,
                             IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
+
+                        uiccPort = getUiccPortFromRequest(request);
+                        IccLogicalChannelRequest channelRequest =
+                                (IccLogicalChannelRequest) request.argument;
+                        channelRequest.channel = channelId;
+                        uiccPort.onLogicalChannelOpened(channelRequest);
                     } else {
                         if (ar.result == null) {
                             loge("iccOpenLogicalChannel: Empty response");
@@ -791,6 +797,9 @@
                     request = (MainThreadRequest) ar.userObj;
                     if (ar.exception == null) {
                         request.result = true;
+                        uiccPort = getUiccPortFromRequest(request);
+                        final int channelId = (Integer) request.argument;
+                        uiccPort.onLogicalChannelClosed(channelId);
                     } else {
                         request.result = false;
                         if (ar.exception instanceof CommandException) {