show more dictionary details when clicking and replacing

and kotlinize DictionaryHeader, because it's much easier to handle that way
This commit is contained in:
Helium314 2023-09-26 17:48:04 +02:00
parent 2be6eaa10d
commit df4bdce00d
5 changed files with 93 additions and 95 deletions

View file

@ -1,91 +0,0 @@
/*
* Copyright (C) 2014 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.
*/
package org.dslul.openboard.inputmethod.latin.makedict;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.dslul.openboard.inputmethod.latin.makedict.FormatSpec.DictionaryOptions;
import org.dslul.openboard.inputmethod.latin.makedict.FormatSpec.FormatOptions;
/**
* Class representing dictionary header.
*/
public final class DictionaryHeader {
public final int mBodyOffset;
@NonNull
public final DictionaryOptions mDictionaryOptions;
@NonNull
public final FormatOptions mFormatOptions;
@NonNull
public final String mLocaleString;
@NonNull
public final String mVersionString;
@NonNull
public final String mIdString;
// Note that these are corresponding definitions in native code in latinime::HeaderPolicy
// and latinime::HeaderReadWriteUtils.
// TODO: Standardize the key names and bump up the format version, taking care not to
// break format version 2 dictionaries.
public static final String DICTIONARY_VERSION_KEY = "version";
public static final String DICTIONARY_LOCALE_KEY = "locale";
public static final String DICTIONARY_ID_KEY = "dictionary";
public static final String DICTIONARY_DESCRIPTION_KEY = "description";
public static final String DICTIONARY_DATE_KEY = "date";
public static final String HAS_HISTORICAL_INFO_KEY = "HAS_HISTORICAL_INFO";
public static final String USES_FORGETTING_CURVE_KEY = "USES_FORGETTING_CURVE";
public static final String FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID_KEY =
"FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID";
public static final String MAX_UNIGRAM_COUNT_KEY = "MAX_UNIGRAM_ENTRY_COUNT";
public static final String MAX_BIGRAM_COUNT_KEY = "MAX_BIGRAM_ENTRY_COUNT";
public static final String MAX_TRIGRAM_COUNT_KEY = "MAX_TRIGRAM_ENTRY_COUNT";
public static final String ATTRIBUTE_VALUE_TRUE = "1";
public static final String CODE_POINT_TABLE_KEY = "codePointTable";
public DictionaryHeader(final int headerSize,
@NonNull final DictionaryOptions dictionaryOptions,
@NonNull final FormatOptions formatOptions) throws UnsupportedFormatException {
mDictionaryOptions = dictionaryOptions;
mFormatOptions = formatOptions;
mBodyOffset = formatOptions.mVersion < FormatSpec.VERSION4 ? headerSize : 0;
final String localeString = dictionaryOptions.mAttributes.get(DICTIONARY_LOCALE_KEY);
if (null == localeString) {
throw new UnsupportedFormatException("Cannot create a FileHeader without a locale");
}
final String versionString = dictionaryOptions.mAttributes.get(DICTIONARY_VERSION_KEY);
if (null == versionString) {
throw new UnsupportedFormatException(
"Cannot create a FileHeader without a version");
}
final String idString = dictionaryOptions.mAttributes.get(DICTIONARY_ID_KEY);
if (null == idString) {
throw new UnsupportedFormatException("Cannot create a FileHeader without an ID");
}
mLocaleString = localeString;
mVersionString = versionString;
mIdString = idString;
}
// Helper method to get the description
@Nullable
public String getDescription() {
// TODO: Right now each dictionary file comes with a description in its own language.
// It will display as is no matter the device's locale. It should be internationalized.
return mDictionaryOptions.mAttributes.get(DICTIONARY_DESCRIPTION_KEY);
}
}

View file

@ -0,0 +1,80 @@
/*
* Copyright (C) 2014 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.
*/
package org.dslul.openboard.inputmethod.latin.makedict
import org.dslul.openboard.inputmethod.latin.common.LocaleUtils
import org.dslul.openboard.inputmethod.latin.makedict.FormatSpec.DictionaryOptions
import org.dslul.openboard.inputmethod.latin.makedict.FormatSpec.FormatOptions
import java.text.DateFormat
import java.util.Date
import java.util.Locale
/**
* Class representing dictionary header.
*/
class DictionaryHeader(
headerSize: Int,
@JvmField
val mDictionaryOptions: DictionaryOptions,
val mFormatOptions: FormatOptions
) {
val mBodyOffset = if (mFormatOptions.mVersion < FormatSpec.VERSION4) headerSize else 0
val mLocaleString = mDictionaryOptions.mAttributes[DICTIONARY_LOCALE_KEY]
?: throw UnsupportedFormatException("Cannot create a FileHeader without a locale")
@JvmField
val mVersionString = mDictionaryOptions.mAttributes[DICTIONARY_VERSION_KEY]
?: throw UnsupportedFormatException(
"Cannot create a FileHeader without a version"
)
@JvmField
val mIdString = mDictionaryOptions.mAttributes[DICTIONARY_ID_KEY]
?: throw UnsupportedFormatException("Cannot create a FileHeader without an ID")
val mDate = mDictionaryOptions.mAttributes[DICTIONARY_DATE_KEY]?.toIntOrNull()
val description: String?
// Helper method to get the description
// TODO: Right now each dictionary file comes with a description in its own language.
// It will display as is no matter the device's locale. It should be internationalized.
get() = mDictionaryOptions.mAttributes[DICTIONARY_DESCRIPTION_KEY]
fun info(locale: Locale): String {
val date = if (mDate == null) ""
else DateFormat.getDateInstance(DateFormat.SHORT, locale).format(Date(mDate * 1000L)) + "\n"
return mIdString + "\n" + LocaleUtils.constructLocaleFromString(mLocaleString).getDisplayName(locale) +
"\nv" + mVersionString + "\n" + date + description
}
companion object {
// Note that these are corresponding definitions in native code in latinime::HeaderPolicy
// and latinime::HeaderReadWriteUtils.
// TODO: Standardize the key names and bump up the format version, taking care not to
// break format version 2 dictionaries.
const val DICTIONARY_VERSION_KEY = "version"
const val DICTIONARY_LOCALE_KEY = "locale"
const val DICTIONARY_ID_KEY = "dictionary"
const val DICTIONARY_DESCRIPTION_KEY = "description"
const val DICTIONARY_DATE_KEY = "date"
const val HAS_HISTORICAL_INFO_KEY = "HAS_HISTORICAL_INFO"
const val USES_FORGETTING_CURVE_KEY = "USES_FORGETTING_CURVE"
const val FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID_KEY =
"FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID"
const val MAX_UNIGRAM_COUNT_KEY = "MAX_UNIGRAM_ENTRY_COUNT"
const val MAX_BIGRAM_COUNT_KEY = "MAX_BIGRAM_ENTRY_COUNT"
const val MAX_TRIGRAM_COUNT_KEY = "MAX_TRIGRAM_ENTRY_COUNT"
const val ATTRIBUTE_VALUE_TRUE = "1"
const val CODE_POINT_TABLE_KEY = "codePointTable"
}
}

View file

@ -242,10 +242,8 @@ class LanguageSettingsDialog(
} }
rowBinding.languageText.setOnClickListener { rowBinding.languageText.setOnClickListener {
if (header == null) return@setOnClickListener if (header == null) return@setOnClickListener
val locale = LocaleUtils.constructLocaleFromString(header.mLocaleString).getDisplayName(context.resources.configuration.locale)
val message = dictType + "\n" + locale + "\nv" + header.mVersionString + "\n" + header.description
Builder(context) Builder(context)
.setMessage(message) .setMessage(header.info(context.resources.configuration.locale))
.setPositiveButton(android.R.string.ok, null) .setPositiveButton(android.R.string.ok, null)
.show() .show()
} }

View file

@ -13,6 +13,7 @@ import org.dslul.openboard.inputmethod.latin.makedict.DictionaryHeader
import org.dslul.openboard.inputmethod.latin.settings.* import org.dslul.openboard.inputmethod.latin.settings.*
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.text.DateFormat
import java.util.* import java.util.*
class NewDictionaryAdder(private val context: Context, private val onAdded: ((Boolean, File) -> Unit)?) { class NewDictionaryAdder(private val context: Context, private val onAdded: ((Boolean, File) -> Unit)?) {
@ -128,7 +129,11 @@ class NewDictionaryAdder(private val context: Context, private val onAdded: ((Bo
if (!dictFile.exists()) { if (!dictFile.exists()) {
return moveDict(false) return moveDict(false)
} }
confirmDialog(context, context.getString(R.string.replace_dictionary_message, dictionaryType), context.getString(
val systemLocale = context.resources.configuration.locale
val newInfo = header.info(systemLocale)
val oldInfo = DictionaryInfoUtils.getDictionaryFileHeaderOrNull(dictFile, 0, dictFile.length())?.info(systemLocale)
confirmDialog(context, context.getString(R.string.replace_dictionary_message2, dictionaryType, newInfo, oldInfo), context.getString(
R.string.replace_dictionary)) { R.string.replace_dictionary)) {
moveDict(true) moveDict(true)
} }

View file

@ -459,6 +459,12 @@ disposition rather than other common dispositions for Latin languages. [CHAR LIM
<string name="button_add_to_language">"Add to %s"</string> <string name="button_add_to_language">"Add to %s"</string>
<!-- Message for user dictionary replacement dialog --> <!-- Message for user dictionary replacement dialog -->
<string name="replace_dictionary_message">"Really replace user-added dictionary \"%s\"?"</string> <string name="replace_dictionary_message">"Really replace user-added dictionary \"%s\"?"</string>
<!-- Message for user dictionary replacement dialog -->
<string name="replace_dictionary_message2">"Really replace user-added dictionary \"%1$s\"?\n
Current dictionary:
%2$s\n
New dictionary:
%3$s"</string>
<!-- Title and confirm button text for user dictionary replacement dialog --> <!-- Title and confirm button text for user dictionary replacement dialog -->
<string name="replace_dictionary">"Replace dictionary"</string> <string name="replace_dictionary">"Replace dictionary"</string>
<!-- Message for user dictionary remove dialog --> <!-- Message for user dictionary remove dialog -->