Close all the channels in use when new SE gets available

All the channels in use should be closed if new SE gets available as the
existing logical channels were opened in accordance with the access
rules previously retrieved.

Bug: 77771494
Test: All the existing channels in use are closed as expected.

Change-Id: Iaca08bdfbdae9b1ba5a1cb863483758242272536
diff --git a/src/com/android/se/Terminal.java b/src/com/android/se/Terminal.java
index f92ff88..b3bedba 100644
--- a/src/com/android/se/Terminal.java
+++ b/src/com/android/se/Terminal.java
@@ -89,6 +89,11 @@
                         mAccessControlEnforcer.reset();
                     }
                 } else {
+                    // If any logical channel in use is in the channel list, it should be closed
+                    // because the access control enfocer allowed to open it by checking the access
+                    // rules retrieved before. Now we are going to retrieve the rules again and
+                    // the new rules can be different from the previous ones.
+                    closeChannels();
                     try {
                         initializeAccessControl();
                     } catch (Exception e) {
@@ -213,14 +218,13 @@
     }
 
     /**
-     * This method is called in SecureElementService:onDestroy to clean up
-     * all open channels.
+     * Cleans up all the channels in use.
      */
     public synchronized void closeChannels() {
         Collection<Channel> col = mChannels.values();
         Channel[] channelList = col.toArray(new Channel[col.size()]);
         for (Channel channel : channelList) {
-            closeChannel(channel);
+            channel.close();
         }
     }