use of com.android.inputmethod.annotations.UsedForTesting in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class LatinIME method replaceDictionariesForTest.
// DO NOT USE THIS for any other purpose than testing. This can break the keyboard badly.
@UsedForTesting
void replaceDictionariesForTest(final Locale locale) {
final SettingsValues settingsValues = mSettings.getCurrent();
mDictionaryFacilitator.resetDictionaries(this, locale, settingsValues.mUseContactsDict, settingsValues.mUsePersonalizedDicts, false, /* forceReloadMainDictionary */
settingsValues.mAccount, "", /* dictionaryNamePrefix */
this);
}
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 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 ImportantNoticeUtils method hasContactsNoticeTimeoutPassed.
@UsedForTesting
static boolean hasContactsNoticeTimeoutPassed(final Context context, final long currentTimeInMillis) {
final SharedPreferences prefs = getImportantNoticePreferences(context);
if (!prefs.contains(KEY_TIMESTAMP_OF_CONTACTS_NOTICE)) {
prefs.edit().putLong(KEY_TIMESTAMP_OF_CONTACTS_NOTICE, currentTimeInMillis).apply();
}
final long firstDisplayTimeInMillis = prefs.getLong(KEY_TIMESTAMP_OF_CONTACTS_NOTICE, currentTimeInMillis);
final long elapsedTime = currentTimeInMillis - firstDisplayTimeInMillis;
return elapsedTime >= TIMEOUT_OF_IMPORTANT_NOTICE;
}
use of com.android.inputmethod.annotations.UsedForTesting in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class SpannableStringUtils method split.
/**
* Splits the given {@code charSequence} with at occurrences of the given {@code regex}.
* <p>
* This is equivalent to
* {@code charSequence.toString().split(regex, preserveTrailingEmptySegments ? -1 : 0)}
* except that the spans are preserved in the result array.
* </p>
* @param charSequence the character sequence to be split.
* @param regex the regex pattern to be used as the separator.
* @param preserveTrailingEmptySegments {@code true} to preserve the trailing empty
* segments. Otherwise, trailing empty segments will be removed before being returned.
* @return the array which contains the result. All the spans in the <code>charSequence</code>
* is preserved.
*/
@UsedForTesting
public static CharSequence[] split(final CharSequence charSequence, final String regex, final boolean preserveTrailingEmptySegments) {
// A short-cut for non-spanned strings.
if (!(charSequence instanceof Spanned)) {
// -1 means that trailing empty segments will be preserved.
return charSequence.toString().split(regex, preserveTrailingEmptySegments ? -1 : 0);
}
// Hereafter, emulate String.split for CharSequence.
final ArrayList<CharSequence> sequences = new ArrayList<>();
final Matcher matcher = Pattern.compile(regex).matcher(charSequence);
int nextStart = 0;
boolean matched = false;
while (matcher.find()) {
sequences.add(charSequence.subSequence(nextStart, matcher.start()));
nextStart = matcher.end();
matched = true;
}
if (!matched) {
// never matched. preserveTrailingEmptySegments is ignored in this case.
return new CharSequence[] { charSequence };
}
sequences.add(charSequence.subSequence(nextStart, charSequence.length()));
if (!preserveTrailingEmptySegments) {
for (int i = sequences.size() - 1; i >= 0; --i) {
if (!TextUtils.isEmpty(sequences.get(i))) {
break;
}
sequences.remove(i);
}
}
return sequences.toArray(new CharSequence[sequences.size()]);
}
Aggregations