Consolidate main dictionary files.

This change is a preparation for upcoming optimizations on dictionary file loading.
* We can consolidate dictionary files because we are no longer relying on Asset Manager.
* Stopping compressing dictionary files as planning to use mmap() on the region in the apk file.
* Probably we won't rely on Asset Manager.  Instead we'll probably use offset and size obtained from AssetFileDescriptor.

Change-Id: Id57dce512fd3d2397a58628f8264bd824194da76
diff --git a/java/Android.mk b/java/Android.mk
index be3b0b9..60c321a 100644
--- a/java/Android.mk
+++ b/java/Android.mk
@@ -13,7 +13,8 @@
 
 LOCAL_STATIC_JAVA_LIBRARIES := android-common
 
-#LOCAL_AAPT_FLAGS := -0 .dict
+# Do not compress dictionary files to mmap dict data runtime
+LOCAL_AAPT_FLAGS := -0 .dict
 
 # Include all the resources regardless of system supported locales
 LOCAL_AAPT_INCLUDE_ALL_RESOURCES := true
diff --git a/java/res/xml/dictionary.xml b/java/res/xml/dictionary.xml
deleted file mode 100644
index 7b770a8..0000000
--- a/java/res/xml/dictionary.xml
+++ /dev/null
@@ -1,23 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-/*
-**
-** Copyright 2010, The Android Open Source Project
-**
-** Licensed under the Apache License, Version 2.0 (the "License");
-** you may not use this file except in compliance with the License.
-** You may obtain a copy of the License at
-**
-**     http://www.apache.org/licenses/LICENSE-2.0
-**
-** Unless required by applicable law or agreed to in writing, software
-** distributed under the License is distributed on an "AS IS" BASIS,
-** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-** See the License for the specific language governing permissions and
-** limitations under the License.
-*/
--->
-
-<dictionary>
-    <part name = "main" />
-</dictionary>
\ No newline at end of file
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
index 961b49f..4bb64ee 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
@@ -71,8 +71,8 @@
      * @param context application context for reading resources
      * @param resId the resource containing the raw binary dictionary
      */
-    public BinaryDictionary(Context context, int[] resId, int dicTypeId) {
-        if (resId != null && resId.length > 0 && resId[0] != 0) {
+    public BinaryDictionary(Context context, int resId, int dicTypeId) {
+        if (resId != 0) {
             loadDictionary(context, resId);
         }
         mDicTypeId = dicTypeId;
@@ -104,30 +104,21 @@
             int fullWordMultiplier, int maxWordLength, int maxWords, int maxAlternatives);
     private native void closeNative(int dict);
     private native boolean isValidWordNative(int nativeData, char[] word, int wordLength);
-    private native int getSuggestionsNative(int dict, int[] inputCodes, int codesSize, 
+    private native int getSuggestionsNative(int dict, int[] inputCodes, int codesSize,
             char[] outputChars, int[] frequencies,
             int[] nextLettersFrequencies, int nextLettersSize);
     private native int getBigramsNative(int dict, char[] prevWord, int prevWordLength,
             int[] inputCodes, int inputCodesLength, char[] outputChars, int[] frequencies,
             int maxWordLength, int maxBigrams, int maxAlternatives);
 
-    private final void loadDictionary(Context context, int[] resId) {
-        InputStream[] is = null;
+    private final void loadDictionary(Context context, int resId) {
+        InputStream is = null;
         try {
-            // merging separated dictionary into one if dictionary is separated
-            int total = 0;
-            is = new InputStream[resId.length];
-            for (int i = 0; i < resId.length; i++) {
-                is[i] = context.getResources().openRawResource(resId[i]);
-                total += is[i].available();
-            }
-
+            is = context.getResources().openRawResource(resId);
+            final int total = is.available();
             mNativeDictDirectBuffer =
                 ByteBuffer.allocateDirect(total).order(ByteOrder.nativeOrder());
-            int got = 0;
-            for (int i = 0; i < resId.length; i++) {
-                 got += Channels.newChannel(is[i]).read(mNativeDictDirectBuffer);
-            }
+            final int got = Channels.newChannel(is).read(mNativeDictDirectBuffer);
             if (got != total) {
                 Log.e(TAG, "Read " + got + " bytes, expected " + total);
             } else {
@@ -140,11 +131,7 @@
             Log.w(TAG, "No available memory for binary dictionary");
         } finally {
             try {
-                if (is != null) {
-                    for (int i = 0; i < is.length; i++) {
-                        is[i].close();
-                    }
-                }
+                if (is != null) is.close();
             } catch (IOException e) {
                 Log.w(TAG, "Failed to close input stream");
             }
diff --git a/java/src/com/android/inputmethod/latin/InputLanguageSelection.java b/java/src/com/android/inputmethod/latin/InputLanguageSelection.java
index 27e0fbe..faee38e 100644
--- a/java/src/com/android/inputmethod/latin/InputLanguageSelection.java
+++ b/java/src/com/android/inputmethod/latin/InputLanguageSelection.java
@@ -99,15 +99,15 @@
     }
 
     private boolean hasDictionary(Locale locale) {
-        Resources res = getResources();
-        Configuration conf = res.getConfiguration();
-        Locale saveLocale = conf.locale;
+        final Resources res = getResources();
+        final Configuration conf = res.getConfiguration();
+        final Locale saveLocale = conf.locale;
         boolean haveDictionary = false;
         conf.locale = locale;
         res.updateConfiguration(conf, res.getDisplayMetrics());
 
-        int[] dictionaries = LatinIME.getDictionary(res);
-        BinaryDictionary bd = new BinaryDictionary(this, dictionaries, Suggest.DIC_MAIN);
+        int mainDicResId = LatinIME.getMainDictionaryResourceId(res);
+        BinaryDictionary bd = new BinaryDictionary(this, mainDicResId, Suggest.DIC_MAIN);
 
         // Is the dictionary larger than a placeholder? Arbitrarily chose a lower limit of
         // 4000-5000 words, whereas the LARGE_DICTIONARY is about 20000+ words.
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 28c5d73..be98f4c 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -347,49 +347,19 @@
     }
 
     /**
-     * Loads a dictionary or multiple separated dictionary
-     * @return returns array of dictionary resource ids
+     * Returns a main dictionary resource id
+     * @return main dictionary resource id
      */
-    public static int[] getDictionary(Resources res) {
+    public static int getMainDictionaryResourceId(Resources res) {
+        final String MAIN_DIC_NAME = "main";
         String packageName = LatinIME.class.getPackage().getName();
-        XmlResourceParser xrp = res.getXml(R.xml.dictionary);
-        ArrayList<Integer> dictionaries = new ArrayList<Integer>();
-
-        try {
-            int current = xrp.getEventType();
-            while (current != XmlPullParser.END_DOCUMENT) {
-                if (current == XmlPullParser.START_TAG) {
-                    String tag = xrp.getName();
-                    if (tag != null) {
-                        if (tag.equals("part")) {
-                            String dictFileName = xrp.getAttributeValue(null, "name");
-                            dictionaries.add(res.getIdentifier(dictFileName, "raw", packageName));
-                        }
-                    }
-                }
-                xrp.next();
-                current = xrp.getEventType();
-            }
-        } catch (XmlPullParserException e) {
-            Log.e(TAG, "Dictionary XML parsing failure");
-        } catch (IOException e) {
-            Log.e(TAG, "Dictionary XML IOException");
-        }
-
-        int count = dictionaries.size();
-        int[] dict = new int[count];
-        for (int i = 0; i < count; i++) {
-            dict[i] = dictionaries.get(i);
-        }
-
-        return dict;
+        return res.getIdentifier(MAIN_DIC_NAME, "raw", packageName);
     }
 
     private void initSuggest() {
         updateAutoTextEnabled();
         String locale = mSubtypeSwitcher.getInputLocaleStr();
 
-        Resources orig = getResources();
         Locale savedLocale = mSubtypeSwitcher.changeSystemLocale(new Locale(locale));
         if (mSuggest != null) {
             mSuggest.close();
@@ -397,8 +367,9 @@
         final SharedPreferences prefs = mPrefs;
         mQuickFixes = prefs.getBoolean(Settings.PREF_QUICK_FIXES, true);
 
-        int[] dictionaries = getDictionary(orig);
-        mSuggest = new Suggest(this, dictionaries);
+        final Resources res = mResources;
+        int mainDicResId = getMainDictionaryResourceId(res);
+        mSuggest = new Suggest(this, mainDicResId);
         loadAndSetAutoCorrectionThreshold(prefs);
         if (mUserDictionary != null) mUserDictionary.close();
         mUserDictionary = new UserDictionary(this, locale);
@@ -418,8 +389,8 @@
         mSuggest.setContactsDictionary(mContactsDictionary);
         mSuggest.setAutoDictionary(mAutoDictionary);
         updateCorrectionMode();
-        mWordSeparators = mResources.getString(R.string.word_separators);
-        mSentenceSeparators = mResources.getString(R.string.sentence_separators);
+        mWordSeparators = res.getString(R.string.word_separators);
+        mSentenceSeparators = res.getString(R.string.sentence_separators);
 
         mSubtypeSwitcher.changeSystemLocale(savedLocale);
     }
@@ -859,10 +830,11 @@
 
     @Override
     public boolean onEvaluateFullscreenMode() {
-        DisplayMetrics dm = getResources().getDisplayMetrics();
+        final Resources res = mResources;
+        DisplayMetrics dm = res.getDisplayMetrics();
         float displayHeight = dm.heightPixels;
         // If the display is more than X inches high, don't go to fullscreen mode
-        float dimen = getResources().getDimension(R.dimen.max_height_for_fullscreen);
+        float dimen = res.getDimension(R.dimen.max_height_for_fullscreen);
         if (displayHeight > dimen) {
             return false;
         } else {
@@ -1874,7 +1846,7 @@
         } else if (Settings.PREF_RECORRECTION_ENABLED.equals(key)) {
             mReCorrectionEnabled = sharedPreferences.getBoolean(
                     Settings.PREF_RECORRECTION_ENABLED,
-                    getResources().getBoolean(R.bool.default_recorrection_enabled));
+                    mResources.getBoolean(R.bool.default_recorrection_enabled));
         }
     }
 
@@ -2006,11 +1978,12 @@
     }
 
     private void updateSuggestionVisibility(SharedPreferences prefs) {
+        final Resources res = mResources;
         final String suggestionVisiblityStr = prefs.getString(
                 Settings.PREF_SHOW_SUGGESTIONS_SETTING,
-                mResources.getString(R.string.prefs_suggestion_visibility_default_value));
+                res.getString(R.string.prefs_suggestion_visibility_default_value));
         for (int visibility : SUGGESTION_VISIBILITY_VALUE_ARRAY) {
-            if (suggestionVisiblityStr.equals(mResources.getString(visibility))) {
+            if (suggestionVisiblityStr.equals(res.getString(visibility))) {
                 mSuggestionVisibility = visibility;
                 break;
             }
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index 2365902..9f979ff 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -103,7 +103,7 @@
 
     private int mCorrectionMode = CORRECTION_BASIC;
 
-    public Suggest(Context context, int[] dictionaryResId) {
+    public Suggest(Context context, int dictionaryResId) {
         mMainDict = new BinaryDictionary(context, dictionaryResId, DIC_MAIN);
         initPool();
     }