preface

This article is one of the research notes in the development process of flutter_hybird_webview cross-process rendering practice technique sharing. Please point out any mistakes.

Source code analysis

First let’s look at the inheritance structure of webView:

public class WebView extends MockView {
    ...
}

public class MockView extends FrameLayout {
    ...
}

Copy the code

It can be known that the model and basic behavior of WebView comply with View, so we can speculate that the way of operating the soft keyboard may be the same as that of local (APP). For this, let’s first understand how app pulls up the soft keyboard.

My personal understanding of MockView and its use in development will be shared in other articles.Copy the code

App pulls up the soft keyboard process

Pull up the soft keyboard as follows:

Private void test() {edittext.requestFocus (); InputMethodManager = ((InputMethodManager)getContext()) .getSystemService(Context.INPUT_METHOD_SERVICE)); if (manager ! = null) manager.showSoftInput(v, 0); }Copy the code

From the above code, we can infer that this is a cross-process behavior, and the internal flow is as follows:

  1. GetContext ().getSystemService(context.input_method_service)) to obtain the IMM
  2. The IMM itself is not a proxy for the input process, it is internalIInputMethodManagerIs the real thingbinderThe agent
// Call manager.showsoftinput (v, 0); Public Boolean showSoftInput(View View, int flags, ResultReceiver) {//... Synchronized (mH) {if (mServedView! = view && (mServedView == null || ! mServedView.checkInputConnectionProxy(view))) { return false; } try {//IInputMethodManager instance return mservice. showSoftInput(mClient, flags, resultReceiver); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); }}}Copy the code

3. Then, the input process will pull up the soft keyboard and send the corresponding keyEvent back to the APP process. Finally, through dispatchKeyEvent(keyEvent event) method, it will be distributed to the decor view of the top window and consumed by the internal view.

So far, for the soft keyboard (APP end) pull we have a general understanding, based on this we will analyze the webView pull process.

Webview pulls up the soft keyboard process

Simply speaking, the essence of Webview is chromium, and the source code of Chromium is very large, so it is like looking for a needle in a haystack to dig the source code directly.

Its working rendering process will be described in other articlesCopy the code

We know from the previous section that a WebView behaves roughly the same as a View, so we can try to reverse that. First we override the getSystemService method:

@Override public Object getSystemService(@NonNull String name) { if(name.equals(Context.INPUT_METHOD_SERVICE)) { / / inside the method, we output the current thread's stack information inputToggleDelegate. InputServiceCall (); } return super.getSystemService(name); }Copy the code

Then, we open a website with an input box and click on the input box to get the following log:

Control of the soft keyboard is via ImeAdapterImpl. Java – Chromium Code Search updateState(…) Method to implement. Internally, the showSoftKeyboard() method is called:

private void showSoftKeyboard() { if (! isValid()) return; if (DEBUG_LOGS) Log.i(TAG, "showSoftKeyboard"); View containerView = getContainerView(); mInputMethodManagerWrapper.showSoftInput(containerView, 0, getNewShowKeyboardReceiver()); if (containerView.getResources().getConfiguration().keyboard ! = Configuration.KEYBOARD_NOKEYS) { mWebContents.scrollFocusedEditableNodeIntoView(); }}Copy the code

Then look at the mInputMethodManagerWrapper. ShowSoftInput () :

@Override public void showSoftInput(View view, int flags, ResultReceiver resultReceiver) { //... ShowSoftInputInternal (View, flags, resultReceiver); } private void showSoftInputInternal(View view, int flags, ResultReceiver resultReceiver) { StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites(); // crbug.com/616283 try {// Get the activity from WindowAndroid's weak reference, and finally get imm InputMethodManager manager = getInputMethodManager();  if (manager ! = null) { boolean result = manager.showSoftInput(view, flags, resultReceiver); if (DEBUG_LOGS) Log.i(TAG, "showSoftInputInternal: " + view + ", " + result); } } finally { StrictMode.setThreadPolicy(oldPolicy); }}Copy the code

At this point, the process merges with that of the previous section, and the distribution of consumption is much the same.

This is the end of WebView to pull up the soft keyboard process analysis, thank you for reading.

Other articles

Flutter_hybird_webview practical technique sharing for cross-process rendering

What does the Native layer do when Flutter launches on Android?

Flutter version of imitation. Parallax effect of Zhihu list

Flutter — Realize progressive card switching for NetEase Cloud Music

Flutter imitates flush list of self-selected stocks