Clean up: Update variable names to comply with spec of ApplicationInfo.

ApplicationInfo.sourceDir may or may not be apk file name.  It can be a directory as well.
The spec just says it's "Full path to the location of this package".

Also, added error handling in loadDictionary().

Change-Id: I5e64d0aba4b1ec7634f4b3ac5537e7a774433ece
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
index 9f934c6..f7e6767 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
@@ -55,7 +55,7 @@
         try {
             System.loadLibrary("jni_latinime");
         } catch (UnsatisfiedLinkError ule) {
-            Log.e("BinaryDictionary", "Could not load native library jni_latinime");
+            Log.e(TAG, "Could not load native library jni_latinime");
         }
     }
 
@@ -71,7 +71,7 @@
         mDicTypeId = dicTypeId;
     }
 
-    private native int openNative(String apkFileName, long dictOffset, long dictSize,
+    private native int openNative(String sourceDir, long dictOffset, long dictSize,
             int typedLetterMultiplier, int fullWordMultiplier, int maxWordLength,
             int maxWords, int maxAlternatives);
     private native void closeNative(int dict);
@@ -84,12 +84,21 @@
             int maxWordLength, int maxBigrams, int maxAlternatives);
 
     private final void loadDictionary(Context context, int resId) {
-        final AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
-        mNativeDict = openNative(context.getApplicationInfo().sourceDir,
-                afd.getStartOffset(), afd.getLength(),
-                TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER,
-                MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES);
-        mDictLength = afd.getLength();
+        try {
+            final AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
+            if (afd == null) {
+                Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
+                return;
+            }
+            mNativeDict = openNative(context.getApplicationInfo().sourceDir,
+                    afd.getStartOffset(), afd.getLength(),
+                    TYPED_LETTER_MULTIPLIER, FULL_WORD_FREQ_MULTIPLIER,
+                    MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES);
+            mDictLength = afd.getLength();
+        } catch (android.content.res.Resources.NotFoundException e) {
+            Log.e(TAG, "Could not find the resource. resId=" + resId);
+            return;
+        }
     }
 
     @Override
@@ -165,7 +174,7 @@
     }
 
     public long getSize() {
-        return mDictLength; // This value is initialized on the call to openNative()
+        return mDictLength; // This value is initialized in loadDictionary()
     }
 
     @Override
diff --git a/native/Android.mk b/native/Android.mk
index 38465ac..a8fe06d 100644
--- a/native/Android.mk
+++ b/native/Android.mk
@@ -32,7 +32,7 @@
 LOCAL_MODULE_TAGS := user
 
 ifeq ($(FLAG_DBG), true)
-    $(warning "Making debug build.")
+    $(warning Making debug version of native library)
     LOCAL_CFLAGS += -DFLAG_DBG
     LOCAL_SHARED_LIBRARIES := libcutils libutils
 endif
diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
index 6374292..6e4e971 100644
--- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
+++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
@@ -50,14 +50,14 @@
 }
 
 static jint latinime_BinaryDictionary_open(JNIEnv *env, jobject object,
-        jstring apkFileName, jlong dictOffset, jlong dictSize,
+        jstring sourceDir, jlong dictOffset, jlong dictSize,
         jint typedLetterMultiplier, jint fullWordMultiplier, jint maxWordLength, jint maxWords,
         jint maxAlternatives) {
     PROF_OPEN;
     PROF_START(66);
-    const char *apkFileNameChars = env->GetStringUTFChars(apkFileName, NULL);
-    if (apkFileNameChars == NULL) {
-        LOGE("DICT: Can't get apk file name");
+    const char *sourceDirChars = env->GetStringUTFChars(sourceDir, NULL);
+    if (sourceDirChars == NULL) {
+        LOGE("DICT: Can't get sourceDir string");
         return 0;
     }
     int fd = 0;
@@ -65,9 +65,9 @@
     int adjust = 0;
 #ifdef USE_MMAP_FOR_DICTIONARY
     /* mmap version */
-    fd = open(apkFileNameChars, O_RDONLY);
+    fd = open(sourceDirChars, O_RDONLY);
     if (fd < 0) {
-        LOGE("DICT: Can't open apk file. errno=%d", errno);
+        LOGE("DICT: Can't open sourceDir. sourceDirChars=%s errno=%d", sourceDirChars, errno);
         return 0;
     }
     int pagesize = getpagesize();
@@ -76,16 +76,16 @@
     int adjDictSize = dictSize + adjust;
     dictBuf = mmap(NULL, sizeof(char) * adjDictSize, PROT_READ, MAP_PRIVATE, fd, adjDictOffset);
     if (dictBuf == MAP_FAILED) {
-        LOGE("DICT: Can't mmap dictionary file. errno=%d", errno);
+        LOGE("DICT: Can't mmap dictionary. errno=%d", errno);
         return 0;
     }
     dictBuf = (void *)((char *)dictBuf + adjust);
 #else // USE_MMAP_FOR_DICTIONARY
     /* malloc version */
     FILE *file = NULL;
-    file = fopen(apkFileNameChars, "rb");
+    file = fopen(sourceDirChars, "rb");
     if (file == NULL) {
-        LOGE("DICT: Can't fopen apk file. errno=%d", errno);
+        LOGE("DICT: Can't fopen sourceDir. sourceDirChars=%s errno=%d", sourceDirChars, errno);
         return 0;
     }
     dictBuf = malloc(sizeof(char) * dictSize);
@@ -109,7 +109,7 @@
         return 0;
     }
 #endif // USE_MMAP_FOR_DICTIONARY
-    env->ReleaseStringUTFChars(apkFileName, apkFileNameChars);
+    env->ReleaseStringUTFChars(sourceDir, sourceDirChars);
 
     if (!dictBuf) {
         LOGE("DICT: dictBuf is null");