aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/utils
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin/utils')
-rw-r--r--java/src/com/android/inputmethod/latin/utils/ExecutorUtils.java57
-rw-r--r--java/src/com/android/inputmethod/latin/utils/ImportantNoticeUtils.java8
-rw-r--r--java/src/com/android/inputmethod/latin/utils/ManagedProfileUtils.java16
-rw-r--r--java/src/com/android/inputmethod/latin/utils/NgramContextUtils.java14
4 files changed, 77 insertions, 18 deletions
diff --git a/java/src/com/android/inputmethod/latin/utils/ExecutorUtils.java b/java/src/com/android/inputmethod/latin/utils/ExecutorUtils.java
index a78446103..8ce6eff92 100644
--- a/java/src/com/android/inputmethod/latin/utils/ExecutorUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/ExecutorUtils.java
@@ -33,17 +33,30 @@ public class ExecutorUtils {
private static final String TAG = "ExecutorUtils";
- private static ScheduledExecutorService sExecutorService =
- Executors.newSingleThreadScheduledExecutor(new ExecutorFactory());
+ public static final String KEYBOARD = "Keyboard";
+ public static final String SPELLING = "Spelling";
+
+ private static ScheduledExecutorService sKeyboardExecutorService = newExecutorService(KEYBOARD);
+ private static ScheduledExecutorService sSpellingExecutorService = newExecutorService(SPELLING);
+
+ private static ScheduledExecutorService newExecutorService(final String name) {
+ return Executors.newSingleThreadScheduledExecutor(new ExecutorFactory(name));
+ }
private static class ExecutorFactory implements ThreadFactory {
+ private final String mName;
+
+ private ExecutorFactory(final String name) {
+ mName = name;
+ }
+
@Override
public Thread newThread(final Runnable runnable) {
Thread thread = new Thread(runnable, TAG);
thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
- Log.w(TAG + "-" + runnable.getClass().getSimpleName(), ex);
+ Log.w(mName + "-" + runnable.getClass().getSimpleName(), ex);
}
});
return thread;
@@ -64,24 +77,44 @@ public class ExecutorUtils {
//
/**
+ * @param name Executor's name.
* @return scheduled executor service used to run background tasks
*/
- public static ScheduledExecutorService getBackgroundExecutor() {
+ public static ScheduledExecutorService getBackgroundExecutor(final String name) {
if (sExecutorServiceForTests != null) {
return sExecutorServiceForTests;
}
- return sExecutorService;
+ switch (name) {
+ case KEYBOARD:
+ return sKeyboardExecutorService;
+ case SPELLING:
+ return sSpellingExecutorService;
+ default:
+ throw new IllegalArgumentException("Invalid executor: " + name);
+ }
}
- public static void killTasks() {
- getBackgroundExecutor().shutdownNow();
+ public static void killTasks(final String name) {
+ final ScheduledExecutorService executorService = getBackgroundExecutor(name);
+ executorService.shutdownNow();
try {
- getBackgroundExecutor().awaitTermination(5, TimeUnit.SECONDS);
+ executorService.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
- Log.wtf(TAG, "Failed to shut down background task.");
- throw new IllegalStateException("Failed to shut down background task.");
- } finally {
- sExecutorService = Executors.newSingleThreadScheduledExecutor(new ExecutorFactory());
+ Log.wtf(TAG, "Failed to shut down: " + name);
+ }
+ if (executorService == sExecutorServiceForTests) {
+ // Don't do anything to the test service.
+ return;
+ }
+ switch (name) {
+ case KEYBOARD:
+ sKeyboardExecutorService = newExecutorService(KEYBOARD);
+ break;
+ case SPELLING:
+ sSpellingExecutorService = newExecutorService(SPELLING);
+ break;
+ default:
+ throw new IllegalArgumentException("Invalid executor: " + name);
}
}
diff --git a/java/src/com/android/inputmethod/latin/utils/ImportantNoticeUtils.java b/java/src/com/android/inputmethod/latin/utils/ImportantNoticeUtils.java
index 142548b25..df0cd8437 100644
--- a/java/src/com/android/inputmethod/latin/utils/ImportantNoticeUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/ImportantNoticeUtils.java
@@ -25,6 +25,7 @@ import android.util.Log;
import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.R;
+import com.android.inputmethod.latin.settings.SettingsValues;
import java.util.concurrent.TimeUnit;
@@ -105,7 +106,12 @@ public final class ImportantNoticeUtils {
return elapsedTime >= TIMEOUT_OF_IMPORTANT_NOTICE;
}
- public static boolean shouldShowImportantNotice(final Context context) {
+ public static boolean shouldShowImportantNotice(final Context context,
+ final SettingsValues settingsValues) {
+ // Check to see whether personalization is enabled by the user.
+ if (!settingsValues.isPersonalizationEnabled()) {
+ return false;
+ }
if (!hasNewImportantNotice(context)) {
return false;
}
diff --git a/java/src/com/android/inputmethod/latin/utils/ManagedProfileUtils.java b/java/src/com/android/inputmethod/latin/utils/ManagedProfileUtils.java
index f0d6d081e..1bd8f314c 100644
--- a/java/src/com/android/inputmethod/latin/utils/ManagedProfileUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/ManagedProfileUtils.java
@@ -23,6 +23,8 @@ import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;
+import com.android.inputmethod.annotations.UsedForTesting;
+
import java.util.List;
/**
@@ -32,16 +34,28 @@ public class ManagedProfileUtils {
private static final boolean DEBUG = false;
private static final String TAG = ManagedProfileUtils.class.getSimpleName();
+ private static ManagedProfileUtils INSTANCE = new ManagedProfileUtils();
+ private static ManagedProfileUtils sTestInstance;
+
private ManagedProfileUtils() {
// This utility class is not publicly instantiable.
}
+ @UsedForTesting
+ public static void setTestInstance(final ManagedProfileUtils testInstance) {
+ sTestInstance = testInstance;
+ }
+
+ public static ManagedProfileUtils getInstance() {
+ return sTestInstance == null ? INSTANCE : sTestInstance;
+ }
+
/**
* Note that {@link UserManager#getUserProfiles} has been introduced
* in API level 21 (Build.VERSION_CODES.LOLLIPOP).
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
- public static boolean hasManagedWorkProfile(final Context context) {
+ public boolean hasManagedWorkProfile(final Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return false;
}
diff --git a/java/src/com/android/inputmethod/latin/utils/NgramContextUtils.java b/java/src/com/android/inputmethod/latin/utils/NgramContextUtils.java
index 727df1a93..c05ffd693 100644
--- a/java/src/com/android/inputmethod/latin/utils/NgramContextUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/NgramContextUtils.java
@@ -31,6 +31,7 @@ public final class NgramContextUtils {
// Intentional empty constructor for utility class.
}
+ private static final Pattern NEWLINE_REGEX = Pattern.compile("[\\r\\n]+");
private static final Pattern SPACE_REGEX = Pattern.compile("\\s+");
// Get context information from nth word before the cursor. n = 1 retrieves the words
// immediately before the cursor, n = 2 retrieves the words before that, and so on. This splits
@@ -58,7 +59,11 @@ public final class NgramContextUtils {
public static NgramContext getNgramContextFromNthPreviousWord(final CharSequence prev,
final SpacingAndPunctuations spacingAndPunctuations, final int n) {
if (prev == null) return NgramContext.EMPTY_PREV_WORDS_INFO;
- final String[] w = SPACE_REGEX.split(prev);
+ final String[] lines = NEWLINE_REGEX.split(prev);
+ if (lines.length == 0) {
+ return new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
+ }
+ final String[] w = SPACE_REGEX.split(lines[lines.length - 1]);
final WordInfo[] prevWordsInfo =
new WordInfo[DecoderSpecificConstants.MAX_PREV_WORD_COUNT_FOR_N_GRAM];
Arrays.fill(prevWordsInfo, WordInfo.EMPTY_WORD_INFO);
@@ -81,16 +86,17 @@ public final class NgramContextUtils {
prevWordsInfo[i] = WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO;
break;
}
+
final String focusedWord = w[focusedWordIndex];
- // If the word is, the context is beginning-of-sentence.
+ // If the word is empty, the context is beginning-of-sentence.
final int length = focusedWord.length();
if (length <= 0) {
prevWordsInfo[i] = WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO;
break;
}
- // If ends in a sentence separator, the context is beginning-of-sentence.
+ // If the word ends in a sentence terminator, the context is beginning-of-sentence.
final char lastChar = focusedWord.charAt(length - 1);
- if (spacingAndPunctuations.isSentenceSeparator(lastChar)) {
+ if (spacingAndPunctuations.isSentenceTerminator(lastChar)) {
prevWordsInfo[i] = WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO;
break;
}