Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update feature-external-dicts #578

Open
wants to merge 1 commit into
base: feature-external-dicts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ public static File[] getCachedWordLists(final String locale, final Context conte
for (File directory : directoryList) {
if (!directory.isDirectory()) continue;
final String dirLocale =
DictionaryInfoUtils.getWordListIdFromFileName(directory.getName());
final int matchLevel = LocaleUtils.getMatchLevel(dirLocale, locale);
DictionaryInfoUtils.getWordListIdFromFileName(directory.getName()).toLowerCase(Locale.ENGLISH);
final int matchLevel = LocaleUtils.getMatchLevel(dirLocale, locale.toLowerCase(Locale.ENGLISH));
if (LocaleUtils.isMatch(matchLevel)) {
final File[] wordLists = directory.listFiles();
if (null != wordLists) {
Expand Down Expand Up @@ -314,10 +314,9 @@ public static File loadDictionaryFromAssets(final String locale, final Context c
if (bestMatchName == null) return null;

// we have a match, now copy contents of the dictionary to cached word lists folder
// but take care to use the correct case, as it will need to match with locale.toString()
final String bestMatchCaseCorrected = LocaleUtils.constructLocaleFromString(bestMatchName).toString();
File dictFile = new File(DictionaryInfoUtils.getCacheDirectoryForLocale(bestMatchCaseCorrected, context) +
File.separator + DictionaryInfoUtils.MAIN_DICTIONARY_INTERNAL_FILE_NAME);
final String bestMatchLocale = extractLocaleFromAssetsDictionaryFile(bestMatchName);
File dictFile = new File(DictionaryInfoUtils.getCacheDirectoryForLocale(bestMatchLocale, context) +
File.separator + DictionaryInfoUtils.getMainDictFilename(bestMatchLocale));
try {
FileUtils.copyStreamToNewFile(
context.getAssets().open(ASSETS_DICTIONARY_FOLDER + File.separator + bestMatchName),
Expand All @@ -337,10 +336,10 @@ public static File loadDictionaryFromAssets(final String locale, final Context c
* Returns the locale, or null if file name does not match the pattern
*/
public static String extractLocaleFromAssetsDictionaryFile(final String dictionaryFileName) {
if (dictionaryFileName.startsWith(BinaryDictionaryGetter.MAIN_DICTIONARY_CATEGORY)
if (dictionaryFileName.startsWith(DictionaryInfoUtils.MAIN_DICT_PREFIX)
&& dictionaryFileName.endsWith(".dict")) {
return dictionaryFileName.substring(
BinaryDictionaryGetter.MAIN_DICTIONARY_CATEGORY.length() + 1,
DictionaryInfoUtils.MAIN_DICT_PREFIX.length(),
dictionaryFileName.lastIndexOf('.')
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.os.Bundle
import android.preference.Preference
import android.text.Html
import android.text.method.LinkMovementMethod
import android.util.Log
import android.util.TypedValue
import android.view.Menu
import android.view.MenuInflater
Expand Down Expand Up @@ -54,14 +53,14 @@ class DictionarySettingsFragment : SubScreenFragment() {
val internalDicts = mutableSetOf<Locale>()
// get available dictionaries
// cached (internal in use and user dicts)
DictionaryInfoUtils.getCachedDirectoryList(activity).forEach { dir ->
DictionaryInfoUtils.getCachedDirectoryList(activity)?.forEach { dir ->
if (!dir.isDirectory)
return@forEach
dir.list()?.forEach {
if (it == DictionaryInfoUtils.MAIN_DICTIONARY_INTERNAL_FILE_NAME)
internalDicts.add(dir.name.toLocale())
else if (it == DictionaryInfoUtils.MAIN_DICTIONARY_USER_FILE_NAME)
if (it == DictionaryInfoUtils.MAIN_DICTIONARY_USER_FILE_NAME)
userDicts.add(dir.name.toLocale())
else if (it.startsWith(DictionaryInfoUtils.MAIN_DICT_PREFIX))
internalDicts.add(dir.name.toLocale())
}
}
// internal only
Expand All @@ -70,7 +69,6 @@ class DictionarySettingsFragment : SubScreenFragment() {
internalDicts.add(it.toLocale())
}
}
Log.i("opb_", "user: $userDicts, internal: $internalDicts")

// TODO: decide sort order: alphabetic? or user and then built-in?
// split using categories?
Expand All @@ -84,7 +82,6 @@ class DictionarySettingsFragment : SubScreenFragment() {
// open dialog for update or delete / reset
currentDictLocale = dict
currentDictState = if (internalDicts.contains(dict)) DICT_INTERNAL_AND_USER else DICT_USER_ONLY
Log.i("opb_", "show for $dict, user: ${userDicts.contains(dict)}, internal: ${internalDicts.contains(dict)}")
showUpdateDialog()
true
}
Expand All @@ -100,7 +97,6 @@ class DictionarySettingsFragment : SubScreenFragment() {
// open dialog for update, maybe disabling if i can make it work?
currentDictLocale = dict
currentDictState = DICT_INTERNAL_ONLY
Log.i("opb_", "show for $dict, user: ${userDicts.contains(dict)}, internal: ${internalDicts.contains(dict)}")
showUpdateDialog()
true
}
Expand All @@ -119,8 +115,6 @@ class DictionarySettingsFragment : SubScreenFragment() {
if (currentDictLocale == null && currentDictState != DICT_NEW)
return

Log.i("opb_", "exists for user: $currentDictExistsForUser")

val link = "<a href='$DICTIONARY_URL'>" +
resources.getString(R.string.dictionary_link_text) + "</a>"
val message = if (currentDictState == DICT_NEW)
Expand Down Expand Up @@ -291,7 +285,7 @@ class DictionarySettingsFragment : SubScreenFragment() {
DictionaryInfoUtils.getCacheDirectoryForLocale(this.toString(), activity) + File.separator + DictionaryInfoUtils.MAIN_DICTIONARY_USER_FILE_NAME

private fun Locale.getInternalDictFilename() =
DictionaryInfoUtils.getCacheDirectoryForLocale(this.toString(), activity) + File.separator + DictionaryInfoUtils.MAIN_DICTIONARY_INTERNAL_FILE_NAME
DictionaryInfoUtils.getCacheDirectoryForLocale(this.toString(), activity) + File.separator + DictionaryInfoUtils.getMainDictFilename(this.toString())

private fun String.displayName() = LocaleUtils.constructLocaleFromString(this)
.getDisplayName(resources.configuration.locale)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public class DictionaryInfoUtils {
private static final String TAG = DictionaryInfoUtils.class.getSimpleName();
public static final String RESOURCE_PACKAGE_NAME = R.class.getPackage().getName();
private static final String DEFAULT_MAIN_DICT = "main";
private static final String MAIN_DICT_PREFIX = "main_";
public static final String MAIN_DICTIONARY_INTERNAL_FILE_NAME = DEFAULT_MAIN_DICT + ".dict";
public static final String MAIN_DICT_PREFIX = DEFAULT_MAIN_DICT + "_";
public static final String MAIN_DICTIONARY_USER_FILE_NAME = MAIN_DICT_PREFIX + "user.dict";
private static final String DICTIONARY_CATEGORY_SEPARATOR_EXPRESSION = "[" + BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR + "_]";
private static final String DECODER_DICT_SUFFIX = DecoderSpecificConstants.DECODER_DICT_SUFFIX;
// 6 digits - unicode is limited to 21 bits
private static final int MAX_HEX_DIGITS_FOR_CODEPOINT = 6;
Expand Down Expand Up @@ -214,10 +214,12 @@ public static File[] getStagingDirectoryList(final Context context) {
@Nullable
public static String getCategoryFromFileName(@Nonnull final String fileName) {
final String id = getWordListIdFromFileName(fileName);
final String[] idArray = id.split(BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR);
final String[] idArray = id.split(DICTIONARY_CATEGORY_SEPARATOR_EXPRESSION);
// An id is supposed to be in format category:locale, so splitting on the separator
// should yield a 2-elements array
if (2 != idArray.length) {
// Also allow '_' as separator, this is ok for locales like pt_br because
// we're interested in the part before first separator anyway
if (1 == idArray.length) {
return null;
}
return idArray[0];
Expand All @@ -227,7 +229,7 @@ public static String getCategoryFromFileName(@Nonnull final String fileName) {
* Find out the cache directory associated with a specific locale.
*/
public static String getCacheDirectoryForLocale(final String locale, final Context context) {
final String relativeDirectoryName = replaceFileNameDangerousCharacters(locale);
final String relativeDirectoryName = replaceFileNameDangerousCharacters(locale).toLowerCase(Locale.ENGLISH);
final String absoluteDirectoryName = getWordListCacheDirectory(context) + File.separator
+ relativeDirectoryName;
final File directory = new File(absoluteDirectoryName);
Expand All @@ -240,11 +242,13 @@ public static String getCacheDirectoryForLocale(final String locale, final Conte
}

public static boolean isMainWordListId(final String id) {
final String[] idArray = id.split(BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR);
final String[] idArray = id.split(DICTIONARY_CATEGORY_SEPARATOR_EXPRESSION);
// An id is supposed to be in format category:locale, so splitting on the separator
// should yield a 2-elements array
if (2 != idArray.length) {
return id.startsWith(BinaryDictionaryGetter.MAIN_DICTIONARY_CATEGORY);
// Also allow '_' as separator, this is ok for locales like pt_br because
// we're interested in the part before first separator anyway
if (1 == idArray.length) {
return false;
}
return BinaryDictionaryGetter.MAIN_DICTIONARY_CATEGORY.equals(idArray[0]);
}
Expand Down Expand Up @@ -320,6 +324,10 @@ public static String getMainDictId(@Nonnull final Locale locale) {
BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR + locale.toString().toLowerCase();
}

public static String getMainDictFilename(@Nonnull final String locale) {
return MAIN_DICT_PREFIX + locale.toLowerCase(Locale.ENGLISH) + ".dict";
}

public static DictionaryHeader getDictionaryFileHeaderOrNull(final File file,
final long offset, final long length) {
try {
Expand Down