Add AttachEventLoop() method
authorSung-jae Park <nicesj.park@samsung.com>
Tue, 19 Oct 2021 02:03:13 +0000 (11:03 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Thu, 28 Oct 2021 10:42:02 +0000 (19:42 +0900)
[Problem] There are duplicated codes
[Solution] Extract them to a method and then move it to the Parent Class

 * AttachEventLoop() can be moved to the NativeInterface class later

Change-Id: Ib89bae76bf89745ea10cc102a663bf2d97ce21b3
Signed-off-by: Sung-jae Park <nicesj.park@samsung.com>
subprojects/libbeyond-android/src/main/jni/authenticator/beyond-authenticator_jni.cc
subprojects/libbeyond-android/src/main/jni/authenticator/beyond-authenticator_jni.h
subprojects/libbeyond-android/src/main/jni/discovery/beyond-discovery_jni.cc
subprojects/libbeyond-android/src/main/jni/discovery/beyond-discovery_jni.h

index e8849537f6946cf97c102f3cef97d558a3312042..973de5c32ffe607f179488901b38122fb058a742 100644 (file)
@@ -414,28 +414,12 @@ long AuthenticatorNativeInterface::Java_com_samsung_android_beyond_Authenticator
         }
 
         if (inst->authenticator->GetHandle() >= 0) {
-            inst->looper = ALooper_forThread();
-            if (inst->looper == nullptr) {
-                ErrPrint("There is no android looper found");
-                delete inst;
-                inst = nullptr;
-                break;
-            }
-
-            ALooper_acquire(inst->looper);
-
-            int ret = ALooper_addFd(inst->looper,
-                                    inst->authenticator->GetHandle(),
-                                    ALOOPER_POLL_CALLBACK,
-                                    ALOOPER_EVENT_INPUT,
-                                    static_cast<ALooper_callbackFunc>(Authenticator_eventHandler),
-                                    static_cast<void *>(inst));
+            int ret = inst->AttachEventLoop();
             if (ret < 0) {
-                ErrPrint("Failed to add event handler");
                 delete inst;
                 inst = nullptr;
                 break;
-            } // otherwise, the ret is 1 if succeed
+            }
         } else {
             DbgPrint("Authenticator module does not support asynchronous mode");
         }
@@ -449,6 +433,29 @@ long AuthenticatorNativeInterface::Java_com_samsung_android_beyond_Authenticator
     return reinterpret_cast<long>(inst);
 }
 
+int AuthenticatorNativeInterface::AttachEventLoop(void)
+{
+    looper = ALooper_forThread();
+    if (looper == nullptr) {
+        ErrPrint("There is no android looper found");
+        return -ENOTSUP;
+    }
+
+    ALooper_acquire(looper);
+
+    int ret = ALooper_addFd(looper,
+                            authenticator->GetHandle(),
+                            ALOOPER_POLL_CALLBACK,
+                            ALOOPER_EVENT_INPUT,
+                            static_cast<ALooper_callbackFunc>(Authenticator_eventHandler),
+                            static_cast<void *>(this));
+    if (ret < 0) {
+        ErrPrint("Failed to add event handler");
+    } // otherwise, the ret is 1 if succeed
+
+    return ret;
+}
+
 int AuthenticatorNativeInterface::Java_com_samsung_android_beyond_Authenticator_configure(JNIEnv *env, jobject thiz, jlong _inst, jchar type, jlong objInst)
 {
     AuthenticatorNativeInterface *inst = reinterpret_cast<AuthenticatorNativeInterface *>(_inst);
index 60056f5da89de1e1fe9cfe006726a770ce6f7882..7cb039cb43f572a5ca30626aecd48577a717892d 100644 (file)
@@ -68,6 +68,7 @@ private: // JNI Cache
 
 private:
     int InvokeEventListener(JNIEnv *env, jobject thiz, int eventType, void *eventData);
+    int AttachEventLoop(void);
 
 private:
     beyond::Authenticator *authenticator;
index 6330f99f3d383cf5dda26c2a531264153e6f32d3..0e2abdd842c7a866debd2cb57455f518c54d0ddd 100644 (file)
@@ -526,28 +526,12 @@ jlong DiscoveryNativeInterface::Java_com_samsung_android_beyond_discovery_Discov
         }
 
         if (inst->discovery->GetHandle() >= 0) {
-            inst->looper = ALooper_forThread();
-            if (inst->looper == nullptr) {
-                ErrPrint("There is no android looper found");
-                delete inst;
-                inst = nullptr;
-                break;
-            }
-
-            ALooper_acquire(inst->looper);
-
-            int ret = ALooper_addFd(inst->looper,
-                                    inst->discovery->GetHandle(),
-                                    ALOOPER_POLL_CALLBACK,
-                                    ALOOPER_EVENT_INPUT,
-                                    static_cast<ALooper_callbackFunc>(Discovery_eventHandler),
-                                    static_cast<void *>(inst));
+            int ret = inst->AttachEventLoop();
             if (ret < 0) {
-                ErrPrint("Failed to add event handler");
                 delete inst;
                 inst = nullptr;
                 break;
-            } // otherwise, the ret is 1 if succeed
+            }
         }
     } while (0);
 
@@ -559,6 +543,29 @@ jlong DiscoveryNativeInterface::Java_com_samsung_android_beyond_discovery_Discov
     return reinterpret_cast<jlong>(inst);
 }
 
+int DiscoveryNativeInterface::AttachEventLoop(void)
+{
+    looper = ALooper_forThread();
+    if (looper == nullptr) {
+        ErrPrint("There is no android looper found");
+        return -ENOTSUP;
+    }
+
+    ALooper_acquire(looper);
+
+    int ret = ALooper_addFd(looper,
+                            discovery->GetHandle(),
+                            ALOOPER_POLL_CALLBACK,
+                            ALOOPER_EVENT_INPUT,
+                            static_cast<ALooper_callbackFunc>(Discovery_eventHandler),
+                            static_cast<void *>(this));
+    if (ret < 0) {
+        ErrPrint("Failed to add event handler");
+    } // otherwise, the ret is 1 if succeed
+
+    return ret;
+}
+
 void DiscoveryNativeInterface::Java_com_samsung_android_beyond_discovery_Discovery_destroy(JNIEnv *env, jclass klass, jlong instance)
 {
     if (instance == 0) {
index c67a6a89515a0bd37575d98e4d62820b10100869..089492ba43080389472d86aba1004b01f36ac95d 100644 (file)
@@ -81,6 +81,7 @@ private: // JNI Cache
 
 private:
     int InvokeEventListener(JNIEnv *env, jobject thiz, int eventType, void *eventData);
+    int AttachEventLoop(void);
 
 private:
     beyond::Discovery *discovery;