use of com.android.inputmethod.annotations.UsedForTesting in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class SuggestionSpanUtils method getTextWithAutoCorrectionIndicatorUnderline.
@UsedForTesting
public static CharSequence getTextWithAutoCorrectionIndicatorUnderline(final Context context, final String text, @Nonnull final Locale locale) {
if (TextUtils.isEmpty(text) || OBJ_FLAG_AUTO_CORRECTION == null) {
return text;
}
final Spannable spannable = new SpannableString(text);
final SuggestionSpan suggestionSpan = new SuggestionSpan(context, locale, new String[] {}, /* suggestions */
OBJ_FLAG_AUTO_CORRECTION, null);
spannable.setSpan(suggestionSpan, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
return spannable;
}
use of com.android.inputmethod.annotations.UsedForTesting in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class LocaleSpanCompatUtils method updateLocaleSpan.
/**
* Ensures that the specified range is covered with only one {@link LocaleSpan} with the given
* locale. If the region is already covered by one or more {@link LocaleSpan}, their ranges are
* updated so that each character has only one locale.
* @param spannable the spannable object to be updated.
* @param start the start index from which {@link LocaleSpan} is attached (inclusive).
* @param end the end index to which {@link LocaleSpan} is attached (exclusive).
* @param locale the locale to be attached to the specified range.
*/
@UsedForTesting
public static void updateLocaleSpan(final Spannable spannable, final int start, final int end, final Locale locale) {
if (end < start) {
Log.e(TAG, "Invalid range: start=" + start + " end=" + end);
return;
}
if (!isLocaleSpanAvailable()) {
return;
}
// A brief summary of our strategy;
// 1. Enumerate all LocaleSpans between [start - 1, end + 1].
// 2. For each LocaleSpan S:
// - Update the range of S so as not to cover [start, end] if S doesn't have the
// expected locale.
// - Mark S as "to be merged" if S has the expected locale.
// 3. Merge all the LocaleSpans that are marked as "to be merged" into one LocaleSpan.
// If no appropriate span is found, create a new one with newLocaleSpan method.
final int searchStart = Math.max(start - 1, 0);
final int searchEnd = Math.min(end + 1, spannable.length());
// LocaleSpans found in the target range. See the step 1 in the above comment.
final Object[] existingLocaleSpans = spannable.getSpans(searchStart, searchEnd, LOCALE_SPAN_TYPE);
// LocaleSpans that are marked as "to be merged". See the step 2 in the above comment.
final ArrayList<Object> existingLocaleSpansToBeMerged = new ArrayList<>();
boolean isStartExclusive = true;
boolean isEndExclusive = true;
int newStart = start;
int newEnd = end;
for (final Object existingLocaleSpan : existingLocaleSpans) {
final Locale attachedLocale = getLocaleFromLocaleSpan(existingLocaleSpan);
if (!locale.equals(attachedLocale)) {
// This LocaleSpan does not have the expected locale. Update its range if it has
// an intersection with the range [start, end] (the first case of the step 2 in the
// above comment).
removeLocaleSpanFromRange(existingLocaleSpan, spannable, start, end);
continue;
}
final int spanStart = spannable.getSpanStart(existingLocaleSpan);
final int spanEnd = spannable.getSpanEnd(existingLocaleSpan);
if (spanEnd < spanStart) {
Log.e(TAG, "Invalid span: spanStart=" + spanStart + " spanEnd=" + spanEnd);
continue;
}
if (spanEnd < start || end < spanStart) {
// No intersection found.
continue;
}
// Here existingLocaleSpan has the expected locale and an intersection with the
// range [start, end] (the second case of the the step 2 in the above comment).
final int spanFlag = spannable.getSpanFlags(existingLocaleSpan);
if (spanStart < newStart) {
newStart = spanStart;
isStartExclusive = ((spanFlag & Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) == Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (newEnd < spanEnd) {
newEnd = spanEnd;
isEndExclusive = ((spanFlag & Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) == Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
existingLocaleSpansToBeMerged.add(existingLocaleSpan);
}
int originalLocaleSpanFlag = 0;
Object localeSpan = null;
if (existingLocaleSpansToBeMerged.isEmpty()) {
// If there is no LocaleSpan that is marked as to be merged, create a new one.
localeSpan = newLocaleSpan(locale);
} else {
// Reuse the first LocaleSpan to avoid unnecessary object instantiation.
localeSpan = existingLocaleSpansToBeMerged.get(0);
originalLocaleSpanFlag = spannable.getSpanFlags(localeSpan);
// No need to keep other instances.
for (int i = 1; i < existingLocaleSpansToBeMerged.size(); ++i) {
spannable.removeSpan(existingLocaleSpansToBeMerged.get(i));
}
}
final int localeSpanFlag = getSpanFlag(originalLocaleSpanFlag, isStartExclusive, isEndExclusive);
spannable.setSpan(localeSpan, newStart, newEnd, localeSpanFlag);
}
use of com.android.inputmethod.annotations.UsedForTesting in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class DictionaryFacilitatorImpl method resetDictionariesForTesting.
@UsedForTesting
public void resetDictionariesForTesting(final Context context, final Locale locale, final ArrayList<String> dictionaryTypes, final HashMap<String, File> dictionaryFiles, final Map<String, Map<String, String>> additionalDictAttributes, @Nullable final String account) {
Dictionary mainDictionary = null;
final Map<String, ExpandableBinaryDictionary> subDicts = new HashMap<>();
for (final String dictType : dictionaryTypes) {
if (dictType.equals(Dictionary.TYPE_MAIN)) {
mainDictionary = DictionaryFactory.createMainDictionaryFromManager(context, locale);
} else {
final File dictFile = dictionaryFiles.get(dictType);
final ExpandableBinaryDictionary dict = getSubDict(dictType, context, locale, dictFile, "", /* dictNamePrefix */
account);
if (additionalDictAttributes.containsKey(dictType)) {
dict.clearAndFlushDictionaryWithAdditionalAttributes(additionalDictAttributes.get(dictType));
}
if (dict == null) {
throw new RuntimeException("Unknown dictionary type: " + dictType);
}
dict.reloadDictionaryIfRequired();
dict.waitAllTasksForTests();
subDicts.put(dictType, dict);
}
}
mDictionaryGroup = new DictionaryGroup(locale, mainDictionary, account, subDicts);
}
use of com.android.inputmethod.annotations.UsedForTesting in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class ExpandableBinaryDictionary method waitAllTasksForTests.
@UsedForTesting
public void waitAllTasksForTests() {
final CountDownLatch countDownLatch = new CountDownLatch(1);
asyncExecuteTaskWithWriteLock(new Runnable() {
@Override
public void run() {
countDownLatch.countDown();
}
});
try {
countDownLatch.await();
} catch (InterruptedException e) {
Log.e(TAG, "Interrupted while waiting for finishing dictionary operations.", e);
}
}
use of com.android.inputmethod.annotations.UsedForTesting in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class LatinIME method loadSettings.
// Has to be package-visible for unit tests
@UsedForTesting
void loadSettings() {
final Locale locale = mRichImm.getCurrentSubtypeLocale();
final EditorInfo editorInfo = getCurrentInputEditorInfo();
final InputAttributes inputAttributes = new InputAttributes(editorInfo, isFullscreenMode(), getPackageName());
mSettings.loadSettings(this, locale, inputAttributes);
final SettingsValues currentSettingsValues = mSettings.getCurrent();
AudioAndHapticFeedbackManager.getInstance().onSettingsChanged(currentSettingsValues);
// asynchronously loaded.
if (!mHandler.hasPendingReopenDictionaries()) {
resetDictionaryFacilitator(locale);
}
refreshPersonalizationDictionarySession(currentSettingsValues);
resetDictionaryFacilitatorIfNecessary();
mStatsUtilsManager.onLoadSettings(this, /* context */
currentSettingsValues);
}
Aggregations