am b56260b8: [AD4] Actually copy the file in place.

* commit 'b56260b8917233a902c8cce45364850afc0b99b2':
  [AD4] Actually copy the file in place.
main
Jean Chalard 2013-01-21 01:29:26 -08:00 committed by Android Git Automerger
commit e7ef116f11
5 changed files with 67 additions and 16 deletions

View File

@ -383,6 +383,8 @@
<string name="read_external_dictionary_multiple_files_title">Select a dictionary file to install</string> <string name="read_external_dictionary_multiple_files_title">Select a dictionary file to install</string>
<!-- Title of the confirmation dialog to install a file as an external dictionary [CHAR LIMIT=50] --> <!-- Title of the confirmation dialog to install a file as an external dictionary [CHAR LIMIT=50] -->
<string name="read_external_dictionary_confirm_install_message">Really install this file for <xliff:g id="locale_name">%s</xliff:g>?</string> <string name="read_external_dictionary_confirm_install_message">Really install this file for <xliff:g id="locale_name">%s</xliff:g>?</string>
<!-- Title for an error dialog that contains the details of the error in the body [CHAR LIMIT=80] -->
<string name="error">There was an error</string>
<!-- Title of the button to revert to the default value of the device in the settings dialog [CHAR LIMIT=15] --> <!-- Title of the button to revert to the default value of the device in the settings dialog [CHAR LIMIT=15] -->
<string name="button_default">Default</string> <string name="button_default">Default</string>

View File

@ -25,6 +25,7 @@ import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -162,9 +163,9 @@ public final class BinaryDictionaryFileDumper {
InputStream inputStream = null; InputStream inputStream = null;
InputStream uncompressedStream = null; InputStream uncompressedStream = null;
InputStream decryptedStream = null; InputStream decryptedStream = null;
BufferedInputStream bufferedStream = null; BufferedInputStream bufferedInputStream = null;
File outputFile = null; File outputFile = null;
FileOutputStream outputStream = null; BufferedOutputStream bufferedOutputStream = null;
AssetFileDescriptor afd = null; AssetFileDescriptor afd = null;
final Uri wordListUri = wordListUriBuilder.build(); final Uri wordListUri = wordListUriBuilder.build();
try { try {
@ -178,7 +179,6 @@ public final class BinaryDictionaryFileDumper {
// Just to be sure, delete the file. This may fail silently, and return false: this // Just to be sure, delete the file. This may fail silently, and return false: this
// is the right thing to do, as we just want to continue anyway. // is the right thing to do, as we just want to continue anyway.
outputFile.delete(); outputFile.delete();
outputStream = new FileOutputStream(outputFile);
// Get the appropriate decryption method for this try // Get the appropriate decryption method for this try
switch (mode) { switch (mode) {
case COMPRESSED_CRYPTED_COMPRESSED: case COMPRESSED_CRYPTED_COMPRESSED:
@ -206,10 +206,11 @@ public final class BinaryDictionaryFileDumper {
inputStream = originalSourceStream; inputStream = originalSourceStream;
break; break;
} }
bufferedStream = new BufferedInputStream(inputStream); bufferedInputStream = new BufferedInputStream(inputStream);
checkMagicAndCopyFileTo(bufferedStream, outputStream); bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
outputStream.flush(); checkMagicAndCopyFileTo(bufferedInputStream, bufferedOutputStream);
outputStream.close(); bufferedOutputStream.flush();
bufferedOutputStream.close();
final File finalFile = new File(finalFileName); final File finalFile = new File(finalFileName);
finalFile.delete(); finalFile.delete();
if (!outputFile.renameTo(finalFile)) { if (!outputFile.renameTo(finalFile)) {
@ -241,12 +242,12 @@ public final class BinaryDictionaryFileDumper {
if (null != inputStream) inputStream.close(); if (null != inputStream) inputStream.close();
if (null != uncompressedStream) uncompressedStream.close(); if (null != uncompressedStream) uncompressedStream.close();
if (null != decryptedStream) decryptedStream.close(); if (null != decryptedStream) decryptedStream.close();
if (null != bufferedStream) bufferedStream.close(); if (null != bufferedInputStream) bufferedInputStream.close();
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "Exception while closing a file descriptor : " + e); Log.e(TAG, "Exception while closing a file descriptor : " + e);
} }
try { try {
if (null != outputStream) outputStream.close(); if (null != bufferedOutputStream) bufferedOutputStream.close();
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "Exception while closing a file : " + e); Log.e(TAG, "Exception while closing a file : " + e);
} }
@ -301,9 +302,8 @@ public final class BinaryDictionaryFileDumper {
* @param input the stream to be copied. * @param input the stream to be copied.
* @param output an output stream to copy the data to. * @param output an output stream to copy the data to.
*/ */
// TODO: make output a BufferedOutputStream public static void checkMagicAndCopyFileTo(final BufferedInputStream input,
private static void checkMagicAndCopyFileTo(final BufferedInputStream input, final BufferedOutputStream output) throws FileNotFoundException, IOException {
final FileOutputStream output) throws FileNotFoundException, IOException {
// Check the magic number // Check the magic number
final int length = MAGIC_NUMBER_VERSION_2.length; final int length = MAGIC_NUMBER_VERSION_2.length;
final byte[] magicNumberBuffer = new byte[length]; final byte[] magicNumberBuffer = new byte[length];

View File

@ -56,7 +56,7 @@ final class BinaryDictionaryGetter {
private static final String COMMON_PREFERENCES_NAME = "LatinImeDictPrefs"; private static final String COMMON_PREFERENCES_NAME = "LatinImeDictPrefs";
// Name of the category for the main dictionary // Name of the category for the main dictionary
private static final String MAIN_DICTIONARY_CATEGORY = "main"; public static final String MAIN_DICTIONARY_CATEGORY = "main";
public static final String ID_CATEGORY_SEPARATOR = ":"; public static final String ID_CATEGORY_SEPARATOR = ":";
// The key considered to read the version attribute in a dictionary file. // The key considered to read the version attribute in a dictionary file.

View File

@ -77,6 +77,7 @@ public final class DebugSettings extends PreferenceFragment
public boolean onPreferenceClick(final Preference arg0) { public boolean onPreferenceClick(final Preference arg0) {
ExternalDictionaryGetterForDebug.chooseAndInstallDictionary( ExternalDictionaryGetterForDebug.chooseAndInstallDictionary(
getActivity()); getActivity());
mServiceNeedsRestart = true;
return true; return true;
} }
}); });

View File

@ -21,12 +21,17 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.os.Environment; import android.os.Environment;
import android.util.Log;
import com.android.inputmethod.latin.makedict.BinaryDictIOUtils; import com.android.inputmethod.latin.makedict.BinaryDictIOUtils;
import com.android.inputmethod.latin.makedict.FormatSpec.FileHeader; import com.android.inputmethod.latin.makedict.FormatSpec.FileHeader;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException; import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Locale; import java.util.Locale;
@ -121,13 +126,56 @@ public class ExternalDictionaryGetterForDebug {
}).setPositiveButton(android.R.string.ok, new OnClickListener() { }).setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override @Override
public void onClick(final DialogInterface dialog, final int which) { public void onClick(final DialogInterface dialog, final int which) {
installFile(file, header); installFile(context, file, header);
dialog.dismiss(); dialog.dismiss();
} }
}).create().show(); }).create().show();
} }
private static void installFile(final File file, final FileHeader header) { private static void installFile(final Context context, final File file,
// TODO: actually install the dictionary final FileHeader header) {
BufferedOutputStream outputStream = null;
File tempFile = null;
try {
final String locale =
header.mDictionaryOptions.mAttributes.get(DICTIONARY_LOCALE_ATTRIBUTE);
// Create the id for a main dictionary for this locale
final String id = BinaryDictionaryGetter.MAIN_DICTIONARY_CATEGORY
+ BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR + locale;
final String finalFileName =
BinaryDictionaryGetter.getCacheFileName(id, locale, context);
final String tempFileName = BinaryDictionaryGetter.getTempFileName(id, context);
tempFile = new File(tempFileName);
tempFile.delete();
outputStream = new BufferedOutputStream(new FileOutputStream(tempFile));
final BufferedInputStream bufferedStream = new BufferedInputStream(
new FileInputStream(file));
BinaryDictionaryFileDumper.checkMagicAndCopyFileTo(bufferedStream, outputStream);
outputStream.flush();
final File finalFile = new File(finalFileName);
finalFile.delete();
if (!tempFile.renameTo(finalFile)) {
throw new IOException("Can't move the file to its final name");
}
} catch (IOException e) {
// There was an error: show a dialog
new AlertDialog.Builder(context)
.setTitle(R.string.error)
.setMessage(e.toString())
.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
dialog.dismiss();
}
}).create().show();
return;
} finally {
try {
if (null != outputStream) outputStream.close();
if (null != tempFile) tempFile.delete();
} catch (IOException e) {
// Don't do anything
}
}
} }
} }