only use locale.toString directly where necessary

stick to Locale as long as possible
This commit is contained in:
Helium314 2024-01-30 14:23:31 +01:00
parent 6211f42963
commit 0ba7b6370a
4 changed files with 39 additions and 40 deletions

View file

@ -136,14 +136,12 @@ public class UserDictionaryAddWordContents {
return;
final ContentResolver resolver = context.getContentResolver();
// Remove the old entry.
UserDictionarySettings.deleteWordInEditMode(mOldWord, mOldShortcut, mOldWeight, mLocale.toString(), resolver);
UserDictionarySettings.deleteWordInEditMode(mOldWord, mOldShortcut, mOldWeight, mLocale, resolver);
}
// requires use of locale string for interaction with Android system
public final int apply(@NonNull final Context context) {
final ContentResolver resolver = context.getContentResolver();
final String newWord = mWordEditText.getText().toString();
final String localeString = mLocale.toString();
if (TextUtils.isEmpty(newWord)) {
// If the word is empty, don't insert it.
@ -163,19 +161,19 @@ public class UserDictionaryAddWordContents {
mSavedWord = newWord;
// In edit mode, everything is modified without overwriting other existing words
if (MODE_EDIT == mMode && hasWord(newWord, localeString, context) && newWord.equals(mOldWord)) {
UserDictionarySettings.deleteWordInEditMode(mOldWord, mOldShortcut, mOldWeight, localeString, resolver);
if (MODE_EDIT == mMode && hasWord(newWord, mLocale, context) && newWord.equals(mOldWord)) {
UserDictionarySettings.deleteWordInEditMode(mOldWord, mOldShortcut, mOldWeight, mLocale, resolver);
} else {
mMode = MODE_INSERT;
}
if (mMode == MODE_INSERT && hasWord(newWord, localeString, context)) {
if (mMode == MODE_INSERT && hasWord(newWord, mLocale, context)) {
return CODE_ALREADY_PRESENT;
}
if (mMode == MODE_INSERT) {
// Delete duplicate when adding or updating new word
UserDictionarySettings.deleteWordInEditMode(mOldWord, mOldShortcut, mOldWeight, localeString, resolver);
UserDictionarySettings.deleteWordInEditMode(mOldWord, mOldShortcut, mOldWeight, mLocale, resolver);
// Update the existing word by adding a new one
UserDictionary.Words.addWord(context, newWord, Integer.parseInt(mSavedWeight),
mSavedShortcut, TextUtils.isEmpty(mLocale.toString()) ? null : mLocale);
@ -184,12 +182,12 @@ public class UserDictionaryAddWordContents {
}
// Delete duplicates
UserDictionarySettings.deleteWord(newWord, localeString, resolver);
UserDictionarySettings.deleteWord(newWord, mLocale, resolver);
// In this class we use the empty string to represent 'all locales' and mLocale cannot
// be null. However the addWord method takes null to mean 'all locales'.
UserDictionary.Words.addWord(context, newWord, Integer.parseInt(mSavedWeight),
mSavedShortcut, TextUtils.isEmpty(mLocale.toString()) ? null : mLocale);
mSavedShortcut, mLocale.equals(UserDictionarySettings.emptyLocale) ? null : mLocale);
return CODE_WORD_ADDED;
}
@ -197,7 +195,7 @@ public class UserDictionaryAddWordContents {
public boolean isExistingWord(final Context context) {
final String newWord = mWordEditText.getText().toString();
if (mMode != MODE_EDIT) {
return hasWord(newWord, mLocale.toString(), context);
return hasWord(newWord, mLocale, context);
} else {
return false;
}
@ -209,18 +207,18 @@ public class UserDictionaryAddWordContents {
private static final String HAS_WORD_AND_ALL_LOCALES_SELECTION = UserDictionary.Words.WORD + "=? AND "
+ UserDictionary.Words.LOCALE + " is null";
// requires use of locale string for interaction with Android system
private boolean hasWord(final String word, final String localeString, final Context context) {
private boolean hasWord(final String word, final Locale locale, final Context context) {
final Cursor cursor;
if ("".equals(localeString)) {
if (locale.equals(UserDictionarySettings.emptyLocale)) {
cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI,
HAS_WORD_PROJECTION, HAS_WORD_AND_ALL_LOCALES_SELECTION,
new String[] { word }, null);
} else {
// requires use of locale string for interaction with Android system
cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI,
HAS_WORD_PROJECTION, HAS_WORD_AND_LOCALE_SELECTION,
new String[] { word, localeString}, null);
new String[] { word, locale.toString()}, null);
}
try {
if (null == cursor) return false;
@ -273,7 +271,7 @@ public class UserDictionaryAddWordContents {
// mLocale is removed from the language list as it will be added to the top of the list
sortedLocales.remove(mLocale);
// "For all languages" is removed from the language list as it will be added at the end of the list
sortedLocales.remove(new Locale(""));
sortedLocales.remove(UserDictionarySettings.emptyLocale);
// final list of locales to show
final ArrayList<LocaleRenderer> localesList = new ArrayList<>();
@ -286,8 +284,8 @@ public class UserDictionaryAddWordContents {
}
// Finally, add "All languages" at the end of the list
if (!"".equals(mLocale.toString())) {
addLocaleDisplayNameToList(context, localesList, new Locale(""));
if (!mLocale.equals(UserDictionarySettings.emptyLocale)) {
addLocaleDisplayNameToList(context, localesList, UserDictionarySettings.emptyLocale);
}
return localesList;

View file

@ -173,7 +173,7 @@ public class UserDictionaryAddWordFragment extends SubScreenFragment {
localesList.remove(position);
// The other languages are then sorted alphabetically by name, with the exception of "For all languages"
Collections.sort(localesList, (localeRenderer1, localeRenderer2) -> {
if (!localeRenderer1.getLocaleString().equals("") && !localeRenderer2.getLocaleString().equals("")) {
if (!localeRenderer1.getLocale().equals(UserDictionarySettings.emptyLocale) && !localeRenderer2.getLocale().equals(UserDictionarySettings.emptyLocale)) {
return localeRenderer1.toString().compareToIgnoreCase(localeRenderer2.toString());
} else {
return localeRenderer1.getLocaleString().compareToIgnoreCase(localeRenderer2.getLocaleString());
@ -181,7 +181,7 @@ public class UserDictionaryAddWordFragment extends SubScreenFragment {
});
// Set "For all languages" to the end of the list
if (!localeRenderer.getLocaleString().equals("")) {
if (!localeRenderer.getLocale().equals(UserDictionarySettings.emptyLocale)) {
// After alphabetical sorting, "For all languages" is always in 1st position.
// (The position is 0 because the spinner menu item count starts at 0)
final LocaleRenderer forAllLanguages = adapter.getItem(0);

View file

@ -97,7 +97,7 @@ public class UserDictionaryListFragment extends SubScreenFragment {
final TreeSet<Locale> sortedLocales = getSortedDictionaryLocales(requireContext());
// Add preference "for all locales"
userDictGroup.addPreference(createUserDictionaryPreference(new Locale("")));
userDictGroup.addPreference(createUserDictionaryPreference(UserDictionarySettings.emptyLocale));
// Add preference for each dictionary locale
for (final Locale locale : sortedLocales) {
userDictGroup.addPreference(createUserDictionaryPreference(locale));

View file

@ -37,7 +37,7 @@ import org.dslul.openboard.inputmethod.latin.common.LocaleUtils;
import java.util.Locale;
public class UserDictionarySettings extends ListFragment {
static final Locale emptyLocale = new Locale("");
private static final String[] QUERY_PROJECTION =
{ UserDictionary.Words._ID, UserDictionary.Words.WORD, UserDictionary.Words.SHORTCUT, UserDictionary.Words.FREQUENCY };
@ -128,7 +128,7 @@ public class UserDictionarySettings extends ListFragment {
} else localeString = localeFromIntent;
mLocale = localeString == null ? null : LocaleUtils.constructLocale(localeString);
createCursor(mLocale == null ? null : mLocale.toString());
createCursor(mLocale == null ? null : mLocale);
TextView emptyView = view.findViewById(android.R.id.empty);
emptyView.setText(R.string.user_dict_settings_empty_text);
@ -158,11 +158,10 @@ public class UserDictionarySettings extends ListFragment {
}
}
// cursor must be created using localeString to be in line with Android system
private void createCursor(@Nullable final String localeString) {
// localeString can be any of:
// - The string representation of a locale, as returned by Locale#toString()
// - The empty string. This means we want a cursor returning words valid for all locales.
private void createCursor(@Nullable final Locale locale) {
// locale can be any of:
// - An actual locale, for use of Locale#toString()
// - The emptyLocale. This means we want a cursor returning words valid for all locales.
// - null. This means we want a cursor for the current locale, whatever this is.
// Note that this contrasts with the data inside the database, where NULL means "all
@ -173,15 +172,16 @@ public class UserDictionarySettings extends ListFragment {
// human-readable, like "all_locales" and "current_locales" strings, provided they
// can be guaranteed not to match locales that may exist.
if ("".equals(localeString)) {
if (emptyLocale.equals(locale)) {
// Case-insensitive sort
mCursor = requireContext().getContentResolver().query(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
QUERY_SELECTION_ALL_LOCALES, null,
"UPPER(" + UserDictionary.Words.WORD + ")");
} else {
final String queryLocale = null != localeString ? localeString : Locale.getDefault().toString();
// requires use of locale string for interaction with Android system
final String queryLocaleString = null != locale ? locale.toString() : Locale.getDefault().toString();
mCursor = requireContext().getContentResolver().query(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
QUERY_SELECTION, new String[] { queryLocale },
QUERY_SELECTION, new String[] { queryLocaleString },
"UPPER(" + UserDictionary.Words.WORD + ")");
}
}
@ -202,7 +202,7 @@ public class UserDictionarySettings extends ListFragment {
}
public static String getLocaleDisplayName(Context context, Locale locale) {
if (locale.toString().isEmpty()) {
if (locale.equals(UserDictionarySettings.emptyLocale)) {
// CAVEAT: localeStr should not be null because a null locale stands for the system
// locale in UserDictionary.Words.addWord.
return context.getResources().getString(R.string.user_dict_settings_all_languages);
@ -252,41 +252,42 @@ public class UserDictionarySettings extends ListFragment {
return mCursor.getString(mCursor.getColumnIndexOrThrow(column));
}
// requires use of locale string for interaction with Android system
public static void deleteWordInEditMode(final String word, final String shortcut, final String weight,
final String localeString, final ContentResolver resolver) {
final Locale locale, final ContentResolver resolver) {
if (TextUtils.isEmpty(shortcut)) {
if ("".equals(localeString)) {
if (locale.equals(UserDictionarySettings.emptyLocale)) {
resolver.delete(
UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITHOUT_SHORTCUT_AND_WITH_ALL_LOCALES,
new String[] { word, weight });
} else {
resolver.delete(
// requires use of locale string for interaction with Android system
UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITHOUT_SHORTCUT_AND_WITH_LOCALE,
new String[] { word, weight, localeString });
new String[] { word, weight, locale.toString() });
}
} else {
if ("".equals(localeString)) {
if (locale.equals(UserDictionarySettings.emptyLocale)) {
resolver.delete(
UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITH_SHORTCUT_AND_WITH_ALL_LOCALES,
new String[] { word, shortcut, weight });
} else {
resolver.delete(
// requires use of locale string for interaction with Android system
UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITH_SHORTCUT_AND_WITH_LOCALE,
new String[] { word, shortcut, weight, localeString });
new String[] { word, shortcut, weight, locale.toString() });
}
}
}
public static void deleteWord(final String word, final String locale, final ContentResolver resolver) {
if ("".equals(locale)) {
public static void deleteWord(final String word, final Locale locale, final ContentResolver resolver) {
if (locale.equals(UserDictionarySettings.emptyLocale)) {
resolver.delete(
UserDictionary.Words.CONTENT_URI, DELETE_WORD_AND_ALL_LOCALES,
new String[] { word });
} else {
resolver.delete(
UserDictionary.Words.CONTENT_URI, DELETE_WORD_AND_LOCALE,
new String[] { word, locale });
new String[] { word, locale.toString() });
}
}