Skip to content

Commit

Permalink
Added isRunning method to the HeadlessInAppWebView class, Fixed iOS W…
Browse files Browse the repository at this point in the history
…ebMessageListener javascript implementation not calling event listeners when onmessage is set, Fixed onCreateContextMenu event on Android where hitTestResult has always null values, fix #742
  • Loading branch information
pichillilorenzo committed Mar 23, 2021
1 parent 01f2108 commit a3e4da3
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 71 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 5.2.1

- Added `isRunning` method to the `HeadlessInAppWebView` class
- Fixed iOS `WebMessageListener` javascript implementation not calling event listeners when `onmessage` is set
- Fixed `onCreateContextMenu` event on Android where `hitTestResult` has always `null` values
- Fixed "java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setQuery(java.lang.CharSequence, boolean)' on a null object reference" [#742](https://github.com/pichillilorenzo/flutter_inappwebview/issues/742)

## 5.2.0

- Added `WebMessageChannel` and `WebMessageListener` features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,43 +401,57 @@ public Activity getActivity() {

@Override
public void didChangeTitle(String title) {
if (options.toolbarTopFixedTitle == null || options.toolbarTopFixedTitle.isEmpty()) {
if (actionBar != null && (options.toolbarTopFixedTitle == null || options.toolbarTopFixedTitle.isEmpty())) {
actionBar.setTitle(title);
}
}

@Override
public void didStartNavigation(String url) {
progressBar.setProgress(0);
searchView.setQuery(url, false);
if (progressBar != null) {
progressBar.setProgress(0);
}
if (searchView != null) {
searchView.setQuery(url, false);
}
}

@Override
public void didUpdateVisitedHistory(String url) {
searchView.setQuery(url, false);
if (searchView != null) {
searchView.setQuery(url, false);
}
}

@Override
public void didFinishNavigation(String url) {
searchView.setQuery(url, false);
progressBar.setProgress(0);
if (searchView != null) {
searchView.setQuery(url, false);
}
if (progressBar != null) {
progressBar.setProgress(0);
}
}

@Override
public void didFailNavigation(String url, int errorCode, String description) {
progressBar.setProgress(0);
if (progressBar != null) {
progressBar.setProgress(0);
}
}

@Override
public void didChangeProgress(int progress) {
progressBar.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
progressBar.setProgress(progress, true);
} else {
progressBar.setProgress(progress);
}
if (progress == 100) {
progressBar.setVisibility(View.GONE);
if (progressBar != null) {
progressBar.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
progressBar.setProgress(progress, true);
} else {
progressBar.setProgress(progress);
}
if (progress == 100) {
progressBar.setVisibility(View.GONE);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ public void prepare() {

if (options.userAgent != null && !options.userAgent.isEmpty())
settings.setUserAgentString(options.userAgent);
else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
settings.setUserAgentString(WebSettings.getDefaultUserAgent(getContext()));

if (options.applicationNameForUserAgent != null && !options.applicationNameForUserAgent.isEmpty()) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
String userAgent = (options.userAgent != null && !options.userAgent.isEmpty()) ? options.userAgent : WebSettings.getDefaultUserAgent(getContext());
String userAgentWithApplicationName = userAgent + " " + options.applicationNameForUserAgent;
settings.setUserAgentString(userAgentWithApplicationName);
Expand Down Expand Up @@ -390,7 +390,7 @@ public boolean onSingleTapUp(MotionEvent ev) {
@Override
public void run() {
int newPosition = getScrollY();
if(initialPositionScrollStoppedTask - newPosition == 0){
if (initialPositionScrollStoppedTask - newPosition == 0) {
// has stopped
onScrollStopped();
} else {
Expand Down Expand Up @@ -436,8 +436,7 @@ public boolean onTouch(View v, MotionEvent event) {

if (options.disableHorizontalScroll && options.disableVerticalScroll) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
else if (options.disableHorizontalScroll || options.disableVerticalScroll) {
} else if (options.disableHorizontalScroll || options.disableVerticalScroll) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
// save the x
Expand Down Expand Up @@ -512,8 +511,7 @@ public void setIncognito(boolean enabled) {
clearFormData();
settings.setSavePassword(false);
settings.setSaveFormData(false);
}
else {
} else {
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
settings.setAppCacheEnabled(true);
settings.setSavePassword(true);
Expand Down Expand Up @@ -703,7 +701,7 @@ public void setOptions(InAppWebViewOptions newOptions, HashMap<String, Object> n
settings.setUserAgentString(newOptions.userAgent);

if (newOptionsMap.get("applicationNameForUserAgent") != null && !options.applicationNameForUserAgent.equals(newOptions.applicationNameForUserAgent) && !newOptions.applicationNameForUserAgent.isEmpty()) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
String userAgent = (newOptions.userAgent != null && !newOptions.userAgent.isEmpty()) ? newOptions.userAgent : WebSettings.getDefaultUserAgent(getContext());
String userAgentWithApplicationName = userAgent + " " + options.applicationNameForUserAgent;
settings.setUserAgentString(userAgentWithApplicationName);
Expand Down Expand Up @@ -797,7 +795,7 @@ else if (newOptionsMap.get("clearSessionCache") != null && newOptions.clearSessi

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
if (newOptionsMap.get("disabledActionModeMenuItems") != null && (options.disabledActionModeMenuItems == null ||
!options.disabledActionModeMenuItems.equals(newOptions.disabledActionModeMenuItems)))
!options.disabledActionModeMenuItems.equals(newOptions.disabledActionModeMenuItems)))
settings.setDisabledActionModeMenuItems(newOptions.disabledActionModeMenuItems);

if (newOptionsMap.get("fantasyFontFamily") != null && !options.fantasyFontFamily.equals(newOptions.fantasyFontFamily))
Expand Down Expand Up @@ -834,7 +832,7 @@ else if (newOptionsMap.get("clearSessionCache") != null && newOptions.clearSessi
settings.setMinimumLogicalFontSize(newOptions.minimumLogicalFontSize);

if (newOptionsMap.get("initialScale") != null && !options.initialScale.equals(newOptions.initialScale))
setInitialScale(newOptions.initialScale);
setInitialScale(newOptions.initialScale);

if (newOptionsMap.get("needInitialFocus") != null && options.needInitialFocus != newOptions.needInitialFocus)
settings.setNeedInitialFocus(newOptions.needInitialFocus);
Expand Down Expand Up @@ -926,7 +924,7 @@ else if (newOptionsMap.get("clearSessionCache") != null && newOptions.clearSessi

if (newOptionsMap.get("rendererPriorityPolicy") != null &&
(options.rendererPriorityPolicy.get("rendererRequestedPriority") != newOptions.rendererPriorityPolicy.get("rendererRequestedPriority") ||
options.rendererPriorityPolicy.get("waivedWhenNotVisible") != newOptions.rendererPriorityPolicy.get("waivedWhenNotVisible")) &&
options.rendererPriorityPolicy.get("waivedWhenNotVisible") != newOptions.rendererPriorityPolicy.get("waivedWhenNotVisible")) &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setRendererPriorityPolicy(
(int) newOptions.rendererPriorityPolicy.get("rendererRequestedPriority"),
Expand Down Expand Up @@ -1120,7 +1118,7 @@ public HashMap<String, Object> getCopyBackForwardList() {

List<HashMap<String, String>> history = new ArrayList<HashMap<String, String>>();

for(int i = 0; i < currentSize; i++) {
for (int i = 0; i < currentSize; i++) {
WebHistoryItem historyItem = currentList.getItemAtIndex(i);
HashMap<String, String> historyItemMap = new HashMap<>();

Expand All @@ -1140,10 +1138,10 @@ public HashMap<String, Object> getCopyBackForwardList() {
}

@Override
protected void onScrollChanged (int x,
int y,
int oldX,
int oldY) {
protected void onScrollChanged(int x,
int y,
int oldX,
int oldY) {
super.onScrollChanged(x, y, oldX, oldY);

if (floatingContextMenu != null) {
Expand Down Expand Up @@ -1194,8 +1192,7 @@ public void setDesktopMode(final boolean enabled) {
final String newUserAgent;
if (enabled) {
newUserAgent = webSettings.getUserAgentString().replace("Mobile", "eliboM").replace("Android", "diordnA");
}
else {
} else {
newUserAgent = webSettings.getUserAgentString().replace("eliboM", "Mobile").replace("diordnA", "Android");
}

Expand Down Expand Up @@ -1237,12 +1234,9 @@ public void onCreateContextMenu(ContextMenu menu) {

private void sendOnCreateContextMenuEvent() {
HitTestResult hitTestResult = getHitTestResult();
Map<String, Object> hitTestResultMap = new HashMap<>();
hitTestResultMap.put("type", hitTestResult.getType());
hitTestResultMap.put("extra", hitTestResult.getExtra());

Map<String, Object> obj = new HashMap<>();
obj.put("hitTestResult", hitTestResultMap);
obj.put("type", hitTestResult.getType());
obj.put("extra", hitTestResult.getExtra());
channel.invokeMethod("onCreateContextMenu", obj);
}

Expand Down Expand Up @@ -1283,21 +1277,21 @@ public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
// workaround to hide the Keyboard when the user click outside
// on something not focusable such as input or a textarea.
containerView
.getHandler()
.postDelayed(
new Runnable() {
@Override
public void run() {
InputMethodManager imm =
(InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE);
if (imm != null && !imm.isAcceptingText()) {

imm.hideSoftInputFromWindow(
containerView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
},
128);
.getHandler()
.postDelayed(
new Runnable() {
@Override
public void run() {
InputMethodManager imm =
(InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE);
if (imm != null && !imm.isAcceptingText()) {

imm.hideSoftInputFromWindow(
containerView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
},
128);
}
return connection;
}
Expand Down Expand Up @@ -1353,9 +1347,9 @@ public ActionMode rebuildActionMode(
if (contextMenu != null) {
customMenuItems = (List<Map<String, Object>>) contextMenu.get("menuItems");
Map<String, Object> contextMenuOptionsMap = (Map<String, Object>) contextMenu.get("options");
if (contextMenuOptionsMap != null) {
contextMenuOptions.parse(contextMenuOptionsMap);
}
if (contextMenuOptionsMap != null) {
contextMenuOptions.parse(contextMenuOptionsMap);
}
}
customMenuItems = customMenuItems == null ? new ArrayList<Map<String, Object>>() : customMenuItems;

Expand Down Expand Up @@ -1604,7 +1598,7 @@ public void callAsyncJavaScript(String functionBody, Map<String, Object> argumen
.replace(PluginScriptsUtil.VAR_RESULT_UUID, resultUuid);

sourceToInject = userContentController.generateCodeForScriptEvaluation(sourceToInject, contentWorld);
evaluateJavascript(sourceToInject, null);
evaluateJavascript(sourceToInject, null);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
Expand All @@ -1621,7 +1615,7 @@ public void onReceiveValue(String value) {
}
});
}

public boolean canScrollVertically() {
return computeVerticalScrollRange() > computeVerticalScrollExtent();
}
Expand Down
2 changes: 1 addition & 1 deletion example/.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_downloader","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/flutter_downloader-1.5.2/","dependencies":[]},{"name":"flutter_inappwebview","path":"/Users/lorenzopichilli/Desktop/flutter_inappwebview/","dependencies":[]},{"name":"integration_test","path":"/Users/lorenzopichilli/fvm/versions/2.1.0-10.0.pre/packages/integration_test/","dependencies":[]},{"name":"path_provider","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/path_provider-2.0.0-nullsafety/","dependencies":[]},{"name":"permission_handler","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/permission_handler-5.1.0+2/","dependencies":[]},{"name":"url_launcher","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/url_launcher-6.0.0-nullsafety.6/","dependencies":[]}],"android":[{"name":"flutter_downloader","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/flutter_downloader-1.5.2/","dependencies":[]},{"name":"flutter_inappwebview","path":"/Users/lorenzopichilli/Desktop/flutter_inappwebview/","dependencies":[]},{"name":"integration_test","path":"/Users/lorenzopichilli/fvm/versions/2.1.0-10.0.pre/packages/integration_test/","dependencies":[]},{"name":"path_provider","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/path_provider-2.0.0-nullsafety/","dependencies":[]},{"name":"permission_handler","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/permission_handler-5.1.0+2/","dependencies":[]},{"name":"url_launcher","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/url_launcher-6.0.0-nullsafety.6/","dependencies":[]}],"macos":[{"name":"path_provider_macos","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/path_provider_macos-0.0.5-nullsafety/","dependencies":[]},{"name":"url_launcher_macos","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/url_launcher_macos-0.1.0-nullsafety.2/","dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/path_provider_linux-0.2.0-nullsafety/","dependencies":[]},{"name":"url_launcher_linux","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/url_launcher_linux-0.1.0-nullsafety.3/","dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/path_provider_windows-0.1.0-nullsafety.3/","dependencies":[]},{"name":"url_launcher_windows","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/url_launcher_windows-0.1.0-nullsafety.2/","dependencies":[]}],"web":[]},"dependencyGraph":[{"name":"flutter_downloader","dependencies":[]},{"name":"flutter_inappwebview","dependencies":[]},{"name":"integration_test","dependencies":[]},{"name":"path_provider","dependencies":["path_provider_macos","path_provider_linux","path_provider_windows"]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_macos","dependencies":[]},{"name":"path_provider_windows","dependencies":[]},{"name":"permission_handler","dependencies":[]},{"name":"url_launcher","dependencies":["url_launcher_linux","url_launcher_macos","url_launcher_windows"]},{"name":"url_launcher_linux","dependencies":[]},{"name":"url_launcher_macos","dependencies":[]},{"name":"url_launcher_windows","dependencies":[]}],"date_created":"2021-03-22 16:27:21.616305","version":"2.1.0-10.0.pre"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_downloader","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/flutter_downloader-1.5.2/","dependencies":[]},{"name":"flutter_inappwebview","path":"/Users/lorenzopichilli/Desktop/flutter_inappwebview/","dependencies":[]},{"name":"integration_test","path":"/Users/lorenzopichilli/fvm/versions/2.1.0-10.0.pre/packages/integration_test/","dependencies":[]},{"name":"path_provider","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/path_provider-2.0.0-nullsafety/","dependencies":[]},{"name":"permission_handler","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/permission_handler-5.1.0+2/","dependencies":[]},{"name":"url_launcher","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/url_launcher-6.0.0-nullsafety.6/","dependencies":[]}],"android":[{"name":"flutter_downloader","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/flutter_downloader-1.5.2/","dependencies":[]},{"name":"flutter_inappwebview","path":"/Users/lorenzopichilli/Desktop/flutter_inappwebview/","dependencies":[]},{"name":"integration_test","path":"/Users/lorenzopichilli/fvm/versions/2.1.0-10.0.pre/packages/integration_test/","dependencies":[]},{"name":"path_provider","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/path_provider-2.0.0-nullsafety/","dependencies":[]},{"name":"permission_handler","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/permission_handler-5.1.0+2/","dependencies":[]},{"name":"url_launcher","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/url_launcher-6.0.0-nullsafety.6/","dependencies":[]}],"macos":[{"name":"path_provider_macos","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/path_provider_macos-0.0.5-nullsafety/","dependencies":[]},{"name":"url_launcher_macos","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/url_launcher_macos-0.1.0-nullsafety.2/","dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/path_provider_linux-0.2.0-nullsafety/","dependencies":[]},{"name":"url_launcher_linux","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/url_launcher_linux-0.1.0-nullsafety.3/","dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/path_provider_windows-0.1.0-nullsafety.3/","dependencies":[]},{"name":"url_launcher_windows","path":"/Users/lorenzopichilli/.pub-cache/hosted/pub.dartlang.org/url_launcher_windows-0.1.0-nullsafety.2/","dependencies":[]}],"web":[]},"dependencyGraph":[{"name":"flutter_downloader","dependencies":[]},{"name":"flutter_inappwebview","dependencies":[]},{"name":"integration_test","dependencies":[]},{"name":"path_provider","dependencies":["path_provider_macos","path_provider_linux","path_provider_windows"]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_macos","dependencies":[]},{"name":"path_provider_windows","dependencies":[]},{"name":"permission_handler","dependencies":[]},{"name":"url_launcher","dependencies":["url_launcher_linux","url_launcher_macos","url_launcher_windows"]},{"name":"url_launcher_linux","dependencies":[]},{"name":"url_launcher_macos","dependencies":[]},{"name":"url_launcher_windows","dependencies":[]}],"date_created":"2021-03-23 16:33:10.809487","version":"2.1.0-10.0.pre"}
7 changes: 3 additions & 4 deletions ios/Classes/Types/WebMessageListener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,9 @@ public class WebMessageListener : FlutterMethodCallDelegate {
var event = {data: '\(messageEscaped)'};
if (webMessageListener.onmessage != null) {
webMessageListener.onmessage(event);
} else {
for (var listener of webMessageListener.listeners) {
listener(event);
}
}
for (var listener of webMessageListener.listeners) {
listener(event);
}
}
})();
Expand Down
Loading

0 comments on commit a3e4da3

Please sign in to comment.