The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.webkit; |
| 18 | |
Tobias Sargeant | d10e4af | 2017-01-19 14:03:09 +0000 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Tima Vaisburd | cf49a23 | 2017-03-28 11:35:58 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 22 | import android.annotation.SystemApi; |
Raphael | 30df237 | 2010-03-06 10:09:54 -0800 | [diff] [blame] | 23 | import android.annotation.Widget; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Hui Shu | f119c52 | 2015-10-08 10:07:13 -0700 | [diff] [blame] | 25 | import android.content.Intent; |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 26 | import android.content.pm.PackageInfo; |
Cary Clark | c5cd5e9 | 2010-11-22 15:20:02 -0500 | [diff] [blame] | 27 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.graphics.Bitmap; |
| 29 | import android.graphics.Canvas; |
John Reck | a5408e6 | 2012-03-16 14:18:44 -0700 | [diff] [blame] | 30 | import android.graphics.Paint; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.graphics.Picture; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.graphics.Rect; |
Mike Reed | e8853fc | 2009-09-04 14:01:48 -0400 | [diff] [blame] | 33 | import android.graphics.drawable.Drawable; |
Selim Gurun | 4c8093a | 2015-03-10 17:40:06 -0700 | [diff] [blame] | 34 | import android.net.Uri; |
Nate Fischer | 0e72c18 | 2017-05-30 11:25:17 -0700 | [diff] [blame] | 35 | import android.net.http.SslCertificate; |
Kristian Monsen | b5cd8c0 | 2013-04-09 17:57:37 -0700 | [diff] [blame] | 36 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.os.Bundle; |
Selim Gurun | e319dad | 2016-03-17 01:40:40 +0000 | [diff] [blame] | 38 | import android.os.Handler; |
Steve Block | 08d584c | 2011-05-17 19:05:03 +0100 | [diff] [blame] | 39 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.os.Message; |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 41 | import android.os.RemoteException; |
Nate Fischer | 0e72c18 | 2017-05-30 11:25:17 -0700 | [diff] [blame] | 42 | import android.os.StrictMode; |
Selim Gurun | da7d642 | 2013-09-06 14:39:05 -0700 | [diff] [blame] | 43 | import android.print.PrintDocumentAdapter; |
Selim Gurun | 87a0351 | 2014-05-02 14:52:49 -0700 | [diff] [blame] | 44 | import android.security.KeyChain; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.util.AttributeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.util.Log; |
Tao Bai | a571733 | 2017-03-30 14:58:53 -0700 | [diff] [blame] | 47 | import android.util.SparseArray; |
Hui Shu | ecdce96 | 2016-02-04 15:00:19 -0800 | [diff] [blame] | 48 | import android.view.DragEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.view.KeyEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.view.MotionEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.view.View; |
John Reck | 926cf56 | 2012-06-14 10:00:31 -0700 | [diff] [blame] | 52 | import android.view.ViewDebug; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.view.ViewGroup; |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 54 | import android.view.ViewHierarchyEncoder; |
Nate Fischer | 0e72c18 | 2017-05-30 11:25:17 -0700 | [diff] [blame] | 55 | import android.view.ViewStructure; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.view.ViewTreeObserver; |
Svetoslav Ganov | d9ee72f | 2011-10-05 22:26:05 -0700 | [diff] [blame] | 57 | import android.view.accessibility.AccessibilityEvent; |
Svetoslav Ganov | d9ee72f | 2011-10-05 22:26:05 -0700 | [diff] [blame] | 58 | import android.view.accessibility.AccessibilityNodeInfo; |
Ben Murdoch | e3f9071 | 2013-06-05 14:19:48 +0100 | [diff] [blame] | 59 | import android.view.accessibility.AccessibilityNodeProvider; |
Tao Bai | a571733 | 2017-03-30 14:58:53 -0700 | [diff] [blame] | 60 | import android.view.autofill.AutofillValue; |
Derek Sollenberger | 7cabb03 | 2010-01-21 10:37:38 -0500 | [diff] [blame] | 61 | import android.view.inputmethod.EditorInfo; |
| 62 | import android.view.inputmethod.InputConnection; |
Tima Vaisburd | cf49a23 | 2017-03-28 11:35:58 -0700 | [diff] [blame] | 63 | import android.view.textclassifier.TextClassifier; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.widget.AbsoluteLayout; |
Steve Block | f95d490 | 2011-06-09 11:53:26 +0100 | [diff] [blame] | 65 | |
John Reck | 926cf56 | 2012-06-14 10:00:31 -0700 | [diff] [blame] | 66 | import java.io.BufferedWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import java.io.File; |
Tobias Sargeant | d10e4af | 2017-01-19 14:03:09 +0000 | [diff] [blame] | 68 | import java.lang.annotation.Retention; |
| 69 | import java.lang.annotation.RetentionPolicy; |
Nate Fischer | 520a8ba | 2017-07-12 17:37:07 -0700 | [diff] [blame] | 70 | import java.util.List; |
Andrei Popescu | 4950b2b | 2009-09-03 13:56:07 +0100 | [diff] [blame] | 71 | import java.util.Map; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | |
| 73 | /** |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 74 | * <p>A View that displays web pages. This class is the basis upon which you |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | * can roll your own web browser or simply display some online content within your Activity. |
| 76 | * It uses the WebKit rendering engine to display |
| 77 | * web pages and includes methods to navigate forward and backward |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 78 | * through a history, zoom in and out, perform text searches and more. |
| 79 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | * <p>Note that, in order for your Activity to access the Internet and load web pages |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 81 | * in a WebView, you must add the {@code INTERNET} permissions to your |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 82 | * Android Manifest file: |
| 83 | * |
| 84 | * <pre> |
| 85 | * {@code <uses-permission android:name="android.permission.INTERNET" />} |
| 86 | * </pre> |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 87 | * |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 88 | * <p>This must be a child of the <a |
Ben Dodson | 4e8620f | 2010-08-25 10:55:47 -0700 | [diff] [blame] | 89 | * href="{@docRoot}guide/topics/manifest/manifest-element.html">{@code <manifest>}</a> |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 90 | * element. |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 91 | * |
Scott Main | 4c359b7 | 2012-07-24 15:51:27 -0700 | [diff] [blame] | 92 | * <p>For more information, read |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 93 | * <a href="{@docRoot}guide/webapps/webview.html">Building Web Apps in WebView</a>. |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 94 | * |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 95 | * <h3>Basic usage</h3> |
| 96 | * |
| 97 | * <p>By default, a WebView provides no browser-like widgets, does not |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 98 | * enable JavaScript and web page errors are ignored. If your goal is only |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 99 | * to display some HTML as a part of your UI, this is probably fine; |
| 100 | * the user won't need to interact with the web page beyond reading |
| 101 | * it, and the web page won't need to interact with the user. If you |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 102 | * actually want a full-blown web browser, then you probably want to |
| 103 | * invoke the Browser application with a URL Intent rather than show it |
| 104 | * with a WebView. For example: |
| 105 | * <pre> |
Andrew Solovay | 5114257 | 2017-05-05 16:55:43 -0700 | [diff] [blame] | 106 | * Uri uri = Uri.parse("https://www.example.com"); |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 107 | * Intent intent = new Intent(Intent.ACTION_VIEW, uri); |
| 108 | * startActivity(intent); |
| 109 | * </pre> |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 110 | * <p>See {@link android.content.Intent} for more information. |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 111 | * |
Neil Fuller | 71fbb81 | 2015-11-30 09:51:33 +0000 | [diff] [blame] | 112 | * <p>To provide a WebView in your own Activity, include a {@code <WebView>} in your layout, |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 113 | * or set the entire Activity window as a WebView during {@link |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 114 | * android.app.Activity#onCreate(Bundle) onCreate()}: |
| 115 | * |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 116 | * <pre class="prettyprint"> |
| 117 | * WebView webview = new WebView(this); |
| 118 | * setContentView(webview); |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 119 | * </pre> |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 120 | * |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 121 | * <p>Then load the desired web page: |
| 122 | * |
Scott Main | e3b9f8b | 2010-05-18 08:41:36 -0700 | [diff] [blame] | 123 | * <pre> |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 124 | * // Simplest usage: note that an exception will NOT be thrown |
| 125 | * // if there is an error loading this page (see below). |
Andrew Solovay | 5114257 | 2017-05-05 16:55:43 -0700 | [diff] [blame] | 126 | * webview.loadUrl("https://example.com/"); |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 127 | * |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 128 | * // OR, you can also load from an HTML string: |
Scott Main | e3b9f8b | 2010-05-18 08:41:36 -0700 | [diff] [blame] | 129 | * String summary = "<html><body>You scored <b>192</b> points.</body></html>"; |
Steve Block | f95d490 | 2011-06-09 11:53:26 +0100 | [diff] [blame] | 130 | * webview.loadData(summary, "text/html", null); |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 131 | * // ... although note that there are restrictions on what this HTML can do. |
Nate Fischer | 867d109 | 2018-01-04 21:57:33 -0800 | [diff] [blame] | 132 | * // See {@link #loadData(String,String,String)} and {@link |
| 133 | * #loadDataWithBaseURL(String,String,String,String,String)} for more info. |
| 134 | * // Also see {@link #loadData(String,String,String)} for information on encoding special |
| 135 | * // characters. |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 136 | * </pre> |
| 137 | * |
| 138 | * <p>A WebView has several customization points where you can add your |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 139 | * own behavior. These are: |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 140 | * |
| 141 | * <ul> |
| 142 | * <li>Creating and setting a {@link android.webkit.WebChromeClient} subclass. |
| 143 | * This class is called when something that might impact a |
| 144 | * browser UI happens, for instance, progress updates and |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 145 | * JavaScript alerts are sent here (see <a |
| 146 | * href="{@docRoot}guide/developing/debug-tasks.html#DebuggingWebPages">Debugging Tasks</a>). |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 147 | * </li> |
| 148 | * <li>Creating and setting a {@link android.webkit.WebViewClient} subclass. |
| 149 | * It will be called when things happen that impact the |
| 150 | * rendering of the content, eg, errors or form submissions. You |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 151 | * can also intercept URL loading here (via {@link |
| 152 | * android.webkit.WebViewClient#shouldOverrideUrlLoading(WebView,String) |
| 153 | * shouldOverrideUrlLoading()}).</li> |
| 154 | * <li>Modifying the {@link android.webkit.WebSettings}, such as |
| 155 | * enabling JavaScript with {@link android.webkit.WebSettings#setJavaScriptEnabled(boolean) |
| 156 | * setJavaScriptEnabled()}. </li> |
Steve Block | 4cd73c5 | 2011-11-09 13:06:52 +0000 | [diff] [blame] | 157 | * <li>Injecting Java objects into the WebView using the |
| 158 | * {@link android.webkit.WebView#addJavascriptInterface} method. This |
| 159 | * method allows you to inject Java objects into a page's JavaScript |
| 160 | * context, so that they can be accessed by JavaScript in the page.</li> |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 161 | * </ul> |
| 162 | * |
| 163 | * <p>Here's a more complicated example, showing error handling, |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 164 | * settings, and progress notification: |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 165 | * |
| 166 | * <pre class="prettyprint"> |
| 167 | * // Let's display the progress in the activity title bar, like the |
| 168 | * // browser app does. |
| 169 | * getWindow().requestFeature(Window.FEATURE_PROGRESS); |
| 170 | * |
| 171 | * webview.getSettings().setJavaScriptEnabled(true); |
| 172 | * |
| 173 | * final Activity activity = this; |
| 174 | * webview.setWebChromeClient(new WebChromeClient() { |
| 175 | * public void onProgressChanged(WebView view, int progress) { |
| 176 | * // Activities and WebViews measure progress with different scales. |
| 177 | * // The progress meter will automatically disappear when we reach 100% |
| 178 | * activity.setProgress(progress * 1000); |
| 179 | * } |
| 180 | * }); |
| 181 | * webview.setWebViewClient(new WebViewClient() { |
| 182 | * public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { |
| 183 | * Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); |
| 184 | * } |
| 185 | * }); |
| 186 | * |
Andrew Solovay | 5114257 | 2017-05-05 16:55:43 -0700 | [diff] [blame] | 187 | * webview.loadUrl("https://developer.android.com/"); |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 188 | * </pre> |
| 189 | * |
Martin Kosiba | 0d147d9 | 2013-09-19 19:05:00 -0700 | [diff] [blame] | 190 | * <h3>Zoom</h3> |
| 191 | * |
| 192 | * <p>To enable the built-in zoom, set |
| 193 | * {@link #getSettings() WebSettings}.{@link WebSettings#setBuiltInZoomControls(boolean)} |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 194 | * (introduced in API level {@link android.os.Build.VERSION_CODES#CUPCAKE}). |
| 195 | * |
Nate Fischer | 7051dd1 | 2017-10-26 14:51:25 -0700 | [diff] [blame] | 196 | * <p class="note"><b>Note:</b> Using zoom if either the height or width is set to |
Martin Kosiba | 0d147d9 | 2013-09-19 19:05:00 -0700 | [diff] [blame] | 197 | * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} may lead to undefined behavior |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 198 | * and should be avoided. |
Martin Kosiba | 0d147d9 | 2013-09-19 19:05:00 -0700 | [diff] [blame] | 199 | * |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 200 | * <h3>Cookie and window management</h3> |
| 201 | * |
| 202 | * <p>For obvious security reasons, your application has its own |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 203 | * cache, cookie store etc.—it does not share the Browser |
Steve Block | c723e35 | 2012-08-14 14:48:05 +0100 | [diff] [blame] | 204 | * application's data. |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 205 | * |
| 206 | * <p>By default, requests by the HTML to open new windows are |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 207 | * ignored. This is {@code true} whether they be opened by JavaScript or by |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 208 | * the target attribute on a link. You can customize your |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 209 | * {@link WebChromeClient} to provide your own behavior for opening multiple windows, |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 210 | * and render them in whatever manner you want. |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 211 | * |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 212 | * <p>The standard behavior for an Activity is to be destroyed and |
| 213 | * recreated when the device orientation or any other configuration changes. This will cause |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 214 | * the WebView to reload the current page. If you don't want that, you |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 215 | * can set your Activity to handle the {@code orientation} and {@code keyboardHidden} |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 216 | * changes, and then just leave the WebView alone. It'll automatically |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 217 | * re-orient itself as appropriate. Read <a |
| 218 | * href="{@docRoot}guide/topics/resources/runtime-changes.html">Handling Runtime Changes</a> for |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 219 | * more information about how to handle configuration changes during runtime. |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 220 | * |
| 221 | * |
| 222 | * <h3>Building web pages to support different screen densities</h3> |
| 223 | * |
| 224 | * <p>The screen density of a device is based on the screen resolution. A screen with low density |
| 225 | * has fewer available pixels per inch, where a screen with high density |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 226 | * has more — sometimes significantly more — pixels per inch. The density of a |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 227 | * screen is important because, other things being equal, a UI element (such as a button) whose |
| 228 | * height and width are defined in terms of screen pixels will appear larger on the lower density |
| 229 | * screen and smaller on the higher density screen. |
| 230 | * For simplicity, Android collapses all actual screen densities into three generalized densities: |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 231 | * high, medium, and low. |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 232 | * <p>By default, WebView scales a web page so that it is drawn at a size that matches the default |
| 233 | * appearance on a medium density screen. So, it applies 1.5x scaling on a high density screen |
| 234 | * (because its pixels are smaller) and 0.75x scaling on a low density screen (because its pixels |
| 235 | * are bigger). |
Steve Block | b838aef | 2012-04-23 18:22:15 +0100 | [diff] [blame] | 236 | * Starting with API level {@link android.os.Build.VERSION_CODES#ECLAIR}, WebView supports DOM, CSS, |
| 237 | * and meta tag features to help you (as a web developer) target screens with different screen |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 238 | * densities. |
| 239 | * <p>Here's a summary of the features you can use to handle different screen densities: |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 240 | * <ul> |
| 241 | * <li>The {@code window.devicePixelRatio} DOM property. The value of this property specifies the |
| 242 | * default scaling factor used for the current device. For example, if the value of {@code |
| 243 | * window.devicePixelRatio} is "1.0", then the device is considered a medium density (mdpi) device |
| 244 | * and default scaling is not applied to the web page; if the value is "1.5", then the device is |
| 245 | * considered a high density device (hdpi) and the page content is scaled 1.5x; if the |
| 246 | * value is "0.75", then the device is considered a low density device (ldpi) and the content is |
Jonathan Dixon | 89f48e9 | 2013-02-27 16:42:48 -0800 | [diff] [blame] | 247 | * scaled 0.75x.</li> |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 248 | * <li>The {@code -webkit-device-pixel-ratio} CSS media query. Use this to specify the screen |
| 249 | * densities for which this style sheet is to be used. The corresponding value should be either |
| 250 | * "0.75", "1", or "1.5", to indicate that the styles are for devices with low density, medium |
| 251 | * density, or high density screens, respectively. For example: |
| 252 | * <pre> |
| 253 | * <link rel="stylesheet" media="screen and (-webkit-device-pixel-ratio:1.5)" href="hdpi.css" /></pre> |
| 254 | * <p>The {@code hdpi.css} stylesheet is only used for devices with a screen pixel ration of 1.5, |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 255 | * which is the high density pixel ratio. |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 256 | * </li> |
Martin Kosiba | 0d147d9 | 2013-09-19 19:05:00 -0700 | [diff] [blame] | 257 | * </ul> |
Scott Main | 8b3cea0 | 2010-05-14 14:12:43 -0700 | [diff] [blame] | 258 | * |
Teng-Hui Zhu | 50ba5a2 | 2012-01-11 15:57:21 -0800 | [diff] [blame] | 259 | * <h3>HTML5 Video support</h3> |
| 260 | * |
Ignacio Solla | 7a4e18f | 2014-12-29 12:21:52 +0000 | [diff] [blame] | 261 | * <p>In order to support inline HTML5 video in your application you need to have hardware |
| 262 | * acceleration turned on. |
Ignacio Solla | 7a4e18f | 2014-12-29 12:21:52 +0000 | [diff] [blame] | 263 | * |
| 264 | * <h3>Full screen support</h3> |
| 265 | * |
| 266 | * <p>In order to support full screen — for video or other HTML content — you need to set a |
| 267 | * {@link android.webkit.WebChromeClient} and implement both |
| 268 | * {@link WebChromeClient#onShowCustomView(View, WebChromeClient.CustomViewCallback)} |
| 269 | * and {@link WebChromeClient#onHideCustomView()}. If the implementation of either of these two methods is |
| 270 | * missing then the web contents will not be allowed to enter full screen. Optionally you can implement |
| 271 | * {@link WebChromeClient#getVideoLoadingProgressView()} to customize the View displayed whilst a video |
| 272 | * is loading. |
Marcin Kosiba | 84644f1 | 2014-09-08 18:37:36 +0100 | [diff] [blame] | 273 | * |
Tim Volodine | b0e9798 | 2016-04-27 14:14:09 +0100 | [diff] [blame] | 274 | * <h3>HTML5 Geolocation API support</h3> |
| 275 | * |
| 276 | * <p>For applications targeting Android N and later releases |
| 277 | * (API level > {@link android.os.Build.VERSION_CODES#M}) the geolocation api is only supported on |
| 278 | * secure origins such as https. For such applications requests to geolocation api on non-secure |
| 279 | * origins are automatically denied without invoking the corresponding |
| 280 | * {@link WebChromeClient#onGeolocationPermissionsShowPrompt(String, GeolocationPermissions.Callback)} |
| 281 | * method. |
Tim Volodine | b0e9798 | 2016-04-27 14:14:09 +0100 | [diff] [blame] | 282 | * |
Marcin Kosiba | 84644f1 | 2014-09-08 18:37:36 +0100 | [diff] [blame] | 283 | * <h3>Layout size</h3> |
| 284 | * <p> |
| 285 | * It is recommended to set the WebView layout height to a fixed value or to |
| 286 | * {@link android.view.ViewGroup.LayoutParams#MATCH_PARENT} instead of using |
| 287 | * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT}. |
| 288 | * When using {@link android.view.ViewGroup.LayoutParams#MATCH_PARENT} |
| 289 | * for the height none of the WebView's parents should use a |
| 290 | * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} layout height since that could result in |
| 291 | * incorrect sizing of the views. |
Marcin Kosiba | 84644f1 | 2014-09-08 18:37:36 +0100 | [diff] [blame] | 292 | * |
| 293 | * <p>Setting the WebView's height to {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} |
| 294 | * enables the following behaviors: |
| 295 | * <ul> |
| 296 | * <li>The HTML body layout height is set to a fixed value. This means that elements with a height |
| 297 | * relative to the HTML body may not be sized correctly. </li> |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 298 | * <li>For applications targeting {@link android.os.Build.VERSION_CODES#KITKAT} and earlier SDKs the |
Marcin Kosiba | 84644f1 | 2014-09-08 18:37:36 +0100 | [diff] [blame] | 299 | * HTML viewport meta tag will be ignored in order to preserve backwards compatibility. </li> |
| 300 | * </ul> |
Marcin Kosiba | 84644f1 | 2014-09-08 18:37:36 +0100 | [diff] [blame] | 301 | * |
| 302 | * <p> |
| 303 | * Using a layout width of {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} is not |
| 304 | * supported. If such a width is used the WebView will attempt to use the width of the parent |
| 305 | * instead. |
Marcin Kosiba | 84644f1 | 2014-09-08 18:37:36 +0100 | [diff] [blame] | 306 | * |
Paul Miller | a469218 | 2016-01-07 11:32:15 -0800 | [diff] [blame] | 307 | * <h3>Metrics</h3> |
| 308 | * |
| 309 | * <p> |
| 310 | * WebView may upload anonymous diagnostic data to Google when the user has consented. This data |
| 311 | * helps Google improve WebView. Data is collected on a per-app basis for each app which has |
| 312 | * instantiated a WebView. An individual app can opt out of this feature by putting the following |
Nate Fischer | 2063508 | 2017-11-08 13:39:08 -0800 | [diff] [blame] | 313 | * tag in its manifest's {@code <application>} element: |
Paul Miller | a469218 | 2016-01-07 11:32:15 -0800 | [diff] [blame] | 314 | * <pre> |
Nate Fischer | 2063508 | 2017-11-08 13:39:08 -0800 | [diff] [blame] | 315 | * <manifest> |
| 316 | * <application> |
| 317 | * ... |
| 318 | * <meta-data android:name="android.webkit.WebView.MetricsOptOut" |
| 319 | * android:value="true" /> |
| 320 | * </application> |
| 321 | * </manifest> |
Paul Miller | a469218 | 2016-01-07 11:32:15 -0800 | [diff] [blame] | 322 | * </pre> |
| 323 | * <p> |
| 324 | * Data will only be uploaded for a given app if the user has consented AND the app has not opted |
| 325 | * out. |
Paul Miller | a469218 | 2016-01-07 11:32:15 -0800 | [diff] [blame] | 326 | * |
Nate Fischer | 471891d | 2017-07-18 19:15:52 -0700 | [diff] [blame] | 327 | * <h3>Safe Browsing</h3> |
| 328 | * |
| 329 | * <p> |
Nate Fischer | fb92ee1 | 2018-01-18 12:01:19 -0800 | [diff] [blame] | 330 | * With Safe Browsing, WebView will block malicious URLs and present a warning UI to the user to |
| 331 | * allow them to navigate back safely or proceed to the malicious page. |
Nate Fischer | 471891d | 2017-07-18 19:15:52 -0700 | [diff] [blame] | 332 | * <p> |
Nate Fischer | fb92ee1 | 2018-01-18 12:01:19 -0800 | [diff] [blame] | 333 | * Safe Browsing is enabled by default on devices which support it. If your app needs to disable |
| 334 | * Safe Browsing for all WebViews, it can do so in the manifest's {@code <application>} element: |
Nate Fischer | 471891d | 2017-07-18 19:15:52 -0700 | [diff] [blame] | 335 | * <p> |
| 336 | * <pre> |
Nate Fischer | 2063508 | 2017-11-08 13:39:08 -0800 | [diff] [blame] | 337 | * <manifest> |
| 338 | * <application> |
| 339 | * ... |
| 340 | * <meta-data android:name="android.webkit.WebView.EnableSafeBrowsing" |
Nate Fischer | fb92ee1 | 2018-01-18 12:01:19 -0800 | [diff] [blame] | 341 | * android:value="false" /> |
Nate Fischer | 2063508 | 2017-11-08 13:39:08 -0800 | [diff] [blame] | 342 | * </application> |
| 343 | * </manifest> |
Nate Fischer | 471891d | 2017-07-18 19:15:52 -0700 | [diff] [blame] | 344 | * </pre> |
Nate Fischer | 471891d | 2017-07-18 19:15:52 -0700 | [diff] [blame] | 345 | * |
Nate Fischer | fb92ee1 | 2018-01-18 12:01:19 -0800 | [diff] [blame] | 346 | * <p> |
| 347 | * Otherwise, see {@link WebSettings#setSafeBrowsingEnabled}. |
| 348 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | */ |
Steve Block | 406aeb2 | 2012-04-23 18:17:19 +0100 | [diff] [blame] | 350 | // Implementation notes. |
| 351 | // The WebView is a thin API class that delegates its public API to a backend WebViewProvider |
| 352 | // class instance. WebView extends {@link AbsoluteLayout} for backward compatibility reasons. |
| 353 | // Methods are delegated to the provider implementation: all public API methods introduced in this |
| 354 | // file are fully delegated, whereas public and protected methods from the View base classes are |
| 355 | // only delegated where a specific need exists for them to do so. |
Raphael | 30df237 | 2010-03-06 10:09:54 -0800 | [diff] [blame] | 356 | @Widget |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 357 | public class WebView extends AbsoluteLayout |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | implements ViewTreeObserver.OnGlobalFocusChangeListener, |
John Reck | 926cf56 | 2012-06-14 10:00:31 -0700 | [diff] [blame] | 359 | ViewGroup.OnHierarchyChangeListener, ViewDebug.HierarchyHandler { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | |
Jonathan Dixon | 69f9f93 | 2013-08-28 15:54:29 -0700 | [diff] [blame] | 361 | private static final String LOGTAG = "WebView"; |
Nicolas Roard | 12c18e6 | 2010-10-13 20:14:31 -0700 | [diff] [blame] | 362 | |
Kristian Monsen | b5cd8c0 | 2013-04-09 17:57:37 -0700 | [diff] [blame] | 363 | // Throwing an exception for incorrect thread usage if the |
| 364 | // build target is JB MR2 or newer. Defaults to false, and is |
| 365 | // set in the WebView constructor. |
Jonathan Dixon | 0d6a145 | 2013-08-21 13:09:36 -0700 | [diff] [blame] | 366 | private static volatile boolean sEnforceThreadChecking = false; |
Kristian Monsen | b5cd8c0 | 2013-04-09 17:57:37 -0700 | [diff] [blame] | 367 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | /** |
| 369 | * Transportation object for returning WebView across thread boundaries. |
| 370 | */ |
| 371 | public class WebViewTransport { |
| 372 | private WebView mWebview; |
| 373 | |
| 374 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 375 | * Sets the WebView to the transportation object. |
| 376 | * |
| 377 | * @param webview the WebView to transport |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | */ |
| 379 | public synchronized void setWebView(WebView webview) { |
| 380 | mWebview = webview; |
| 381 | } |
| 382 | |
| 383 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 384 | * Gets the WebView object. |
| 385 | * |
| 386 | * @return the transported WebView object |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | */ |
| 388 | public synchronized WebView getWebView() { |
| 389 | return mWebview; |
| 390 | } |
| 391 | } |
| 392 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 394 | * URI scheme for telephone number. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | */ |
| 396 | public static final String SCHEME_TEL = "tel:"; |
| 397 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 398 | * URI scheme for email address. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | */ |
| 400 | public static final String SCHEME_MAILTO = "mailto:"; |
| 401 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 402 | * URI scheme for map address. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | */ |
| 404 | public static final String SCHEME_GEO = "geo:0,0?q="; |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 405 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | /** |
Victoria Lease | d405a43 | 2012-03-22 15:53:48 -0700 | [diff] [blame] | 407 | * Interface to listen for find results. |
Victoria Lease | d405a43 | 2012-03-22 15:53:48 -0700 | [diff] [blame] | 408 | */ |
| 409 | public interface FindListener { |
| 410 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 411 | * Notifies the listener about progress made by a find operation. |
Victoria Lease | d405a43 | 2012-03-22 15:53:48 -0700 | [diff] [blame] | 412 | * |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 413 | * @param activeMatchOrdinal the zero-based ordinal of the currently selected match |
Selim Gurun | 92b81a3 | 2012-08-21 17:32:35 -0700 | [diff] [blame] | 414 | * @param numberOfMatches how many matches have been found |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 415 | * @param isDoneCounting whether the find operation has actually completed. The listener |
| 416 | * may be notified multiple times while the |
| 417 | * operation is underway, and the numberOfMatches |
| 418 | * value should not be considered final unless |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 419 | * isDoneCounting is {@code true}. |
Victoria Lease | d405a43 | 2012-03-22 15:53:48 -0700 | [diff] [blame] | 420 | */ |
Selim Gurun | 92b81a3 | 2012-08-21 17:32:35 -0700 | [diff] [blame] | 421 | public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, |
Victoria Lease | d405a43 | 2012-03-22 15:53:48 -0700 | [diff] [blame] | 422 | boolean isDoneCounting); |
| 423 | } |
| 424 | |
| 425 | /** |
Tobias Sargeant | b365604 | 2015-05-13 11:23:57 +0100 | [diff] [blame] | 426 | * Callback interface supplied to {@link #postVisualStateCallback} for receiving |
Tobias Sargeant | a8352f4 | 2015-03-03 18:07:40 +0000 | [diff] [blame] | 427 | * notifications about the visual state. |
| 428 | */ |
| 429 | public static abstract class VisualStateCallback { |
| 430 | /** |
| 431 | * Invoked when the visual state is ready to be drawn in the next {@link #onDraw}. |
| 432 | * |
Tobias Sargeant | b365604 | 2015-05-13 11:23:57 +0100 | [diff] [blame] | 433 | * @param requestId The identifier passed to {@link #postVisualStateCallback} when this |
| 434 | * callback was posted. |
Tobias Sargeant | a8352f4 | 2015-03-03 18:07:40 +0000 | [diff] [blame] | 435 | */ |
| 436 | public abstract void onComplete(long requestId); |
| 437 | } |
| 438 | |
| 439 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | * Interface to listen for new pictures as they change. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 441 | * |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 442 | * @deprecated This interface is now obsolete. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | */ |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 444 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | public interface PictureListener { |
| 446 | /** |
Steve Block | 72498ed | 2012-07-17 15:57:25 +0100 | [diff] [blame] | 447 | * Used to provide notification that the WebView's picture has changed. |
| 448 | * See {@link WebView#capturePicture} for details of the picture. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 449 | * |
| 450 | * @param view the WebView that owns the picture |
Ben Murdoch | 52643e0 | 2013-02-26 12:01:00 +0000 | [diff] [blame] | 451 | * @param picture the new picture. Applications targeting |
| 452 | * {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2} or above |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 453 | * will always receive a {@code null} Picture. |
Steve Block | 72498ed | 2012-07-17 15:57:25 +0100 | [diff] [blame] | 454 | * @deprecated Deprecated due to internal changes. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | */ |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 456 | @Deprecated |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 457 | void onNewPicture(WebView view, @Nullable Picture picture); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Jonathan Dixon | 19644b6 | 2011-12-21 14:21:36 +0000 | [diff] [blame] | 460 | public static class HitTestResult { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 462 | * Default HitTestResult, where the target is unknown. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | */ |
| 464 | public static final int UNKNOWN_TYPE = 0; |
| 465 | /** |
Steve Block | 1854ddb | 2011-04-19 12:18:19 +0100 | [diff] [blame] | 466 | * @deprecated This type is no longer used. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | */ |
Steve Block | 1854ddb | 2011-04-19 12:18:19 +0100 | [diff] [blame] | 468 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | public static final int ANCHOR_TYPE = 1; |
| 470 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 471 | * HitTestResult for hitting a phone number. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | */ |
| 473 | public static final int PHONE_TYPE = 2; |
| 474 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 475 | * HitTestResult for hitting a map address. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | */ |
| 477 | public static final int GEO_TYPE = 3; |
| 478 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 479 | * HitTestResult for hitting an email address. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | */ |
| 481 | public static final int EMAIL_TYPE = 4; |
| 482 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 483 | * HitTestResult for hitting an HTML::img tag. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | */ |
| 485 | public static final int IMAGE_TYPE = 5; |
| 486 | /** |
Steve Block | 1854ddb | 2011-04-19 12:18:19 +0100 | [diff] [blame] | 487 | * @deprecated This type is no longer used. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | */ |
Steve Block | 1854ddb | 2011-04-19 12:18:19 +0100 | [diff] [blame] | 489 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | public static final int IMAGE_ANCHOR_TYPE = 6; |
| 491 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 492 | * HitTestResult for hitting a HTML::a tag with src=http. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | */ |
| 494 | public static final int SRC_ANCHOR_TYPE = 7; |
| 495 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 496 | * HitTestResult for hitting a HTML::a tag with src=http + HTML::img. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | */ |
| 498 | public static final int SRC_IMAGE_ANCHOR_TYPE = 8; |
| 499 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 500 | * HitTestResult for hitting an edit text area. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | */ |
| 502 | public static final int EDIT_TEXT_TYPE = 9; |
| 503 | |
| 504 | private int mType; |
| 505 | private String mExtra; |
| 506 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 507 | /** |
| 508 | * @hide Only for use by WebViewProvider implementations |
| 509 | */ |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 510 | @SystemApi |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 511 | public HitTestResult() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | mType = UNKNOWN_TYPE; |
| 513 | } |
| 514 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 515 | /** |
| 516 | * @hide Only for use by WebViewProvider implementations |
| 517 | */ |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 518 | @SystemApi |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 519 | public void setType(int type) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | mType = type; |
| 521 | } |
| 522 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 523 | /** |
| 524 | * @hide Only for use by WebViewProvider implementations |
| 525 | */ |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 526 | @SystemApi |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 527 | public void setExtra(String extra) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | mExtra = extra; |
| 529 | } |
| 530 | |
Jonathan Dixon | e230e54 | 2011-12-21 10:57:00 +0000 | [diff] [blame] | 531 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 532 | * Gets the type of the hit test result. See the XXX_TYPE constants |
| 533 | * defined in this class. |
| 534 | * |
| 535 | * @return the type of the hit test result |
Jonathan Dixon | e230e54 | 2011-12-21 10:57:00 +0000 | [diff] [blame] | 536 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | public int getType() { |
| 538 | return mType; |
| 539 | } |
| 540 | |
Jonathan Dixon | e230e54 | 2011-12-21 10:57:00 +0000 | [diff] [blame] | 541 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 542 | * Gets additional type-dependant information about the result. See |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 543 | * {@link WebView#getHitTestResult()} for details. May either be {@code null} |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 544 | * or contain extra information about this result. |
| 545 | * |
| 546 | * @return additional type-dependant information about the result |
Jonathan Dixon | e230e54 | 2011-12-21 10:57:00 +0000 | [diff] [blame] | 547 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 548 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | public String getExtra() { |
| 550 | return mExtra; |
| 551 | } |
| 552 | } |
| 553 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 554 | /** |
Nate Fischer | 674561a | 2017-10-26 12:28:01 -0700 | [diff] [blame] | 555 | * Constructs a new WebView with an Activity Context object. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 556 | * |
Nate Fischer | 674561a | 2017-10-26 12:28:01 -0700 | [diff] [blame] | 557 | * <p class="note"><b>Note:</b> WebView should always be instantiated with an Activity Context. |
| 558 | * If instantiated with an Application Context, WebView will be unable to provide several |
| 559 | * features, such as JavaScript dialogs and autofill. |
| 560 | * |
| 561 | * @param context an Activity Context to access application assets |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | */ |
| 563 | public WebView(Context context) { |
| 564 | this(context, null); |
| 565 | } |
| 566 | |
| 567 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 568 | * Constructs a new WebView with layout parameters. |
| 569 | * |
Nate Fischer | 674561a | 2017-10-26 12:28:01 -0700 | [diff] [blame] | 570 | * @param context an Activity Context to access application assets |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 571 | * @param attrs an AttributeSet passed to our parent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | */ |
| 573 | public WebView(Context context, AttributeSet attrs) { |
| 574 | this(context, attrs, com.android.internal.R.attr.webViewStyle); |
| 575 | } |
| 576 | |
| 577 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 578 | * Constructs a new WebView with layout parameters and a default style. |
| 579 | * |
Nate Fischer | 674561a | 2017-10-26 12:28:01 -0700 | [diff] [blame] | 580 | * @param context an Activity Context to access application assets |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 581 | * @param attrs an AttributeSet passed to our parent |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 582 | * @param defStyleAttr an attribute in the current theme that contains a |
| 583 | * reference to a style resource that supplies default values for |
| 584 | * the view. Can be 0 to not look for defaults. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | */ |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 586 | public WebView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 587 | this(context, attrs, defStyleAttr, 0); |
Elliott Slaughter | f21d2e3 | 2010-07-14 18:08:54 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 591 | * Constructs a new WebView with layout parameters and a default style. |
| 592 | * |
Nate Fischer | 674561a | 2017-10-26 12:28:01 -0700 | [diff] [blame] | 593 | * @param context an Activity Context to access application assets |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 594 | * @param attrs an AttributeSet passed to our parent |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 595 | * @param defStyleAttr an attribute in the current theme that contains a |
| 596 | * reference to a style resource that supplies default values for |
| 597 | * the view. Can be 0 to not look for defaults. |
| 598 | * @param defStyleRes a resource identifier of a style resource that |
| 599 | * supplies default values for the view, used only if |
| 600 | * defStyleAttr is 0 or can not be found in the theme. Can be 0 |
| 601 | * to not look for defaults. |
| 602 | */ |
| 603 | public WebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 604 | this(context, attrs, defStyleAttr, defStyleRes, null, false); |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Constructs a new WebView with layout parameters and a default style. |
| 609 | * |
Nate Fischer | 674561a | 2017-10-26 12:28:01 -0700 | [diff] [blame] | 610 | * @param context an Activity Context to access application assets |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 611 | * @param attrs an AttributeSet passed to our parent |
| 612 | * @param defStyleAttr an attribute in the current theme that contains a |
| 613 | * reference to a style resource that supplies default values for |
| 614 | * the view. Can be 0 to not look for defaults. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 615 | * @param privateBrowsing whether this WebView will be initialized in |
| 616 | * private mode |
Kristian Monsen | 5cc2351 | 2012-08-09 15:33:08 -0400 | [diff] [blame] | 617 | * |
Selim Gurun | b6aa97e | 2014-03-26 14:10:28 -0700 | [diff] [blame] | 618 | * @deprecated Private browsing is no longer supported directly via |
Kristian Monsen | 5cc2351 | 2012-08-09 15:33:08 -0400 | [diff] [blame] | 619 | * WebView and will be removed in a future release. Prefer using |
| 620 | * {@link WebSettings}, {@link WebViewDatabase}, {@link CookieManager} |
| 621 | * and {@link WebStorage} for fine-grained control of privacy data. |
Elliott Slaughter | f21d2e3 | 2010-07-14 18:08:54 -0700 | [diff] [blame] | 622 | */ |
Kristian Monsen | 5cc2351 | 2012-08-09 15:33:08 -0400 | [diff] [blame] | 623 | @Deprecated |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 624 | public WebView(Context context, AttributeSet attrs, int defStyleAttr, |
Elliott Slaughter | f21d2e3 | 2010-07-14 18:08:54 -0700 | [diff] [blame] | 625 | boolean privateBrowsing) { |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 626 | this(context, attrs, defStyleAttr, 0, null, privateBrowsing); |
Andrei Popescu | 4950b2b | 2009-09-03 13:56:07 +0100 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 630 | * Constructs a new WebView with layout parameters, a default style and a set |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 631 | * of custom JavaScript interfaces to be added to this WebView at initialization |
Romain Guy | 01d0fbf | 2009-12-01 14:52:19 -0800 | [diff] [blame] | 632 | * time. This guarantees that these interfaces will be available when the JS |
Andrei Popescu | 4950b2b | 2009-09-03 13:56:07 +0100 | [diff] [blame] | 633 | * context is initialized. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 634 | * |
Nate Fischer | 674561a | 2017-10-26 12:28:01 -0700 | [diff] [blame] | 635 | * @param context an Activity Context to access application assets |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 636 | * @param attrs an AttributeSet passed to our parent |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 637 | * @param defStyleAttr an attribute in the current theme that contains a |
| 638 | * reference to a style resource that supplies default values for |
| 639 | * the view. Can be 0 to not look for defaults. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 640 | * @param javaScriptInterfaces a Map of interface names, as keys, and |
| 641 | * object implementing those interfaces, as |
| 642 | * values |
| 643 | * @param privateBrowsing whether this WebView will be initialized in |
| 644 | * private mode |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 645 | * @hide This is used internally by dumprendertree, as it requires the JavaScript interfaces to |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 646 | * be added synchronously, before a subsequent loadUrl call takes effect. |
Andrei Popescu | 4950b2b | 2009-09-03 13:56:07 +0100 | [diff] [blame] | 647 | */ |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 648 | protected WebView(Context context, AttributeSet attrs, int defStyleAttr, |
Steve Block | 81f19ff | 2010-11-01 13:23:24 +0000 | [diff] [blame] | 649 | Map<String, Object> javaScriptInterfaces, boolean privateBrowsing) { |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 650 | this(context, attrs, defStyleAttr, 0, javaScriptInterfaces, privateBrowsing); |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * @hide |
| 655 | */ |
| 656 | @SuppressWarnings("deprecation") // for super() call into deprecated base class constructor. |
| 657 | protected WebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, |
| 658 | Map<String, Object> javaScriptInterfaces, boolean privateBrowsing) { |
| 659 | super(context, attrs, defStyleAttr, defStyleRes); |
Felipe Leme | d04a697 | 2017-03-02 12:56:18 -0800 | [diff] [blame] | 660 | |
| 661 | // WebView is important by default, unless app developer overrode attribute. |
| 662 | if (getImportantForAutofill() == IMPORTANT_FOR_AUTOFILL_AUTO) { |
| 663 | setImportantForAutofill(IMPORTANT_FOR_AUTOFILL_YES); |
| 664 | } |
| 665 | |
Kristian Monsen | 87af731 | 2011-09-09 02:12:51 +0100 | [diff] [blame] | 666 | if (context == null) { |
| 667 | throw new IllegalArgumentException("Invalid context argument"); |
| 668 | } |
Gustav Sennton | 0167369 | 2018-01-19 12:11:25 +0000 | [diff] [blame] | 669 | if (mWebViewThread == null) { |
| 670 | throw new RuntimeException( |
| 671 | "WebView cannot be initialized on a thread that has no Looper."); |
| 672 | } |
Kristian Monsen | b5cd8c0 | 2013-04-09 17:57:37 -0700 | [diff] [blame] | 673 | sEnforceThreadChecking = context.getApplicationInfo().targetSdkVersion >= |
| 674 | Build.VERSION_CODES.JELLY_BEAN_MR2; |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 675 | checkThread(); |
Kristian Monsen | 87af731 | 2011-09-09 02:12:51 +0100 | [diff] [blame] | 676 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 677 | ensureProviderCreated(); |
| 678 | mProvider.init(javaScriptInterfaces, privateBrowsing); |
Jonathan Dixon | eb9e801 | 2013-10-03 11:03:31 +0100 | [diff] [blame] | 679 | // Post condition of creating a webview is the CookieSyncManager.getInstance() is allowed. |
| 680 | CookieSyncManager.setGetInstanceIsAllowed(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 684 | * Specifies whether the horizontal scrollbar has overlay style. |
| 685 | * |
Paul Miller | c8694a0 | 2015-06-05 11:23:03 -0700 | [diff] [blame] | 686 | * @deprecated This method has no effect. |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 687 | * @param overlay {@code true} if horizontal scrollbar should have overlay style |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 688 | */ |
Paul Miller | c8694a0 | 2015-06-05 11:23:03 -0700 | [diff] [blame] | 689 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | public void setHorizontalScrollbarOverlay(boolean overlay) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 694 | * Specifies whether the vertical scrollbar has overlay style. |
| 695 | * |
Paul Miller | c8694a0 | 2015-06-05 11:23:03 -0700 | [diff] [blame] | 696 | * @deprecated This method has no effect. |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 697 | * @param overlay {@code true} if vertical scrollbar should have overlay style |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | */ |
Paul Miller | c8694a0 | 2015-06-05 11:23:03 -0700 | [diff] [blame] | 699 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 700 | public void setVerticalScrollbarOverlay(boolean overlay) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 704 | * Gets whether horizontal scrollbar has overlay style. |
| 705 | * |
Paul Miller | c8694a0 | 2015-06-05 11:23:03 -0700 | [diff] [blame] | 706 | * @deprecated This method is now obsolete. |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 707 | * @return {@code true} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 708 | */ |
Paul Miller | c8694a0 | 2015-06-05 11:23:03 -0700 | [diff] [blame] | 709 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | public boolean overlayHorizontalScrollbar() { |
Paul Miller | c8694a0 | 2015-06-05 11:23:03 -0700 | [diff] [blame] | 711 | // The old implementation defaulted to true, so return true for consistency |
| 712 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 716 | * Gets whether vertical scrollbar has overlay style. |
| 717 | * |
Paul Miller | c8694a0 | 2015-06-05 11:23:03 -0700 | [diff] [blame] | 718 | * @deprecated This method is now obsolete. |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 719 | * @return {@code false} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | */ |
Paul Miller | c8694a0 | 2015-06-05 11:23:03 -0700 | [diff] [blame] | 721 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | public boolean overlayVerticalScrollbar() { |
Paul Miller | c8694a0 | 2015-06-05 11:23:03 -0700 | [diff] [blame] | 723 | // The old implementation defaulted to false, so return false for consistency |
| 724 | return false; |
Mike Reed | e8853fc | 2009-09-04 14:01:48 -0400 | [diff] [blame] | 725 | } |
| 726 | |
Michael Kolb | e54f665 | 2011-03-16 09:11:51 -0700 | [diff] [blame] | 727 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 728 | * Gets the visible height (in pixels) of the embedded title bar (if any). |
Michael Kolb | 73980a9 | 2010-08-05 16:32:51 -0700 | [diff] [blame] | 729 | * |
Michael Kolb | 24e53b0 | 2011-03-16 12:52:04 -0700 | [diff] [blame] | 730 | * @deprecated This method is now obsolete. |
Kristian Monsen | f491258 | 2012-08-16 15:26:24 -0400 | [diff] [blame] | 731 | * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} |
Mike Reed | e8853fc | 2009-09-04 14:01:48 -0400 | [diff] [blame] | 732 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 733 | @Deprecated |
Michael Kolb | 73980a9 | 2010-08-05 16:32:51 -0700 | [diff] [blame] | 734 | public int getVisibleTitleHeight() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 735 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 736 | return mProvider.getVisibleTitleHeight(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /** |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 740 | * Gets the SSL certificate for the main top-level page or {@code null} if there is |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 741 | * no certificate (the site is not secure). |
| 742 | * |
| 743 | * @return the SSL certificate for the main top-level page |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 744 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 745 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | public SslCertificate getCertificate() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 747 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 748 | return mProvider.getCertificate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /** |
| 752 | * Sets the SSL certificate for the main top-level page. |
Kristian Monsen | 5cc2351 | 2012-08-09 15:33:08 -0400 | [diff] [blame] | 753 | * |
| 754 | * @deprecated Calling this function has no useful effect, and will be |
| 755 | * ignored in future releases. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 756 | */ |
Kristian Monsen | 5cc2351 | 2012-08-09 15:33:08 -0400 | [diff] [blame] | 757 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | public void setCertificate(SslCertificate certificate) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 759 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 760 | mProvider.setCertificate(certificate); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | //------------------------------------------------------------------------- |
| 764 | // Methods called by activity |
| 765 | //------------------------------------------------------------------------- |
| 766 | |
| 767 | /** |
Steve Block | 32fe410 | 2012-07-17 16:29:11 +0100 | [diff] [blame] | 768 | * Sets a username and password pair for the specified host. This data is |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 769 | * used by the WebView to autocomplete username and password fields in web |
Steve Block | 32fe410 | 2012-07-17 16:29:11 +0100 | [diff] [blame] | 770 | * forms. Note that this is unrelated to the credentials used for HTTP |
| 771 | * authentication. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 772 | * |
Selim Gurun | 38915fd | 2013-04-04 17:14:29 +0000 | [diff] [blame] | 773 | * @param host the host that required the credentials |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 774 | * @param username the username for the given host |
| 775 | * @param password the password for the given host |
Steve Block | 32fe410 | 2012-07-17 16:29:11 +0100 | [diff] [blame] | 776 | * @see WebViewDatabase#clearUsernamePassword |
| 777 | * @see WebViewDatabase#hasUsernamePassword |
Jonathan Dixon | 835b1fc | 2013-02-25 12:29:33 -0800 | [diff] [blame] | 778 | * @deprecated Saving passwords in WebView will not be supported in future versions. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 779 | */ |
Jonathan Dixon | 835b1fc | 2013-02-25 12:29:33 -0800 | [diff] [blame] | 780 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 781 | public void savePassword(String host, String username, String password) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 782 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 783 | mProvider.savePassword(host, username, password); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | /** |
Hui Shu | 433fb93 | 2016-08-29 11:49:46 -0700 | [diff] [blame] | 787 | * Stores HTTP authentication credentials for a given host and realm to the {@link WebViewDatabase} |
| 788 | * instance. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | * |
Steve Block | 46ce1db | 2012-07-17 16:43:00 +0100 | [diff] [blame] | 790 | * @param host the host to which the credentials apply |
| 791 | * @param realm the realm to which the credentials apply |
| 792 | * @param username the username |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 793 | * @param password the password |
Nate Fischer | c7edfb0 | 2016-09-23 15:59:21 -0700 | [diff] [blame] | 794 | * @deprecated Use {@link WebViewDatabase#setHttpAuthUsernamePassword} instead |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | */ |
Nate Fischer | c7edfb0 | 2016-09-23 15:59:21 -0700 | [diff] [blame] | 796 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 797 | public void setHttpAuthUsernamePassword(String host, String realm, |
| 798 | String username, String password) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 799 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 800 | mProvider.setHttpAuthUsernamePassword(host, realm, username, password); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /** |
Hui Shu | 433fb93 | 2016-08-29 11:49:46 -0700 | [diff] [blame] | 804 | * Retrieves HTTP authentication credentials for a given host and realm from the {@link |
| 805 | * WebViewDatabase} instance. |
Steve Block | 46ce1db | 2012-07-17 16:43:00 +0100 | [diff] [blame] | 806 | * @param host the host to which the credentials apply |
| 807 | * @param realm the realm to which the credentials apply |
| 808 | * @return the credentials as a String array, if found. The first element |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 809 | * is the username and the second element is the password. {@code null} if |
Steve Block | 46ce1db | 2012-07-17 16:43:00 +0100 | [diff] [blame] | 810 | * no credentials are found. |
Nate Fischer | c7edfb0 | 2016-09-23 15:59:21 -0700 | [diff] [blame] | 811 | * @deprecated Use {@link WebViewDatabase#getHttpAuthUsernamePassword} instead |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | */ |
Nate Fischer | c7edfb0 | 2016-09-23 15:59:21 -0700 | [diff] [blame] | 813 | @Deprecated |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 814 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | public String[] getHttpAuthUsernamePassword(String host, String realm) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 816 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 817 | return mProvider.getHttpAuthUsernamePassword(host, realm); |
Leon Scroggins | 05919f2 | 2010-09-14 17:22:36 -0400 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 821 | * Destroys the internal state of this WebView. This method should be called |
| 822 | * after this WebView has been removed from the view system. No other |
| 823 | * methods may be called on this WebView after destroy. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | */ |
| 825 | public void destroy() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 826 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 827 | mProvider.destroy(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | /** |
| 831 | * Enables platform notifications of data state and proxy changes. |
Kristian Monsen | cbb59db | 2011-05-09 16:04:34 +0100 | [diff] [blame] | 832 | * Notifications are enabled by default. |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 833 | * |
| 834 | * @deprecated This method is now obsolete. |
Kristian Monsen | f491258 | 2012-08-16 15:26:24 -0400 | [diff] [blame] | 835 | * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | */ |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 837 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | public static void enablePlatformNotifications() { |
Kristian Monsen | 10ca808 | 2014-04-29 11:02:42 -0700 | [diff] [blame] | 839 | // noop |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /** |
Kristian Monsen | cbb59db | 2011-05-09 16:04:34 +0100 | [diff] [blame] | 843 | * Disables platform notifications of data state and proxy changes. |
| 844 | * Notifications are enabled by default. |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 845 | * |
| 846 | * @deprecated This method is now obsolete. |
Kristian Monsen | f491258 | 2012-08-16 15:26:24 -0400 | [diff] [blame] | 847 | * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | */ |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 849 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | public static void disablePlatformNotifications() { |
Kristian Monsen | 10ca808 | 2014-04-29 11:02:42 -0700 | [diff] [blame] | 851 | // noop |
Feng Qian | b308137 | 2009-06-29 15:55:18 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /** |
Primiano Tucci | 2442675 | 2013-09-05 12:01:51 +0100 | [diff] [blame] | 855 | * Used only by internal tests to free up memory. |
| 856 | * |
| 857 | * @hide |
| 858 | */ |
| 859 | public static void freeMemoryForTests() { |
Primiano Tucci | 2442675 | 2013-09-05 12:01:51 +0100 | [diff] [blame] | 860 | getFactory().getStatics().freeMemoryForTests(); |
| 861 | } |
| 862 | |
| 863 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 864 | * Informs WebView of the network state. This is used to set |
Steve Block | 81f19ff | 2010-11-01 13:23:24 +0000 | [diff] [blame] | 865 | * the JavaScript property window.navigator.isOnline and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | * generates the online/offline event as specified in HTML5, sec. 5.7.7 |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 867 | * |
| 868 | * @param networkUp a boolean indicating if network is available |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | */ |
| 870 | public void setNetworkAvailable(boolean networkUp) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 871 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 872 | mProvider.setNetworkAvailable(networkUp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 876 | * Saves the state of this WebView used in |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | * {@link android.app.Activity#onSaveInstanceState}. Please note that this |
| 878 | * method no longer stores the display data for this WebView. The previous |
| 879 | * behavior could potentially leak files if {@link #restoreState} was never |
Kristian Monsen | f491258 | 2012-08-16 15:26:24 -0400 | [diff] [blame] | 880 | * called. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 881 | * |
| 882 | * @param outState the Bundle to store this WebView's state |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 883 | * @return the same copy of the back/forward list used to save the state, {@code null} if the |
| 884 | * method fails. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 885 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 886 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 887 | public WebBackForwardList saveState(Bundle outState) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 888 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 889 | return mProvider.saveState(outState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 893 | * Saves the current display data to the Bundle given. Used in conjunction |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 894 | * with {@link #saveState}. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 895 | * @param b a Bundle to store the display data |
| 896 | * @param dest the file to store the serialized picture data. Will be |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | * overwritten with this WebView's picture data. |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 898 | * @return {@code true} if the picture was successfully saved |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 899 | * @deprecated This method is now obsolete. |
Kristian Monsen | f491258 | 2012-08-16 15:26:24 -0400 | [diff] [blame] | 900 | * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | */ |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 902 | @Deprecated |
Patrick Scott | da9a22b | 2010-04-08 08:32:52 -0400 | [diff] [blame] | 903 | public boolean savePicture(Bundle b, final File dest) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 904 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 905 | return mProvider.savePicture(b, dest); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 909 | * Restores the display data that was saved in {@link #savePicture}. Used in |
| 910 | * conjunction with {@link #restoreState}. Note that this will not work if |
| 911 | * this WebView is hardware accelerated. |
John Reck | 2df8f42 | 2011-09-22 19:50:41 -0700 | [diff] [blame] | 912 | * |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 913 | * @param b a Bundle containing the saved display data |
| 914 | * @param src the file where the picture data was stored |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 915 | * @return {@code true} if the picture was successfully restored |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 916 | * @deprecated This method is now obsolete. |
Kristian Monsen | f491258 | 2012-08-16 15:26:24 -0400 | [diff] [blame] | 917 | * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | */ |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 919 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 920 | public boolean restorePicture(Bundle b, File src) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 921 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 922 | return mProvider.restorePicture(b, src); |
John Reck | 95b7d6f | 2011-06-03 15:23:43 -0700 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | /** |
Steve Block | 4249906 | 2012-07-17 15:34:46 +0100 | [diff] [blame] | 926 | * Restores the state of this WebView from the given Bundle. This method is |
| 927 | * intended for use in {@link android.app.Activity#onRestoreInstanceState} |
| 928 | * and should be called to restore the state of this WebView. If |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 929 | * it is called after this WebView has had a chance to build state (load |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 930 | * pages, create a back/forward list, etc.) there may be undesirable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 931 | * side-effects. Please note that this method no longer restores the |
Kristian Monsen | f491258 | 2012-08-16 15:26:24 -0400 | [diff] [blame] | 932 | * display data for this WebView. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 933 | * |
| 934 | * @param inState the incoming Bundle of state |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 935 | * @return the restored back/forward list or {@code null} if restoreState failed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 937 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | public WebBackForwardList restoreState(Bundle inState) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 939 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 940 | return mProvider.restoreState(inState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 944 | * Loads the given URL with the specified additional HTTP headers. |
Bo Liu | e29d313 | 2016-04-22 16:23:22 -0700 | [diff] [blame] | 945 | * <p> |
| 946 | * Also see compatibility note on {@link #evaluateJavascript}. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 947 | * |
| 948 | * @param url the URL of the resource to load |
| 949 | * @param additionalHttpHeaders the additional headers to be used in the |
Steve Block | f71dea0 | 2011-08-01 12:29:14 +0100 | [diff] [blame] | 950 | * HTTP request for this URL, specified as a map from name to |
| 951 | * value. Note that if this map contains any of the headers |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 952 | * that are set by default by this WebView, such as those |
Steve Block | f71dea0 | 2011-08-01 12:29:14 +0100 | [diff] [blame] | 953 | * controlling caching, accept types or the User-Agent, their |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 954 | * values may be overridden by this WebView's defaults. |
Grace Kloba | d0d9bc2 | 2010-01-26 18:08:28 -0800 | [diff] [blame] | 955 | */ |
Steve Block | f71dea0 | 2011-08-01 12:29:14 +0100 | [diff] [blame] | 956 | public void loadUrl(String url, Map<String, String> additionalHttpHeaders) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 957 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 958 | mProvider.loadUrl(url, additionalHttpHeaders); |
Grace Kloba | d0d9bc2 | 2010-01-26 18:08:28 -0800 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 962 | * Loads the given URL. |
Bo Liu | e29d313 | 2016-04-22 16:23:22 -0700 | [diff] [blame] | 963 | * <p> |
| 964 | * Also see compatibility note on {@link #evaluateJavascript}. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 965 | * |
| 966 | * @param url the URL of the resource to load |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 967 | */ |
| 968 | public void loadUrl(String url) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 969 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 970 | mProvider.loadUrl(url); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 974 | * Loads the URL with postData using "POST" method into this WebView. If url |
Hui Shu | c60ad9c | 2014-02-05 09:30:36 -0800 | [diff] [blame] | 975 | * is not a network URL, it will be loaded with {@link #loadUrl(String)} |
| 976 | * instead, ignoring the postData param. |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 977 | * |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 978 | * @param url the URL of the resource to load |
Ben Murdoch | 851004a | 2013-08-16 16:43:22 +0100 | [diff] [blame] | 979 | * @param postData the data will be passed to "POST" request, which must be |
| 980 | * be "application/x-www-form-urlencoded" encoded. |
Grace Kloba | 5753430 | 2009-05-22 18:55:02 -0700 | [diff] [blame] | 981 | */ |
| 982 | public void postUrl(String url, byte[] postData) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 983 | checkThread(); |
Hui Shu | c60ad9c | 2014-02-05 09:30:36 -0800 | [diff] [blame] | 984 | if (URLUtil.isNetworkUrl(url)) { |
| 985 | mProvider.postUrl(url, postData); |
| 986 | } else { |
| 987 | mProvider.loadUrl(url); |
| 988 | } |
Grace Kloba | 5753430 | 2009-05-22 18:55:02 -0700 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 992 | * Loads the given data into this WebView using a 'data' scheme URL. |
Steve Block | b28b22a | 2011-07-04 13:01:25 +0100 | [diff] [blame] | 993 | * <p> |
| 994 | * Note that JavaScript's same origin policy means that script running in a |
| 995 | * page loaded using this method will be unable to access content loaded |
| 996 | * using any scheme other than 'data', including 'http(s)'. To avoid this |
| 997 | * restriction, use {@link |
| 998 | * #loadDataWithBaseURL(String,String,String,String,String) |
| 999 | * loadDataWithBaseURL()} with an appropriate base URL. |
Steve Block | f95d490 | 2011-06-09 11:53:26 +0100 | [diff] [blame] | 1000 | * <p> |
Nate Fischer | 867d109 | 2018-01-04 21:57:33 -0800 | [diff] [blame] | 1001 | * The {@code encoding} parameter specifies whether the data is base64 or URL |
Steve Block | 27f3e32 | 2012-07-23 10:45:59 +0100 | [diff] [blame] | 1002 | * encoded. If the data is base64 encoded, the value of the encoding |
Nate Fischer | 867d109 | 2018-01-04 21:57:33 -0800 | [diff] [blame] | 1003 | * parameter must be 'base64'. HTML can be encoded with {@link |
| 1004 | * android.util.Base64#encodeToString(byte[],int)} like so: |
| 1005 | * <pre> |
| 1006 | * String unencodedHtml = |
| 1007 | * "<html><body>'%28' is the code for '('</body></html>"; |
| 1008 | * String encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(), Base64.NO_PADDING); |
| 1009 | * webView.loadData(encodedHtml, "text/html", "base64"); |
| 1010 | * </pre> |
| 1011 | * <p> |
| 1012 | * For all other values of {@code encoding} (including {@code null}) it is assumed that the |
| 1013 | * data uses ASCII encoding for octets inside the range of safe URL characters and use the |
| 1014 | * standard %xx hex encoding of URLs for octets outside that range. See <a |
| 1015 | * href="https://tools.ietf.org/html/rfc3986#section-2.2">RFC 3986</a> for more information. |
Steve Block | b19c787 | 2011-10-10 14:09:44 +0100 | [diff] [blame] | 1016 | * <p> |
James Wallace-Lee | d38c2e3 | 2017-12-11 16:11:15 -0800 | [diff] [blame] | 1017 | * The {@code mimeType} parameter specifies the format of the data. |
| 1018 | * If WebView can't handle the specified MIME type, it will download the data. |
| 1019 | * If {@code null}, defaults to 'text/html'. |
| 1020 | * <p> |
Steve Block | b19c787 | 2011-10-10 14:09:44 +0100 | [diff] [blame] | 1021 | * The 'data' scheme URL formed by this method uses the default US-ASCII |
| 1022 | * charset. If you need need to set a different charset, you should form a |
Steve Block | 33f962b | 2011-10-11 14:49:47 +0100 | [diff] [blame] | 1023 | * 'data' scheme URL which explicitly specifies a charset parameter in the |
| 1024 | * mediatype portion of the URL and call {@link #loadUrl(String)} instead. |
| 1025 | * Note that the charset obtained from the mediatype portion of a data URL |
| 1026 | * always overrides that specified in the HTML or XML document itself. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1027 | * |
| 1028 | * @param data a String of data in the given encoding |
James Wallace-Lee | d38c2e3 | 2017-12-11 16:11:15 -0800 | [diff] [blame] | 1029 | * @param mimeType the MIME type of the data, e.g. 'text/html'. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1030 | * @param encoding the encoding of the data |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1031 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 1032 | public void loadData(String data, @Nullable String mimeType, @Nullable String encoding) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1033 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1034 | mProvider.loadData(data, mimeType, encoding); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1038 | * Loads the given data into this WebView, using baseUrl as the base URL for |
Steve Block | b28b22a | 2011-07-04 13:01:25 +0100 | [diff] [blame] | 1039 | * the content. The base URL is used both to resolve relative URLs and when |
| 1040 | * applying JavaScript's same origin policy. The historyUrl is used for the |
| 1041 | * history entry. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1042 | * <p> |
James Wallace-Lee | d38c2e3 | 2017-12-11 16:11:15 -0800 | [diff] [blame] | 1043 | * The {@code mimeType} parameter specifies the format of the data. |
| 1044 | * If WebView can't handle the specified MIME type, it will download the data. |
| 1045 | * If {@code null}, defaults to 'text/html'. |
| 1046 | * <p> |
Steve Block | ae32857 | 2011-06-08 18:58:51 +0100 | [diff] [blame] | 1047 | * Note that content specified in this way can access local device files |
| 1048 | * (via 'file' scheme URLs) only if baseUrl specifies a scheme other than |
| 1049 | * 'http', 'https', 'ftp', 'ftps', 'about' or 'javascript'. |
Steve Block | e482d89 | 2011-07-01 13:57:32 +0100 | [diff] [blame] | 1050 | * <p> |
| 1051 | * If the base URL uses the data scheme, this method is equivalent to |
| 1052 | * calling {@link #loadData(String,String,String) loadData()} and the |
Ben Murdoch | 597256e | 2013-07-22 16:52:12 +0100 | [diff] [blame] | 1053 | * historyUrl is ignored, and the data will be treated as part of a data: URL. |
| 1054 | * If the base URL uses any other scheme, then the data will be loaded into |
| 1055 | * the WebView as a plain string (i.e. not part of a data URL) and any URL-encoded |
| 1056 | * entities in the string will not be decoded. |
Mikhail Naganov | b29e0f6 | 2015-09-16 13:05:53 -0700 | [diff] [blame] | 1057 | * <p> |
| 1058 | * Note that the baseUrl is sent in the 'Referer' HTTP header when |
| 1059 | * requesting subresources (images, etc.) of the page loaded using this method. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1060 | * |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 1061 | * @param baseUrl the URL to use as the page's base URL. If {@code null} defaults to |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1062 | * 'about:blank'. |
| 1063 | * @param data a String of data in the given encoding |
James Wallace-Lee | d38c2e3 | 2017-12-11 16:11:15 -0800 | [diff] [blame] | 1064 | * @param mimeType the MIME type of the data, e.g. 'text/html'. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1065 | * @param encoding the encoding of the data |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 1066 | * @param historyUrl the URL to use as the history entry. If {@code null} defaults |
Ben Murdoch | 95afb3b | 2013-02-25 19:18:19 +0000 | [diff] [blame] | 1067 | * to 'about:blank'. If non-null, this must be a valid URL. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 1069 | public void loadDataWithBaseURL(@Nullable String baseUrl, String data, |
| 1070 | @Nullable String mimeType, @Nullable String encoding, @Nullable String historyUrl) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1071 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1072 | mProvider.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | /** |
Ben Murdoch | a5cdd51 | 2013-07-17 16:25:07 +0100 | [diff] [blame] | 1076 | * Asynchronously evaluates JavaScript in the context of the currently displayed page. |
| 1077 | * If non-null, |resultCallback| will be invoked with any result returned from that |
| 1078 | * execution. This method must be called on the UI thread and the callback will |
| 1079 | * be made on the UI thread. |
Bo Liu | e29d313 | 2016-04-22 16:23:22 -0700 | [diff] [blame] | 1080 | * <p> |
| 1081 | * Compatibility note. Applications targeting {@link android.os.Build.VERSION_CODES#N} or |
| 1082 | * later, JavaScript state from an empty WebView is no longer persisted across navigations like |
| 1083 | * {@link #loadUrl(String)}. For example, global variables and functions defined before calling |
| 1084 | * {@link #loadUrl(String)} will not exist in the loaded page. Applications should use |
| 1085 | * {@link #addJavascriptInterface} instead to persist JavaScript objects across navigations. |
Ben Murdoch | a5cdd51 | 2013-07-17 16:25:07 +0100 | [diff] [blame] | 1086 | * |
| 1087 | * @param script the JavaScript to execute. |
| 1088 | * @param resultCallback A callback to be invoked when the script execution |
| 1089 | * completes with the result of the execution (if any). |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 1090 | * May be {@code null} if no notification of the result is required. |
Ben Murdoch | a5cdd51 | 2013-07-17 16:25:07 +0100 | [diff] [blame] | 1091 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 1092 | public void evaluateJavascript(String script, @Nullable ValueCallback<String> resultCallback) { |
Ben Murdoch | a5cdd51 | 2013-07-17 16:25:07 +0100 | [diff] [blame] | 1093 | checkThread(); |
| 1094 | mProvider.evaluateJavaScript(script, resultCallback); |
| 1095 | } |
| 1096 | |
| 1097 | /** |
Elliott Slaughter | b48fdbe | 2010-06-30 11:39:52 -0700 | [diff] [blame] | 1098 | * Saves the current view as a web archive. |
| 1099 | * |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1100 | * @param filename the filename where the archive should be placed |
Elliott Slaughter | b48fdbe | 2010-06-30 11:39:52 -0700 | [diff] [blame] | 1101 | */ |
| 1102 | public void saveWebArchive(String filename) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1103 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1104 | mProvider.saveWebArchive(filename); |
Elliott Slaughter | b48fdbe | 2010-06-30 11:39:52 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | /** |
| 1108 | * Saves the current view as a web archive. |
| 1109 | * |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1110 | * @param basename the filename where the archive should be placed |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 1111 | * @param autoname if {@code false}, takes basename to be a file. If {@code true}, basename |
Elliott Slaughter | b48fdbe | 2010-06-30 11:39:52 -0700 | [diff] [blame] | 1112 | * is assumed to be a directory in which a filename will be |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1113 | * chosen according to the URL of the current page. |
| 1114 | * @param callback called after the web archive has been saved. The |
Elliott Slaughter | b48fdbe | 2010-06-30 11:39:52 -0700 | [diff] [blame] | 1115 | * parameter for onReceiveValue will either be the filename |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 1116 | * under which the file was saved, or {@code null} if saving the |
Elliott Slaughter | b48fdbe | 2010-06-30 11:39:52 -0700 | [diff] [blame] | 1117 | * file failed. |
| 1118 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 1119 | public void saveWebArchive(String basename, boolean autoname, @Nullable ValueCallback<String> |
| 1120 | callback) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1121 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1122 | mProvider.saveWebArchive(basename, autoname, callback); |
Elliott Slaughter | b48fdbe | 2010-06-30 11:39:52 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1126 | * Stops the current load. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1127 | */ |
| 1128 | public void stopLoading() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1129 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1130 | mProvider.stopLoading(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1134 | * Reloads the current URL. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1135 | */ |
| 1136 | public void reload() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1137 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1138 | mProvider.reload(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1142 | * Gets whether this WebView has a back history item. |
| 1143 | * |
Nate Fischer | 2be201e | 2017-10-26 10:43:00 -0700 | [diff] [blame] | 1144 | * @return {@code true} if this WebView has a back history item |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1145 | */ |
| 1146 | public boolean canGoBack() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1147 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1148 | return mProvider.canGoBack(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1152 | * Goes back in the history of this WebView. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1153 | */ |
| 1154 | public void goBack() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1155 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1156 | mProvider.goBack(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1160 | * Gets whether this WebView has a forward history item. |
| 1161 | * |
Nate Fischer | 2be201e | 2017-10-26 10:43:00 -0700 | [diff] [blame] | 1162 | * @return {@code true} if this WebView has a forward history item |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 | */ |
| 1164 | public boolean canGoForward() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1165 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1166 | return mProvider.canGoForward(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1170 | * Goes forward in the history of this WebView. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1171 | */ |
| 1172 | public void goForward() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1173 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1174 | mProvider.goForward(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1178 | * Gets whether the page can go back or forward the given |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1179 | * number of steps. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1180 | * |
| 1181 | * @param steps the negative or positive number of steps to move the |
| 1182 | * history |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1183 | */ |
| 1184 | public boolean canGoBackOrForward(int steps) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1185 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1186 | return mProvider.canGoBackOrForward(steps); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1190 | * Goes to the history item that is the number of steps away from |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1191 | * the current item. Steps is negative if backward and positive |
| 1192 | * if forward. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1193 | * |
| 1194 | * @param steps the number of steps to take back or forward in the back |
| 1195 | * forward list |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1196 | */ |
| 1197 | public void goBackOrForward(int steps) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1198 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1199 | mProvider.goBackOrForward(steps); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | } |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 1201 | |
Elliott Slaughter | f21d2e3 | 2010-07-14 18:08:54 -0700 | [diff] [blame] | 1202 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1203 | * Gets whether private browsing is enabled in this WebView. |
Elliott Slaughter | f21d2e3 | 2010-07-14 18:08:54 -0700 | [diff] [blame] | 1204 | */ |
Elliott Slaughter | 7c2d135 | 2010-08-20 15:57:18 -0700 | [diff] [blame] | 1205 | public boolean isPrivateBrowsingEnabled() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1206 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1207 | return mProvider.isPrivateBrowsingEnabled(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 | } |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 1209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1210 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1211 | * Scrolls the contents of this WebView up by half the view size. |
| 1212 | * |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 1213 | * @param top {@code true} to jump to the top of the page |
| 1214 | * @return {@code true} if the page was scrolled |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1215 | */ |
| 1216 | public boolean pageUp(boolean top) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1217 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1218 | return mProvider.pageUp(top); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1219 | } |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 1220 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1221 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1222 | * Scrolls the contents of this WebView down by half the page size. |
| 1223 | * |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 1224 | * @param bottom {@code true} to jump to bottom of page |
| 1225 | * @return {@code true} if the page was scrolled |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1226 | */ |
| 1227 | public boolean pageDown(boolean bottom) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1228 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1229 | return mProvider.pageDown(bottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /** |
Tobias Sargeant | b365604 | 2015-05-13 11:23:57 +0100 | [diff] [blame] | 1233 | * Posts a {@link VisualStateCallback}, which will be called when |
| 1234 | * the current state of the WebView is ready to be drawn. |
Tobias Sargeant | a8352f4 | 2015-03-03 18:07:40 +0000 | [diff] [blame] | 1235 | * |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 1236 | * <p>Because updates to the DOM are processed asynchronously, updates to the DOM may not |
Tobias Sargeant | b365604 | 2015-05-13 11:23:57 +0100 | [diff] [blame] | 1237 | * immediately be reflected visually by subsequent {@link WebView#onDraw} invocations. The |
Tobias Sargeant | a8352f4 | 2015-03-03 18:07:40 +0000 | [diff] [blame] | 1238 | * {@link VisualStateCallback} provides a mechanism to notify the caller when the contents of |
Tobias Sargeant | b365604 | 2015-05-13 11:23:57 +0100 | [diff] [blame] | 1239 | * the DOM at the current time are ready to be drawn the next time the {@link WebView} |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 1240 | * draws. |
Tobias Sargeant | b365604 | 2015-05-13 11:23:57 +0100 | [diff] [blame] | 1241 | * |
| 1242 | * <p>The next draw after the callback completes is guaranteed to reflect all the updates to the |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 1243 | * DOM up to the point at which the {@link VisualStateCallback} was posted, but it may also |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 1244 | * contain updates applied after the callback was posted. |
Tobias Sargeant | a8352f4 | 2015-03-03 18:07:40 +0000 | [diff] [blame] | 1245 | * |
| 1246 | * <p>The state of the DOM covered by this API includes the following: |
| 1247 | * <ul> |
| 1248 | * <li>primitive HTML elements (div, img, span, etc..)</li> |
| 1249 | * <li>images</li> |
| 1250 | * <li>CSS animations</li> |
| 1251 | * <li>WebGL</li> |
| 1252 | * <li>canvas</li> |
| 1253 | * </ul> |
| 1254 | * It does not include the state of: |
| 1255 | * <ul> |
| 1256 | * <li>the video tag</li> |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 1257 | * </ul> |
Tobias Sargeant | a8352f4 | 2015-03-03 18:07:40 +0000 | [diff] [blame] | 1258 | * |
| 1259 | * <p>To guarantee that the {@link WebView} will successfully render the first frame |
| 1260 | * after the {@link VisualStateCallback#onComplete} method has been called a set of conditions |
| 1261 | * must be met: |
| 1262 | * <ul> |
| 1263 | * <li>If the {@link WebView}'s visibility is set to {@link View#VISIBLE VISIBLE} then |
| 1264 | * the {@link WebView} must be attached to the view hierarchy.</li> |
| 1265 | * <li>If the {@link WebView}'s visibility is set to {@link View#INVISIBLE INVISIBLE} |
| 1266 | * then the {@link WebView} must be attached to the view hierarchy and must be made |
| 1267 | * {@link View#VISIBLE VISIBLE} from the {@link VisualStateCallback#onComplete} method.</li> |
| 1268 | * <li>If the {@link WebView}'s visibility is set to {@link View#GONE GONE} then the |
| 1269 | * {@link WebView} must be attached to the view hierarchy and its |
| 1270 | * {@link AbsoluteLayout.LayoutParams LayoutParams}'s width and height need to be set to fixed |
| 1271 | * values and must be made {@link View#VISIBLE VISIBLE} from the |
| 1272 | * {@link VisualStateCallback#onComplete} method.</li> |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 1273 | * </ul> |
Tobias Sargeant | a8352f4 | 2015-03-03 18:07:40 +0000 | [diff] [blame] | 1274 | * |
Tobias Sargeant | b365604 | 2015-05-13 11:23:57 +0100 | [diff] [blame] | 1275 | * <p>When using this API it is also recommended to enable pre-rasterization if the {@link |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 1276 | * WebView} is off screen to avoid flickering. See {@link WebSettings#setOffscreenPreRaster} for |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 1277 | * more details and do consider its caveats. |
Tobias Sargeant | a8352f4 | 2015-03-03 18:07:40 +0000 | [diff] [blame] | 1278 | * |
Tobias Sargeant | b365604 | 2015-05-13 11:23:57 +0100 | [diff] [blame] | 1279 | * @param requestId An id that will be returned in the callback to allow callers to match |
| 1280 | * requests with callbacks. |
| 1281 | * @param callback The callback to be invoked. |
Tobias Sargeant | a8352f4 | 2015-03-03 18:07:40 +0000 | [diff] [blame] | 1282 | */ |
Tobias Sargeant | b365604 | 2015-05-13 11:23:57 +0100 | [diff] [blame] | 1283 | public void postVisualStateCallback(long requestId, VisualStateCallback callback) { |
Tobias Sargeant | a8352f4 | 2015-03-03 18:07:40 +0000 | [diff] [blame] | 1284 | checkThread(); |
Tobias Sargeant | a8352f4 | 2015-03-03 18:07:40 +0000 | [diff] [blame] | 1285 | mProvider.insertVisualStateCallback(requestId, callback); |
| 1286 | } |
| 1287 | |
| 1288 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1289 | * Clears this WebView so that onDraw() will draw nothing but white background, |
| 1290 | * and onMeasure() will return 0 if MeasureSpec is not MeasureSpec.EXACTLY. |
Jonathan Dixon | 835b1fc | 2013-02-25 12:29:33 -0800 | [diff] [blame] | 1291 | * @deprecated Use WebView.loadUrl("about:blank") to reliably reset the view state |
| 1292 | * and release page resources (including any running JavaScript). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1293 | */ |
Jonathan Dixon | 835b1fc | 2013-02-25 12:29:33 -0800 | [diff] [blame] | 1294 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1295 | public void clearView() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1296 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1297 | mProvider.clearView(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1298 | } |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 1299 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1300 | /** |
Steve Block | 72498ed | 2012-07-17 15:57:25 +0100 | [diff] [blame] | 1301 | * Gets a new picture that captures the current contents of this WebView. |
| 1302 | * The picture is of the entire document being displayed, and is not |
| 1303 | * limited to the area currently displayed by this WebView. Also, the |
| 1304 | * picture is a static copy and is unaffected by later changes to the |
| 1305 | * content being displayed. |
| 1306 | * <p> |
| 1307 | * Note that due to internal changes, for API levels between |
| 1308 | * {@link android.os.Build.VERSION_CODES#HONEYCOMB} and |
| 1309 | * {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH} inclusive, the |
| 1310 | * picture does not include fixed position elements or scrollable divs. |
Jonathan Dixon | 5545d08 | 2013-08-31 22:43:11 -0700 | [diff] [blame] | 1311 | * <p> |
| 1312 | * Note that from {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} the returned picture |
| 1313 | * should only be drawn into bitmap-backed Canvas - using any other type of Canvas will involve |
| 1314 | * additional conversion at a cost in memory and performance. Also the |
| 1315 | * {@link android.graphics.Picture#createFromStream} and |
| 1316 | * {@link android.graphics.Picture#writeToStream} methods are not supported on the |
| 1317 | * returned object. |
| 1318 | * |
| 1319 | * @deprecated Use {@link #onDraw} to obtain a bitmap snapshot of the WebView, or |
Jonathan Dixon | df3859e | 2013-09-18 12:51:47 -0700 | [diff] [blame] | 1320 | * {@link #saveWebArchive} to save the content to a file. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1321 | * |
Steve Block | 72498ed | 2012-07-17 15:57:25 +0100 | [diff] [blame] | 1322 | * @return a picture that captures the current contents of this WebView |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1323 | */ |
Jonathan Dixon | 5545d08 | 2013-08-31 22:43:11 -0700 | [diff] [blame] | 1324 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1325 | public Picture capturePicture() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1326 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1327 | return mProvider.capturePicture(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1328 | } |
| 1329 | |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 1330 | /** |
Selim Gurun | 23a1af6 | 2014-03-14 16:52:45 -0700 | [diff] [blame] | 1331 | * @deprecated Use {@link #createPrintDocumentAdapter(String)} which requires user |
| 1332 | * to provide a print document name. |
| 1333 | */ |
| 1334 | @Deprecated |
| 1335 | public PrintDocumentAdapter createPrintDocumentAdapter() { |
| 1336 | checkThread(); |
Selim Gurun | 23a1af6 | 2014-03-14 16:52:45 -0700 | [diff] [blame] | 1337 | return mProvider.createPrintDocumentAdapter("default"); |
| 1338 | } |
| 1339 | |
| 1340 | /** |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 1341 | * Creates a PrintDocumentAdapter that provides the content of this WebView for printing. |
Selim Gurun | 2167c0b | 2013-08-05 15:23:09 -0700 | [diff] [blame] | 1342 | * |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 1343 | * The adapter works by converting the WebView contents to a PDF stream. The WebView cannot |
Selim Gurun | da7d642 | 2013-09-06 14:39:05 -0700 | [diff] [blame] | 1344 | * be drawn during the conversion process - any such draws are undefined. It is recommended |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 1345 | * to use a dedicated off screen WebView for the printing. If necessary, an application may |
Selim Gurun | da7d642 | 2013-09-06 14:39:05 -0700 | [diff] [blame] | 1346 | * temporarily hide a visible WebView by using a custom PrintDocumentAdapter instance |
| 1347 | * wrapped around the object returned and observing the onStart and onFinish methods. See |
| 1348 | * {@link android.print.PrintDocumentAdapter} for more information. |
Selim Gurun | 23a1af6 | 2014-03-14 16:52:45 -0700 | [diff] [blame] | 1349 | * |
| 1350 | * @param documentName The user-facing name of the printed document. See |
| 1351 | * {@link android.print.PrintDocumentInfo} |
Selim Gurun | 2167c0b | 2013-08-05 15:23:09 -0700 | [diff] [blame] | 1352 | */ |
Selim Gurun | 23a1af6 | 2014-03-14 16:52:45 -0700 | [diff] [blame] | 1353 | public PrintDocumentAdapter createPrintDocumentAdapter(String documentName) { |
Selim Gurun | 2167c0b | 2013-08-05 15:23:09 -0700 | [diff] [blame] | 1354 | checkThread(); |
Selim Gurun | 23a1af6 | 2014-03-14 16:52:45 -0700 | [diff] [blame] | 1355 | return mProvider.createPrintDocumentAdapter(documentName); |
Selim Gurun | 2167c0b | 2013-08-05 15:23:09 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1359 | * Gets the current scale of this WebView. |
| 1360 | * |
| 1361 | * @return the current scale |
Kristian Monsen | 5cc2351 | 2012-08-09 15:33:08 -0400 | [diff] [blame] | 1362 | * |
| 1363 | * @deprecated This method is prone to inaccuracy due to race conditions |
| 1364 | * between the web rendering and UI threads; prefer |
| 1365 | * {@link WebViewClient#onScaleChanged}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1366 | */ |
Kristian Monsen | 5cc2351 | 2012-08-09 15:33:08 -0400 | [diff] [blame] | 1367 | @Deprecated |
John Reck | 926cf56 | 2012-06-14 10:00:31 -0700 | [diff] [blame] | 1368 | @ViewDebug.ExportedProperty(category = "webview") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1369 | public float getScale() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1370 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1371 | return mProvider.getScale(); |
Mangesh Ghiware | b5f9fc3 | 2011-08-31 17:49:07 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | /** |
Mikhail Naganov | 0030336 | 2013-09-02 10:57:04 +0100 | [diff] [blame] | 1375 | * Sets the initial scale for this WebView. 0 means default. |
| 1376 | * The behavior for the default scale depends on the state of |
| 1377 | * {@link WebSettings#getUseWideViewPort()} and |
| 1378 | * {@link WebSettings#getLoadWithOverviewMode()}. |
| 1379 | * If the content fits into the WebView control by width, then |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 1380 | * the zoom is set to 100%. For wide content, the behavior |
Mikhail Naganov | 0030336 | 2013-09-02 10:57:04 +0100 | [diff] [blame] | 1381 | * depends on the state of {@link WebSettings#getLoadWithOverviewMode()}. |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 1382 | * If its value is {@code true}, the content will be zoomed out to be fit |
Mikhail Naganov | 0030336 | 2013-09-02 10:57:04 +0100 | [diff] [blame] | 1383 | * by width into the WebView control, otherwise not. |
| 1384 | * |
| 1385 | * If initial scale is greater than 0, WebView starts with this value |
| 1386 | * as initial scale. |
Mangesh Ghiware | e832b63 | 2011-11-16 11:46:39 -0800 | [diff] [blame] | 1387 | * Please note that unlike the scale properties in the viewport meta tag, |
| 1388 | * this method doesn't take the screen density into account. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1389 | * |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1390 | * @param scaleInPercent the initial scale in percent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1391 | */ |
| 1392 | public void setInitialScale(int scaleInPercent) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1393 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1394 | mProvider.setInitialScale(scaleInPercent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1398 | * Invokes the graphical zoom picker widget for this WebView. This will |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1399 | * result in the zoom widget appearing on the screen to control the zoom |
| 1400 | * level of this WebView. |
| 1401 | */ |
| 1402 | public void invokeZoomPicker() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1403 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1404 | mProvider.invokeZoomPicker(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1408 | * Gets a HitTestResult based on the current cursor node. If a HTML::a |
| 1409 | * tag is found and the anchor has a non-JavaScript URL, the HitTestResult |
| 1410 | * type is set to SRC_ANCHOR_TYPE and the URL is set in the "extra" field. |
| 1411 | * If the anchor does not have a URL or if it is a JavaScript URL, the type |
| 1412 | * will be UNKNOWN_TYPE and the URL has to be retrieved through |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1413 | * {@link #requestFocusNodeHref} asynchronously. If a HTML::img tag is |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1414 | * found, the HitTestResult type is set to IMAGE_TYPE and the URL is set in |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1415 | * the "extra" field. A type of |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1416 | * SRC_IMAGE_ANCHOR_TYPE indicates an anchor with a URL that has an image as |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1417 | * a child node. If a phone number is found, the HitTestResult type is set |
| 1418 | * to PHONE_TYPE and the phone number is set in the "extra" field of |
| 1419 | * HitTestResult. If a map address is found, the HitTestResult type is set |
| 1420 | * to GEO_TYPE and the address is set in the "extra" field of HitTestResult. |
| 1421 | * If an email address is found, the HitTestResult type is set to EMAIL_TYPE |
| 1422 | * and the email is set in the "extra" field of HitTestResult. Otherwise, |
| 1423 | * HitTestResult type is set to UNKNOWN_TYPE. |
| 1424 | */ |
| 1425 | public HitTestResult getHitTestResult() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1426 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1427 | return mProvider.getHitTestResult(); |
Cary Clark | b849134 | 2010-11-29 16:23:19 -0500 | [diff] [blame] | 1428 | } |
| 1429 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1430 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1431 | * Requests the anchor or image element URL at the last tapped point. |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 1432 | * If hrefMsg is {@code null}, this method returns immediately and does not |
Cary Clark | 861368a | 2010-12-15 11:24:37 -0500 | [diff] [blame] | 1433 | * dispatch hrefMsg to its target. If the tapped point hits an image, |
| 1434 | * an anchor, or an image in an anchor, the message associates |
| 1435 | * strings in named keys in its data. The value paired with the key |
| 1436 | * may be an empty string. |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 1437 | * |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1438 | * @param hrefMsg the message to be dispatched with the result of the |
| 1439 | * request. The message data contains three keys. "url" |
| 1440 | * returns the anchor's href attribute. "title" returns the |
| 1441 | * anchor's text. "src" returns the image's src attribute. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1442 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 1443 | public void requestFocusNodeHref(@Nullable Message hrefMsg) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1444 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1445 | mProvider.requestFocusNodeHref(hrefMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1446 | } |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 1447 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1448 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1449 | * Requests the URL of the image last touched by the user. msg will be sent |
| 1450 | * to its target with a String representing the URL as its object. |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 1451 | * |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1452 | * @param msg the message to be dispatched with the result of the request |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 1453 | * as the data member with "url" as key. The result can be {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1454 | */ |
| 1455 | public void requestImageRef(Message msg) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1456 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1457 | mProvider.requestImageRef(msg); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1460 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1461 | * Gets the URL for the current page. This is not always the same as the URL |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1462 | * passed to WebViewClient.onPageStarted because although the load for |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1463 | * that URL has begun, the current page may not have changed. |
| 1464 | * |
| 1465 | * @return the URL for the current page |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1466 | */ |
John Reck | 926cf56 | 2012-06-14 10:00:31 -0700 | [diff] [blame] | 1467 | @ViewDebug.ExportedProperty(category = "webview") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1468 | public String getUrl() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1469 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1470 | return mProvider.getUrl(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1471 | } |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 1472 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1473 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1474 | * Gets the original URL for the current page. This is not always the same |
| 1475 | * as the URL passed to WebViewClient.onPageStarted because although the |
| 1476 | * load for that URL has begun, the current page may not have changed. |
| 1477 | * Also, there may have been redirects resulting in a different URL to that |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1478 | * originally requested. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1479 | * |
| 1480 | * @return the URL that was originally requested for the current page |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1481 | */ |
John Reck | 926cf56 | 2012-06-14 10:00:31 -0700 | [diff] [blame] | 1482 | @ViewDebug.ExportedProperty(category = "webview") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1483 | public String getOriginalUrl() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1484 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1485 | return mProvider.getOriginalUrl(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1489 | * Gets the title for the current page. This is the title of the current page |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1490 | * until WebViewClient.onReceivedTitle is called. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1491 | * |
| 1492 | * @return the title for the current page |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1493 | */ |
John Reck | 926cf56 | 2012-06-14 10:00:31 -0700 | [diff] [blame] | 1494 | @ViewDebug.ExportedProperty(category = "webview") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1495 | public String getTitle() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1496 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1497 | return mProvider.getTitle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1501 | * Gets the favicon for the current page. This is the favicon of the current |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1502 | * page until WebViewClient.onReceivedIcon is called. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1503 | * |
| 1504 | * @return the favicon for the current page |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1505 | */ |
| 1506 | public Bitmap getFavicon() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1507 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1508 | return mProvider.getFavicon(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1512 | * Gets the touch icon URL for the apple-touch-icon <link> element, or |
Ben Murdoch | 372dfc8 | 2010-07-01 15:56:01 +0100 | [diff] [blame] | 1513 | * a URL on this site's server pointing to the standard location of a |
| 1514 | * touch icon. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1515 | * |
Patrick Scott | 2ba1262 | 2009-08-04 13:20:05 -0400 | [diff] [blame] | 1516 | * @hide |
| 1517 | */ |
| 1518 | public String getTouchIconUrl() { |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1519 | return mProvider.getTouchIconUrl(); |
Patrick Scott | 2ba1262 | 2009-08-04 13:20:05 -0400 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1523 | * Gets the progress for the current page. |
| 1524 | * |
| 1525 | * @return the progress for the current page between 0 and 100 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1526 | */ |
| 1527 | public int getProgress() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1528 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1529 | return mProvider.getProgress(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1530 | } |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 1531 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1532 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1533 | * Gets the height of the HTML content. |
| 1534 | * |
| 1535 | * @return the height of the HTML content |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1536 | */ |
John Reck | 926cf56 | 2012-06-14 10:00:31 -0700 | [diff] [blame] | 1537 | @ViewDebug.ExportedProperty(category = "webview") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1538 | public int getContentHeight() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1539 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1540 | return mProvider.getContentHeight(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1544 | * Gets the width of the HTML content. |
| 1545 | * |
| 1546 | * @return the width of the HTML content |
Leon Scroggins | ea96d1e | 2009-09-23 13:41:01 -0400 | [diff] [blame] | 1547 | * @hide |
| 1548 | */ |
John Reck | 926cf56 | 2012-06-14 10:00:31 -0700 | [diff] [blame] | 1549 | @ViewDebug.ExportedProperty(category = "webview") |
Leon Scroggins | ea96d1e | 2009-09-23 13:41:01 -0400 | [diff] [blame] | 1550 | public int getContentWidth() { |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1551 | return mProvider.getContentWidth(); |
John Reck | 9b90d55 | 2011-06-14 15:19:17 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1555 | * Pauses all layout, parsing, and JavaScript timers for all WebViews. This |
| 1556 | * is a global requests, not restricted to just this WebView. This can be |
Mike Reed | d205d5b | 2009-05-27 11:02:29 -0400 | [diff] [blame] | 1557 | * useful if the application has been paused. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1558 | */ |
| 1559 | public void pauseTimers() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1560 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1561 | mProvider.pauseTimers(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1565 | * Resumes all layout, parsing, and JavaScript timers for all WebViews. |
Mike Reed | d205d5b | 2009-05-27 11:02:29 -0400 | [diff] [blame] | 1566 | * This will resume dispatching all timers. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1567 | */ |
| 1568 | public void resumeTimers() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1569 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1570 | mProvider.resumeTimers(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | /** |
Selim Gurun | 922cb58 | 2015-07-31 11:49:22 -0700 | [diff] [blame] | 1574 | * Does a best-effort attempt to pause any processing that can be paused |
| 1575 | * safely, such as animations and geolocation. Note that this call |
| 1576 | * does not pause JavaScript. To pause JavaScript globally, use |
| 1577 | * {@link #pauseTimers}. |
| 1578 | * |
| 1579 | * To resume WebView, call {@link #onResume}. |
Mike Reed | d205d5b | 2009-05-27 11:02:29 -0400 | [diff] [blame] | 1580 | */ |
| 1581 | public void onPause() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1582 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1583 | mProvider.onPause(); |
Mike Reed | d205d5b | 2009-05-27 11:02:29 -0400 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | /** |
Selim Gurun | 922cb58 | 2015-07-31 11:49:22 -0700 | [diff] [blame] | 1587 | * Resumes a WebView after a previous call to {@link #onPause}. |
Mike Reed | d205d5b | 2009-05-27 11:02:29 -0400 | [diff] [blame] | 1588 | */ |
| 1589 | public void onResume() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1590 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1591 | mProvider.onResume(); |
Mike Reed | d205d5b | 2009-05-27 11:02:29 -0400 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1595 | * Gets whether this WebView is paused, meaning onPause() was called. |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 1596 | * Calling onResume() sets the paused state back to {@code false}. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1597 | * |
Mike Reed | d205d5b | 2009-05-27 11:02:29 -0400 | [diff] [blame] | 1598 | * @hide |
| 1599 | */ |
| 1600 | public boolean isPaused() { |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1601 | return mProvider.isPaused(); |
Mike Reed | d205d5b | 2009-05-27 11:02:29 -0400 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1605 | * Informs this WebView that memory is low so that it can free any available |
| 1606 | * memory. |
Jonathan Dixon | 5545d08 | 2013-08-31 22:43:11 -0700 | [diff] [blame] | 1607 | * @deprecated Memory caches are automatically dropped when no longer needed, and in response |
| 1608 | * to system memory pressure. |
Derek Sollenberger | e0155e9 | 2009-06-10 15:35:45 -0400 | [diff] [blame] | 1609 | */ |
Jonathan Dixon | 5545d08 | 2013-08-31 22:43:11 -0700 | [diff] [blame] | 1610 | @Deprecated |
Derek Sollenberger | e0155e9 | 2009-06-10 15:35:45 -0400 | [diff] [blame] | 1611 | public void freeMemory() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1612 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1613 | mProvider.freeMemory(); |
Derek Sollenberger | e0155e9 | 2009-06-10 15:35:45 -0400 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1617 | * Clears the resource cache. Note that the cache is per-application, so |
Mike Hearn | adcd2ed | 2009-01-21 16:44:36 +0100 | [diff] [blame] | 1618 | * this will clear the cache for all WebViews used. |
| 1619 | * |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 1620 | * @param includeDiskFiles if {@code false}, only the RAM cache is cleared |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1621 | */ |
| 1622 | public void clearCache(boolean includeDiskFiles) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1623 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1624 | mProvider.clearCache(includeDiskFiles); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | /** |
Steve Block | 219dfa4 | 2012-07-20 10:31:21 +0100 | [diff] [blame] | 1628 | * Removes the autocomplete popup from the currently focused form field, if |
| 1629 | * present. Note this only affects the display of the autocomplete popup, |
| 1630 | * it does not remove any saved form data from this WebView's store. To do |
| 1631 | * that, use {@link WebViewDatabase#clearFormData}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1632 | */ |
| 1633 | public void clearFormData() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1634 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1635 | mProvider.clearFormData(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1639 | * Tells this WebView to clear its internal back/forward list. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1640 | */ |
| 1641 | public void clearHistory() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1642 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1643 | mProvider.clearHistory(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1647 | * Clears the SSL preferences table stored in response to proceeding with |
| 1648 | * SSL certificate errors. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1649 | */ |
| 1650 | public void clearSslPreferences() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1651 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1652 | mProvider.clearSslPreferences(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | /** |
Selim Gurun | ee89ab4 | 2014-06-13 10:53:55 -0700 | [diff] [blame] | 1656 | * Clears the client certificate preferences stored in response |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 1657 | * to proceeding/cancelling client cert requests. Note that WebView |
Selim Gurun | b6aa97e | 2014-03-26 14:10:28 -0700 | [diff] [blame] | 1658 | * automatically clears these preferences when it receives a |
Selim Gurun | ee89ab4 | 2014-06-13 10:53:55 -0700 | [diff] [blame] | 1659 | * {@link KeyChain#ACTION_STORAGE_CHANGED} intent. The preferences are |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 1660 | * shared by all the WebViews that are created by the embedder application. |
Selim Gurun | b6aa97e | 2014-03-26 14:10:28 -0700 | [diff] [blame] | 1661 | * |
Selim Gurun | e1c6c3a | 2014-04-28 14:21:21 -0700 | [diff] [blame] | 1662 | * @param onCleared A runnable to be invoked when client certs are cleared. |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 1663 | * The runnable will be called in UI thread. |
Selim Gurun | b6aa97e | 2014-03-26 14:10:28 -0700 | [diff] [blame] | 1664 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 1665 | public static void clearClientCertPreferences(@Nullable Runnable onCleared) { |
Selim Gurun | e1c6c3a | 2014-04-28 14:21:21 -0700 | [diff] [blame] | 1666 | getFactory().getStatics().clearClientCertPreferences(onCleared); |
Selim Gurun | b6aa97e | 2014-03-26 14:10:28 -0700 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | /** |
Nate Fischer | 2b108d8 | 2017-08-21 17:59:55 -0700 | [diff] [blame] | 1670 | * Starts Safe Browsing initialization. |
Nate Fischer | 471891d | 2017-07-18 19:15:52 -0700 | [diff] [blame] | 1671 | * <p> |
Nate Fischer | 2b108d8 | 2017-08-21 17:59:55 -0700 | [diff] [blame] | 1672 | * URL loads are not guaranteed to be protected by Safe Browsing until after {@code callback} is |
| 1673 | * invoked with {@code true}. Safe Browsing is not fully supported on all devices. For those |
| 1674 | * devices {@code callback} will receive {@code false}. |
Nate Fischer | 471891d | 2017-07-18 19:15:52 -0700 | [diff] [blame] | 1675 | * <p> |
Nate Fischer | fb92ee1 | 2018-01-18 12:01:19 -0800 | [diff] [blame] | 1676 | * This should not be called if Safe Browsing has been disabled by manifest tag or {@link |
| 1677 | * WebSettings#setSafeBrowsingEnabled}. This prepares resources used for Safe Browsing. |
Nate Fischer | 471891d | 2017-07-18 19:15:52 -0700 | [diff] [blame] | 1678 | * <p> |
Nate Fischer | 2b108d8 | 2017-08-21 17:59:55 -0700 | [diff] [blame] | 1679 | * This should be called with the Application Context (and will always use the Application |
| 1680 | * context to do its work regardless). |
Nate Fischer | 471891d | 2017-07-18 19:15:52 -0700 | [diff] [blame] | 1681 | * |
Nate Fischer | 3ca196a | 2017-07-12 18:45:03 -0700 | [diff] [blame] | 1682 | * @param context Application Context. |
Nate Fischer | 2b108d8 | 2017-08-21 17:59:55 -0700 | [diff] [blame] | 1683 | * @param callback will be called on the UI thread with {@code true} if initialization is |
| 1684 | * successful, {@code false} otherwise. |
Nate Fischer | 0e72c18 | 2017-05-30 11:25:17 -0700 | [diff] [blame] | 1685 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 1686 | public static void startSafeBrowsing(Context context, |
| 1687 | @Nullable ValueCallback<Boolean> callback) { |
Nate Fischer | 0e72c18 | 2017-05-30 11:25:17 -0700 | [diff] [blame] | 1688 | getFactory().getStatics().initSafeBrowsing(context, callback); |
| 1689 | } |
| 1690 | |
| 1691 | /** |
Nate Fischer | bf158ec | 2017-06-27 22:52:20 -0700 | [diff] [blame] | 1692 | * Sets the list of domains that are exempt from SafeBrowsing checks. The list is |
| 1693 | * global for all the WebViews. |
Selim Gurun | 89e9359 | 2017-07-28 10:33:58 -0700 | [diff] [blame] | 1694 | * <p> |
| 1695 | * Each rule should take one of these: |
| 1696 | * <table> |
| 1697 | * <tr><th> Rule </th> <th> Example </th> <th> Matches Subdomain</th> </tr> |
Nate Fischer | fbebfa9 | 2017-08-29 16:17:22 -0700 | [diff] [blame] | 1698 | * <tr><td> HOSTNAME </td> <td> example.com </td> <td> Yes </td> </tr> |
| 1699 | * <tr><td> .HOSTNAME </td> <td> .example.com </td> <td> No </td> </tr> |
| 1700 | * <tr><td> IPV4_LITERAL </td> <td> 192.168.1.1 </td> <td> No </td></tr> |
| 1701 | * <tr><td> IPV6_LITERAL_WITH_BRACKETS </td><td>[10:20:30:40:50:60:70:80]</td><td>No</td></tr> |
Selim Gurun | 89e9359 | 2017-07-28 10:33:58 -0700 | [diff] [blame] | 1702 | * </table> |
| 1703 | * <p> |
| 1704 | * All other rules, including wildcards, are invalid. |
Nate Fischer | bf158ec | 2017-06-27 22:52:20 -0700 | [diff] [blame] | 1705 | * |
| 1706 | * @param urls the list of URLs |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 1707 | * @param callback will be called with {@code true} if URLs are successfully added to the |
| 1708 | * whitelist. It will be called with {@code false} if any URLs are malformed. The callback will |
| 1709 | * be run on the UI thread |
Nate Fischer | bf158ec | 2017-06-27 22:52:20 -0700 | [diff] [blame] | 1710 | */ |
Nate Fischer | c5ec745 | 2017-07-18 16:08:42 -0700 | [diff] [blame] | 1711 | public static void setSafeBrowsingWhitelist(@NonNull List<String> urls, |
Nate Fischer | d6385d3 | 2017-07-17 12:58:18 -0700 | [diff] [blame] | 1712 | @Nullable ValueCallback<Boolean> callback) { |
| 1713 | getFactory().getStatics().setSafeBrowsingWhitelist(urls, callback); |
Nate Fischer | bf158ec | 2017-06-27 22:52:20 -0700 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | /** |
Nate Fischer | 3898ac1 | 2017-08-09 23:02:36 -0700 | [diff] [blame] | 1717 | * Returns a URL pointing to the privacy policy for Safe Browsing reporting. |
| 1718 | * |
| 1719 | * @return the url pointing to a privacy policy document which can be displayed to users. |
| 1720 | */ |
| 1721 | @NonNull |
| 1722 | public static Uri getSafeBrowsingPrivacyPolicyUrl() { |
| 1723 | return getFactory().getStatics().getSafeBrowsingPrivacyPolicyUrl(); |
| 1724 | } |
| 1725 | |
| 1726 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1727 | * Gets the WebBackForwardList for this WebView. This contains the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1728 | * back/forward list for use in querying each item in the history stack. |
| 1729 | * This is a copy of the private WebBackForwardList so it contains only a |
| 1730 | * snapshot of the current state. Multiple calls to this method may return |
| 1731 | * different objects. The object returned from this method will not be |
| 1732 | * updated to reflect any new state. |
| 1733 | */ |
| 1734 | public WebBackForwardList copyBackForwardList() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1735 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1736 | return mProvider.copyBackForwardList(); |
| 1737 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1738 | } |
| 1739 | |
Victoria Lease | abeb6a7 | 2012-03-05 16:29:12 -0800 | [diff] [blame] | 1740 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1741 | * Registers the listener to be notified as find-on-page operations |
| 1742 | * progress. This will replace the current listener. |
Victoria Lease | abeb6a7 | 2012-03-05 16:29:12 -0800 | [diff] [blame] | 1743 | * |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1744 | * @param listener an implementation of {@link FindListener} |
Victoria Lease | abeb6a7 | 2012-03-05 16:29:12 -0800 | [diff] [blame] | 1745 | */ |
| 1746 | public void setFindListener(FindListener listener) { |
| 1747 | checkThread(); |
Ben Murdoch | 52c9f7f | 2013-01-18 00:50:37 +0000 | [diff] [blame] | 1748 | setupFindListenerIfNeeded(); |
| 1749 | mFindListener.mUserFindListener = listener; |
Victoria Lease | abeb6a7 | 2012-03-05 16:29:12 -0800 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | /** |
Kristian Monsen | f491258 | 2012-08-16 15:26:24 -0400 | [diff] [blame] | 1753 | * Highlights and scrolls to the next match found by |
Victoria Lease | 0b8413b | 2012-03-26 13:04:10 -0700 | [diff] [blame] | 1754 | * {@link #findAllAsync}, wrapping around page boundaries as necessary. |
Kristian Monsen | f491258 | 2012-08-16 15:26:24 -0400 | [diff] [blame] | 1755 | * Notifies any registered {@link FindListener}. If {@link #findAllAsync(String)} |
| 1756 | * has not been called yet, or if {@link #clearMatches} has been called since the |
| 1757 | * last find operation, this function does nothing. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1758 | * |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1759 | * @param forward the direction to search |
Victoria Lease | 0b8413b | 2012-03-26 13:04:10 -0700 | [diff] [blame] | 1760 | * @see #setFindListener |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1761 | */ |
| 1762 | public void findNext(boolean forward) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1763 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1764 | mProvider.findNext(forward); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1765 | } |
| 1766 | |
Victoria Lease | abeb6a7 | 2012-03-05 16:29:12 -0800 | [diff] [blame] | 1767 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1768 | * Finds all instances of find on the page and highlights them. |
Victoria Lease | 0b8413b | 2012-03-26 13:04:10 -0700 | [diff] [blame] | 1769 | * Notifies any registered {@link FindListener}. |
Victoria Lease | abeb6a7 | 2012-03-05 16:29:12 -0800 | [diff] [blame] | 1770 | * |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1771 | * @param find the string to find |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 1772 | * @return the number of occurrences of the String "find" that were found |
Victoria Lease | 0b8413b | 2012-03-26 13:04:10 -0700 | [diff] [blame] | 1773 | * @deprecated {@link #findAllAsync} is preferred. |
| 1774 | * @see #setFindListener |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1775 | */ |
Jonathan Dixon | 9f21c1c | 2012-04-12 11:14:20 +0100 | [diff] [blame] | 1776 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1777 | public int findAll(String find) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1778 | checkThread(); |
Jonathan Dixon | 9f21c1c | 2012-04-12 11:14:20 +0100 | [diff] [blame] | 1779 | StrictMode.noteSlowCall("findAll blocks UI: prefer findAllAsync"); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1780 | return mProvider.findAll(find); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1781 | } |
| 1782 | |
Cary Clark | 3403eb3 | 2010-03-03 10:05:16 -0500 | [diff] [blame] | 1783 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1784 | * Finds all instances of find on the page and highlights them, |
Victoria Lease | 0b8413b | 2012-03-26 13:04:10 -0700 | [diff] [blame] | 1785 | * asynchronously. Notifies any registered {@link FindListener}. |
Kristian Monsen | f491258 | 2012-08-16 15:26:24 -0400 | [diff] [blame] | 1786 | * Successive calls to this will cancel any pending searches. |
Victoria Lease | abeb6a7 | 2012-03-05 16:29:12 -0800 | [diff] [blame] | 1787 | * |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1788 | * @param find the string to find. |
Victoria Lease | 0b8413b | 2012-03-26 13:04:10 -0700 | [diff] [blame] | 1789 | * @see #setFindListener |
Victoria Lease | abeb6a7 | 2012-03-05 16:29:12 -0800 | [diff] [blame] | 1790 | */ |
| 1791 | public void findAllAsync(String find) { |
| 1792 | checkThread(); |
| 1793 | mProvider.findAllAsync(find); |
| 1794 | } |
| 1795 | |
| 1796 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1797 | * Starts an ActionMode for finding text in this WebView. Only works if this |
| 1798 | * WebView is attached to the view system. |
| 1799 | * |
| 1800 | * @param text if non-null, will be the initial text to search for. |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 1801 | * Otherwise, the last String searched for in this WebView will |
| 1802 | * be used to start. |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 1803 | * @param showIme if {@code true}, show the IME, assuming the user will begin typing. |
| 1804 | * If {@code false} and text is non-null, perform a find all. |
| 1805 | * @return {@code true} if the find dialog is shown, {@code false} otherwise |
Jonathan Dixon | 835b1fc | 2013-02-25 12:29:33 -0800 | [diff] [blame] | 1806 | * @deprecated This method does not work reliably on all Android versions; |
| 1807 | * implementing a custom find dialog using WebView.findAllAsync() |
| 1808 | * provides a more robust solution. |
Cary Clark | de023c1 | 2010-03-03 10:05:16 -0500 | [diff] [blame] | 1809 | */ |
Jonathan Dixon | 835b1fc | 2013-02-25 12:29:33 -0800 | [diff] [blame] | 1810 | @Deprecated |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 1811 | public boolean showFindDialog(@Nullable String text, boolean showIme) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1812 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1813 | return mProvider.showFindDialog(text, showIme); |
Leon Scroggins | fe026bd | 2010-08-24 14:16:09 -0400 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1817 | * Gets the first substring consisting of the address of a physical |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1818 | * location. Currently, only addresses in the United States are detected, |
| 1819 | * and consist of: |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1820 | * <ul> |
| 1821 | * <li>a house number</li> |
| 1822 | * <li>a street name</li> |
| 1823 | * <li>a street type (Road, Circle, etc), either spelled out or |
| 1824 | * abbreviated</li> |
| 1825 | * <li>a city name</li> |
| 1826 | * <li>a state or territory, either spelled out or two-letter abbr</li> |
| 1827 | * <li>an optional 5 digit or 9 digit zip code</li> |
| 1828 | * </ul> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1829 | * All names must be correctly capitalized, and the zip code, if present, |
| 1830 | * must be valid for the state. The street type must be a standard USPS |
| 1831 | * spelling or abbreviation. The state or territory must also be spelled |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 1832 | * or abbreviated using USPS standards. The house number may not exceed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1833 | * five digits. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1834 | * |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1835 | * @param addr the string to search for addresses |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 1836 | * @return the address, or if no address is found, {@code null} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1837 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 1838 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1839 | public static String findAddress(String addr) { |
Tobias Sargeant | f4d85d8 | 2018-01-05 14:34:38 +0000 | [diff] [blame] | 1840 | if (addr == null) { |
| 1841 | throw new NullPointerException("addr is null"); |
| 1842 | } |
| 1843 | return FindAddress.findAddress(addr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1844 | } |
| 1845 | |
Victoria Lease | 0b8413b | 2012-03-26 13:04:10 -0700 | [diff] [blame] | 1846 | /** |
Selim Gurun | 17d1adb | 2014-08-27 11:22:26 -0700 | [diff] [blame] | 1847 | * For apps targeting the L release, WebView has a new default behavior that reduces |
| 1848 | * memory footprint and increases performance by intelligently choosing |
| 1849 | * the portion of the HTML document that needs to be drawn. These |
| 1850 | * optimizations are transparent to the developers. However, under certain |
| 1851 | * circumstances, an App developer may want to disable them: |
Ben Murdoch | 574fa34 | 2015-01-07 14:20:45 +0000 | [diff] [blame] | 1852 | * <ol> |
| 1853 | * <li>When an app uses {@link #onDraw} to do own drawing and accesses portions |
| 1854 | * of the page that is way outside the visible portion of the page.</li> |
| 1855 | * <li>When an app uses {@link #capturePicture} to capture a very large HTML document. |
| 1856 | * Note that capturePicture is a deprecated API.</li> |
| 1857 | * </ol> |
Selim Gurun | 17d1adb | 2014-08-27 11:22:26 -0700 | [diff] [blame] | 1858 | * Enabling drawing the entire HTML document has a significant performance |
| 1859 | * cost. This method should be called before any WebViews are created. |
Bo Liu | 9f3ed85 | 2014-07-18 10:03:12 -0700 | [diff] [blame] | 1860 | */ |
| 1861 | public static void enableSlowWholeDocumentDraw() { |
| 1862 | getFactory().getStatics().enableSlowWholeDocumentDraw(); |
| 1863 | } |
| 1864 | |
| 1865 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1866 | * Clears the highlighting surrounding text matches created by |
Kristian Monsen | f491258 | 2012-08-16 15:26:24 -0400 | [diff] [blame] | 1867 | * {@link #findAllAsync}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1868 | */ |
| 1869 | public void clearMatches() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1870 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1871 | mProvider.clearMatches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1875 | * Queries the document to see if it contains any image references. The |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1876 | * message object will be dispatched with arg1 being set to 1 if images |
| 1877 | * were found and 0 if the document does not reference any images. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1878 | * |
| 1879 | * @param response the message that will be dispatched with the result |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1880 | */ |
| 1881 | public void documentHasImages(Message response) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1882 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1883 | mProvider.documentHasImages(response); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1887 | * Sets the WebViewClient that will receive various notifications and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1888 | * requests. This will replace the current handler. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1889 | * |
| 1890 | * @param client an implementation of WebViewClient |
Nate Fischer | 6e58584 | 2016-09-29 18:30:58 -0700 | [diff] [blame] | 1891 | * @see #getWebViewClient |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1892 | */ |
| 1893 | public void setWebViewClient(WebViewClient client) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1894 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1895 | mProvider.setWebViewClient(client); |
Grace Kloba | 94ab3b6 | 2009-10-07 18:00:19 -0700 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | /** |
Nate Fischer | 6e58584 | 2016-09-29 18:30:58 -0700 | [diff] [blame] | 1899 | * Gets the WebViewClient. |
| 1900 | * |
| 1901 | * @return the WebViewClient, or a default client if not yet set |
| 1902 | * @see #setWebViewClient |
| 1903 | */ |
| 1904 | public WebViewClient getWebViewClient() { |
| 1905 | checkThread(); |
| 1906 | return mProvider.getWebViewClient(); |
| 1907 | } |
| 1908 | |
| 1909 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1910 | * Registers the interface to be used when content can not be handled by |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1911 | * the rendering engine, and should be downloaded instead. This will replace |
| 1912 | * the current handler. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1913 | * |
| 1914 | * @param listener an implementation of DownloadListener |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1915 | */ |
| 1916 | public void setDownloadListener(DownloadListener listener) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1917 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1918 | mProvider.setDownloadListener(listener); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1922 | * Sets the chrome handler. This is an implementation of WebChromeClient for |
Steve Block | 81f19ff | 2010-11-01 13:23:24 +0000 | [diff] [blame] | 1923 | * use in handling JavaScript dialogs, favicons, titles, and the progress. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1924 | * This will replace the current handler. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1925 | * |
| 1926 | * @param client an implementation of WebChromeClient |
Nate Fischer | 6e58584 | 2016-09-29 18:30:58 -0700 | [diff] [blame] | 1927 | * @see #getWebChromeClient |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1928 | */ |
| 1929 | public void setWebChromeClient(WebChromeClient client) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1930 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1931 | mProvider.setWebChromeClient(client); |
Patrick Scott | 0b2e84b | 2010-03-02 08:58:44 -0500 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | /** |
Nate Fischer | 6e58584 | 2016-09-29 18:30:58 -0700 | [diff] [blame] | 1935 | * Gets the chrome handler. |
| 1936 | * |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 1937 | * @return the WebChromeClient, or {@code null} if not yet set |
Nate Fischer | 6e58584 | 2016-09-29 18:30:58 -0700 | [diff] [blame] | 1938 | * @see #setWebChromeClient |
| 1939 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 1940 | @Nullable |
Nate Fischer | 6e58584 | 2016-09-29 18:30:58 -0700 | [diff] [blame] | 1941 | public WebChromeClient getWebChromeClient() { |
| 1942 | checkThread(); |
| 1943 | return mProvider.getWebChromeClient(); |
| 1944 | } |
| 1945 | |
| 1946 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1947 | * Sets the Picture listener. This is an interface used to receive |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1948 | * notifications of a new Picture. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1949 | * |
| 1950 | * @param listener an implementation of WebView.PictureListener |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 1951 | * @deprecated This method is now obsolete. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1952 | */ |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 1953 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1954 | public void setPictureListener(PictureListener listener) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 1955 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 1956 | mProvider.setPictureListener(listener); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | /** |
Steve Block | 3aa800b | 2012-04-24 13:01:34 +0100 | [diff] [blame] | 1960 | * Injects the supplied Java object into this WebView. The object is |
| 1961 | * injected into the JavaScript context of the main frame, using the |
Selim Gurun | e91d5be | 2012-09-11 16:11:22 -0700 | [diff] [blame] | 1962 | * supplied name. This allows the Java object's methods to be |
Selim Gurun | b4c02e6 | 2012-09-12 13:36:21 -0700 | [diff] [blame] | 1963 | * accessed from JavaScript. For applications targeted to API |
| 1964 | * level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} |
Selim Gurun | e91d5be | 2012-09-11 16:11:22 -0700 | [diff] [blame] | 1965 | * and above, only public methods that are annotated with |
| 1966 | * {@link android.webkit.JavascriptInterface} can be accessed from JavaScript. |
Selim Gurun | b4c02e6 | 2012-09-12 13:36:21 -0700 | [diff] [blame] | 1967 | * For applications targeted to API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN} or below, |
Selim Gurun | e91d5be | 2012-09-11 16:11:22 -0700 | [diff] [blame] | 1968 | * all public methods (including the inherited ones) can be accessed, see the |
Selim Gurun | b4c02e6 | 2012-09-12 13:36:21 -0700 | [diff] [blame] | 1969 | * important security note below for implications. |
Nate Fischer | 02e281a | 2017-08-21 12:03:58 -0700 | [diff] [blame] | 1970 | * <p> Note that injected objects will not appear in JavaScript until the page is next |
| 1971 | * (re)loaded. JavaScript should be enabled before injecting the object. For example: |
Selim Gurun | e91d5be | 2012-09-11 16:11:22 -0700 | [diff] [blame] | 1972 | * <pre> |
| 1973 | * class JsObject { |
| 1974 | * {@literal @}JavascriptInterface |
| 1975 | * public String toString() { return "injectedObject"; } |
| 1976 | * } |
Nate Fischer | 02e281a | 2017-08-21 12:03:58 -0700 | [diff] [blame] | 1977 | * webview.getSettings().setJavaScriptEnabled(true); |
Selim Gurun | e91d5be | 2012-09-11 16:11:22 -0700 | [diff] [blame] | 1978 | * webView.addJavascriptInterface(new JsObject(), "injectedObject"); |
Steve Block | 4cd73c5 | 2011-11-09 13:06:52 +0000 | [diff] [blame] | 1979 | * webView.loadData("<!DOCTYPE html><title></title>", "text/html", null); |
| 1980 | * webView.loadUrl("javascript:alert(injectedObject.toString())");</pre> |
Steve Block | 3aa800b | 2012-04-24 13:01:34 +0100 | [diff] [blame] | 1981 | * <p> |
| 1982 | * <strong>IMPORTANT:</strong> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1983 | * <ul> |
Steve Block | 3aa800b | 2012-04-24 13:01:34 +0100 | [diff] [blame] | 1984 | * <li> This method can be used to allow JavaScript to control the host |
| 1985 | * application. This is a powerful feature, but also presents a security |
Scott Rowe | 607c17f | 2014-08-07 11:42:09 -0700 | [diff] [blame] | 1986 | * risk for apps targeting {@link android.os.Build.VERSION_CODES#JELLY_BEAN} or earlier. |
| 1987 | * Apps that target a version later than {@link android.os.Build.VERSION_CODES#JELLY_BEAN} |
| 1988 | * are still vulnerable if the app runs on a device running Android earlier than 4.2. |
| 1989 | * The most secure way to use this method is to target {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} |
| 1990 | * and to ensure the method is called only when running on Android 4.2 or later. |
| 1991 | * With these older versions, JavaScript could use reflection to access an |
Steve Block | 3aa800b | 2012-04-24 13:01:34 +0100 | [diff] [blame] | 1992 | * injected object's public fields. Use of this method in a WebView |
| 1993 | * containing untrusted content could allow an attacker to manipulate the |
| 1994 | * host application in unintended ways, executing Java code with the |
| 1995 | * permissions of the host application. Use extreme care when using this |
| 1996 | * method in a WebView which could contain untrusted content.</li> |
Steve Block | 4cd73c5 | 2011-11-09 13:06:52 +0000 | [diff] [blame] | 1997 | * <li> JavaScript interacts with Java object on a private, background |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 1998 | * thread of this WebView. Care is therefore required to maintain thread |
Scott Rowe | 607c17f | 2014-08-07 11:42:09 -0700 | [diff] [blame] | 1999 | * safety. |
| 2000 | * </li> |
Selim Gurun | e91d5be | 2012-09-11 16:11:22 -0700 | [diff] [blame] | 2001 | * <li> The Java object's fields are not accessible.</li> |
Dianne Hackborn | 955d8d6 | 2014-10-07 20:17:19 -0700 | [diff] [blame] | 2002 | * <li> For applications targeted to API level {@link android.os.Build.VERSION_CODES#LOLLIPOP} |
Mikhail Naganov | d380b20 | 2014-04-10 18:07:42 +0100 | [diff] [blame] | 2003 | * and above, methods of injected Java objects are enumerable from |
| 2004 | * JavaScript.</li> |
Steve Block | 3aa800b | 2012-04-24 13:01:34 +0100 | [diff] [blame] | 2005 | * </ul> |
| 2006 | * |
| 2007 | * @param object the Java object to inject into this WebView's JavaScript |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 2008 | * context. {@code null} values are ignored. |
Steve Block | 3aa800b | 2012-04-24 13:01:34 +0100 | [diff] [blame] | 2009 | * @param name the name used to expose the object in JavaScript |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2010 | */ |
Steve Block | 4cd73c5 | 2011-11-09 13:06:52 +0000 | [diff] [blame] | 2011 | public void addJavascriptInterface(Object object, String name) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 2012 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2013 | mProvider.addJavascriptInterface(object, name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | /** |
Steve Block | 3aa800b | 2012-04-24 13:01:34 +0100 | [diff] [blame] | 2017 | * Removes a previously injected Java object from this WebView. Note that |
| 2018 | * the removal will not be reflected in JavaScript until the page is next |
| 2019 | * (re)loaded. See {@link #addJavascriptInterface}. |
| 2020 | * |
| 2021 | * @param name the name used to expose the object in JavaScript |
Steve Block | 689a342 | 2010-12-07 18:18:26 +0000 | [diff] [blame] | 2022 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 2023 | public void removeJavascriptInterface(@NonNull String name) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 2024 | checkThread(); |
Steve Block | 3aa800b | 2012-04-24 13:01:34 +0100 | [diff] [blame] | 2025 | mProvider.removeJavascriptInterface(name); |
Steve Block | 689a342 | 2010-12-07 18:18:26 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | /** |
Selim Gurun | 4c8093a | 2015-03-10 17:40:06 -0700 | [diff] [blame] | 2029 | * Creates a message channel to communicate with JS and returns the message |
| 2030 | * ports that represent the endpoints of this message channel. The HTML5 message |
Selim Gurun | 0a814b6 | 2015-05-13 10:15:55 -0700 | [diff] [blame] | 2031 | * channel functionality is described |
| 2032 | * <a href="https://html.spec.whatwg.org/multipage/comms.html#messagechannel">here |
| 2033 | * </a> |
Selim Gurun | 4c8093a | 2015-03-10 17:40:06 -0700 | [diff] [blame] | 2034 | * |
Nate Fischer | f02f846 | 2017-09-11 15:16:33 -0700 | [diff] [blame] | 2035 | * <p>The returned message channels are entangled and already in started state. |
Selim Gurun | 4c8093a | 2015-03-10 17:40:06 -0700 | [diff] [blame] | 2036 | * |
| 2037 | * @return the two message ports that form the message channel. |
| 2038 | */ |
| 2039 | public WebMessagePort[] createWebMessageChannel() { |
| 2040 | checkThread(); |
Selim Gurun | 4c8093a | 2015-03-10 17:40:06 -0700 | [diff] [blame] | 2041 | return mProvider.createWebMessageChannel(); |
| 2042 | } |
| 2043 | |
| 2044 | /** |
| 2045 | * Post a message to main frame. The embedded application can restrict the |
| 2046 | * messages to a certain target origin. See |
Selim Gurun | 0a814b6 | 2015-05-13 10:15:55 -0700 | [diff] [blame] | 2047 | * <a href="https://html.spec.whatwg.org/multipage/comms.html#posting-messages"> |
| 2048 | * HTML5 spec</a> for how target origin can be used. |
Selim Gurun | b18fbfa | 2017-05-18 11:50:26 -0700 | [diff] [blame] | 2049 | * <p> |
| 2050 | * A target origin can be set as a wildcard ("*"). However this is not recommended. |
| 2051 | * See the page above for security issues. |
Selim Gurun | 4c8093a | 2015-03-10 17:40:06 -0700 | [diff] [blame] | 2052 | * |
| 2053 | * @param message the WebMessage |
Selim Gurun | b18fbfa | 2017-05-18 11:50:26 -0700 | [diff] [blame] | 2054 | * @param targetOrigin the target origin. |
Selim Gurun | 4c8093a | 2015-03-10 17:40:06 -0700 | [diff] [blame] | 2055 | */ |
Selim Gurun | 26de90a | 2015-05-15 11:02:45 -0700 | [diff] [blame] | 2056 | public void postWebMessage(WebMessage message, Uri targetOrigin) { |
Selim Gurun | 4c8093a | 2015-03-10 17:40:06 -0700 | [diff] [blame] | 2057 | checkThread(); |
Selim Gurun | 4c8093a | 2015-03-10 17:40:06 -0700 | [diff] [blame] | 2058 | mProvider.postMessageToMainFrame(message, targetOrigin); |
| 2059 | } |
| 2060 | |
| 2061 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 2062 | * Gets the WebSettings object used to control the settings for this |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2063 | * WebView. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 2064 | * |
| 2065 | * @return a WebSettings object that can be used to control this WebView's |
| 2066 | * settings |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2067 | */ |
| 2068 | public WebSettings getSettings() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 2069 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2070 | return mProvider.getSettings(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2071 | } |
| 2072 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2073 | /** |
Mikhail Naganov | 057989e | 2013-09-06 16:35:25 -0700 | [diff] [blame] | 2074 | * Enables debugging of web contents (HTML / CSS / JavaScript) |
| 2075 | * loaded into any WebViews of this application. This flag can be enabled |
| 2076 | * in order to facilitate debugging of web layouts and JavaScript |
| 2077 | * code running inside WebViews. Please refer to WebView documentation |
| 2078 | * for the debugging guide. |
| 2079 | * |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 2080 | * The default is {@code false}. |
Mikhail Naganov | 057989e | 2013-09-06 16:35:25 -0700 | [diff] [blame] | 2081 | * |
| 2082 | * @param enabled whether to enable web contents debugging |
| 2083 | */ |
| 2084 | public static void setWebContentsDebuggingEnabled(boolean enabled) { |
Mikhail Naganov | 057989e | 2013-09-06 16:35:25 -0700 | [diff] [blame] | 2085 | getFactory().getStatics().setWebContentsDebuggingEnabled(enabled); |
| 2086 | } |
| 2087 | |
| 2088 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 2089 | * Gets the list of currently loaded plugins. |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2090 | * |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 2091 | * @return the list of currently loaded plugins |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2092 | * @deprecated This was used for Gears, which has been deprecated. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 2093 | * @hide |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2094 | */ |
Andrei Popescu | 385df69 | 2009-08-13 11:59:57 +0100 | [diff] [blame] | 2095 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2096 | public static synchronized PluginList getPluginList() { |
Grace Kloba | bb245ea | 2009-11-10 13:13:24 -0800 | [diff] [blame] | 2097 | return new PluginList(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2098 | } |
| 2099 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2100 | /** |
Torne (Richard Coles) | 12f64da | 2017-10-24 14:59:07 -0400 | [diff] [blame] | 2101 | * Define the directory used to store WebView data for the current process. |
| 2102 | * The provided suffix will be used when constructing data and cache |
| 2103 | * directory paths. If this API is not called, no suffix will be used. |
| 2104 | * Each directory can be used by only one process in the application. If more |
| 2105 | * than one process in an app wishes to use WebView, only one process can use |
| 2106 | * the default directory, and other processes must call this API to define |
| 2107 | * a unique suffix. |
| 2108 | * <p> |
Torne (Richard Coles) | de956b7 | 2018-01-29 16:58:44 -0500 | [diff] [blame] | 2109 | * This means that different processes in the same application cannot directly |
| 2110 | * share WebView-related data, since the data directories must be distinct. |
| 2111 | * Applications that use this API may have to explicitly pass data between |
| 2112 | * processes. For example, login cookies may have to be copied from one |
| 2113 | * process's cookie jar to the other using {@link CookieManager} if both |
| 2114 | * processes' WebViews are intended to be logged in. |
| 2115 | * <p> |
| 2116 | * Most applications should simply ensure that all components of the app |
| 2117 | * that rely on WebView are in the same process, to avoid needing multiple |
| 2118 | * data directories. The {@link #disableWebView} method can be used to ensure |
| 2119 | * that the other processes do not use WebView by accident in this case. |
| 2120 | * <p> |
Torne (Richard Coles) | 12f64da | 2017-10-24 14:59:07 -0400 | [diff] [blame] | 2121 | * This API must be called before any instances of WebView are created in |
| 2122 | * this process and before any other methods in the android.webkit package |
| 2123 | * are called by this process. |
| 2124 | * |
| 2125 | * @param suffix The directory name suffix to be used for the current |
| 2126 | * process. Must not contain a path separator. |
| 2127 | * @throws IllegalStateException if WebView has already been initialized |
| 2128 | * in the current process. |
| 2129 | * @throws IllegalArgumentException if the suffix contains a path separator. |
| 2130 | */ |
| 2131 | public static void setDataDirectorySuffix(String suffix) { |
| 2132 | WebViewFactory.setDataDirectorySuffix(suffix); |
| 2133 | } |
| 2134 | |
| 2135 | /** |
| 2136 | * Indicate that the current process does not intend to use WebView, and |
| 2137 | * that an exception should be thrown if a WebView is created or any other |
| 2138 | * methods in the android.webkit package are used. |
| 2139 | * <p> |
| 2140 | * Applications with multiple processes may wish to call this in processes |
Torne (Richard Coles) | de956b7 | 2018-01-29 16:58:44 -0500 | [diff] [blame] | 2141 | * that are not intended to use WebView to avoid accidentally incurring |
| 2142 | * the memory usage of initializing WebView in long-lived processes that |
| 2143 | * have no need for it, and to prevent potential data directory conflicts |
| 2144 | * (see {@link #setDataDirectorySuffix}). |
| 2145 | * <p> |
| 2146 | * For example, an audio player application with one process for its |
| 2147 | * activities and another process for its playback service may wish to call |
| 2148 | * this method in the playback service's {@link android.app.Service#onCreate}. |
Torne (Richard Coles) | 12f64da | 2017-10-24 14:59:07 -0400 | [diff] [blame] | 2149 | * |
| 2150 | * @throws IllegalStateException if WebView has already been initialized |
| 2151 | * in the current process. |
| 2152 | */ |
| 2153 | public static void disableWebView() { |
| 2154 | WebViewFactory.disableWebView(); |
| 2155 | } |
| 2156 | |
| 2157 | |
| 2158 | /** |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2159 | * @deprecated This was used for Gears, which has been deprecated. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 2160 | * @hide |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2161 | */ |
Andrei Popescu | 385df69 | 2009-08-13 11:59:57 +0100 | [diff] [blame] | 2162 | @Deprecated |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 2163 | public void refreshPlugins(boolean reloadOpenPages) { |
| 2164 | checkThread(); |
| 2165 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2166 | |
Cary Clark | 966641a | 2010-03-04 08:41:56 -0500 | [diff] [blame] | 2167 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 2168 | * Puts this WebView into text selection mode. Do not rely on this |
| 2169 | * functionality; it will be deprecated in the future. |
| 2170 | * |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 2171 | * @deprecated This method is now obsolete. |
Kristian Monsen | f491258 | 2012-08-16 15:26:24 -0400 | [diff] [blame] | 2172 | * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} |
Cary Clark | 966641a | 2010-03-04 08:41:56 -0500 | [diff] [blame] | 2173 | */ |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 2174 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2175 | public void emulateShiftHeld() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 2176 | checkThread(); |
Steve Howard | 16bd937 | 2010-04-12 14:46:09 -0700 | [diff] [blame] | 2177 | } |
| 2178 | |
Leon Scroggins | a57632f | 2009-11-16 10:51:12 -0500 | [diff] [blame] | 2179 | /** |
| 2180 | * @deprecated WebView no longer needs to implement |
| 2181 | * ViewGroup.OnHierarchyChangeListener. This method does nothing now. |
| 2182 | */ |
George Mount | fab67a1 | 2012-01-05 09:58:53 -0800 | [diff] [blame] | 2183 | @Override |
Jonathan Dixon | e230e54 | 2011-12-21 10:57:00 +0000 | [diff] [blame] | 2184 | // Cannot add @hide as this can always be accessed via the interface. |
Leon Scroggins | a57632f | 2009-11-16 10:51:12 -0500 | [diff] [blame] | 2185 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2186 | public void onChildViewAdded(View parent, View child) {} |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 2187 | |
Leon Scroggins | a57632f | 2009-11-16 10:51:12 -0500 | [diff] [blame] | 2188 | /** |
| 2189 | * @deprecated WebView no longer needs to implement |
| 2190 | * ViewGroup.OnHierarchyChangeListener. This method does nothing now. |
| 2191 | */ |
George Mount | fab67a1 | 2012-01-05 09:58:53 -0800 | [diff] [blame] | 2192 | @Override |
Jonathan Dixon | e230e54 | 2011-12-21 10:57:00 +0000 | [diff] [blame] | 2193 | // Cannot add @hide as this can always be accessed via the interface. |
Leon Scroggins | a57632f | 2009-11-16 10:51:12 -0500 | [diff] [blame] | 2194 | @Deprecated |
| 2195 | public void onChildViewRemoved(View p, View child) {} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2196 | |
| 2197 | /** |
| 2198 | * @deprecated WebView should not have implemented |
Gilles Debunne | 0e7d652d | 2011-02-22 15:26:14 -0800 | [diff] [blame] | 2199 | * ViewTreeObserver.OnGlobalFocusChangeListener. This method does nothing now. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2200 | */ |
George Mount | fab67a1 | 2012-01-05 09:58:53 -0800 | [diff] [blame] | 2201 | @Override |
Jonathan Dixon | e230e54 | 2011-12-21 10:57:00 +0000 | [diff] [blame] | 2202 | // Cannot add @hide as this can always be accessed via the interface. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2203 | @Deprecated |
| 2204 | public void onGlobalFocusChanged(View oldFocus, View newFocus) { |
| 2205 | } |
| 2206 | |
Kristian Monsen | 5cc2351 | 2012-08-09 15:33:08 -0400 | [diff] [blame] | 2207 | /** |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 2208 | * @deprecated Only the default case, {@code true}, will be supported in a future version. |
Kristian Monsen | 5cc2351 | 2012-08-09 15:33:08 -0400 | [diff] [blame] | 2209 | */ |
| 2210 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2211 | public void setMapTrackballToArrowKeys(boolean setMap) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 2212 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2213 | mProvider.setMapTrackballToArrowKeys(setMap); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2214 | } |
| 2215 | |
Derek Sollenberger | 03e4891 | 2010-05-18 17:03:42 -0400 | [diff] [blame] | 2216 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2217 | public void flingScroll(int vx, int vy) { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 2218 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2219 | mProvider.flingScroll(vx, vy); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2220 | } |
| 2221 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 2222 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 2223 | * Gets the zoom controls for this WebView, as a separate View. The caller |
| 2224 | * is responsible for inserting this View into the layout hierarchy. |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 2225 | * <p/> |
Steve Block | b838aef | 2012-04-23 18:22:15 +0100 | [diff] [blame] | 2226 | * API level {@link android.os.Build.VERSION_CODES#CUPCAKE} introduced |
| 2227 | * built-in zoom mechanisms for the WebView, as opposed to these separate |
| 2228 | * zoom controls. The built-in mechanisms are preferred and can be enabled |
| 2229 | * using {@link WebSettings#setBuiltInZoomControls}. |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2230 | * |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 2231 | * @deprecated the built-in zoom mechanisms are preferred |
Steve Block | b838aef | 2012-04-23 18:22:15 +0100 | [diff] [blame] | 2232 | * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2233 | */ |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2234 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2235 | public View getZoomControls() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 2236 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2237 | return mProvider.getZoomControls(); |
Mangesh Ghiware | faab93d | 2011-09-21 13:58:47 -0700 | [diff] [blame] | 2238 | } |
| 2239 | |
| 2240 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 2241 | * Gets whether this WebView can be zoomed in. |
| 2242 | * |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 2243 | * @return {@code true} if this WebView can be zoomed in |
Kristian Monsen | 5cc2351 | 2012-08-09 15:33:08 -0400 | [diff] [blame] | 2244 | * |
| 2245 | * @deprecated This method is prone to inaccuracy due to race conditions |
| 2246 | * between the web rendering and UI threads; prefer |
| 2247 | * {@link WebViewClient#onScaleChanged}. |
Grace Kloba | 6164ef1 | 2010-06-01 15:59:13 -0700 | [diff] [blame] | 2248 | */ |
Kristian Monsen | 5cc2351 | 2012-08-09 15:33:08 -0400 | [diff] [blame] | 2249 | @Deprecated |
Grace Kloba | 6164ef1 | 2010-06-01 15:59:13 -0700 | [diff] [blame] | 2250 | public boolean canZoomIn() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 2251 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2252 | return mProvider.canZoomIn(); |
Grace Kloba | 6164ef1 | 2010-06-01 15:59:13 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 2256 | * Gets whether this WebView can be zoomed out. |
| 2257 | * |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 2258 | * @return {@code true} if this WebView can be zoomed out |
Kristian Monsen | 5cc2351 | 2012-08-09 15:33:08 -0400 | [diff] [blame] | 2259 | * |
| 2260 | * @deprecated This method is prone to inaccuracy due to race conditions |
| 2261 | * between the web rendering and UI threads; prefer |
| 2262 | * {@link WebViewClient#onScaleChanged}. |
Grace Kloba | 6164ef1 | 2010-06-01 15:59:13 -0700 | [diff] [blame] | 2263 | */ |
Kristian Monsen | 5cc2351 | 2012-08-09 15:33:08 -0400 | [diff] [blame] | 2264 | @Deprecated |
Grace Kloba | 6164ef1 | 2010-06-01 15:59:13 -0700 | [diff] [blame] | 2265 | public boolean canZoomOut() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 2266 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2267 | return mProvider.canZoomOut(); |
Grace Kloba | 6164ef1 | 2010-06-01 15:59:13 -0700 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | /** |
Marcin Kosiba | 3ee06ef | 2014-07-15 17:59:51 +0100 | [diff] [blame] | 2271 | * Performs a zoom operation in this WebView. |
| 2272 | * |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 2273 | * @param zoomFactor the zoom factor to apply. The zoom factor will be clamped to the WebView's |
Marcin Kosiba | 3ee06ef | 2014-07-15 17:59:51 +0100 | [diff] [blame] | 2274 | * zoom limits. This value must be in the range 0.01 to 100.0 inclusive. |
Marcin Kosiba | 3ee06ef | 2014-07-15 17:59:51 +0100 | [diff] [blame] | 2275 | */ |
Martin Kosiba | 3a82acd | 2014-09-05 11:03:58 +0100 | [diff] [blame] | 2276 | public void zoomBy(float zoomFactor) { |
Marcin Kosiba | 3ee06ef | 2014-07-15 17:59:51 +0100 | [diff] [blame] | 2277 | checkThread(); |
| 2278 | if (zoomFactor < 0.01) |
| 2279 | throw new IllegalArgumentException("zoomFactor must be greater than 0.01."); |
| 2280 | if (zoomFactor > 100.0) |
| 2281 | throw new IllegalArgumentException("zoomFactor must be less than 100."); |
Martin Kosiba | 3a82acd | 2014-09-05 11:03:58 +0100 | [diff] [blame] | 2282 | mProvider.zoomBy(zoomFactor); |
Marcin Kosiba | 3ee06ef | 2014-07-15 17:59:51 +0100 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 2286 | * Performs zoom in in this WebView. |
| 2287 | * |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 2288 | * @return {@code true} if zoom in succeeds, {@code false} if no zoom changes |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2289 | */ |
| 2290 | public boolean zoomIn() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 2291 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2292 | return mProvider.zoomIn(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 2296 | * Performs zoom out in this WebView. |
| 2297 | * |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 2298 | * @return {@code true} if zoom out succeeds, {@code false} if no zoom changes |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2299 | */ |
| 2300 | public boolean zoomOut() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 2301 | checkThread(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2302 | return mProvider.zoomOut(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2303 | } |
| 2304 | |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 2305 | /** |
| 2306 | * @deprecated This method is now obsolete. |
Kristian Monsen | f491258 | 2012-08-16 15:26:24 -0400 | [diff] [blame] | 2307 | * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} |
Kristian Monsen | fc77165 | 2011-05-10 16:44:05 +0100 | [diff] [blame] | 2308 | */ |
| 2309 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2310 | public void debugDump() { |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 2311 | checkThread(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2312 | } |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 2313 | |
John Reck | 926cf56 | 2012-06-14 10:00:31 -0700 | [diff] [blame] | 2314 | /** |
John Reck | f236115 | 2012-06-14 15:23:22 -0700 | [diff] [blame] | 2315 | * See {@link ViewDebug.HierarchyHandler#dumpViewHierarchyWithProperties(BufferedWriter, int)} |
John Reck | 926cf56 | 2012-06-14 10:00:31 -0700 | [diff] [blame] | 2316 | * @hide |
| 2317 | */ |
| 2318 | @Override |
| 2319 | public void dumpViewHierarchyWithProperties(BufferedWriter out, int level) { |
| 2320 | mProvider.dumpViewHierarchyWithProperties(out, level); |
| 2321 | } |
| 2322 | |
| 2323 | /** |
| 2324 | * See {@link ViewDebug.HierarchyHandler#findHierarchyView(String, int)} |
| 2325 | * @hide |
| 2326 | */ |
| 2327 | @Override |
| 2328 | public View findHierarchyView(String className, int hashCode) { |
| 2329 | return mProvider.findHierarchyView(className, hashCode); |
| 2330 | } |
| 2331 | |
Tobias Sargeant | d10e4af | 2017-01-19 14:03:09 +0000 | [diff] [blame] | 2332 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 2333 | @IntDef(prefix = { "RENDERER_PRIORITY_" }, value = { |
| 2334 | RENDERER_PRIORITY_WAIVED, |
| 2335 | RENDERER_PRIORITY_BOUND, |
| 2336 | RENDERER_PRIORITY_IMPORTANT |
Tobias Sargeant | d10e4af | 2017-01-19 14:03:09 +0000 | [diff] [blame] | 2337 | }) |
| 2338 | @Retention(RetentionPolicy.SOURCE) |
| 2339 | public @interface RendererPriority {} |
| 2340 | |
| 2341 | /** |
| 2342 | * The renderer associated with this WebView is bound with |
| 2343 | * {@link Context#BIND_WAIVE_PRIORITY}. At this priority level |
| 2344 | * {@link WebView} renderers will be strong targets for out of memory |
| 2345 | * killing. |
| 2346 | * |
| 2347 | * Use with {@link #setRendererPriorityPolicy}. |
| 2348 | */ |
| 2349 | public static final int RENDERER_PRIORITY_WAIVED = 0; |
| 2350 | /** |
| 2351 | * The renderer associated with this WebView is bound with |
| 2352 | * the default priority for services. |
| 2353 | * |
| 2354 | * Use with {@link #setRendererPriorityPolicy}. |
| 2355 | */ |
| 2356 | public static final int RENDERER_PRIORITY_BOUND = 1; |
| 2357 | /** |
| 2358 | * The renderer associated with this WebView is bound with |
| 2359 | * {@link Context#BIND_IMPORTANT}. |
| 2360 | * |
| 2361 | * Use with {@link #setRendererPriorityPolicy}. |
| 2362 | */ |
| 2363 | public static final int RENDERER_PRIORITY_IMPORTANT = 2; |
| 2364 | |
| 2365 | /** |
| 2366 | * Set the renderer priority policy for this {@link WebView}. The |
| 2367 | * priority policy will be used to determine whether an out of |
| 2368 | * process renderer should be considered to be a target for OOM |
| 2369 | * killing. |
| 2370 | * |
| 2371 | * Because a renderer can be associated with more than one |
| 2372 | * WebView, the final priority it is computed as the maximum of |
| 2373 | * any attached WebViews. When a WebView is destroyed it will |
| 2374 | * cease to be considerered when calculating the renderer |
| 2375 | * priority. Once no WebViews remain associated with the renderer, |
| 2376 | * the priority of the renderer will be reduced to |
| 2377 | * {@link #RENDERER_PRIORITY_WAIVED}. |
| 2378 | * |
| 2379 | * The default policy is to set the priority to |
| 2380 | * {@link #RENDERER_PRIORITY_IMPORTANT} regardless of visibility, |
| 2381 | * and this should not be changed unless the caller also handles |
| 2382 | * renderer crashes with |
| 2383 | * {@link WebViewClient#onRenderProcessGone}. Any other setting |
| 2384 | * will result in WebView renderers being killed by the system |
| 2385 | * more aggressively than the application. |
| 2386 | * |
| 2387 | * @param rendererRequestedPriority the minimum priority at which |
| 2388 | * this WebView desires the renderer process to be bound. |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 2389 | * @param waivedWhenNotVisible if {@code true}, this flag specifies that |
Tobias Sargeant | d10e4af | 2017-01-19 14:03:09 +0000 | [diff] [blame] | 2390 | * when this WebView is not visible, it will be treated as |
| 2391 | * if it had requested a priority of |
| 2392 | * {@link #RENDERER_PRIORITY_WAIVED}. |
| 2393 | */ |
| 2394 | public void setRendererPriorityPolicy( |
| 2395 | @RendererPriority int rendererRequestedPriority, |
| 2396 | boolean waivedWhenNotVisible) { |
| 2397 | mProvider.setRendererPriorityPolicy(rendererRequestedPriority, waivedWhenNotVisible); |
| 2398 | } |
| 2399 | |
| 2400 | /** |
| 2401 | * Get the requested renderer priority for this WebView. |
| 2402 | * |
| 2403 | * @return the requested renderer priority policy. |
| 2404 | */ |
| 2405 | @RendererPriority |
| 2406 | public int getRendererRequestedPriority() { |
| 2407 | return mProvider.getRendererRequestedPriority(); |
| 2408 | } |
| 2409 | |
| 2410 | /** |
| 2411 | * Return whether this WebView requests a priority of |
| 2412 | * {@link #RENDERER_PRIORITY_WAIVED} when not visible. |
| 2413 | * |
| 2414 | * @return whether this WebView requests a priority of |
| 2415 | * {@link #RENDERER_PRIORITY_WAIVED} when not visible. |
| 2416 | */ |
| 2417 | public boolean getRendererPriorityWaivedWhenNotVisible() { |
| 2418 | return mProvider.getRendererPriorityWaivedWhenNotVisible(); |
| 2419 | } |
Tima Vaisburd | cf49a23 | 2017-03-28 11:35:58 -0700 | [diff] [blame] | 2420 | |
| 2421 | /** |
| 2422 | * Sets the {@link TextClassifier} for this WebView. |
| 2423 | */ |
| 2424 | public void setTextClassifier(@Nullable TextClassifier textClassifier) { |
| 2425 | mProvider.setTextClassifier(textClassifier); |
| 2426 | } |
| 2427 | |
| 2428 | /** |
| 2429 | * Returns the {@link TextClassifier} used by this WebView. |
| 2430 | * If no TextClassifier has been set, this WebView uses the default set by the system. |
| 2431 | */ |
| 2432 | @NonNull |
| 2433 | public TextClassifier getTextClassifier() { |
| 2434 | return mProvider.getTextClassifier(); |
| 2435 | } |
| 2436 | |
Gustav Sennton | 265afdb | 2018-01-19 11:56:55 +0000 | [diff] [blame] | 2437 | /** |
| 2438 | * Returns the {@link ClassLoader} used to load internal WebView classes. |
| 2439 | * This method is meant for use by the WebView Support Library, there is no reason to use this |
| 2440 | * method otherwise. |
| 2441 | */ |
| 2442 | @NonNull |
| 2443 | public static ClassLoader getWebViewClassLoader() { |
| 2444 | return getFactory().getWebViewClassLoader(); |
| 2445 | } |
| 2446 | |
Gustav Sennton | 0167369 | 2018-01-19 12:11:25 +0000 | [diff] [blame] | 2447 | /** |
| 2448 | * Returns the {@link Looper} corresponding to the thread on which WebView calls must be made. |
| 2449 | */ |
| 2450 | @NonNull |
| 2451 | public Looper getLooper() { |
| 2452 | return mWebViewThread; |
| 2453 | } |
| 2454 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2455 | //------------------------------------------------------------------------- |
| 2456 | // Interface for WebView providers |
| 2457 | //------------------------------------------------------------------------- |
| 2458 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2459 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 2460 | * Gets the WebViewProvider. Used by providers to obtain the underlying |
Nate Fischer | ffb81c4 | 2016-09-26 12:35:47 -0700 | [diff] [blame] | 2461 | * implementation, e.g. when the application responds to |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 2462 | * WebViewClient.onCreateWindow() request. |
Mike Reed | efe2c72 | 2009-10-14 09:42:02 -0400 | [diff] [blame] | 2463 | * |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2464 | * @hide WebViewProvider is not public API. |
Mike Reed | efe2c72 | 2009-10-14 09:42:02 -0400 | [diff] [blame] | 2465 | */ |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 2466 | @SystemApi |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2467 | public WebViewProvider getWebViewProvider() { |
| 2468 | return mProvider; |
Mike Reed | efe2c72 | 2009-10-14 09:42:02 -0400 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | /** |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2472 | * Callback interface, allows the provider implementation to access non-public methods |
| 2473 | * and fields, and make super-class calls in this WebView instance. |
| 2474 | * @hide Only for use by WebViewProvider implementations |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 2475 | */ |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 2476 | @SystemApi |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2477 | public class PrivateAccess { |
| 2478 | // ---- Access to super-class methods ---- |
| 2479 | public int super_getScrollBarStyle() { |
| 2480 | return WebView.super.getScrollBarStyle(); |
| 2481 | } |
| 2482 | |
| 2483 | public void super_scrollTo(int scrollX, int scrollY) { |
| 2484 | WebView.super.scrollTo(scrollX, scrollY); |
| 2485 | } |
| 2486 | |
| 2487 | public void super_computeScroll() { |
| 2488 | WebView.super.computeScroll(); |
| 2489 | } |
| 2490 | |
alanv | eebebc9 | 2012-06-15 18:59:11 -0700 | [diff] [blame] | 2491 | public boolean super_onHoverEvent(MotionEvent event) { |
| 2492 | return WebView.super.onHoverEvent(event); |
| 2493 | } |
| 2494 | |
Marcin Kosiba | 3e853fb | 2015-01-08 12:45:40 +0000 | [diff] [blame] | 2495 | public boolean super_performAccessibilityAction(int action, Bundle arguments) { |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 2496 | return WebView.super.performAccessibilityActionInternal(action, arguments); |
alanv | 448902d | 2012-05-16 20:27:47 -0700 | [diff] [blame] | 2497 | } |
| 2498 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2499 | public boolean super_performLongClick() { |
| 2500 | return WebView.super.performLongClick(); |
| 2501 | } |
| 2502 | |
| 2503 | public boolean super_setFrame(int left, int top, int right, int bottom) { |
| 2504 | return WebView.super.setFrame(left, top, right, bottom); |
| 2505 | } |
| 2506 | |
| 2507 | public boolean super_dispatchKeyEvent(KeyEvent event) { |
| 2508 | return WebView.super.dispatchKeyEvent(event); |
| 2509 | } |
| 2510 | |
| 2511 | public boolean super_onGenericMotionEvent(MotionEvent event) { |
| 2512 | return WebView.super.onGenericMotionEvent(event); |
| 2513 | } |
| 2514 | |
| 2515 | public boolean super_requestFocus(int direction, Rect previouslyFocusedRect) { |
| 2516 | return WebView.super.requestFocus(direction, previouslyFocusedRect); |
| 2517 | } |
| 2518 | |
| 2519 | public void super_setLayoutParams(ViewGroup.LayoutParams params) { |
| 2520 | WebView.super.setLayoutParams(params); |
| 2521 | } |
| 2522 | |
Hui Shu | f119c52 | 2015-10-08 10:07:13 -0700 | [diff] [blame] | 2523 | public void super_startActivityForResult(Intent intent, int requestCode) { |
| 2524 | WebView.super.startActivityForResult(intent, requestCode); |
| 2525 | } |
| 2526 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2527 | // ---- Access to non-public methods ---- |
| 2528 | public void overScrollBy(int deltaX, int deltaY, |
| 2529 | int scrollX, int scrollY, |
| 2530 | int scrollRangeX, int scrollRangeY, |
| 2531 | int maxOverScrollX, int maxOverScrollY, |
| 2532 | boolean isTouchEvent) { |
| 2533 | WebView.this.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, |
| 2534 | maxOverScrollX, maxOverScrollY, isTouchEvent); |
| 2535 | } |
| 2536 | |
| 2537 | public void awakenScrollBars(int duration) { |
| 2538 | WebView.this.awakenScrollBars(duration); |
| 2539 | } |
| 2540 | |
| 2541 | public void awakenScrollBars(int duration, boolean invalidate) { |
| 2542 | WebView.this.awakenScrollBars(duration, invalidate); |
| 2543 | } |
| 2544 | |
| 2545 | public float getVerticalScrollFactor() { |
| 2546 | return WebView.this.getVerticalScrollFactor(); |
| 2547 | } |
| 2548 | |
| 2549 | public float getHorizontalScrollFactor() { |
| 2550 | return WebView.this.getHorizontalScrollFactor(); |
| 2551 | } |
| 2552 | |
| 2553 | public void setMeasuredDimension(int measuredWidth, int measuredHeight) { |
| 2554 | WebView.this.setMeasuredDimension(measuredWidth, measuredHeight); |
| 2555 | } |
| 2556 | |
| 2557 | public void onScrollChanged(int l, int t, int oldl, int oldt) { |
| 2558 | WebView.this.onScrollChanged(l, t, oldl, oldt); |
| 2559 | } |
| 2560 | |
| 2561 | public int getHorizontalScrollbarHeight() { |
| 2562 | return WebView.this.getHorizontalScrollbarHeight(); |
| 2563 | } |
| 2564 | |
Martin Kosiba | 9a68f82 | 2013-08-08 14:02:41 +0100 | [diff] [blame] | 2565 | public void super_onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar, |
| 2566 | int l, int t, int r, int b) { |
| 2567 | WebView.super.onDrawVerticalScrollBar(canvas, scrollBar, l, t, r, b); |
| 2568 | } |
| 2569 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2570 | // ---- Access to (non-public) fields ---- |
| 2571 | /** Raw setter for the scroll X value, without invoking onScrollChanged handlers etc. */ |
| 2572 | public void setScrollXRaw(int scrollX) { |
| 2573 | WebView.this.mScrollX = scrollX; |
| 2574 | } |
| 2575 | |
| 2576 | /** Raw setter for the scroll Y value, without invoking onScrollChanged handlers etc. */ |
| 2577 | public void setScrollYRaw(int scrollY) { |
| 2578 | WebView.this.mScrollY = scrollY; |
| 2579 | } |
| 2580 | |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 2581 | } |
| 2582 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2583 | //------------------------------------------------------------------------- |
Ben Murdoch | 52c9f7f | 2013-01-18 00:50:37 +0000 | [diff] [blame] | 2584 | // Package-private internal stuff |
| 2585 | //------------------------------------------------------------------------- |
| 2586 | |
| 2587 | // Only used by android.webkit.FindActionModeCallback. |
| 2588 | void setFindDialogFindListener(FindListener listener) { |
| 2589 | checkThread(); |
| 2590 | setupFindListenerIfNeeded(); |
| 2591 | mFindListener.mFindDialogFindListener = listener; |
| 2592 | } |
| 2593 | |
| 2594 | // Only used by android.webkit.FindActionModeCallback. |
| 2595 | void notifyFindDialogDismissed() { |
| 2596 | checkThread(); |
| 2597 | mProvider.notifyFindDialogDismissed(); |
| 2598 | } |
| 2599 | |
| 2600 | //------------------------------------------------------------------------- |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2601 | // Private internal stuff |
| 2602 | //------------------------------------------------------------------------- |
| 2603 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2604 | private WebViewProvider mProvider; |
| 2605 | |
Ben Murdoch | 52c9f7f | 2013-01-18 00:50:37 +0000 | [diff] [blame] | 2606 | /** |
| 2607 | * In addition to the FindListener that the user may set via the WebView.setFindListener |
| 2608 | * API, FindActionModeCallback will register it's own FindListener. We keep them separate |
Mark Doliner | d0646dc | 2014-08-27 16:04:02 -0700 | [diff] [blame] | 2609 | * via this class so that the two FindListeners can potentially exist at once. |
Ben Murdoch | 52c9f7f | 2013-01-18 00:50:37 +0000 | [diff] [blame] | 2610 | */ |
| 2611 | private class FindListenerDistributor implements FindListener { |
| 2612 | private FindListener mFindDialogFindListener; |
| 2613 | private FindListener mUserFindListener; |
| 2614 | |
| 2615 | @Override |
| 2616 | public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, |
| 2617 | boolean isDoneCounting) { |
| 2618 | if (mFindDialogFindListener != null) { |
| 2619 | mFindDialogFindListener.onFindResultReceived(activeMatchOrdinal, numberOfMatches, |
| 2620 | isDoneCounting); |
| 2621 | } |
| 2622 | |
| 2623 | if (mUserFindListener != null) { |
| 2624 | mUserFindListener.onFindResultReceived(activeMatchOrdinal, numberOfMatches, |
| 2625 | isDoneCounting); |
| 2626 | } |
| 2627 | } |
| 2628 | } |
| 2629 | private FindListenerDistributor mFindListener; |
| 2630 | |
| 2631 | private void setupFindListenerIfNeeded() { |
| 2632 | if (mFindListener == null) { |
| 2633 | mFindListener = new FindListenerDistributor(); |
| 2634 | mProvider.setFindListener(mFindListener); |
| 2635 | } |
| 2636 | } |
| 2637 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2638 | private void ensureProviderCreated() { |
| 2639 | checkThread(); |
| 2640 | if (mProvider == null) { |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2641 | // As this can get called during the base class constructor chain, pass the minimum |
| 2642 | // number of dependencies here; the rest are deferred to init(). |
| 2643 | mProvider = getFactory().createWebView(this, new PrivateAccess()); |
| 2644 | } |
Ben Murdoch | ecbc65c | 2010-01-13 10:54:56 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
Torne (Richard Coles) | 59375a0 | 2016-07-18 18:34:02 +0100 | [diff] [blame] | 2647 | private static WebViewFactoryProvider getFactory() { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 2648 | return WebViewFactory.getProvider(); |
Ben Murdoch | 1708ad5 | 2010-11-11 15:56:16 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
Jonathan Dixon | 517771b | 2013-10-08 13:32:11 -0700 | [diff] [blame] | 2651 | private final Looper mWebViewThread = Looper.myLooper(); |
| 2652 | |
| 2653 | private void checkThread() { |
| 2654 | // Ignore mWebViewThread == null because this can be called during in the super class |
| 2655 | // constructor, before this class's own constructor has even started. |
| 2656 | if (mWebViewThread != null && Looper.myLooper() != mWebViewThread) { |
Steve Block | 7a01d94 | 2011-09-26 12:30:31 +0100 | [diff] [blame] | 2657 | Throwable throwable = new Throwable( |
Jonathan Dixon | 517771b | 2013-10-08 13:32:11 -0700 | [diff] [blame] | 2658 | "A WebView method was called on thread '" + |
Steve Block | 08d584c | 2011-05-17 19:05:03 +0100 | [diff] [blame] | 2659 | Thread.currentThread().getName() + "'. " + |
Jonathan Dixon | 517771b | 2013-10-08 13:32:11 -0700 | [diff] [blame] | 2660 | "All WebView methods must be called on the same thread. " + |
| 2661 | "(Expected Looper " + mWebViewThread + " called on " + Looper.myLooper() + |
| 2662 | ", FYI main Looper is " + Looper.getMainLooper() + ")"); |
Steve Block | 7a01d94 | 2011-09-26 12:30:31 +0100 | [diff] [blame] | 2663 | Log.w(LOGTAG, Log.getStackTraceString(throwable)); |
| 2664 | StrictMode.onWebViewMethodCalledOnWrongThread(throwable); |
Kristian Monsen | b5cd8c0 | 2013-04-09 17:57:37 -0700 | [diff] [blame] | 2665 | |
| 2666 | if (sEnforceThreadChecking) { |
| 2667 | throw new RuntimeException(throwable); |
| 2668 | } |
Steve Block | 51b0891 | 2011-04-27 15:04:48 +0100 | [diff] [blame] | 2669 | } |
| 2670 | } |
| 2671 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2672 | //------------------------------------------------------------------------- |
| 2673 | // Override View methods |
| 2674 | //------------------------------------------------------------------------- |
| 2675 | |
| 2676 | // TODO: Add a test that enumerates all methods in ViewDelegte & ScrollDelegate, and ensures |
| 2677 | // there's a corresponding override (or better, caller) for each of them in here. |
| 2678 | |
| 2679 | @Override |
| 2680 | protected void onAttachedToWindow() { |
| 2681 | super.onAttachedToWindow(); |
| 2682 | mProvider.getViewDelegate().onAttachedToWindow(); |
Chris Craik | 555c55e | 2011-07-28 15:39:43 -0700 | [diff] [blame] | 2683 | } |
| 2684 | |
John Reck | 34dbce1 | 2014-03-06 12:52:24 -0800 | [diff] [blame] | 2685 | /** @hide */ |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2686 | @Override |
John Reck | 34dbce1 | 2014-03-06 12:52:24 -0800 | [diff] [blame] | 2687 | protected void onDetachedFromWindowInternal() { |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2688 | mProvider.getViewDelegate().onDetachedFromWindow(); |
John Reck | 34dbce1 | 2014-03-06 12:52:24 -0800 | [diff] [blame] | 2689 | super.onDetachedFromWindowInternal(); |
Chris Craik | 555c55e | 2011-07-28 15:39:43 -0700 | [diff] [blame] | 2690 | } |
| 2691 | |
Tima Vaisburd | 5544f6c | 2017-04-27 10:53:27 -0700 | [diff] [blame] | 2692 | /** @hide */ |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2693 | @Override |
Tima Vaisburd | 134752b | 2017-04-06 15:50:05 -0700 | [diff] [blame] | 2694 | public void onMovedToDisplay(int displayId, Configuration config) { |
| 2695 | mProvider.getViewDelegate().onMovedToDisplay(displayId, config); |
| 2696 | } |
| 2697 | |
| 2698 | @Override |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2699 | public void setLayoutParams(ViewGroup.LayoutParams params) { |
| 2700 | mProvider.getViewDelegate().setLayoutParams(params); |
Chris Craik | 445ab74 | 2011-07-13 13:19:37 -0700 | [diff] [blame] | 2701 | } |
| 2702 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2703 | @Override |
| 2704 | public void setOverScrollMode(int mode) { |
| 2705 | super.setOverScrollMode(mode); |
Martin Kosiba | 5ee743e | 2012-12-21 12:39:50 +0000 | [diff] [blame] | 2706 | // This method may be called in the constructor chain, before the WebView provider is |
| 2707 | // created. |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2708 | ensureProviderCreated(); |
| 2709 | mProvider.getViewDelegate().setOverScrollMode(mode); |
Chris Craik | 445ab74 | 2011-07-13 13:19:37 -0700 | [diff] [blame] | 2710 | } |
| 2711 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2712 | @Override |
| 2713 | public void setScrollBarStyle(int style) { |
| 2714 | mProvider.getViewDelegate().setScrollBarStyle(style); |
| 2715 | super.setScrollBarStyle(style); |
George Mount | 3d09531 | 2012-01-10 11:24:02 -0800 | [diff] [blame] | 2716 | } |
| 2717 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2718 | @Override |
| 2719 | protected int computeHorizontalScrollRange() { |
| 2720 | return mProvider.getScrollDelegate().computeHorizontalScrollRange(); |
John Reck | 5528d7c | 2012-02-28 11:29:29 -0800 | [diff] [blame] | 2721 | } |
| 2722 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2723 | @Override |
| 2724 | protected int computeHorizontalScrollOffset() { |
| 2725 | return mProvider.getScrollDelegate().computeHorizontalScrollOffset(); |
| 2726 | } |
Grace Kloba | 8abd50b | 2010-07-08 15:02:14 -0700 | [diff] [blame] | 2727 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2728 | @Override |
| 2729 | protected int computeVerticalScrollRange() { |
| 2730 | return mProvider.getScrollDelegate().computeVerticalScrollRange(); |
| 2731 | } |
Patrick Scott | a3ebcc9 | 2010-07-16 11:52:22 -0400 | [diff] [blame] | 2732 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2733 | @Override |
| 2734 | protected int computeVerticalScrollOffset() { |
| 2735 | return mProvider.getScrollDelegate().computeVerticalScrollOffset(); |
| 2736 | } |
Ben Murdoch | fa148f6 | 2011-02-01 20:51:27 +0000 | [diff] [blame] | 2737 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2738 | @Override |
| 2739 | protected int computeVerticalScrollExtent() { |
| 2740 | return mProvider.getScrollDelegate().computeVerticalScrollExtent(); |
| 2741 | } |
| 2742 | |
| 2743 | @Override |
| 2744 | public void computeScroll() { |
| 2745 | mProvider.getScrollDelegate().computeScroll(); |
| 2746 | } |
| 2747 | |
| 2748 | @Override |
| 2749 | public boolean onHoverEvent(MotionEvent event) { |
| 2750 | return mProvider.getViewDelegate().onHoverEvent(event); |
| 2751 | } |
| 2752 | |
| 2753 | @Override |
| 2754 | public boolean onTouchEvent(MotionEvent event) { |
| 2755 | return mProvider.getViewDelegate().onTouchEvent(event); |
| 2756 | } |
| 2757 | |
| 2758 | @Override |
| 2759 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 2760 | return mProvider.getViewDelegate().onGenericMotionEvent(event); |
| 2761 | } |
| 2762 | |
| 2763 | @Override |
| 2764 | public boolean onTrackballEvent(MotionEvent event) { |
| 2765 | return mProvider.getViewDelegate().onTrackballEvent(event); |
| 2766 | } |
| 2767 | |
| 2768 | @Override |
| 2769 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 2770 | return mProvider.getViewDelegate().onKeyDown(keyCode, event); |
| 2771 | } |
| 2772 | |
| 2773 | @Override |
| 2774 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 2775 | return mProvider.getViewDelegate().onKeyUp(keyCode, event); |
| 2776 | } |
| 2777 | |
| 2778 | @Override |
| 2779 | public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { |
| 2780 | return mProvider.getViewDelegate().onKeyMultiple(keyCode, repeatCount, event); |
| 2781 | } |
| 2782 | |
| 2783 | /* |
| 2784 | TODO: These are not currently implemented in WebViewClassic, but it seems inconsistent not |
| 2785 | to be delegating them too. |
| 2786 | |
| 2787 | @Override |
| 2788 | public boolean onKeyPreIme(int keyCode, KeyEvent event) { |
| 2789 | return mProvider.getViewDelegate().onKeyPreIme(keyCode, event); |
| 2790 | } |
| 2791 | @Override |
| 2792 | public boolean onKeyLongPress(int keyCode, KeyEvent event) { |
| 2793 | return mProvider.getViewDelegate().onKeyLongPress(keyCode, event); |
| 2794 | } |
| 2795 | @Override |
| 2796 | public boolean onKeyShortcut(int keyCode, KeyEvent event) { |
| 2797 | return mProvider.getViewDelegate().onKeyShortcut(keyCode, event); |
| 2798 | } |
| 2799 | */ |
| 2800 | |
Ben Murdoch | e3f9071 | 2013-06-05 14:19:48 +0100 | [diff] [blame] | 2801 | @Override |
| 2802 | public AccessibilityNodeProvider getAccessibilityNodeProvider() { |
| 2803 | AccessibilityNodeProvider provider = |
| 2804 | mProvider.getViewDelegate().getAccessibilityNodeProvider(); |
| 2805 | return provider == null ? super.getAccessibilityNodeProvider() : provider; |
| 2806 | } |
| 2807 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2808 | @Deprecated |
| 2809 | @Override |
| 2810 | public boolean shouldDelayChildPressedState() { |
| 2811 | return mProvider.getViewDelegate().shouldDelayChildPressedState(); |
| 2812 | } |
| 2813 | |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 2814 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 2815 | public CharSequence getAccessibilityClassName() { |
| 2816 | return WebView.class.getName(); |
| 2817 | } |
| 2818 | |
Selim Gurun | 1b650cb | 2015-04-06 20:13:37 -0700 | [diff] [blame] | 2819 | @Override |
Dianne Hackborn | 49b043f | 2015-05-07 14:21:38 -0700 | [diff] [blame] | 2820 | public void onProvideVirtualStructure(ViewStructure structure) { |
Selim Gurun | 50b26a5 | 2015-06-01 16:26:10 -0700 | [diff] [blame] | 2821 | mProvider.getViewDelegate().onProvideVirtualStructure(structure); |
Selim Gurun | 1b650cb | 2015-04-06 20:13:37 -0700 | [diff] [blame] | 2822 | } |
| 2823 | |
Felipe Leme | 1a1e468 | 2017-03-13 16:34:03 -0700 | [diff] [blame] | 2824 | /** |
| 2825 | * {@inheritDoc} |
| 2826 | * |
| 2827 | * <p>The {@link ViewStructure} traditionally represents a {@link View}, while for web pages |
| 2828 | * it represent HTML nodes. Hence, it's necessary to "map" the HTML properties in a way that is |
| 2829 | * understood by the {@link android.service.autofill.AutofillService} implementations: |
| 2830 | * |
| 2831 | * <ol> |
Felipe Leme | 303b609 | 2017-07-31 18:01:15 -0700 | [diff] [blame] | 2832 | * <li>Only the HTML nodes inside a {@code FORM} are generated. |
| 2833 | * <li>The source of the HTML is set using {@link ViewStructure#setWebDomain(String)} in the |
| 2834 | * node representing the WebView. |
| 2835 | * <li>If a web page has multiple {@code FORM}s, only the data for the current form is |
| 2836 | * represented—if the user taps a field from another form, then the current autofill |
| 2837 | * context is canceled (by calling {@link android.view.autofill.AutofillManager#cancel()} and |
| 2838 | * a new context is created for that {@code FORM}. |
| 2839 | * <li>Similarly, if the page has {@code IFRAME} nodes, they are not initially represented in |
| 2840 | * the view structure until the user taps a field from a {@code FORM} inside the |
| 2841 | * {@code IFRAME}, in which case it would be treated the same way as multiple forms described |
| 2842 | * above, except that the {@link ViewStructure#setWebDomain(String) web domain} of the |
| 2843 | * {@code FORM} contains the {@code src} attribute from the {@code IFRAME} node. |
Felipe Leme | 1a1e468 | 2017-03-13 16:34:03 -0700 | [diff] [blame] | 2844 | * <li>The W3C autofill field ({@code autocomplete} tag attribute) maps to |
Felipe Leme | 58390fe | 2017-09-15 15:16:28 -0700 | [diff] [blame] | 2845 | * {@link ViewStructure#setAutofillHints(String[])}. |
| 2846 | * <li>If the view is editable, the {@link ViewStructure#setAutofillType(int)} and |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 2847 | * {@link ViewStructure#setAutofillValue(AutofillValue)} must be set. |
Felipe Leme | 1a1e468 | 2017-03-13 16:34:03 -0700 | [diff] [blame] | 2848 | * <li>The {@code placeholder} attribute maps to {@link ViewStructure#setHint(CharSequence)}. |
Felipe Leme | 25bf787 | 2017-03-28 15:32:29 -0700 | [diff] [blame] | 2849 | * <li>Other HTML attributes can be represented through |
| 2850 | * {@link ViewStructure#setHtmlInfo(android.view.ViewStructure.HtmlInfo)}. |
Felipe Leme | 1a1e468 | 2017-03-13 16:34:03 -0700 | [diff] [blame] | 2851 | * </ol> |
| 2852 | * |
Felipe Leme | 58390fe | 2017-09-15 15:16:28 -0700 | [diff] [blame] | 2853 | * <p>If the WebView implementation can determine that the value of a field was set statically |
| 2854 | * (for example, not through Javascript), it should also call |
| 2855 | * {@code structure.setDataIsSensitive(false)}. |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 2856 | * |
Felipe Leme | 58390fe | 2017-09-15 15:16:28 -0700 | [diff] [blame] | 2857 | * <p>For example, an HTML form with 2 fields for username and password: |
Felipe Leme | 1a1e468 | 2017-03-13 16:34:03 -0700 | [diff] [blame] | 2858 | * |
| 2859 | * <pre class="prettyprint"> |
Felipe Leme | f485f89 | 2017-11-22 08:15:59 -0800 | [diff] [blame] | 2860 | * <label>Username:</label> |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 2861 | * <input type="text" name="username" id="user" value="Type your username" autocomplete="username" placeholder="Email or username"> |
Felipe Leme | f485f89 | 2017-11-22 08:15:59 -0800 | [diff] [blame] | 2862 | * <label>Password:</label> |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 2863 | * <input type="password" name="password" id="pass" autocomplete="current-password" placeholder="Password"> |
Felipe Leme | 1a1e468 | 2017-03-13 16:34:03 -0700 | [diff] [blame] | 2864 | * </pre> |
| 2865 | * |
| 2866 | * <p>Would map to: |
| 2867 | * |
| 2868 | * <pre class="prettyprint"> |
Felipe Leme | 25bf787 | 2017-03-28 15:32:29 -0700 | [diff] [blame] | 2869 | * int index = structure.addChildCount(2); |
| 2870 | * ViewStructure username = structure.newChild(index); |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 2871 | * username.setAutofillId(structure.getAutofillId(), 1); // id 1 - first child |
Felipe Leme | 1a1e468 | 2017-03-13 16:34:03 -0700 | [diff] [blame] | 2872 | * username.setAutofillHints("username"); |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 2873 | * username.setHtmlInfo(username.newHtmlInfoBuilder("input") |
| 2874 | * .addAttribute("type", "text") |
Felipe Leme | 25bf787 | 2017-03-28 15:32:29 -0700 | [diff] [blame] | 2875 | * .addAttribute("name", "username") |
Felipe Leme | f485f89 | 2017-11-22 08:15:59 -0800 | [diff] [blame] | 2876 | * .addAttribute("label", "Username:") |
Felipe Leme | 25bf787 | 2017-03-28 15:32:29 -0700 | [diff] [blame] | 2877 | * .build()); |
Felipe Leme | 1a1e468 | 2017-03-13 16:34:03 -0700 | [diff] [blame] | 2878 | * username.setHint("Email or username"); |
| 2879 | * username.setAutofillType(View.AUTOFILL_TYPE_TEXT); |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 2880 | * username.setAutofillValue(AutofillValue.forText("Type your username")); |
Felipe Leme | 58390fe | 2017-09-15 15:16:28 -0700 | [diff] [blame] | 2881 | * // Value of the field is not sensitive because it was created statically and not changed. |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 2882 | * username.setDataIsSensitive(false); |
Felipe Leme | 1a1e468 | 2017-03-13 16:34:03 -0700 | [diff] [blame] | 2883 | * |
Felipe Leme | 25bf787 | 2017-03-28 15:32:29 -0700 | [diff] [blame] | 2884 | * ViewStructure password = structure.newChild(index + 1); |
| 2885 | * username.setAutofillId(structure, 2); // id 2 - second child |
Felipe Leme | 1a1e468 | 2017-03-13 16:34:03 -0700 | [diff] [blame] | 2886 | * password.setAutofillHints("current-password"); |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 2887 | * password.setHtmlInfo(password.newHtmlInfoBuilder("input") |
| 2888 | * .addAttribute("type", "password") |
Felipe Leme | 25bf787 | 2017-03-28 15:32:29 -0700 | [diff] [blame] | 2889 | * .addAttribute("name", "password") |
Felipe Leme | f485f89 | 2017-11-22 08:15:59 -0800 | [diff] [blame] | 2890 | * .addAttribute("label", "Password:") |
Felipe Leme | 25bf787 | 2017-03-28 15:32:29 -0700 | [diff] [blame] | 2891 | * .build()); |
Felipe Leme | 1a1e468 | 2017-03-13 16:34:03 -0700 | [diff] [blame] | 2892 | * password.setHint("Password"); |
| 2893 | * password.setAutofillType(View.AUTOFILL_TYPE_TEXT); |
Felipe Leme | 1a1e468 | 2017-03-13 16:34:03 -0700 | [diff] [blame] | 2894 | * </pre> |
Felipe Leme | 1a1e468 | 2017-03-13 16:34:03 -0700 | [diff] [blame] | 2895 | */ |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 2896 | @Override |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 2897 | public void onProvideAutofillVirtualStructure(ViewStructure structure, int flags) { |
| 2898 | mProvider.getViewDelegate().onProvideAutofillVirtualStructure(structure, flags); |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 2899 | } |
| 2900 | |
Tao Bai | a571733 | 2017-03-30 14:58:53 -0700 | [diff] [blame] | 2901 | @Override |
| 2902 | public void autofill(SparseArray<AutofillValue>values) { |
| 2903 | mProvider.getViewDelegate().autofill(values); |
| 2904 | } |
| 2905 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 2906 | /** @hide */ |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2907 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 2908 | public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 2909 | super.onInitializeAccessibilityNodeInfoInternal(info); |
Marcin Kosiba | 3e853fb | 2015-01-08 12:45:40 +0000 | [diff] [blame] | 2910 | mProvider.getViewDelegate().onInitializeAccessibilityNodeInfo(info); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 2913 | /** @hide */ |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2914 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 2915 | public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) { |
| 2916 | super.onInitializeAccessibilityEventInternal(event); |
Marcin Kosiba | 3e853fb | 2015-01-08 12:45:40 +0000 | [diff] [blame] | 2917 | mProvider.getViewDelegate().onInitializeAccessibilityEvent(event); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 2920 | /** @hide */ |
alanv | 448902d | 2012-05-16 20:27:47 -0700 | [diff] [blame] | 2921 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 2922 | public boolean performAccessibilityActionInternal(int action, Bundle arguments) { |
Marcin Kosiba | 3e853fb | 2015-01-08 12:45:40 +0000 | [diff] [blame] | 2923 | return mProvider.getViewDelegate().performAccessibilityAction(action, arguments); |
alanv | 448902d | 2012-05-16 20:27:47 -0700 | [diff] [blame] | 2924 | } |
| 2925 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2926 | /** @hide */ |
| 2927 | @Override |
| 2928 | protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar, |
| 2929 | int l, int t, int r, int b) { |
| 2930 | mProvider.getViewDelegate().onDrawVerticalScrollBar(canvas, scrollBar, l, t, r, b); |
| 2931 | } |
| 2932 | |
| 2933 | @Override |
| 2934 | protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) { |
| 2935 | mProvider.getViewDelegate().onOverScrolled(scrollX, scrollY, clampedX, clampedY); |
| 2936 | } |
| 2937 | |
| 2938 | @Override |
| 2939 | protected void onWindowVisibilityChanged(int visibility) { |
| 2940 | super.onWindowVisibilityChanged(visibility); |
| 2941 | mProvider.getViewDelegate().onWindowVisibilityChanged(visibility); |
| 2942 | } |
| 2943 | |
| 2944 | @Override |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2945 | protected void onDraw(Canvas canvas) { |
| 2946 | mProvider.getViewDelegate().onDraw(canvas); |
| 2947 | } |
| 2948 | |
| 2949 | @Override |
| 2950 | public boolean performLongClick() { |
| 2951 | return mProvider.getViewDelegate().performLongClick(); |
| 2952 | } |
| 2953 | |
| 2954 | @Override |
| 2955 | protected void onConfigurationChanged(Configuration newConfig) { |
| 2956 | mProvider.getViewDelegate().onConfigurationChanged(newConfig); |
| 2957 | } |
| 2958 | |
Selim Gurun | c92080b | 2016-10-20 11:53:14 -0700 | [diff] [blame] | 2959 | /** |
| 2960 | * Creates a new InputConnection for an InputMethod to interact with the WebView. |
| 2961 | * This is similar to {@link View#onCreateInputConnection} but note that WebView |
| 2962 | * calls InputConnection methods on a thread other than the UI thread. |
| 2963 | * If these methods are overridden, then the overriding methods should respect |
| 2964 | * thread restrictions when calling View methods or accessing data. |
| 2965 | */ |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2966 | @Override |
| 2967 | public InputConnection onCreateInputConnection(EditorInfo outAttrs) { |
| 2968 | return mProvider.getViewDelegate().onCreateInputConnection(outAttrs); |
| 2969 | } |
| 2970 | |
| 2971 | @Override |
Hui Shu | ecdce96 | 2016-02-04 15:00:19 -0800 | [diff] [blame] | 2972 | public boolean onDragEvent(DragEvent event) { |
| 2973 | return mProvider.getViewDelegate().onDragEvent(event); |
| 2974 | } |
| 2975 | |
| 2976 | @Override |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2977 | protected void onVisibilityChanged(View changedView, int visibility) { |
| 2978 | super.onVisibilityChanged(changedView, visibility); |
Martin Kosiba | 5ee743e | 2012-12-21 12:39:50 +0000 | [diff] [blame] | 2979 | // This method may be called in the constructor chain, before the WebView provider is |
| 2980 | // created. |
| 2981 | ensureProviderCreated(); |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 2982 | mProvider.getViewDelegate().onVisibilityChanged(changedView, visibility); |
| 2983 | } |
| 2984 | |
| 2985 | @Override |
| 2986 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 2987 | mProvider.getViewDelegate().onWindowFocusChanged(hasWindowFocus); |
| 2988 | super.onWindowFocusChanged(hasWindowFocus); |
| 2989 | } |
| 2990 | |
| 2991 | @Override |
| 2992 | protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { |
| 2993 | mProvider.getViewDelegate().onFocusChanged(focused, direction, previouslyFocusedRect); |
| 2994 | super.onFocusChanged(focused, direction, previouslyFocusedRect); |
| 2995 | } |
| 2996 | |
| 2997 | /** @hide */ |
| 2998 | @Override |
| 2999 | protected boolean setFrame(int left, int top, int right, int bottom) { |
| 3000 | return mProvider.getViewDelegate().setFrame(left, top, right, bottom); |
| 3001 | } |
| 3002 | |
| 3003 | @Override |
| 3004 | protected void onSizeChanged(int w, int h, int ow, int oh) { |
| 3005 | super.onSizeChanged(w, h, ow, oh); |
| 3006 | mProvider.getViewDelegate().onSizeChanged(w, h, ow, oh); |
| 3007 | } |
| 3008 | |
| 3009 | @Override |
| 3010 | protected void onScrollChanged(int l, int t, int oldl, int oldt) { |
| 3011 | super.onScrollChanged(l, t, oldl, oldt); |
| 3012 | mProvider.getViewDelegate().onScrollChanged(l, t, oldl, oldt); |
| 3013 | } |
| 3014 | |
| 3015 | @Override |
| 3016 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 3017 | return mProvider.getViewDelegate().dispatchKeyEvent(event); |
| 3018 | } |
| 3019 | |
| 3020 | @Override |
| 3021 | public boolean requestFocus(int direction, Rect previouslyFocusedRect) { |
| 3022 | return mProvider.getViewDelegate().requestFocus(direction, previouslyFocusedRect); |
| 3023 | } |
| 3024 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 3025 | @Override |
| 3026 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 3027 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 3028 | mProvider.getViewDelegate().onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 3029 | } |
| 3030 | |
| 3031 | @Override |
| 3032 | public boolean requestChildRectangleOnScreen(View child, Rect rect, boolean immediate) { |
| 3033 | return mProvider.getViewDelegate().requestChildRectangleOnScreen(child, rect, immediate); |
| 3034 | } |
| 3035 | |
| 3036 | @Override |
| 3037 | public void setBackgroundColor(int color) { |
| 3038 | mProvider.getViewDelegate().setBackgroundColor(color); |
| 3039 | } |
John Reck | a5408e6 | 2012-03-16 14:18:44 -0700 | [diff] [blame] | 3040 | |
| 3041 | @Override |
| 3042 | public void setLayerType(int layerType, Paint paint) { |
| 3043 | super.setLayerType(layerType, paint); |
| 3044 | mProvider.getViewDelegate().setLayerType(layerType, paint); |
| 3045 | } |
Ben Murdoch | e623e24 | 2012-09-07 20:47:07 +0100 | [diff] [blame] | 3046 | |
| 3047 | @Override |
| 3048 | protected void dispatchDraw(Canvas canvas) { |
| 3049 | mProvider.getViewDelegate().preDispatchDraw(canvas); |
| 3050 | super.dispatchDraw(canvas); |
| 3051 | } |
Ben Murdoch | 7302f4f | 2014-07-03 14:47:44 +0100 | [diff] [blame] | 3052 | |
| 3053 | @Override |
| 3054 | public void onStartTemporaryDetach() { |
| 3055 | super.onStartTemporaryDetach(); |
| 3056 | mProvider.getViewDelegate().onStartTemporaryDetach(); |
| 3057 | } |
| 3058 | |
| 3059 | @Override |
| 3060 | public void onFinishTemporaryDetach() { |
| 3061 | super.onFinishTemporaryDetach(); |
| 3062 | mProvider.getViewDelegate().onFinishTemporaryDetach(); |
| 3063 | } |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 3064 | |
Selim Gurun | e319dad | 2016-03-17 01:40:40 +0000 | [diff] [blame] | 3065 | @Override |
| 3066 | public Handler getHandler() { |
| 3067 | return mProvider.getViewDelegate().getHandler(super.getHandler()); |
| 3068 | } |
| 3069 | |
| 3070 | @Override |
| 3071 | public View findFocus() { |
| 3072 | return mProvider.getViewDelegate().findFocus(super.findFocus()); |
| 3073 | } |
| 3074 | |
Hui Shu | f119c52 | 2015-10-08 10:07:13 -0700 | [diff] [blame] | 3075 | /** |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 3076 | * If WebView has already been loaded into the current process this method will return the |
| 3077 | * package that was used to load it. Otherwise, the package that would be used if the WebView |
| 3078 | * was loaded right now will be returned; this does not cause WebView to be loaded, so this |
| 3079 | * information may become outdated at any time. |
Gustav Sennton | 4cbfe71 | 2017-03-07 14:22:01 +0000 | [diff] [blame] | 3080 | * The WebView package changes either when the current WebView package is updated, disabled, or |
| 3081 | * uninstalled. It can also be changed through a Developer Setting. |
| 3082 | * If the WebView package changes, any app process that has loaded WebView will be killed. The |
| 3083 | * next time the app starts and loads WebView it will use the new WebView package instead. |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 3084 | * @return the current WebView package, or {@code null} if there is none. |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 3085 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 3086 | @Nullable |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 3087 | public static PackageInfo getCurrentWebViewPackage() { |
| 3088 | PackageInfo webviewPackage = WebViewFactory.getLoadedPackageInfo(); |
| 3089 | if (webviewPackage != null) { |
| 3090 | return webviewPackage; |
| 3091 | } |
| 3092 | |
Torne (Richard Coles) | ff937ac | 2017-10-02 17:09:32 -0400 | [diff] [blame] | 3093 | IWebViewUpdateService service = WebViewFactory.getUpdateService(); |
| 3094 | if (service == null) { |
| 3095 | return null; |
| 3096 | } |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 3097 | try { |
Torne (Richard Coles) | ff937ac | 2017-10-02 17:09:32 -0400 | [diff] [blame] | 3098 | return service.getCurrentWebViewPackage(); |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 3099 | } catch (RemoteException e) { |
| 3100 | throw e.rethrowFromSystemServer(); |
| 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | /** |
Hui Shu | f119c52 | 2015-10-08 10:07:13 -0700 | [diff] [blame] | 3105 | * Receive the result from a previous call to {@link #startActivityForResult(Intent, int)}. |
| 3106 | * |
| 3107 | * @param requestCode The integer request code originally supplied to |
| 3108 | * startActivityForResult(), allowing you to identify who this |
| 3109 | * result came from. |
| 3110 | * @param resultCode The integer result code returned by the child activity |
| 3111 | * through its setResult(). |
| 3112 | * @param data An Intent, which can return result data to the caller |
| 3113 | * (various data can be attached to Intent "extras"). |
| 3114 | * @hide |
| 3115 | */ |
| 3116 | @Override |
| 3117 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 3118 | mProvider.getViewDelegate().onActivityResult(requestCode, resultCode, data); |
| 3119 | } |
| 3120 | |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 3121 | /** @hide */ |
| 3122 | @Override |
| 3123 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 3124 | super.encodeProperties(encoder); |
| 3125 | |
| 3126 | checkThread(); |
| 3127 | encoder.addProperty("webview:contentHeight", mProvider.getContentHeight()); |
| 3128 | encoder.addProperty("webview:contentWidth", mProvider.getContentWidth()); |
| 3129 | encoder.addProperty("webview:scale", mProvider.getScale()); |
| 3130 | encoder.addProperty("webview:title", mProvider.getTitle()); |
| 3131 | encoder.addProperty("webview:url", mProvider.getUrl()); |
| 3132 | encoder.addProperty("webview:originalUrl", mProvider.getOriginalUrl()); |
| 3133 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3134 | } |