diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin')
3 files changed, 52 insertions, 19 deletions
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java index e95172d1f..38563be4a 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java @@ -25,12 +25,15 @@ import android.net.Uri; import android.text.TextUtils; import android.util.Log; +import java.io.BufferedInputStream; +import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Locale; @@ -46,7 +49,9 @@ public class BinaryDictionaryFileDumper { /** * The size of the temporary buffer to copy files. */ - static final int FILE_READ_BUFFER_SIZE = 1024; + private static final int FILE_READ_BUFFER_SIZE = 1024; + // TODO: make the following data common with the native code + private static final byte[] MAGIC_NUMBER = new byte[] { 0x78, (byte)0xB1 }; private static final String DICTIONARY_PROJECTION[] = { "id" }; @@ -135,6 +140,7 @@ public class BinaryDictionaryFileDumper { for (int mode = MODE_MIN; mode <= MODE_MAX; ++mode) { InputStream originalSourceStream = null; InputStream inputStream = null; + File outputFile = null; FileOutputStream outputStream = null; AssetFileDescriptor afd = null; try { @@ -144,7 +150,8 @@ public class BinaryDictionaryFileDumper { if (null == afd) return null; originalSourceStream = afd.createInputStream(); // Open output. - outputStream = new FileOutputStream(outputFileName); + outputFile = new File(outputFileName); + outputStream = new FileOutputStream(outputFile); // Get the appropriate decryption method for this try switch (mode) { case COMPRESSED_CRYPTED_COMPRESSED: @@ -171,7 +178,7 @@ public class BinaryDictionaryFileDumper { inputStream = originalSourceStream; break; } - copyFileTo(inputStream, outputStream); + checkMagicAndCopyFileTo(new BufferedInputStream(inputStream), outputStream); if (0 >= resolver.delete(wordListUri, null, null)) { Log.e(TAG, "Could not have the dictionary pack delete a word list"); } @@ -181,6 +188,12 @@ public class BinaryDictionaryFileDumper { if (DEBUG) { Log.i(TAG, "Can't open word list in mode " + mode + " : " + e); } + if (null != outputFile) { + // This may or may not fail. The file may not have been created if the + // exception was thrown before it could be. Hence, both failure and + // success are expected outcomes, so we don't check the return value. + outputFile.delete(); + } // Try the next method. } finally { // Ignore exceptions while closing files. @@ -234,12 +247,29 @@ public class BinaryDictionaryFileDumper { } /** - * Copies the data in an input stream to a target file. + * Copies the data in an input stream to a target file if the magic number matches. + * + * If the magic number does not match the expected value, this method throws an + * IOException. Other usual conditions for IOException or FileNotFoundException + * also apply. + * * @param input the stream to be copied. * @param outputFile an outputstream to copy the data to. */ - private static void copyFileTo(final InputStream input, final FileOutputStream output) - throws FileNotFoundException, IOException { + private static void checkMagicAndCopyFileTo(final BufferedInputStream input, + final FileOutputStream output) throws FileNotFoundException, IOException { + // Check the magic number + final byte[] magicNumberBuffer = new byte[MAGIC_NUMBER.length]; + final int readMagicNumberSize = input.read(magicNumberBuffer, 0, MAGIC_NUMBER.length); + if (readMagicNumberSize < MAGIC_NUMBER.length) { + throw new IOException("Less bytes to read than the magic number length"); + } + if (!Arrays.equals(MAGIC_NUMBER, magicNumberBuffer)) { + throw new IOException("Wrong magic number for downloaded file"); + } + output.write(MAGIC_NUMBER); + + // Actually copy the file final byte[] buffer = new byte[FILE_READ_BUFFER_SIZE]; for (int readBytes = input.read(buffer); readBytes >= 0; readBytes = input.read(buffer)) output.write(buffer, 0, readBytes); diff --git a/java/src/com/android/inputmethod/latin/CandidateView.java b/java/src/com/android/inputmethod/latin/CandidateView.java index f445abf48..b9ded31cb 100644 --- a/java/src/com/android/inputmethod/latin/CandidateView.java +++ b/java/src/com/android/inputmethod/latin/CandidateView.java @@ -59,8 +59,6 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo // The maximum number of suggestions available. See {@link Suggest#mPrefMaxSuggestions}. private static final int MAX_SUGGESTIONS = 18; - private static final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT; - private static final int MATCH_PARENT = ViewGroup.LayoutParams.MATCH_PARENT; private static final boolean DBG = LatinImeLogger.sDBG; @@ -155,7 +153,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo final TextView word = words.get(0); final View divider = dividers.get(0); mPadding = word.getCompoundPaddingLeft() + word.getCompoundPaddingRight(); - divider.measure(WRAP_CONTENT, MATCH_PARENT); + divider.measure( + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); mDividerWidth = divider.getMeasuredWidth(); mDividerHeight = divider.getMeasuredHeight(); @@ -224,7 +223,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo lastView = word; if (x == 0) centeringFrom = word; - word.measure(WRAP_CONTENT, + word.measure(ViewGroup.LayoutParams.WRAP_CONTENT, MeasureSpec.makeMeasureSpec(mCandidateStripHeight, MeasureSpec.EXACTLY)); final int width = word.getMeasuredWidth(); final int height = word.getMeasuredHeight(); @@ -234,7 +233,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo if (info != null) { paneView.addView(info); lastView = info; - info.measure(WRAP_CONTENT, WRAP_CONTENT); + info.measure(ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT); final int infoWidth = info.getMeasuredWidth(); FrameLayoutCompatUtils.placeViewAt( info, x - infoWidth, y, infoWidth, info.getMeasuredHeight()); @@ -430,7 +430,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo word.setText(text); // TextView.setText() resets text scale x to 1.0. word.setTextScaleX(scaleX); stripView.addView(word); - setLayoutWeight(word, getCandidateWeight(index), MATCH_PARENT); + setLayoutWeight( + word, getCandidateWeight(index), ViewGroup.LayoutParams.MATCH_PARENT); x += word.getMeasuredWidth(); if (DBG) { @@ -439,7 +440,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo final TextView info = mInfos.get(pos); info.setText(debugInfo); placer.addView(info); - info.measure(WRAP_CONTENT, WRAP_CONTENT); + info.measure(ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT); final int infoWidth = info.getMeasuredWidth(); final int y = info.getMeasuredHeight(); FrameLayoutCompatUtils.placeViewAt( @@ -515,7 +517,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo wordView.setText(text); wordView.setTextScaleX(wordScaleX); stripView.addView(wordView); - setLayoutWeight(wordView, mCenterCandidateWeight, MATCH_PARENT); + setLayoutWeight(wordView, mCenterCandidateWeight, ViewGroup.LayoutParams.MATCH_PARENT); stripView.addView(mDividers.get(0)); @@ -526,7 +528,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo hintView.setText(mHintToSaveText); hintView.setTextScaleX(hintScaleX); stripView.addView(hintView); - setLayoutWeight(hintView, 1.0f - mCenterCandidateWeight, MATCH_PARENT); + setLayoutWeight( + hintView, 1.0f - mCenterCandidateWeight, ViewGroup.LayoutParams.MATCH_PARENT); } } @@ -558,7 +561,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo mPreviewPopup = new PopupWindow(context); mPreviewText = (TextView) inflater.inflate(R.layout.candidate_preview, null); - mPreviewPopup.setWindowLayoutMode(WRAP_CONTENT, WRAP_CONTENT); + mPreviewPopup.setWindowLayoutMode( + ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); mPreviewPopup.setContentView(mPreviewText); mPreviewPopup.setBackgroundDrawable(null); @@ -796,8 +800,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo final TextView previewText = mPreviewText; previewText.setTextColor(mStripParams.mColorTypedWord); previewText.setText(word); - previewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), - MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); + previewText.measure( + ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); final int[] offsetInWindow = new int[2]; view.getLocationInWindow(offsetInWindow); final int posX = offsetInWindow[0]; diff --git a/java/src/com/android/inputmethod/latin/Settings.java b/java/src/com/android/inputmethod/latin/Settings.java index a5eed9015..b79c441d7 100644 --- a/java/src/com/android/inputmethod/latin/Settings.java +++ b/java/src/com/android/inputmethod/latin/Settings.java @@ -73,7 +73,6 @@ public class Settings extends InputMethodSettingsActivity public static final String PREF_AUTO_CORRECTION_THRESHOLD = "auto_correction_threshold"; public static final String PREF_DEBUG_SETTINGS = "debug_settings"; - public static final String PREF_NGRAM_SETTINGS_KEY = "ngram_settings"; public static final String PREF_BIGRAM_SUGGESTIONS = "bigram_suggestion"; public static final String PREF_BIGRAM_PREDICTIONS = "bigram_prediction"; |