OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.content.browser.input; |
| 6 |
| 7 import android.os.Handler; |
| 8 import android.os.HandlerThread; |
| 9 |
| 10 import org.chromium.base.CommandLine; |
| 11 import org.chromium.base.Log; |
| 12 import org.chromium.content.common.ContentSwitches; |
| 13 |
| 14 /** |
| 15 * A class to create InputConnection Handler as necessary. |
| 16 */ |
| 17 public class InputConnectionHandlerFactory { |
| 18 private static final String TAG = "cr.Ime"; |
| 19 |
| 20 public static Handler getHandler(Handler defaultHandler) { |
| 21 if (shouldReturnInputConnectionHandler()) { |
| 22 return getInputConnectionHandler(); |
| 23 } |
| 24 return defaultHandler; |
| 25 } |
| 26 |
| 27 private static boolean shouldReturnInputConnectionHandler() { |
| 28 if (!CommandLine.getInstance().hasSwitch(ContentSwitches.USE_IME_THREAD)
) { |
| 29 Log.d(TAG, "USE_IME_THREAD not set. Returning the default handler.")
; |
| 30 return false; |
| 31 } |
| 32 for (StackTraceElement frame : Thread.currentThread().getStackTrace()) { |
| 33 // We only return our custom Handler to InputMethodManager's InputCo
nnection |
| 34 // proxy. For all other purposes, we return the regular Handler. |
| 35 // InputMethodManager retrieves the Handler for its InputConnection
proxy |
| 36 // inside its method startInputInner(), so we check for that here. T
his is |
| 37 // valid from Android 2.2 to at least Android 4.2. If this situation
ever |
| 38 // changes, we gracefully fall back to using the regular Handler. |
| 39 if ("startInputInner".equals(frame.getMethodName()) |
| 40 && android.view.inputmethod.InputMethodManager.class.getName
().equals( |
| 41 frame.getClassName())) { |
| 42 // Return our own Handler to InputMethodManager |
| 43 Log.d(TAG, "getHandler() is called from InputMethodManager." |
| 44 + " Returning custom handler."); |
| 45 return true; |
| 46 } |
| 47 } |
| 48 Log.d(TAG, "getHandler() is not called from InputMethodManager. Returnin
g the default" |
| 49 + " handler."); |
| 50 return false; |
| 51 } |
| 52 |
| 53 // Initialization-on-demand holder for thread-safe-lazy initialization. |
| 54 static Handler getInputConnectionHandler() { |
| 55 return LazyHandlerHolder.sInstance.getHandler(); |
| 56 } |
| 57 |
| 58 // Initialization-on-demand holder for thread-safe lazy initialization. |
| 59 private static class LazyHandlerHolder { |
| 60 private static final InputConnectionHandlerContainer sInstance = |
| 61 new InputConnectionHandlerContainer(); |
| 62 } |
| 63 |
| 64 private static class InputConnectionHandlerContainer { |
| 65 private final Handler mHandler; |
| 66 |
| 67 public InputConnectionHandlerContainer() { |
| 68 HandlerThread thread = |
| 69 new HandlerThread("InputConnectionHandlerThread", HandlerThr
ead.NORM_PRIORITY); |
| 70 thread.start(); |
| 71 mHandler = new Handler(thread.getLooper()); |
| 72 } |
| 73 |
| 74 public Handler getHandler() { |
| 75 return mHandler; |
| 76 } |
| 77 } |
| 78 } |
OLD | NEW |