Search in sources :

Example 1 with UsedForTesting

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;
}
Also used : SpannableString(android.text.SpannableString) SuggestionSpan(android.text.style.SuggestionSpan) Spannable(android.text.Spannable) UsedForTesting(com.android.inputmethod.annotations.UsedForTesting)

Example 2 with UsedForTesting

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);
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) UsedForTesting(com.android.inputmethod.annotations.UsedForTesting)

Example 3 with UsedForTesting

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);
}
Also used : UserHistoryDictionary(com.android.inputmethod.latin.personalization.UserHistoryDictionary) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) File(java.io.File) UsedForTesting(com.android.inputmethod.annotations.UsedForTesting)

Example 4 with UsedForTesting

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);
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) UsedForTesting(com.android.inputmethod.annotations.UsedForTesting)

Example 5 with UsedForTesting

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);
}
Also used : Locale(java.util.Locale) EditorInfo(android.view.inputmethod.EditorInfo) SettingsValues(com.android.inputmethod.latin.settings.SettingsValues) UsedForTesting(com.android.inputmethod.annotations.UsedForTesting)

Aggregations

UsedForTesting (com.android.inputmethod.annotations.UsedForTesting)11 ArrayList (java.util.ArrayList)3 Spannable (android.text.Spannable)2 SpannableString (android.text.SpannableString)2 SuggestionSpan (android.text.style.SuggestionSpan)2 SettingsValues (com.android.inputmethod.latin.settings.SettingsValues)2 Locale (java.util.Locale)2 SharedPreferences (android.content.SharedPreferences)1 Paint (android.graphics.Paint)1 Spanned (android.text.Spanned)1 TextPaint (android.text.TextPaint)1 EditorInfo (android.view.inputmethod.EditorInfo)1 SuggestedWordInfo (com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo)1 UserHistoryDictionary (com.android.inputmethod.latin.personalization.UserHistoryDictionary)1 File (java.io.File)1 HttpURLConnection (java.net.HttpURLConnection)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Matcher (java.util.regex.Matcher)1