Merge "Use probability table for decaying dictionaries."
diff --git a/java/src/com/android/inputmethod/dictionarypack/DictionarySettingsActivity.java b/java/src/com/android/inputmethod/dictionarypack/DictionarySettingsActivity.java
index 6841652..c28d729 100644
--- a/java/src/com/android/inputmethod/dictionarypack/DictionarySettingsActivity.java
+++ b/java/src/com/android/inputmethod/dictionarypack/DictionarySettingsActivity.java
@@ -24,6 +24,8 @@
  * Preference screen.
  */
 public final class DictionarySettingsActivity extends PreferenceActivity {
+    private static final String DEFAULT_FRAGMENT = DictionarySettingsFragment.class.getName();
+
     @Override
     protected void onCreate(final Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -32,11 +34,17 @@
     @Override
     public Intent getIntent() {
         final Intent modIntent = new Intent(super.getIntent());
-        modIntent.putExtra(EXTRA_SHOW_FRAGMENT, DictionarySettingsFragment.class.getName());
+        modIntent.putExtra(EXTRA_SHOW_FRAGMENT, DEFAULT_FRAGMENT);
         modIntent.putExtra(EXTRA_NO_HEADERS, true);
         // Important note : the original intent should contain a String extra with the key
         // DictionarySettingsFragment.DICT_SETTINGS_FRAGMENT_CLIENT_ID_ARGUMENT so that the
         // fragment can know who the client is.
         return modIntent;
     }
+
+    // TODO: Uncomment the override annotation once we start using SDK version 19.
+    // @Override
+    public boolean isValidFragment(String fragmentName) {
+        return fragmentName.equals(DEFAULT_FRAGMENT);
+    }
 }
diff --git a/java/src/com/android/inputmethod/keyboard/EmojiLayoutParams.java b/java/src/com/android/inputmethod/keyboard/EmojiLayoutParams.java
index 267fad5..71790b7 100644
--- a/java/src/com/android/inputmethod/keyboard/EmojiLayoutParams.java
+++ b/java/src/com/android/inputmethod/keyboard/EmojiLayoutParams.java
@@ -75,9 +75,7 @@
 
     public void setActionBarProperties(LinearLayout ll) {
         final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) ll.getLayoutParams();
-        lp.height = mEmojiActionBarHeight;
-        lp.topMargin = 0;
-        lp.bottomMargin = mBottomPadding;
+        lp.height = mEmojiActionBarHeight - mBottomPadding;
         ll.setLayoutParams(lp);
     }
 
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
index 74edd87..ad6e2c0 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
@@ -155,7 +155,7 @@
     }
 
     public void saveKeyboardState() {
-        if (getKeyboard() != null) {
+        if (getKeyboard() != null || isShowingEmojiKeyboard()) {
             mState.onSaveKeyboardState();
         }
     }
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java
index 9f9fdaa..506dfa7 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java
@@ -178,6 +178,8 @@
             if (!state.mIsAlphabetShiftLocked) {
                 setShifted(state.mShiftMode);
             }
+            // TODO: is this the right place to do this? Should we do this in setShift* instead?
+            mSwitchActions.requestUpdatingShiftState();
         } else {
             mPrevMainKeyboardWasShiftLocked = state.mIsAlphabetShiftLocked;
         }
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 96e16de..003dcfb 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -2929,6 +2929,7 @@
             return;
         }
         tryFixLyingCursorPosition();
+        mKeyboardSwitcher.updateShiftState();
         if (tryResumeSuggestions) mHandler.postResumeSuggestions();
     }
 
diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java
index 665c7a2..2c3d134 100644
--- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java
+++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java
@@ -295,7 +295,6 @@
                 return address;
             }
         }
-        int address;
         switch (optionFlags & FormatSpec.MASK_CHILDREN_ADDRESS_TYPE) {
             case FormatSpec.FLAG_CHILDREN_ADDRESS_TYPE_ONEBYTE:
                 return dictBuffer.readUnsignedByte();
diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictEncoderUtils.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictEncoderUtils.java
index af61f29..b602424 100644
--- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictEncoderUtils.java
+++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictEncoderUtils.java
@@ -278,7 +278,6 @@
         // For future reference, the code to remove duplicate is a simple : list.remove(node);
         list.add(ptNodeArray);
         final ArrayList<PtNode> branches = ptNodeArray.mData;
-        final int nodeSize = branches.size();
         for (PtNode ptNode : branches) {
             if (null != ptNode.mChildren) flattenTreeInner(list, ptNode.mChildren);
         }
@@ -427,9 +426,6 @@
                         nodeCountSize + nodeArrayOffset + nodeffset;
                 nodeffset += ptNode.mCachedSize;
             }
-            final int nodeSize = nodeCountSize + nodeffset
-                    + (formatOptions.mSupportsDynamicUpdate
-                            ? FormatSpec.FORWARD_LINK_ADDRESS_SIZE : 0);
             nodeArrayOffset += nodeArray.mCachedSize;
         }
         return nodeArrayOffset;
@@ -653,8 +649,8 @@
         return flags;
     }
 
-    /* package */ static byte makePtNodeFlags(final PtNode node, final int ptNodeAddress,
-            final int childrenOffset, final FormatOptions formatOptions) {
+    /* package */ static byte makePtNodeFlags(final PtNode node, final int childrenOffset,
+            final FormatOptions formatOptions) {
         return (byte) makePtNodeFlags(node.mChars.length > 1, node.mFrequency >= 0,
                 getByteSize(childrenOffset),
                 node.mShortcutTargets != null && !node.mShortcutTargets.isEmpty(),
diff --git a/java/src/com/android/inputmethod/latin/makedict/DynamicBinaryDictIOUtils.java b/java/src/com/android/inputmethod/latin/makedict/DynamicBinaryDictIOUtils.java
index 411e265..5c69941 100644
--- a/java/src/com/android/inputmethod/latin/makedict/DynamicBinaryDictIOUtils.java
+++ b/java/src/com/android/inputmethod/latin/makedict/DynamicBinaryDictIOUtils.java
@@ -59,7 +59,7 @@
             throws IOException, UnsupportedFormatException {
         final DictBuffer dictBuffer = dictDecoder.getDictBuffer();
         dictBuffer.position(0);
-        final FileHeader header = dictDecoder.readHeader();
+        dictDecoder.readHeader();
         final int wordPosition = dictDecoder.getTerminalPosition(word);
         if (wordPosition == FormatSpec.NOT_VALID_WORD) return;
 
@@ -142,8 +142,7 @@
         final int originalPosition = dictBuffer.position();
         dictBuffer.position(ptNodeOriginAddress);
         final int flags = dictBuffer.readUnsignedByte();
-        final int parentAddress = BinaryDictDecoderUtils.readParentAddress(dictBuffer,
-                formatOptions);
+        BinaryDictDecoderUtils.readParentAddress(dictBuffer, formatOptions);
         BinaryDictIOUtils.skipString(dictBuffer, (flags & FormatSpec.FLAG_HAS_MULTIPLE_CHARS) != 0);
         if ((flags & FormatSpec.FLAG_IS_TERMINAL) != 0) dictBuffer.readUnsignedByte();
         final int childrenOffset = newChildrenAddress == FormatSpec.NO_CHILDREN_ADDRESS
diff --git a/java/src/com/android/inputmethod/latin/makedict/SparseTable.java b/java/src/com/android/inputmethod/latin/makedict/SparseTable.java
index 96d057a..7592a0c 100644
--- a/java/src/com/android/inputmethod/latin/makedict/SparseTable.java
+++ b/java/src/com/android/inputmethod/latin/makedict/SparseTable.java
@@ -17,6 +17,7 @@
 package com.android.inputmethod.latin.makedict;
 
 import com.android.inputmethod.annotations.UsedForTesting;
+import com.android.inputmethod.latin.utils.CollectionUtils;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -37,35 +38,39 @@
     /**
      * mLookupTable is indexed by terminal ID, containing exactly one entry for every mBlockSize
      * terminals.
-     * It contains at index i = j / mBlockSize the index in mContentsTable where the values for
-     * terminals with IDs j to j + mBlockSize - 1 are stored as an mBlockSize-sized integer array.
+     * It contains at index i = j / mBlockSize the index in each ArrayList in mContentsTables where
+     * the values for terminals with IDs j to j + mBlockSize - 1 are stored as an mBlockSize-sized
+     * integer array.
      */
     private final ArrayList<Integer> mLookupTable;
-    private final ArrayList<Integer> mContentTable;
+    private final ArrayList<ArrayList<Integer>> mContentTables;
 
     private final int mBlockSize;
+    private final int mContentTableCount;
     public static final int NOT_EXIST = -1;
+    public static final int SIZE_OF_INT_IN_BYTES = 4;
 
     @UsedForTesting
-    public SparseTable(final int initialCapacity, final int blockSize) {
+    public SparseTable(final int initialCapacity, final int blockSize,
+            final int contentTableCount) {
         mBlockSize = blockSize;
         final int lookupTableSize = initialCapacity / mBlockSize
                 + (initialCapacity % mBlockSize > 0 ? 1 : 0);
         mLookupTable = new ArrayList<Integer>(Collections.nCopies(lookupTableSize, NOT_EXIST));
-        mContentTable = new ArrayList<Integer>();
+        mContentTableCount = contentTableCount;
+        mContentTables = CollectionUtils.newArrayList();
+        for (int i = 0; i < mContentTableCount; ++i) {
+            mContentTables.add(new ArrayList<Integer>());
+        }
     }
 
     @UsedForTesting
-    public SparseTable(final int[] lookupTable, final int[] contentTable, final int blockSize) {
+    public SparseTable(final ArrayList<Integer> lookupTable,
+            final ArrayList<ArrayList<Integer>> contentTables, final int blockSize) {
         mBlockSize = blockSize;
-        mLookupTable = new ArrayList<Integer>(lookupTable.length);
-        for (int i = 0; i < lookupTable.length; ++i) {
-            mLookupTable.add(lookupTable[i]);
-        }
-        mContentTable = new ArrayList<Integer>(contentTable.length);
-        for (int i = 0; i < contentTable.length; ++i) {
-            mContentTable.add(contentTable[i]);
-        }
+        mContentTableCount = contentTables.size();
+        mLookupTable = lookupTable;
+        mContentTables = contentTables;
     }
 
     /**
@@ -75,8 +80,8 @@
      * Otherwise, IndexOutOfBoundsException will be raised.
      */
     @UsedForTesting
-    private static void convertByteArrayToIntegerArray(final byte[] byteArray,
-            final ArrayList<Integer> integerArray) {
+    private static ArrayList<Integer> convertByteArrayToIntegerArray(final byte[] byteArray) {
+        final ArrayList<Integer> integerArray = new ArrayList<Integer>(byteArray.length / 4);
         for (int i = 0; i < byteArray.length; i += 4) {
             int value = 0;
             for (int j = i; j < i + 4; ++j) {
@@ -85,39 +90,43 @@
              }
             integerArray.add(value);
         }
+        return integerArray;
     }
 
     @UsedForTesting
-    public SparseTable(final byte[] lookupTable, final byte[] contentTable, final int blockSize) {
-        mBlockSize = blockSize;
-        mLookupTable = new ArrayList<Integer>(lookupTable.length / 4);
-        mContentTable = new ArrayList<Integer>(contentTable.length / 4);
-        convertByteArrayToIntegerArray(lookupTable, mLookupTable);
-        convertByteArrayToIntegerArray(contentTable, mContentTable);
-    }
-
-    @UsedForTesting
-    public int get(final int index) {
-        if (index < 0 || index / mBlockSize >= mLookupTable.size()
-                || mLookupTable.get(index / mBlockSize) == NOT_EXIST) {
+    public int get(final int contentTableIndex, final int index) {
+        if (!contains(index)) {
             return NOT_EXIST;
         }
-        return mContentTable.get(mLookupTable.get(index / mBlockSize) + (index % mBlockSize));
+        return mContentTables.get(contentTableIndex).get(
+                mLookupTable.get(index / mBlockSize) + (index % mBlockSize));
     }
 
     @UsedForTesting
-    public void set(final int index, final int value) {
-        if (mLookupTable.get(index / mBlockSize) == NOT_EXIST) {
-            mLookupTable.set(index / mBlockSize, mContentTable.size());
-            for (int i = 0; i < mBlockSize; ++i) {
-                mContentTable.add(NOT_EXIST);
-            }
+    public ArrayList<Integer> getAll(final int index) {
+        final ArrayList<Integer> ret = CollectionUtils.newArrayList();
+        for (int i = 0; i < mContentTableCount; ++i) {
+            ret.add(get(i, index));
         }
-        mContentTable.set(mLookupTable.get(index / mBlockSize) + (index % mBlockSize), value);
+        return ret;
     }
 
-    public void remove(final int index) {
-        set(index, NOT_EXIST);
+    @UsedForTesting
+    public void set(final int contentTableIndex, final int index, final int value) {
+        if (mLookupTable.get(index / mBlockSize) == NOT_EXIST) {
+            mLookupTable.set(index / mBlockSize, mContentTables.get(contentTableIndex).size());
+            for (int i = 0; i < mContentTableCount; ++i) {
+                for (int j = 0; j < mBlockSize; ++j) {
+                    mContentTables.get(i).add(NOT_EXIST);
+                }
+            }
+        }
+        mContentTables.get(contentTableIndex).set(
+                mLookupTable.get(index / mBlockSize) + (index % mBlockSize), value);
+    }
+
+    public void remove(final int indexOfContent, final int index) {
+        set(indexOfContent, index, NOT_EXIST);
     }
 
     @UsedForTesting
@@ -127,7 +136,8 @@
 
     @UsedForTesting
     /* package */ int getContentTableSize() {
-        return mContentTable.size();
+        // This class always has at least one content table.
+        return mContentTables.get(0).size();
     }
 
     @UsedForTesting
@@ -136,36 +146,51 @@
     }
 
     public boolean contains(final int index) {
-        return get(index) != NOT_EXIST;
+        if (index < 0 || index / mBlockSize >= mLookupTable.size()
+                || mLookupTable.get(index / mBlockSize) == NOT_EXIST) {
+            return false;
+        }
+        return true;
     }
 
     @UsedForTesting
-    public void write(final OutputStream lookupOutStream, final OutputStream contentOutStream)
+    public void write(final OutputStream lookupOutStream, final OutputStream[] contentOutStreams)
             throws IOException {
+         if (contentOutStreams.length != mContentTableCount) {
+             throw new RuntimeException(contentOutStreams.length + " streams are given, but the"
+                     + " table has " + mContentTableCount + " content tables.");
+         }
         for (final int index : mLookupTable) {
-          BinaryDictEncoderUtils.writeUIntToStream(lookupOutStream, index, 4);
+          BinaryDictEncoderUtils.writeUIntToStream(lookupOutStream, index, SIZE_OF_INT_IN_BYTES);
         }
 
-        for (final int index : mContentTable) {
-            BinaryDictEncoderUtils.writeUIntToStream(contentOutStream, index, 4);
+        for (int i = 0; i < contentOutStreams.length; ++i) {
+            for (final int data : mContentTables.get(i)) {
+                BinaryDictEncoderUtils.writeUIntToStream(contentOutStreams[i], data,
+                        SIZE_OF_INT_IN_BYTES);
+            }
         }
     }
 
     @UsedForTesting
-    public void writeToFiles(final File lookupTableFile, final File contentFile)
+    public void writeToFiles(final File lookupTableFile, final File[] contentFiles)
             throws IOException {
-      FileOutputStream lookupTableOutStream = null;
-      FileOutputStream contentOutStream = null;
+        FileOutputStream lookupTableOutStream = null;
+        final FileOutputStream[] contentTableOutStreams = new FileOutputStream[mContentTableCount];
         try {
             lookupTableOutStream = new FileOutputStream(lookupTableFile);
-            contentOutStream = new FileOutputStream(contentFile);
-            write(lookupTableOutStream, contentOutStream);
+            for (int i = 0; i < contentFiles.length; ++i) {
+                contentTableOutStreams[i] = new FileOutputStream(contentFiles[i]);
+            }
+            write(lookupTableOutStream, contentTableOutStreams);
         } finally {
             if (lookupTableOutStream != null) {
                 lookupTableOutStream.close();
             }
-            if (contentOutStream != null) {
-                contentOutStream.close();
+            for (int i = 0; i < contentTableOutStreams.length; ++i) {
+                if (contentTableOutStreams[i] != null) {
+                    contentTableOutStreams[i].close();
+                }
             }
         }
     }
@@ -185,10 +210,14 @@
     }
 
     @UsedForTesting
-    public static SparseTable readFromFiles(final File lookupTableFile, final File contentFile,
+    public static SparseTable readFromFiles(final File lookupTableFile, final File[] contentFiles,
             final int blockSize) throws IOException {
-        final byte[] lookupTable = readFileToByteArray(lookupTableFile);
-        final byte[] content = readFileToByteArray(contentFile);
-        return new SparseTable(lookupTable, content, blockSize);
+        final ArrayList<ArrayList<Integer>> contentTables =
+                new ArrayList<ArrayList<Integer>>(contentFiles.length);
+        for (int i = 0; i < contentFiles.length; ++i) {
+            contentTables.add(convertByteArrayToIntegerArray(readFileToByteArray(contentFiles[i])));
+        }
+        return new SparseTable(convertByteArrayToIntegerArray(readFileToByteArray(lookupTableFile)),
+                contentTables, blockSize);
     }
 }
diff --git a/java/src/com/android/inputmethod/latin/makedict/Ver3DictDecoder.java b/java/src/com/android/inputmethod/latin/makedict/Ver3DictDecoder.java
index 75d1058..bf5a28d 100644
--- a/java/src/com/android/inputmethod/latin/makedict/Ver3DictDecoder.java
+++ b/java/src/com/android/inputmethod/latin/makedict/Ver3DictDecoder.java
@@ -169,7 +169,8 @@
             addressPointer += PtNodeReader.readBigramAddresses(mDictBuffer, bigrams, 
                     addressPointer);
             if (bigrams.size() >= FormatSpec.MAX_BIGRAMS_IN_A_PTNODE) {
-                MakedictLog.d("too many bigrams in a PtNode.");
+                throw new RuntimeException("Too many bigrams in a PtNode (" + bigrams.size()
+                        + " but max is " + FormatSpec.MAX_BIGRAMS_IN_A_PTNODE + ")");
             }
         } else {
             bigrams = null;
diff --git a/java/src/com/android/inputmethod/latin/makedict/Ver3DictEncoder.java b/java/src/com/android/inputmethod/latin/makedict/Ver3DictEncoder.java
index 76f0f40..d9e1989 100644
--- a/java/src/com/android/inputmethod/latin/makedict/Ver3DictEncoder.java
+++ b/java/src/com/android/inputmethod/latin/makedict/Ver3DictEncoder.java
@@ -133,12 +133,10 @@
                 countSize);
     }
 
-    private void writePtNodeFlags(final PtNode ptNode, final int parentAddress,
-            final FormatOptions formatOptions) {
+    private void writePtNodeFlags(final PtNode ptNode, final FormatOptions formatOptions) {
         final int childrenPos = BinaryDictEncoderUtils.getChildrenPosition(ptNode, formatOptions);
         mPosition = BinaryDictEncoderUtils.writeUIntToBuffer(mBuffer, mPosition,
-                BinaryDictEncoderUtils.makePtNodeFlags(ptNode, mPosition, childrenPos,
-                        formatOptions),
+                BinaryDictEncoderUtils.makePtNodeFlags(ptNode, childrenPos, formatOptions),
                 FormatSpec.PTNODE_FLAGS_SIZE);
     }
 
@@ -244,7 +242,7 @@
     @Override
     public void writePtNode(final PtNode ptNode, final int parentPosition,
             final FormatOptions formatOptions, final FusionDictionary dict) {
-        writePtNodeFlags(ptNode, parentPosition, formatOptions);
+        writePtNodeFlags(ptNode, formatOptions);
         writeParentPosition(parentPosition, ptNode, formatOptions);
         writeCharacters(ptNode.mChars, ptNode.hasSeveralChars());
         writeFrequency(ptNode.mFrequency);
diff --git a/java/src/com/android/inputmethod/latin/makedict/Ver4DictDecoder.java b/java/src/com/android/inputmethod/latin/makedict/Ver4DictDecoder.java
index fa19e26..624b278 100644
--- a/java/src/com/android/inputmethod/latin/makedict/Ver4DictDecoder.java
+++ b/java/src/com/android/inputmethod/latin/makedict/Ver4DictDecoder.java
@@ -95,7 +95,6 @@
 
     @Override
     public void openDictBuffer() throws FileNotFoundException, IOException {
-        final String filename = mDictDirectory.getName();
         mDictBuffer = mBufferFactory.getDictionaryBuffer(getFile(FILETYPE_TRIE));
         mFrequencyBuffer = mBufferFactory.getDictionaryBuffer(getFile(FILETYPE_FREQUENCY));
         mTerminalAddressTableBuffer = mBufferFactory.getDictionaryBuffer(
@@ -131,7 +130,7 @@
                 mDictDirectory.getName() + FormatSpec.BIGRAM_LOOKUP_TABLE_FILE_EXTENSION);
         final File contentFile = new File(mDictDirectory,
                 mDictDirectory.getName() + FormatSpec.BIGRAM_ADDRESS_TABLE_FILE_EXTENSION);
-        mBigramAddressTable = SparseTable.readFromFiles(lookupIndexFile, contentFile,
+        mBigramAddressTable = SparseTable.readFromFiles(lookupIndexFile, new File[] { contentFile },
                 FormatSpec.BIGRAM_ADDRESS_TABLE_BLOCK_SIZE);
     }
 
@@ -208,7 +207,7 @@
         final ArrayList<PendingAttribute> bigrams;
         if (0 != (flags & FormatSpec.FLAG_HAS_BIGRAMS)) {
             bigrams = new ArrayList<PendingAttribute>();
-            final int posOfBigrams = mBigramAddressTable.get(terminalId);
+            final int posOfBigrams = mBigramAddressTable.get(0 /* contentTableIndex */, terminalId);
             mBigramBuffer.position(posOfBigrams);
             while (bigrams.size() < FormatSpec.MAX_BIGRAMS_IN_A_PTNODE) {
                 // If bigrams.size() reaches FormatSpec.MAX_BIGRAMS_IN_A_PTNODE,
@@ -224,7 +223,8 @@
                 if (0 == (bigramFlags & FormatSpec.FLAG_BIGRAM_SHORTCUT_ATTR_HAS_NEXT)) break;
             }
             if (bigrams.size() >= FormatSpec.MAX_BIGRAMS_IN_A_PTNODE) {
-                MakedictLog.d("too many bigrams in a node.");
+                throw new RuntimeException("Too many bigrams in a PtNode (" + bigrams.size()
+                        + " but max is " + FormatSpec.MAX_BIGRAMS_IN_A_PTNODE + ")");
             }
         } else {
             bigrams = null;
diff --git a/java/src/com/android/inputmethod/latin/makedict/Ver4DictEncoder.java b/java/src/com/android/inputmethod/latin/makedict/Ver4DictEncoder.java
index 4c25faf..a403e25 100644
--- a/java/src/com/android/inputmethod/latin/makedict/Ver4DictEncoder.java
+++ b/java/src/com/android/inputmethod/latin/makedict/Ver4DictEncoder.java
@@ -136,7 +136,7 @@
 
         writeTerminalData(flatNodes, terminalCount);
         mBigramAddressTable = new SparseTable(terminalCount,
-                FormatSpec.BIGRAM_ADDRESS_TABLE_BLOCK_SIZE);
+                FormatSpec.BIGRAM_ADDRESS_TABLE_BLOCK_SIZE, 1 /* contentTableCount */);
         writeBigrams(flatNodes, dict);
         writeBigramAddressSparseTable();
 
@@ -181,12 +181,10 @@
                 countSize);
     }
 
-    private void writePtNodeFlags(final PtNode ptNode, final int parentAddress,
-            final FormatOptions formatOptions) {
+    private void writePtNodeFlags(final PtNode ptNode, final FormatOptions formatOptions) {
         final int childrenPos = BinaryDictEncoderUtils.getChildrenPosition(ptNode, formatOptions);
         mTriePos = BinaryDictEncoderUtils.writeUIntToBuffer(mTrieBuf, mTriePos,
-                BinaryDictEncoderUtils.makePtNodeFlags(ptNode, mTriePos, childrenPos,
-                        formatOptions),
+                BinaryDictEncoderUtils.makePtNodeFlags(ptNode, childrenPos, formatOptions),
                 FormatSpec.PTNODE_FLAGS_SIZE);
     }
 
@@ -231,8 +229,7 @@
         while (shortcutIterator.hasNext()) {
             final WeightedString target = shortcutIterator.next();
             final int shortcutFlags = BinaryDictEncoderUtils.makeShortcutFlags(
-                    shortcutIterator.hasNext(),
-                    target.mFrequency);
+                    shortcutIterator.hasNext(), target.mFrequency);
             mTrieBuf[mTriePos++] = (byte)shortcutFlags;
             final int shortcutShift = CharEncoding.writeString(mTrieBuf, mTriePos,
                     target.mWord);
@@ -254,7 +251,8 @@
             for (final PtNode ptNode : nodeArray.mData) {
                 if (ptNode.mBigrams != null) {
                     final int startPos = bigramBuffer.size();
-                    mBigramAddressTable.set(ptNode.mTerminalId, startPos);
+                    mBigramAddressTable.set(0 /* contentTableIndex */, ptNode.mTerminalId,
+                            startPos);
                     final Iterator<WeightedString> bigramIterator = ptNode.mBigrams.iterator();
                     while (bigramIterator.hasNext()) {
                         final WeightedString bigram = bigramIterator.next();
@@ -280,7 +278,7 @@
                 new File(mDictDir, mBaseFilename + FormatSpec.BIGRAM_LOOKUP_TABLE_FILE_EXTENSION);
         final File contentFile =
                 new File(mDictDir, mBaseFilename + FormatSpec.BIGRAM_ADDRESS_TABLE_FILE_EXTENSION);
-        mBigramAddressTable.writeToFiles(lookupIndexFile, contentFile);
+        mBigramAddressTable.writeToFiles(lookupIndexFile, new File[] { contentFile });
     }
 
     @Override
@@ -292,7 +290,7 @@
     @Override
     public void writePtNode(final PtNode ptNode, final int parentPosition,
             final FormatOptions formatOptions, final FusionDictionary dict) {
-        writePtNodeFlags(ptNode, parentPosition, formatOptions);
+        writePtNodeFlags(ptNode, formatOptions);
         writeParentPosition(parentPosition, ptNode, formatOptions);
         writeCharacters(ptNode.mChars, ptNode.hasSeveralChars());
         if (ptNode.isTerminal()) {
diff --git a/java/src/com/android/inputmethod/latin/settings/DebugSettingsActivity.java b/java/src/com/android/inputmethod/latin/settings/DebugSettingsActivity.java
index b499c26..ef6ab2a 100644
--- a/java/src/com/android/inputmethod/latin/settings/DebugSettingsActivity.java
+++ b/java/src/com/android/inputmethod/latin/settings/DebugSettingsActivity.java
@@ -38,4 +38,10 @@
         super.onCreate(savedInstanceState);
         setTitle(R.string.english_ime_debug_settings);
     }
+
+    // TODO: Uncomment the override annotation once we start using SDK version 19.
+    // @Override
+    public boolean isValidFragment(String fragmentName) {
+        return fragmentName.equals(DEFAULT_FRAGMENT);
+    }
 }
diff --git a/java/src/com/android/inputmethod/latin/settings/SettingsActivity.java b/java/src/com/android/inputmethod/latin/settings/SettingsActivity.java
index 6c38186..ad68f8c 100644
--- a/java/src/com/android/inputmethod/latin/settings/SettingsActivity.java
+++ b/java/src/com/android/inputmethod/latin/settings/SettingsActivity.java
@@ -32,4 +32,10 @@
         intent.putExtra(EXTRA_NO_HEADERS, true);
         return intent;
     }
+
+    // TODO: Uncomment the override annotation once we start using SDK version 19.
+    // @Override
+    public boolean isValidFragment(String fragmentName) {
+        return fragmentName.equals(DEFAULT_FRAGMENT);
+    }
 }
diff --git a/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerSettingsActivity.java b/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerSettingsActivity.java
index 119ca47..aba5637 100644
--- a/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerSettingsActivity.java
+++ b/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerSettingsActivity.java
@@ -24,6 +24,8 @@
  * Spell checker preference screen.
  */
 public final class SpellCheckerSettingsActivity extends PreferenceActivity {
+    private static final String DEFAULT_FRAGMENT = SpellCheckerSettingsFragment.class.getName();
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -32,8 +34,14 @@
     @Override
     public Intent getIntent() {
         final Intent modIntent = new Intent(super.getIntent());
-        modIntent.putExtra(EXTRA_SHOW_FRAGMENT, SpellCheckerSettingsFragment.class.getName());
+        modIntent.putExtra(EXTRA_SHOW_FRAGMENT, DEFAULT_FRAGMENT);
         modIntent.putExtra(EXTRA_NO_HEADERS, true);
         return modIntent;
     }
+
+    // TODO: Uncomment the override annotation once we start using SDK version 19.
+    // @Override
+    public boolean isValidFragment(String fragmentName) {
+        return fragmentName.equals(DEFAULT_FRAGMENT);
+    }
 }
diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java
index a4d9426..0cf4ef9 100644
--- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java
+++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java
@@ -104,7 +104,9 @@
         }
 
         sStarBigrams.put(0, new ArrayList<Integer>());
-        for (int i = 1; i < sWords.size(); ++i) {
+        // MAX - 1 because we added one above already
+        final int maxBigrams = Math.min(sWords.size(), FormatSpec.MAX_BIGRAMS_IN_A_PTNODE - 1);
+        for (int i = 1; i < maxBigrams; ++i) {
             sStarBigrams.get(0).add(i);
         }
 
@@ -544,8 +546,7 @@
     }
 
     private long checkGetTerminalPosition(final DictDecoder dictDecoder, final String word,
-            int index, boolean contained) {
-        final int expectedFrequency = (UNIGRAM_FREQ + index) % 255;
+            final boolean contained) {
         long diff = -1;
         int position = -1;
         try {
@@ -603,7 +604,7 @@
         // Test a word that is contained within the dictionary.
         long sum = 0;
         for (int i = 0; i < sWords.size(); ++i) {
-            final long time = checkGetTerminalPosition(dictDecoder, sWords.get(i), i, true);
+            final long time = checkGetTerminalPosition(dictDecoder, sWords.get(i), true);
             sum += time == -1 ? 0 : time;
         }
         Log.d(TAG, "per search : " + (((double)sum) / sWords.size() / 1000000) + " : " + message
@@ -616,11 +617,11 @@
         for (int i = 0; i < 1000; ++i) {
             final String word = CodePointUtils.generateWord(random, codePointSet);
             if (sWords.indexOf(word) != -1) continue;
-            checkGetTerminalPosition(dictDecoder, word, i, false);
+            checkGetTerminalPosition(dictDecoder, word, false);
         }
     }
 
-    private void runGetTerminalPositionTests(final ArrayList<String> results, final int bufferType,
+    private void runGetTerminalPositionTests(final int bufferType,
             final FormatOptions formatOptions) {
         runGetTerminalPosition(sWords, sEmptyBigrams, bufferType, formatOptions, "unigram");
     }
@@ -628,17 +629,17 @@
     public void testGetTerminalPosition() {
         final ArrayList<String> results = CollectionUtils.newArrayList();
 
-        runGetTerminalPositionTests(results, USE_BYTE_ARRAY, VERSION2);
-        runGetTerminalPositionTests(results, USE_BYTE_ARRAY, VERSION3_WITHOUT_DYNAMIC_UPDATE);
-        runGetTerminalPositionTests(results, USE_BYTE_ARRAY, VERSION3_WITH_DYNAMIC_UPDATE);
-        runGetTerminalPositionTests(results, USE_BYTE_ARRAY, VERSION4_WITHOUT_DYNAMIC_UPDATE);
-        runGetTerminalPositionTests(results, USE_BYTE_ARRAY, VERSION4_WITH_DYNAMIC_UPDATE);
+        runGetTerminalPositionTests(USE_BYTE_ARRAY, VERSION2);
+        runGetTerminalPositionTests(USE_BYTE_ARRAY, VERSION3_WITHOUT_DYNAMIC_UPDATE);
+        runGetTerminalPositionTests(USE_BYTE_ARRAY, VERSION3_WITH_DYNAMIC_UPDATE);
+        runGetTerminalPositionTests(USE_BYTE_ARRAY, VERSION4_WITHOUT_DYNAMIC_UPDATE);
+        runGetTerminalPositionTests(USE_BYTE_ARRAY, VERSION4_WITH_DYNAMIC_UPDATE);
 
-        runGetTerminalPositionTests(results, USE_BYTE_BUFFER, VERSION2);
-        runGetTerminalPositionTests(results, USE_BYTE_BUFFER, VERSION3_WITHOUT_DYNAMIC_UPDATE);
-        runGetTerminalPositionTests(results, USE_BYTE_BUFFER, VERSION3_WITH_DYNAMIC_UPDATE);
-        runGetTerminalPositionTests(results, USE_BYTE_BUFFER, VERSION4_WITHOUT_DYNAMIC_UPDATE);
-        runGetTerminalPositionTests(results, USE_BYTE_BUFFER, VERSION4_WITH_DYNAMIC_UPDATE);
+        runGetTerminalPositionTests(USE_BYTE_BUFFER, VERSION2);
+        runGetTerminalPositionTests(USE_BYTE_BUFFER, VERSION3_WITHOUT_DYNAMIC_UPDATE);
+        runGetTerminalPositionTests(USE_BYTE_BUFFER, VERSION3_WITH_DYNAMIC_UPDATE);
+        runGetTerminalPositionTests(USE_BYTE_BUFFER, VERSION4_WITHOUT_DYNAMIC_UPDATE);
+        runGetTerminalPositionTests(USE_BYTE_BUFFER, VERSION4_WITH_DYNAMIC_UPDATE);
 
         for (final String result : results) {
             Log.d(TAG, result);
diff --git a/tests/src/com/android/inputmethod/latin/makedict/SparseTableTests.java b/tests/src/com/android/inputmethod/latin/makedict/SparseTableTests.java
index 132483d..aeb8552 100644
--- a/tests/src/com/android/inputmethod/latin/makedict/SparseTableTests.java
+++ b/tests/src/com/android/inputmethod/latin/makedict/SparseTableTests.java
@@ -21,10 +21,8 @@
 import android.util.Log;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Random;
@@ -36,9 +34,6 @@
 public class SparseTableTests extends AndroidTestCase {
     private static final String TAG = SparseTableTests.class.getSimpleName();
 
-    private static final int[] SMALL_INDEX = { SparseTable.NOT_EXIST, 0 };
-    private static final int[] BIG_INDEX = { SparseTable.NOT_EXIST, 1, 2, 3, 4, 5, 6, 7};
-
     private final Random mRandom;
     private final ArrayList<Integer> mRandomIndex;
 
@@ -59,32 +54,21 @@
         }
     }
 
-    public void testInitializeWithArray() {
-        final SparseTable table = new SparseTable(SMALL_INDEX, BIG_INDEX, BLOCK_SIZE);
-        for (int i = 0; i < 8; ++i) {
-            assertEquals(SparseTable.NOT_EXIST, table.get(i));
-        }
-        assertEquals(SparseTable.NOT_EXIST, table.get(8));
-        for (int i = 9; i < 16; ++i) {
-            assertEquals(i - 8, table.get(i));
-        }
-    }
-
     public void testSet() {
-        final SparseTable table = new SparseTable(16, BLOCK_SIZE);
-        table.set(3, 6);
-        table.set(8, 16);
+        final SparseTable table = new SparseTable(16, BLOCK_SIZE, 1);
+        table.set(0, 3, 6);
+        table.set(0, 8, 16);
         for (int i = 0; i < 16; ++i) {
             if (i == 3 || i == 8) {
-                assertEquals(i * 2, table.get(i));
+                assertEquals(i * 2, table.get(0, i));
             } else {
-                assertEquals(SparseTable.NOT_EXIST, table.get(i));
+                assertEquals(SparseTable.NOT_EXIST, table.get(0, i));
             }
         }
     }
 
     private void generateRandomIndex(final int size, final int prop) {
-        for (int i = 0; i < DEFAULT_SIZE; ++i) {
+        for (int i = 0; i < size; ++i) {
             if (mRandom.nextInt(100) < prop) {
                 mRandomIndex.set(i, mRandom.nextInt());
             } else {
@@ -94,11 +78,11 @@
     }
 
     private void runTestRandomSet() {
-        final SparseTable table = new SparseTable(DEFAULT_SIZE, BLOCK_SIZE);
+        final SparseTable table = new SparseTable(DEFAULT_SIZE, BLOCK_SIZE, 1);
         int elementCount = 0;
         for (int i = 0; i < DEFAULT_SIZE; ++i) {
             if (mRandomIndex.get(i) != SparseTable.NOT_EXIST) {
-                table.set(i, mRandomIndex.get(i));
+                table.set(0, i, mRandomIndex.get(i));
                 elementCount++;
             }
         }
@@ -107,29 +91,24 @@
               + table.getContentTableSize());
         Log.d(TAG, "the table has " + elementCount + " elements");
         for (int i = 0; i < DEFAULT_SIZE; ++i) {
-            assertEquals(table.get(i), (int)mRandomIndex.get(i));
+            assertEquals(table.get(0, i), (int)mRandomIndex.get(i));
         }
 
         // flush and reload
         OutputStream lookupOutStream = null;
         OutputStream contentOutStream = null;
-        InputStream lookupInStream = null;
-        InputStream contentInStream = null;
         try {
             final File lookupIndexFile = File.createTempFile("testRandomSet", ".small");
             final File contentFile = File.createTempFile("testRandomSet", ".big");
             lookupOutStream = new FileOutputStream(lookupIndexFile);
             contentOutStream = new FileOutputStream(contentFile);
-            table.write(lookupOutStream, contentOutStream);
-            lookupInStream = new FileInputStream(lookupIndexFile);
-            contentInStream = new FileInputStream(contentFile);
-            final byte[] lookupArray = new byte[(int) lookupIndexFile.length()];
-            final byte[] contentArray = new byte[(int) contentFile.length()];
-            lookupInStream.read(lookupArray);
-            contentInStream.read(contentArray);
-            final SparseTable newTable = new SparseTable(lookupArray, contentArray, BLOCK_SIZE);
+            table.write(lookupOutStream, new OutputStream[] { contentOutStream });
+            lookupOutStream.flush();
+            contentOutStream.flush();
+            final SparseTable newTable = SparseTable.readFromFiles(lookupIndexFile,
+                    new File[] { contentFile }, BLOCK_SIZE);
             for (int i = 0; i < DEFAULT_SIZE; ++i) {
-                assertEquals(table.get(i), newTable.get(i));
+                assertEquals(table.get(0, i), newTable.get(0, i));
             }
         } catch (IOException e) {
             Log.d(TAG, "IOException while flushing and realoding", e);
@@ -157,4 +136,60 @@
             runTestRandomSet();
         }
     }
+
+    public void testMultipleContents() {
+        final int numOfContents = 5;
+        generateRandomIndex(DEFAULT_SIZE, 20);
+        final SparseTable table = new SparseTable(DEFAULT_SIZE, BLOCK_SIZE, numOfContents);
+        for (int i = 0; i < mRandomIndex.size(); ++i) {
+            if (mRandomIndex.get(i) != SparseTable.NOT_EXIST) {
+                for (int j = 0; j < numOfContents; ++j) {
+                    table.set(j, i, mRandomIndex.get(i));
+                }
+            }
+        }
+
+        OutputStream lookupOutStream = null;
+        OutputStream[] contentsOutStream = new OutputStream[numOfContents];
+        try {
+            final File lookupIndexFile = File.createTempFile("testMultipleContents", "small");
+            lookupOutStream = new FileOutputStream(lookupIndexFile);
+            final File[] contentFiles = new File[numOfContents];
+            for (int i = 0; i < numOfContents; ++i) {
+                contentFiles[i] = File.createTempFile("testMultipleContents", "big" + i);
+                contentsOutStream[i] = new FileOutputStream(contentFiles[i]);
+            }
+            table.write(lookupOutStream, contentsOutStream);
+            lookupOutStream.flush();
+            for (int i = 0; i < numOfContents; ++i) {
+                contentsOutStream[i].flush();
+            }
+            final SparseTable newTable = SparseTable.readFromFiles(lookupIndexFile, contentFiles,
+                    BLOCK_SIZE);
+            for (int i = 0; i < numOfContents; ++i) {
+                for (int j = 0; j < DEFAULT_SIZE; ++j) {
+                    assertEquals(table.get(i, j), newTable.get(i, j));
+                }
+            }
+        } catch (IOException e) {
+            Log.d(TAG, "IOException while flushing and reloading", e);
+        } finally {
+            if (lookupOutStream != null) {
+                try {
+                    lookupOutStream.close();
+                } catch (IOException e) {
+                    Log.d(TAG, "IOException while closing the stream", e);
+                }
+            }
+            for (int i = 0; i < numOfContents; ++i) {
+                if (contentsOutStream[i] != null) {
+                    try {
+                        contentsOutStream[i].close();
+                    } catch (IOException e) {
+                        Log.d(TAG, "IOException while closing the stream.", e);
+                    }
+                }
+            }
+        }
+    }
 }