| 1 |
package com.pywebview; |
| 2 |
|
| 3 |
import android.app.AlertDialog; |
| 4 |
import android.app.Activity; |
| 5 |
import android.content.DialogInterface; |
| 6 |
import android.util.Log; |
| 7 |
import android.view.View.OnClickListener; |
| 8 |
import android.webkit.JsResult; |
| 9 |
import android.webkit.WebView; |
| 10 |
import android.webkit.WebChromeClient; |
| 11 |
|
| 12 |
|
| 13 |
public class PyWebChromeClient extends WebChromeClient { |
| 14 |
private EventCallbackWrapper callbackWrapper = null; |
| 15 |
|
| 16 |
public void setCallback(EventCallbackWrapper callback) { |
| 17 |
this.callbackWrapper = callback; |
| 18 |
} |
| 19 |
|
| 20 |
@Override |
| 21 |
public boolean onJsAlert(WebView view, String url, String message, JsResult result) { |
| 22 |
// TODO: This code crashes the app. |
| 23 |
|
| 24 |
Activity context = (Activity)view.getContext(); |
| 25 |
new AlertDialog.Builder(context) |
| 26 |
.setMessage(message) |
| 27 |
.setPositiveButton(android.R.string.ok, (dialog, which) -> result.confirm()) |
| 28 |
.setOnCancelListener(dialog -> result.cancel()) |
| 29 |
.show(); |
| 30 |
|
| 31 |
return true; |
| 32 |
} |
| 33 |
|
| 34 |
@Override |
| 35 |
public boolean onJsConfirm(WebView view, String url, String message, JsResult result) { |
| 36 |
new AlertDialog.Builder(view.getContext()) |
| 37 |
.setMessage(message) |
| 38 |
.setPositiveButton(android.R.string.ok, (dialog, which) -> result.confirm()) |
| 39 |
.setNegativeButton(android.R.string.cancel, (dialog, which) -> result.cancel()) |
| 40 |
.setOnCancelListener(dialog -> result.cancel()) |
| 41 |
.show(); |
| 42 |
return true; |
| 43 |
} |
| 44 |
|
| 45 |
} |