From ba400449e1bebf13ca9553bfb0fcc88b6a118805 Mon Sep 17 00:00:00 2001 From: lumark Date: Thu, 9 Jan 2020 01:37:05 +0800 Subject: [PATCH] Use ExecutorService to replace CompletableFuture CL[1] introduced starting input with CompletableFuture for improving IME performance. Since CompletableFuture is not recommanded way to use in system server side for memory usage concern, we use java ExecutorService and Future to remove this dependency. [1]: I6aa4a664cfd0c86f75cee2457715317194bbe5e2 e0172102b9c9a9640209e3223bb6c60fe1d5cabb Fix: 147331480 Test: atest CtsInputMethodTestCases Change-Id: I10391b834c33458c3e0ac846ab738e805d235c26 --- .../view/inputmethod/InputMethodManager.java | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java index 67ce8d2e49b58..f3007a7943441 100644 --- a/core/java/android/view/inputmethod/InputMethodManager.java +++ b/core/java/android/view/inputmethod/InputMethodManager.java @@ -93,9 +93,12 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.concurrent.CancellationException; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; /** @@ -429,7 +432,10 @@ public final class InputMethodManager { * in a background thread. Later, if there is an actual startInput it will wait on * main thread till the background thread completes. */ - private CompletableFuture mWindowFocusGainFuture; + private Future mWindowFocusGainFuture; + + private ExecutorService mStartInputWorker = Executors.newSingleThreadExecutor( + new ImeThreadFactory("StartInputWorker")); /** * The instance that has previously been sent to the input method. @@ -790,6 +796,19 @@ public final class InputMethodManager { } } + private static class ImeThreadFactory implements ThreadFactory { + private final String mThreadName; + + ImeThreadFactory(String name) { + mThreadName = name; + } + + @Override + public Thread newThread(Runnable r) { + return new Thread(r, mThreadName); + } + } + final IInputMethodClient.Stub mClient = new IInputMethodClient.Stub() { @Override protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { @@ -1978,7 +1997,7 @@ public final class InputMethodManager { if (mWindowFocusGainFuture != null) { mWindowFocusGainFuture.cancel(false/* mayInterruptIfRunning */); } - mWindowFocusGainFuture = CompletableFuture.runAsync(() -> { + mWindowFocusGainFuture = mStartInputWorker.submit(() -> { if (checkFocusNoStartInput(forceNewFocus1)) { // We need to restart input on the current focus view. This // should be done in conjunction with telling the system service