LatinIME: Add fallback loader for JNI lib

Prefer the JNI lib with gesture typing support.
If not available / installed fall back to the standard one without
geture typing.

Change-Id: I70a5f43386a9c72809a70bd286f3702378d6f9be

LatinIME: Only enable gesture mode if gesture lib is available

 * Prevents keyboard FC when gapps is not loaded.

Change-Id: I0d48e1cd80534513205f61913c798b48573ea38c

LatinIME: Remove gesture preference if gesture lib is unavailable

Gesture typing won't work without the gesture library,
so remove the preference to not mislead users
into thinking that gesture typing is supported.

Change-Id: I387ae83fd174b57fc48fe7bcfc37e3010ce89b12
diff --git a/java/src/com/android/inputmethod/latin/define/JniLibName.java b/java/src/com/android/inputmethod/latin/define/JniLibName.java
index abfc36d..83ed5eb 100644
--- a/java/src/com/android/inputmethod/latin/define/JniLibName.java
+++ b/java/src/com/android/inputmethod/latin/define/JniLibName.java
@@ -22,4 +22,5 @@
     }
 
     public static final String JNI_LIB_NAME = "jni_latinime";
+    public static final String JNI_LIB_NAME2 = "jni_latinimegoogle";
 }
diff --git a/java/src/com/android/inputmethod/latin/settings/Settings.java b/java/src/com/android/inputmethod/latin/settings/Settings.java
index 940f1bd..803b3bd 100644
--- a/java/src/com/android/inputmethod/latin/settings/Settings.java
+++ b/java/src/com/android/inputmethod/latin/settings/Settings.java
@@ -31,6 +31,7 @@
 import com.android.inputmethod.latin.R;
 import com.android.inputmethod.latin.common.StringUtils;
 import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils;
+import com.android.inputmethod.latin.utils.JniUtils;
 import com.android.inputmethod.latin.utils.ResourceUtils;
 import com.android.inputmethod.latin.utils.RunInLocale;
 import com.android.inputmethod.latin.utils.StatsUtils;
@@ -48,6 +49,7 @@
     public static final String SCREEN_ACCOUNTS = "screen_accounts";
     public static final String SCREEN_THEME = "screen_theme";
     public static final String SCREEN_DEBUG = "screen_debug";
+    public static final String SCREEN_GESTURE = "screen_gesture";
     // In the same order as xml/prefs.xml
     public static final String PREF_AUTO_CAP = "auto_cap";
     public static final String PREF_VIBRATE_ON = "vibrate_on";
@@ -230,6 +232,9 @@
     }
 
     public static boolean readFromBuildConfigIfGestureInputEnabled(final Resources res) {
+        if (!JniUtils.sHaveGestureLib) {
+            return false;
+        }
         return res.getBoolean(R.bool.config_gesture_input_enabled_by_build_config);
     }
 
diff --git a/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java
index 874f221..ca7bd3a 100644
--- a/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java
+++ b/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java
@@ -31,6 +31,7 @@
 import com.android.inputmethod.latin.define.ProductionFlags;
 import com.android.inputmethod.latin.utils.ApplicationUtils;
 import com.android.inputmethod.latin.utils.FeedbackUtils;
+import com.android.inputmethod.latin.utils.JniUtils;
 import com.android.inputmethodcommon.InputMethodSettingsFragment;
 
 public final class SettingsFragment extends InputMethodSettingsFragment {
@@ -55,6 +56,10 @@
             final Preference accountsPreference = findPreference(Settings.SCREEN_ACCOUNTS);
             preferenceScreen.removePreference(accountsPreference);
         }
+        if (!JniUtils.sHaveGestureLib) {
+            final Preference gesturePreference = findPreference(Settings.SCREEN_GESTURE);
+            preferenceScreen.removePreference(gesturePreference);
+        }
     }
 
     @Override
diff --git a/java/src/com/android/inputmethod/latin/utils/JniUtils.java b/java/src/com/android/inputmethod/latin/utils/JniUtils.java
index e7fdafa..458c169 100644
--- a/java/src/com/android/inputmethod/latin/utils/JniUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/JniUtils.java
@@ -23,11 +23,18 @@
 public final class JniUtils {
     private static final String TAG = JniUtils.class.getSimpleName();
 
+    public static boolean sHaveGestureLib = false;
     static {
         try {
-            System.loadLibrary(JniLibName.JNI_LIB_NAME);
-        } catch (UnsatisfiedLinkError ule) {
-            Log.e(TAG, "Could not load native library " + JniLibName.JNI_LIB_NAME, ule);
+            System.loadLibrary(JniLibName.JNI_LIB_NAME2);
+            sHaveGestureLib = true;
+        } catch (UnsatisfiedLinkError ue) {
+            Log.e(TAG, "Could not load native library " + JniLibName.JNI_LIB_NAME2, ue);
+            try {
+                System.loadLibrary(JniLibName.JNI_LIB_NAME);
+            } catch (UnsatisfiedLinkError ule) {
+                Log.e(TAG, "Could not load native library " + JniLibName.JNI_LIB_NAME, ule);
+            }
         }
     }