Snap for 6324712 from c3f935b630910cb90e96da1484f02ef49f9e1013 to qt-qpr3-release

Change-Id: Ia0730265e5ae4cc02b0f93195bb4fd1cb7ec3259
diff --git a/src/com/android/se/Terminal.java b/src/com/android/se/Terminal.java
index d21c323..521d696 100644
--- a/src/com/android/se/Terminal.java
+++ b/src/com/android/se/Terminal.java
@@ -247,22 +247,22 @@
         if (channel == null) {
             return;
         }
-        if (mIsConnected) {
-            try {
-                byte status = mSEHal.closeChannel((byte) channel.getChannelNumber());
-                /* For Basic Channels, errors are expected.
-                 * Underlying implementations use this call as an indication when there
-                 * aren't any users actively using the channel, and the chip can go
-                 * into low power state.
-                 */
-                if (!channel.isBasicChannel() && status != SecureElementStatus.SUCCESS) {
-                    Log.e(mTag, "Error closing channel " + channel.getChannelNumber());
-                }
-            } catch (RemoteException e) {
-                Log.e(mTag, "Exception in closeChannel() " + e);
-            }
-        }
         synchronized (mLock) {
+            if (mIsConnected) {
+                try {
+                    byte status = mSEHal.closeChannel((byte) channel.getChannelNumber());
+                    /* For Basic Channels, errors are expected.
+                     * Underlying implementations use this call as an indication when there
+                     * aren't any users actively using the channel, and the chip can go
+                     * into low power state.
+                     */
+                    if (!channel.isBasicChannel() && status != SecureElementStatus.SUCCESS) {
+                        Log.e(mTag, "Error closing channel " + channel.getChannelNumber());
+                    }
+                } catch (RemoteException e) {
+                    Log.e(mTag, "Exception in closeChannel() " + e);
+                }
+            }
             mChannels.remove(channel.getChannelNumber(), channel);
             if (mChannels.get(channel.getChannelNumber()) != null) {
                 Log.e(mTag, "Removing channel failed");