Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on Android 6 and 7 using plugin from master #386

Closed
caioinova opened this issue Dec 28, 2018 · 4 comments · Fixed by #427
Closed

Error on Android 6 and 7 using plugin from master #386

caioinova opened this issue Dec 28, 2018 · 4 comments · Fixed by #427

Comments

@caioinova
Copy link

I'm using the code from master in my plugin, to get the 'beforeload' option in my application.

Running on Pocophone F1 with Android 9, it runs just fine, but the same app crashes on Androids 6 and 7 (couldn't test for Android 8 yet). The error thrown in the logcat is:

12-28 17:52:32.574 E/AndroidRuntime( 9825): FATAL EXCEPTION: main
12-28 17:52:32.574 E/AndroidRuntime( 9825): Process: io.cordova.onofreapp, PID: 9825
12-28 17:52:32.574 E/AndroidRuntime( 9825): java.lang.NoSuchMethodError: No virtual method getWebViewClient()Landroid/webkit/WebViewClient; in class Landroid/webkit/WebView; or its super classes (declaration of 'android.webkit.WebView' appears in /system/framework/framework.jar:classes2.dex)
12-28 17:52:32.574 E/AndroidRuntime( 9825): at org.apache.cordova.inappbrowser.InAppBrowser$2.run(InAppBrowser.java:265)
12-28 17:52:32.574 E/AndroidRuntime( 9825): at android.os.Handler.handleCallback(Handler.java:751)
12-28 17:52:32.574 E/AndroidRuntime( 9825): at android.os.Handler.dispatchMessage(Handler.java:95)
12-28 17:52:32.574 E/AndroidRuntime( 9825): at android.os.Looper.loop(Looper.java:154)
12-28 17:52:32.574 E/AndroidRuntime( 9825): at android.app.ActivityThread.main(ActivityThread.java:6776)
12-28 17:52:32.574 E/AndroidRuntime( 9825): at java.lang.reflect.Method.invoke(Native Method)
12-28 17:52:32.574 E/AndroidRuntime( 9825): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
12-28 17:52:32.574 E/AndroidRuntime( 9825): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

Any ideas?

@milanorszagh
Copy link

Same issue here. Did you find a solution? Is there any other way to intercept document links other than using the master InAppBrowser with 'beforeload'?

@caiocsl
Copy link

caiocsl commented Jan 18, 2019

The problem begins at line 261:

this.cordova.getActivity().runOnUiThread(new Runnable() {

            @SuppressLint("NewApi")
            @Override
            public void run() {
                ((InAppBrowserClient)inAppWebView.getWebViewClient()).waitForBeforeload = false;
                inAppWebView.loadUrl(url);
            }
        });

inAppWebView.getWebViewClient() throws the error because the method getWebViewClient() was added in API level 26 (Android 7.1) as you can see in the Android Developers documentation

So, the workaround I've came up with, is to actually make an API if:

this.cordova.getActivity().runOnUiThread(new Runnable() {
        @SuppressLint("NewApi")
        @Override
        public void run() {

                if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.N_MR1) {
                    currentClient.waitForBeforeload = false;
		    inAppWebView.setWebViewClient(currentClient);
                } else {
                    ((InAppBrowserClient)inAppWebView.getWebViewClient()).waitForBeforeload = false;
                }	

                inAppWebView.loadUrl(url);
        }
    });

Now, this is the main change, but the currentClient does not exist, so i've had to create this class property:

private InAppBrowserClient currentClient;

And on line 951, where we have this local object assign:

WebViewClient client = new InAppBrowserClient(thatWebView, edittext, beforeload);
inAppWebView.setWebViewClient(client);

I've changed it to this:

currentClient = new InAppBrowserClient(thatWebView, edittext, beforeload);
inAppWebView.setWebViewClient(currentClient);

Now what happens is, if you're on Android 7.1 or higher, nothing changes.

But now, on Android 7 or lower, when the code needs to change the waitForBeforeload value of the InAppBrowserClient in use by the webview, you have access to that on the class scope, instead of the local only as it was before.

It works for me =)

@milanorszagh
Copy link

Any plans on adding a PR for this? It looks to me that the master is still broken on Android 7 and lower.

@nounderline
Copy link
Contributor

@caiocsl Made a PR based on your fixes. I implemented them and covered one additional case that throwed during testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants