|
2 | 2 |
|
3 | 3 | import android.annotation.SuppressLint;
|
4 | 4 | import android.app.Activity;
|
| 5 | +import android.app.AlertDialog; |
| 6 | +import android.app.Dialog; |
| 7 | +import android.content.DialogInterface; |
5 | 8 | import android.content.Intent;
|
6 | 9 | import android.content.pm.ActivityInfo;
|
7 | 10 | import android.graphics.Bitmap;
|
8 | 11 | import android.graphics.BitmapFactory;
|
9 | 12 | import android.net.Uri;
|
10 | 13 | import android.os.Build;
|
| 14 | +import android.os.Message; |
11 | 15 | import android.text.TextUtils;
|
12 |
| -import android.util.Log; |
| 16 | +import android.view.Gravity; |
13 | 17 | import android.view.LayoutInflater;
|
14 | 18 | import android.view.View;
|
| 19 | +import android.view.ViewGroup; |
| 20 | +import android.webkit.JsPromptResult; |
| 21 | +import android.webkit.JsResult; |
15 | 22 | import android.webkit.PermissionRequest;
|
16 | 23 | import android.webkit.ValueCallback;
|
17 | 24 | import android.webkit.WebChromeClient;
|
18 | 25 | import android.webkit.WebView;
|
| 26 | +import android.widget.EditText; |
19 | 27 | import android.widget.FrameLayout;
|
20 | 28 |
|
21 | 29 | import androidx.annotation.Nullable;
|
@@ -181,6 +189,94 @@ public void onReceivedTitle(WebView view, String title) {
|
181 | 189 | }
|
182 | 190 | }
|
183 | 191 |
|
| 192 | + @Override |
| 193 | + public boolean onJsAlert(WebView view, String url, String message, final JsResult result) { |
| 194 | + if (onByWebChromeCallback != null && onByWebChromeCallback.onJsAlert(view, url, message, result)) { |
| 195 | + return true; |
| 196 | + } |
| 197 | + Dialog alertDialog = new AlertDialog.Builder(view.getContext()). |
| 198 | + setMessage(message). |
| 199 | + setCancelable(false). |
| 200 | + setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 201 | + @Override |
| 202 | + public void onClick(DialogInterface dialog, int which) { |
| 203 | + dialog.dismiss(); |
| 204 | + result.confirm(); |
| 205 | + } |
| 206 | + }) |
| 207 | + .create(); |
| 208 | + alertDialog.show(); |
| 209 | + return true; |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) { |
| 214 | + if (onByWebChromeCallback != null && onByWebChromeCallback.onJsConfirm(view, url, message, result)) { |
| 215 | + return true; |
| 216 | + } |
| 217 | + DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { |
| 218 | + @Override |
| 219 | + public void onClick(DialogInterface dialog, int which) { |
| 220 | + if (which == Dialog.BUTTON_POSITIVE) { |
| 221 | + result.confirm(); |
| 222 | + } else { |
| 223 | + result.cancel(); |
| 224 | + } |
| 225 | + } |
| 226 | + }; |
| 227 | + new AlertDialog.Builder(view.getContext()) |
| 228 | + .setMessage(message) |
| 229 | + .setCancelable(false) |
| 230 | + .setPositiveButton(android.R.string.ok, listener) |
| 231 | + .setNegativeButton(android.R.string.cancel, listener).show(); |
| 232 | + return true; |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public boolean onJsPrompt(final WebView view, String url, final String message, final String defaultValue, final JsPromptResult result) { |
| 237 | + if (onByWebChromeCallback != null && onByWebChromeCallback.onJsPrompt(view, url, message, defaultValue, result)) { |
| 238 | + return true; |
| 239 | + } |
| 240 | + view.post(new Runnable() { |
| 241 | + @Override |
| 242 | + public void run() { |
| 243 | + final EditText editText = new EditText(view.getContext()); |
| 244 | + editText.setText(defaultValue); |
| 245 | + if (defaultValue != null) { |
| 246 | + editText.setSelection(defaultValue.length()); |
| 247 | + } |
| 248 | + float dpi = view.getContext().getResources().getDisplayMetrics().density; |
| 249 | + DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { |
| 250 | + @Override |
| 251 | + public void onClick(DialogInterface dialog, int which) { |
| 252 | + if (which == Dialog.BUTTON_POSITIVE) { |
| 253 | + result.confirm(editText.getText().toString()); |
| 254 | + } else { |
| 255 | + result.cancel(); |
| 256 | + } |
| 257 | + } |
| 258 | + }; |
| 259 | + new AlertDialog.Builder(view.getContext()) |
| 260 | + .setTitle(message) |
| 261 | + .setView(editText) |
| 262 | + .setCancelable(false) |
| 263 | + .setPositiveButton(android.R.string.ok, listener) |
| 264 | + .setNegativeButton(android.R.string.cancel, listener) |
| 265 | + .show(); |
| 266 | + FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams( |
| 267 | + ViewGroup.LayoutParams.MATCH_PARENT, |
| 268 | + ViewGroup.LayoutParams.WRAP_CONTENT); |
| 269 | + int t = (int) (dpi * 16); |
| 270 | + layoutParams.setMargins(t, 0, t, 0); |
| 271 | + layoutParams.gravity = Gravity.CENTER_HORIZONTAL; |
| 272 | + editText.setLayoutParams(layoutParams); |
| 273 | + int padding = (int) (15 * dpi); |
| 274 | + editText.setPadding(padding - (int) (5 * dpi), padding, padding, padding); |
| 275 | + } |
| 276 | + }); |
| 277 | + return true; |
| 278 | + } |
| 279 | + |
184 | 280 | //扩展浏览器上传文件
|
185 | 281 | //3.0++版本
|
186 | 282 | public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
|
|
0 commit comments