FaceDetect: Catch linker errors during initialization

Author: Christopher N. Hesse <raymanfx@gmail.com>
Date:   Wed Apr 26 18:21:39 2017 +0200

    FaceDetect: Catch linker errors during initialization

    Right now we only check if we can load the native
    functions from the JNI, but unsatisfied linker errors
    can still occur even if the lib is present.

    Change-Id: Id2ac36374eb8341b278949d120dc7674d6d62691
    Signed-off-by: Alex Naidis <alex.naidis@linux.com>

Author: Anas Karbila <anaskarbila@gmail.com>
Date:   Wed May 17 00:24:01 2017 +0900

    FaceDetect: Catch more linker errors during initialization

    Change-Id: I24d6afe0d3ba625aa8f6d42d9755a4553bad693d
    Signed-off-by: Alex Naidis <alex.naidis@linux.com>

Change-Id: I0fdde6b24dd75916449dcf1c7f3504130d958026
diff --git a/src/com/thundersoft/hz/selfportrait/detect/FaceDetect.java b/src/com/thundersoft/hz/selfportrait/detect/FaceDetect.java
index 90ccc59..a37ca09 100644
--- a/src/com/thundersoft/hz/selfportrait/detect/FaceDetect.java
+++ b/src/com/thundersoft/hz/selfportrait/detect/FaceDetect.java
@@ -42,7 +42,12 @@
      * initialize method,MUST called at first time.
      */
     public void initialize() {
-        mHandle = native_create();
+        try {
+            mHandle = native_create();
+        } catch (UnsatisfiedLinkError e) {
+            e.printStackTrace();
+            Log.e(TAG, "could not link native handle for ts_detected_face_jni library!");
+        }
     }
 
     public boolean isLibLoaded() {
@@ -54,7 +59,9 @@
      */
 
     public void uninitialize() {
-        native_destroy(mHandle);
+        if (mHandle != 0) {
+            native_destroy(mHandle);
+        }
     }
 
     /**
@@ -65,7 +72,18 @@
      * @return FaceInfo array if success, otherwise return null.
      */
     public FaceInfo[] dectectFeatures(Bitmap bmp) {
-        int count = native_detect(mHandle, bmp);
+        // check if the initialization failed
+        if (mHandle == 0) {
+            return null;
+        }
+
+        int count = 0;
+        try {
+            count = native_detect(mHandle, bmp);
+        } catch (UnsatisfiedLinkError e) {
+            e.printStackTrace();
+            Log.e(TAG, "could not link native handle for ts_detected_face_jni library!");
+        }
         if (count < 1) {
             return null;
         }