Skip to content

Commit

Permalink
[wpe] Fix focus issues with soft input
Browse files Browse the repository at this point in the history
  • Loading branch information
zhani committed May 18, 2024
1 parent 340481b commit 5f5071c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
2 changes: 1 addition & 1 deletion wpe/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ android {
}

dependencies {
implementation 'androidx.annotation:annotation:1.7.1'
implementation 'androidx.annotation:annotation:1.8.0'
}

gradle.afterProject { project ->
Expand Down
32 changes: 26 additions & 6 deletions wpe/src/main/java/com/wpe/wpe/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
Expand All @@ -39,6 +41,8 @@

import com.wpe.wpeview.WPEView;

import java.lang.ref.WeakReference;

/**
* A Page roughly corresponds with a tab in a regular browser UI.
* There is a 1:1 relationship between WPEView and Page.
Expand Down Expand Up @@ -192,16 +196,32 @@ public void onTitleChanged(@NonNull String title, boolean canGoBack, boolean can

@Keep
public void onInputMethodContextIn() {
InputMethodManager imm =
(InputMethodManager)wpeView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(wpeView, 0);
WeakReference<WPEView> weakRefecence = new WeakReference<>(wpeView);
Handler handler = new Handler(Looper.getMainLooper());
handler.post(() -> {
WPEView view = weakRefecence.get();
if (view != null) {
if (view.requestFocus()) {
InputMethodManager imm =
(InputMethodManager)wpeView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(wpeView, InputMethodManager.SHOW_IMPLICIT);
}
}
});
}

@Keep
public void onInputMethodContextOut() {
InputMethodManager imm =
(InputMethodManager)wpeView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(surfaceView.getWindowToken(), 0);
WeakReference<WPEView> weakRefecence = new WeakReference<>(wpeView);
Handler handler = new Handler(Looper.getMainLooper());
handler.post(() -> {
WPEView view = weakRefecence.get();
if (view != null) {
InputMethodManager imm =
(InputMethodManager)wpeView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(wpeView.getWindowToken(), 0);
}
});
}

@Keep
Expand Down
29 changes: 16 additions & 13 deletions wpe/src/main/java/com/wpe/wpeview/WPEView.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
Expand All @@ -47,35 +48,37 @@
public class WPEView extends FrameLayout {
private static final String LOGTAG = "WPEView";

private final WPEContext wpeContext;
private final boolean ownsContext;
private WPEContext wpeContext;
private boolean ownsContext;

private final Page page;
private Page page;

public WPEView(@NonNull Context context) {
super(context);

wpeContext = new WPEContext(context);
ownsContext = true;
page = new Page(this, wpeContext.getWebContext(), false);
init(new WPEContext(context), true, false);
}

public WPEView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);

wpeContext = new WPEContext(context);
ownsContext = true;
page = new Page(this, wpeContext.getWebContext(), false);
init(new WPEContext(context), true, false);
}

public WPEView(@NonNull WPEContext context) { this(context, false); }

public WPEView(@NonNull WPEContext context, boolean headless) {
super(context.getApplicationContext());
init(context, false, false);
}

private void init(@NonNull WPEContext context, boolean ownsContext, boolean headless) {
wpeContext = context;
ownsContext = false;
page = new Page(this, wpeContext.getWebContext(), headless);
this.ownsContext = ownsContext;
page = new Page(this, wpeContext.getWebContext(), false);

setFocusable(true);
setFocusableInTouchMode(true);
setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
}

private SurfaceView surfaceView = null;
Expand Down

0 comments on commit 5f5071c

Please sign in to comment.