SecureElementService may be unavailable when NfcService is started

Currently NfcService attempts to get an interface to
SecureElementService only in its constructor, so there is no chance to
do it later again if it fails. Introducing a lazy initialization
mechanism is required.

Bug: 111775064
Bug: 110121800
Test: Confirmed that NfcService gets SEService even if it failed once.

Change-Id: I2b3c873d7661cde63af8c9f8c05945c3568f012f
(cherry picked from commit a9ef080f9964f46d4983bf7637a2c52a6e0eadd2)
diff --git a/src/com/android/nfc/NfcService.java b/src/com/android/nfc/NfcService.java
old mode 100755
new mode 100644
index 18589d8..d6d4fd1
--- a/src/com/android/nfc/NfcService.java
+++ b/src/com/android/nfc/NfcService.java
@@ -514,6 +514,14 @@
                 Context.SECURE_ELEMENT_SERVICE));
     }
 
+    private boolean isSEServiceAvailable() {
+        if (mSEService == null) {
+            mSEService = ISecureElementService.Stub.asInterface(ServiceManager.getService(
+                    Context.SECURE_ELEMENT_SERVICE));
+        }
+        return (mSEService != null);
+    }
+
     void initSoundPool() {
         synchronized (this) {
             if (mSoundPool == null) {
@@ -2241,7 +2249,7 @@
         }
 
         private void sendOffHostTransactionEvent(byte[] aid, byte[] data, byte[] readerByteArray) {
-            if (mSEService == null || mNfcEventInstalledPackages.isEmpty()) {
+            if (!isSEServiceAvailable() || mNfcEventInstalledPackages.isEmpty()) {
                 return;
             }
 
@@ -2281,7 +2289,7 @@
 
         /* Returns the list of packages that have access to NFC Events on any SE */
         private ArrayList<String> getSEAccessAllowedPackages() {
-            if (mSEService == null || mNfcEventInstalledPackages.isEmpty()) {
+            if (!isSEServiceAvailable() || mNfcEventInstalledPackages.isEmpty()) {
                 return null;
             }
             String[] readers = null;