blob: d7f1d6e3b2d182f85d9689d6777952fa9b07e467 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.webkit;
18
Tobias Sargeantd10e4af2017-01-19 14:03:09 +000019import android.annotation.IntDef;
Siva Velusamy94a6d152015-05-05 15:07:00 -070020import android.annotation.NonNull;
Tima Vaisburdcf49a232017-03-28 11:35:58 -070021import android.annotation.Nullable;
Ignacio Solla451e3382014-11-10 10:35:54 +000022import android.annotation.SystemApi;
Raphael30df2372010-03-06 10:09:54 -080023import android.annotation.Widget;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
Hui Shuf119c522015-10-08 10:07:13 -070025import android.content.Intent;
Gustav Senntonbf683e02016-09-15 14:42:50 +010026import android.content.pm.PackageInfo;
Cary Clarkc5cd5e92010-11-22 15:20:02 -050027import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.Bitmap;
29import android.graphics.Canvas;
John Recka5408e62012-03-16 14:18:44 -070030import android.graphics.Paint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.graphics.Picture;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.graphics.Rect;
Mike Reede8853fc2009-09-04 14:01:48 -040033import android.graphics.drawable.Drawable;
Selim Gurun4c8093a2015-03-10 17:40:06 -070034import android.net.Uri;
Nate Fischer0e72c182017-05-30 11:25:17 -070035import android.net.http.SslCertificate;
Kristian Monsenb5cd8c02013-04-09 17:57:37 -070036import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.Bundle;
Selim Gurune319dad2016-03-17 01:40:40 +000038import android.os.Handler;
Steve Block08d584c2011-05-17 19:05:03 +010039import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.Message;
Gustav Senntonbf683e02016-09-15 14:42:50 +010041import android.os.RemoteException;
Nate Fischer0e72c182017-05-30 11:25:17 -070042import android.os.StrictMode;
Selim Gurunda7d6422013-09-06 14:39:05 -070043import android.print.PrintDocumentAdapter;
Selim Gurun87a03512014-05-02 14:52:49 -070044import android.security.KeyChain;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.util.Log;
Tao Baia5717332017-03-30 14:58:53 -070047import android.util.SparseArray;
Hui Shuecdce962016-02-04 15:00:19 -080048import android.view.DragEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.view.KeyEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.view.MotionEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.view.View;
John Reck926cf562012-06-14 10:00:31 -070052import android.view.ViewDebug;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.view.ViewGroup;
Siva Velusamy94a6d152015-05-05 15:07:00 -070054import android.view.ViewHierarchyEncoder;
Nate Fischer0e72c182017-05-30 11:25:17 -070055import android.view.ViewStructure;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.view.ViewTreeObserver;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -070057import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -070058import android.view.accessibility.AccessibilityNodeInfo;
Ben Murdoche3f90712013-06-05 14:19:48 +010059import android.view.accessibility.AccessibilityNodeProvider;
Tao Baia5717332017-03-30 14:58:53 -070060import android.view.autofill.AutofillValue;
Derek Sollenberger7cabb032010-01-21 10:37:38 -050061import android.view.inputmethod.EditorInfo;
62import android.view.inputmethod.InputConnection;
Tima Vaisburdcf49a232017-03-28 11:35:58 -070063import android.view.textclassifier.TextClassifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.widget.AbsoluteLayout;
Steve Blockf95d4902011-06-09 11:53:26 +010065
John Reck926cf562012-06-14 10:00:31 -070066import java.io.BufferedWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import java.io.File;
Tobias Sargeantd10e4af2017-01-19 14:03:09 +000068import java.lang.annotation.Retention;
69import java.lang.annotation.RetentionPolicy;
Nate Fischer520a8ba2017-07-12 17:37:07 -070070import java.util.List;
Andrei Popescu4950b2b2009-09-03 13:56:07 +010071import java.util.Map;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
73/**
Cary Clarkd6982c92009-05-29 11:02:22 -040074 * <p>A View that displays web pages. This class is the basis upon which you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 * 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 Fischerf02f8462017-09-11 15:16:33 -070078 * through a history, zoom in and out, perform text searches and more.
79 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 * <p>Note that, in order for your Activity to access the Internet and load web pages
Scott Main8b3cea02010-05-14 14:12:43 -070081 * in a WebView, you must add the {@code INTERNET} permissions to your
Nate Fischerf02f8462017-09-11 15:16:33 -070082 * Android Manifest file:
83 *
84 * <pre>
85 * {@code <uses-permission android:name="android.permission.INTERNET" />}
86 * </pre>
Mike Hearnadcd2ed2009-01-21 16:44:36 +010087 *
Scott Main8b3cea02010-05-14 14:12:43 -070088 * <p>This must be a child of the <a
Ben Dodson4e8620f2010-08-25 10:55:47 -070089 * href="{@docRoot}guide/topics/manifest/manifest-element.html">{@code <manifest>}</a>
Nate Fischerf02f8462017-09-11 15:16:33 -070090 * element.
Mike Hearnadcd2ed2009-01-21 16:44:36 +010091 *
Scott Main4c359b72012-07-24 15:51:27 -070092 * <p>For more information, read
Nate Fischerf02f8462017-09-11 15:16:33 -070093 * <a href="{@docRoot}guide/webapps/webview.html">Building Web Apps in WebView</a>.
Scott Main41ec6532010-08-19 16:57:07 -070094 *
Mike Hearnadcd2ed2009-01-21 16:44:36 +010095 * <h3>Basic usage</h3>
96 *
97 * <p>By default, a WebView provides no browser-like widgets, does not
Scott Main8b3cea02010-05-14 14:12:43 -070098 * enable JavaScript and web page errors are ignored. If your goal is only
Mike Hearnadcd2ed2009-01-21 16:44:36 +010099 * 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 Main8b3cea02010-05-14 14:12:43 -0700102 * 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 Solovay51142572017-05-05 16:55:43 -0700106 * Uri uri = Uri.parse("https://www.example.com");
Scott Main8b3cea02010-05-14 14:12:43 -0700107 * Intent intent = new Intent(Intent.ACTION_VIEW, uri);
108 * startActivity(intent);
109 * </pre>
Nate Fischerf02f8462017-09-11 15:16:33 -0700110 * <p>See {@link android.content.Intent} for more information.
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100111 *
Neil Fuller71fbb812015-11-30 09:51:33 +0000112 * <p>To provide a WebView in your own Activity, include a {@code <WebView>} in your layout,
Scott Main8b3cea02010-05-14 14:12:43 -0700113 * or set the entire Activity window as a WebView during {@link
Nate Fischerf02f8462017-09-11 15:16:33 -0700114 * android.app.Activity#onCreate(Bundle) onCreate()}:
115 *
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100116 * <pre class="prettyprint">
117 * WebView webview = new WebView(this);
118 * setContentView(webview);
Scott Main8b3cea02010-05-14 14:12:43 -0700119 * </pre>
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100120 *
Nate Fischerf02f8462017-09-11 15:16:33 -0700121 * <p>Then load the desired web page:
122 *
Scott Maine3b9f8b2010-05-18 08:41:36 -0700123 * <pre>
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100124 * // Simplest usage: note that an exception will NOT be thrown
125 * // if there is an error loading this page (see below).
Andrew Solovay51142572017-05-05 16:55:43 -0700126 * webview.loadUrl("https://example.com/");
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100127 *
Scott Main8b3cea02010-05-14 14:12:43 -0700128 * // OR, you can also load from an HTML string:
Scott Maine3b9f8b2010-05-18 08:41:36 -0700129 * String summary = "&lt;html>&lt;body>You scored &lt;b>192&lt;/b> points.&lt;/body>&lt;/html>";
Steve Blockf95d4902011-06-09 11:53:26 +0100130 * webview.loadData(summary, "text/html", null);
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100131 * // ... although note that there are restrictions on what this HTML can do.
Nate Fischer867d1092018-01-04 21:57:33 -0800132 * // 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 Hearnadcd2ed2009-01-21 16:44:36 +0100136 * </pre>
137 *
138 * <p>A WebView has several customization points where you can add your
Nate Fischerf02f8462017-09-11 15:16:33 -0700139 * own behavior. These are:
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100140 *
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 Main8b3cea02010-05-14 14:12:43 -0700145 * JavaScript alerts are sent here (see <a
146 * href="{@docRoot}guide/developing/debug-tasks.html#DebuggingWebPages">Debugging Tasks</a>).
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100147 * </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 Main8b3cea02010-05-14 14:12:43 -0700151 * 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 Block4cd73c52011-11-09 13:06:52 +0000157 * <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 Hearnadcd2ed2009-01-21 16:44:36 +0100161 * </ul>
162 *
163 * <p>Here's a more complicated example, showing error handling,
Nate Fischerf02f8462017-09-11 15:16:33 -0700164 * settings, and progress notification:
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100165 *
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 Solovay51142572017-05-05 16:55:43 -0700187 * webview.loadUrl("https://developer.android.com/");
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100188 * </pre>
189 *
Martin Kosiba0d147d92013-09-19 19:05:00 -0700190 * <h3>Zoom</h3>
191 *
192 * <p>To enable the built-in zoom, set
193 * {@link #getSettings() WebSettings}.{@link WebSettings#setBuiltInZoomControls(boolean)}
Nate Fischerf02f8462017-09-11 15:16:33 -0700194 * (introduced in API level {@link android.os.Build.VERSION_CODES#CUPCAKE}).
195 *
Nate Fischer7051dd12017-10-26 14:51:25 -0700196 * <p class="note"><b>Note:</b> Using zoom if either the height or width is set to
Martin Kosiba0d147d92013-09-19 19:05:00 -0700197 * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} may lead to undefined behavior
Nate Fischerf02f8462017-09-11 15:16:33 -0700198 * and should be avoided.
Martin Kosiba0d147d92013-09-19 19:05:00 -0700199 *
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100200 * <h3>Cookie and window management</h3>
201 *
202 * <p>For obvious security reasons, your application has its own
Scott Main8b3cea02010-05-14 14:12:43 -0700203 * cache, cookie store etc.&mdash;it does not share the Browser
Steve Blockc723e352012-08-14 14:48:05 +0100204 * application's data.
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100205 *
206 * <p>By default, requests by the HTML to open new windows are
Nate Fischer0a6140d2017-09-05 12:37:49 -0700207 * ignored. This is {@code true} whether they be opened by JavaScript or by
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100208 * the target attribute on a link. You can customize your
Nate Fischerffb81c42016-09-26 12:35:47 -0700209 * {@link WebChromeClient} to provide your own behavior for opening multiple windows,
Nate Fischerf02f8462017-09-11 15:16:33 -0700210 * and render them in whatever manner you want.
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100211 *
Scott Main8b3cea02010-05-14 14:12:43 -0700212 * <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 Hearnadcd2ed2009-01-21 16:44:36 +0100214 * the WebView to reload the current page. If you don't want that, you
Scott Main8b3cea02010-05-14 14:12:43 -0700215 * can set your Activity to handle the {@code orientation} and {@code keyboardHidden}
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100216 * changes, and then just leave the WebView alone. It'll automatically
Scott Main8b3cea02010-05-14 14:12:43 -0700217 * re-orient itself as appropriate. Read <a
218 * href="{@docRoot}guide/topics/resources/runtime-changes.html">Handling Runtime Changes</a> for
Nate Fischerf02f8462017-09-11 15:16:33 -0700219 * more information about how to handle configuration changes during runtime.
Scott Main8b3cea02010-05-14 14:12:43 -0700220 *
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 Fitzpatrick438d0592010-06-10 12:19:19 -0700226 * has more &mdash; sometimes significantly more &mdash; pixels per inch. The density of a
Scott Main8b3cea02010-05-14 14:12:43 -0700227 * 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 Fischerf02f8462017-09-11 15:16:33 -0700231 * high, medium, and low.
Scott Main8b3cea02010-05-14 14:12:43 -0700232 * <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 Blockb838aef2012-04-23 18:22:15 +0100236 * 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 Fischerf02f8462017-09-11 15:16:33 -0700238 * densities.
239 * <p>Here's a summary of the features you can use to handle different screen densities:
Scott Main8b3cea02010-05-14 14:12:43 -0700240 * <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 Dixon89f48e92013-02-27 16:42:48 -0800247 * scaled 0.75x.</li>
Scott Main8b3cea02010-05-14 14:12:43 -0700248 * <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 * &lt;link rel="stylesheet" media="screen and (-webkit-device-pixel-ratio:1.5)" href="hdpi.css" /&gt;</pre>
Andrew Solovay4ea753f2018-03-12 15:11:50 -0700254 * <p>The {@code hdpi.css} stylesheet is only used for devices with a screen pixel ratio of 1.5,
Nate Fischerf02f8462017-09-11 15:16:33 -0700255 * which is the high density pixel ratio.
Scott Main8b3cea02010-05-14 14:12:43 -0700256 * </li>
Martin Kosiba0d147d92013-09-19 19:05:00 -0700257 * </ul>
Scott Main8b3cea02010-05-14 14:12:43 -0700258 *
Teng-Hui Zhu50ba5a22012-01-11 15:57:21 -0800259 * <h3>HTML5 Video support</h3>
260 *
Ignacio Solla7a4e18f2014-12-29 12:21:52 +0000261 * <p>In order to support inline HTML5 video in your application you need to have hardware
262 * acceleration turned on.
Ignacio Solla7a4e18f2014-12-29 12:21:52 +0000263 *
264 * <h3>Full screen support</h3>
265 *
266 * <p>In order to support full screen &mdash; for video or other HTML content &mdash; 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 Kosiba84644f12014-09-08 18:37:36 +0100273 *
Tim Volodineb0e97982016-04-27 14:14:09 +0100274 * <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 Volodineb0e97982016-04-27 14:14:09 +0100282 *
Marcin Kosiba84644f12014-09-08 18:37:36 +0100283 * <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 Kosiba84644f12014-09-08 18:37:36 +0100292 *
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 Fischerffb81c42016-09-26 12:35:47 -0700298 * <li>For applications targeting {@link android.os.Build.VERSION_CODES#KITKAT} and earlier SDKs the
Marcin Kosiba84644f12014-09-08 18:37:36 +0100299 * HTML viewport meta tag will be ignored in order to preserve backwards compatibility. </li>
300 * </ul>
Marcin Kosiba84644f12014-09-08 18:37:36 +0100301 *
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 Kosiba84644f12014-09-08 18:37:36 +0100306 *
Paul Millera4692182016-01-07 11:32:15 -0800307 * <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 Fischer20635082017-11-08 13:39:08 -0800313 * tag in its manifest's {@code <application>} element:
Paul Millera4692182016-01-07 11:32:15 -0800314 * <pre>
Nate Fischer20635082017-11-08 13:39:08 -0800315 * &lt;manifest&gt;
316 * &lt;application&gt;
317 * ...
318 * &lt;meta-data android:name=&quot;android.webkit.WebView.MetricsOptOut&quot;
319 * android:value=&quot;true&quot; /&gt;
320 * &lt;/application&gt;
321 * &lt;/manifest&gt;
Paul Millera4692182016-01-07 11:32:15 -0800322 * </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 Millera4692182016-01-07 11:32:15 -0800326 *
Nate Fischer471891d2017-07-18 19:15:52 -0700327 * <h3>Safe Browsing</h3>
328 *
329 * <p>
Nate Fischerfb92ee12018-01-18 12:01:19 -0800330 * 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 Fischer471891d2017-07-18 19:15:52 -0700332 * <p>
Nate Fischerfb92ee12018-01-18 12:01:19 -0800333 * 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 Fischer471891d2017-07-18 19:15:52 -0700335 * <p>
336 * <pre>
Nate Fischer20635082017-11-08 13:39:08 -0800337 * &lt;manifest&gt;
338 * &lt;application&gt;
339 * ...
340 * &lt;meta-data android:name=&quot;android.webkit.WebView.EnableSafeBrowsing&quot;
Nate Fischerfb92ee12018-01-18 12:01:19 -0800341 * android:value=&quot;false&quot; /&gt;
Nate Fischer20635082017-11-08 13:39:08 -0800342 * &lt;/application&gt;
343 * &lt;/manifest&gt;
Nate Fischer471891d2017-07-18 19:15:52 -0700344 * </pre>
Nate Fischer471891d2017-07-18 19:15:52 -0700345 *
Nate Fischerfb92ee12018-01-18 12:01:19 -0800346 * <p>
347 * Otherwise, see {@link WebSettings#setSafeBrowsingEnabled}.
348 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 */
Steve Block406aeb22012-04-23 18:17:19 +0100350// 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.
Raphael30df2372010-03-06 10:09:54 -0800356@Widget
Cary Clarkd6982c92009-05-29 11:02:22 -0400357public class WebView extends AbsoluteLayout
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 implements ViewTreeObserver.OnGlobalFocusChangeListener,
John Reck926cf562012-06-14 10:00:31 -0700359 ViewGroup.OnHierarchyChangeListener, ViewDebug.HierarchyHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360
Jonathan Dixon69f9f932013-08-28 15:54:29 -0700361 private static final String LOGTAG = "WebView";
Nicolas Roard12c18e62010-10-13 20:14:31 -0700362
Kristian Monsenb5cd8c02013-04-09 17:57:37 -0700363 // 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 Dixon0d6a1452013-08-21 13:09:36 -0700366 private static volatile boolean sEnforceThreadChecking = false;
Kristian Monsenb5cd8c02013-04-09 17:57:37 -0700367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 /**
369 * Transportation object for returning WebView across thread boundaries.
370 */
371 public class WebViewTransport {
372 private WebView mWebview;
373
374 /**
Steve Block4e584df2012-04-24 23:12:47 +0100375 * Sets the WebView to the transportation object.
376 *
377 * @param webview the WebView to transport
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 */
379 public synchronized void setWebView(WebView webview) {
380 mWebview = webview;
381 }
382
383 /**
Steve Block4e584df2012-04-24 23:12:47 +0100384 * Gets the WebView object.
385 *
386 * @return the transported WebView object
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 */
388 public synchronized WebView getWebView() {
389 return mWebview;
390 }
391 }
392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 /**
Steve Block4e584df2012-04-24 23:12:47 +0100394 * URI scheme for telephone number.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 */
396 public static final String SCHEME_TEL = "tel:";
397 /**
Steve Block4e584df2012-04-24 23:12:47 +0100398 * URI scheme for email address.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 */
400 public static final String SCHEME_MAILTO = "mailto:";
401 /**
Steve Block4e584df2012-04-24 23:12:47 +0100402 * URI scheme for map address.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 */
404 public static final String SCHEME_GEO = "geo:0,0?q=";
Cary Clarkd6982c92009-05-29 11:02:22 -0400405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 /**
Victoria Leased405a432012-03-22 15:53:48 -0700407 * Interface to listen for find results.
Victoria Leased405a432012-03-22 15:53:48 -0700408 */
409 public interface FindListener {
410 /**
Steve Block4e584df2012-04-24 23:12:47 +0100411 * Notifies the listener about progress made by a find operation.
Victoria Leased405a432012-03-22 15:53:48 -0700412 *
Steve Block4e584df2012-04-24 23:12:47 +0100413 * @param activeMatchOrdinal the zero-based ordinal of the currently selected match
Selim Gurun92b81a32012-08-21 17:32:35 -0700414 * @param numberOfMatches how many matches have been found
Steve Block4e584df2012-04-24 23:12:47 +0100415 * @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 Fischer0a6140d2017-09-05 12:37:49 -0700419 * isDoneCounting is {@code true}.
Victoria Leased405a432012-03-22 15:53:48 -0700420 */
Selim Gurun92b81a32012-08-21 17:32:35 -0700421 public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
Victoria Leased405a432012-03-22 15:53:48 -0700422 boolean isDoneCounting);
423 }
424
425 /**
Tobias Sargeantb3656042015-05-13 11:23:57 +0100426 * Callback interface supplied to {@link #postVisualStateCallback} for receiving
Tobias Sargeanta8352f42015-03-03 18:07:40 +0000427 * 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 Sargeantb3656042015-05-13 11:23:57 +0100433 * @param requestId The identifier passed to {@link #postVisualStateCallback} when this
434 * callback was posted.
Tobias Sargeanta8352f42015-03-03 18:07:40 +0000435 */
436 public abstract void onComplete(long requestId);
437 }
438
439 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 * Interface to listen for new pictures as they change.
Steve Block4e584df2012-04-24 23:12:47 +0100441 *
Kristian Monsenfc771652011-05-10 16:44:05 +0100442 * @deprecated This interface is now obsolete.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 */
Kristian Monsenfc771652011-05-10 16:44:05 +0100444 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 public interface PictureListener {
446 /**
Steve Block72498ed2012-07-17 15:57:25 +0100447 * Used to provide notification that the WebView's picture has changed.
448 * See {@link WebView#capturePicture} for details of the picture.
Steve Block4e584df2012-04-24 23:12:47 +0100449 *
450 * @param view the WebView that owns the picture
Ben Murdoch52643e02013-02-26 12:01:00 +0000451 * @param picture the new picture. Applications targeting
452 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2} or above
Nate Fischer0a6140d2017-09-05 12:37:49 -0700453 * will always receive a {@code null} Picture.
Steve Block72498ed2012-07-17 15:57:25 +0100454 * @deprecated Deprecated due to internal changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 */
Kristian Monsenfc771652011-05-10 16:44:05 +0100456 @Deprecated
Nate Fischer3442c742017-09-08 17:02:00 -0700457 void onNewPicture(WebView view, @Nullable Picture picture);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 }
459
Jonathan Dixon19644b62011-12-21 14:21:36 +0000460 public static class HitTestResult {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 /**
Steve Block4e584df2012-04-24 23:12:47 +0100462 * Default HitTestResult, where the target is unknown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 */
464 public static final int UNKNOWN_TYPE = 0;
465 /**
Steve Block1854ddb2011-04-19 12:18:19 +0100466 * @deprecated This type is no longer used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 */
Steve Block1854ddb2011-04-19 12:18:19 +0100468 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 public static final int ANCHOR_TYPE = 1;
470 /**
Steve Block4e584df2012-04-24 23:12:47 +0100471 * HitTestResult for hitting a phone number.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 */
473 public static final int PHONE_TYPE = 2;
474 /**
Steve Block4e584df2012-04-24 23:12:47 +0100475 * HitTestResult for hitting a map address.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 */
477 public static final int GEO_TYPE = 3;
478 /**
Steve Block4e584df2012-04-24 23:12:47 +0100479 * HitTestResult for hitting an email address.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 */
481 public static final int EMAIL_TYPE = 4;
482 /**
Steve Block4e584df2012-04-24 23:12:47 +0100483 * HitTestResult for hitting an HTML::img tag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 */
485 public static final int IMAGE_TYPE = 5;
486 /**
Steve Block1854ddb2011-04-19 12:18:19 +0100487 * @deprecated This type is no longer used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 */
Steve Block1854ddb2011-04-19 12:18:19 +0100489 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 public static final int IMAGE_ANCHOR_TYPE = 6;
491 /**
Steve Block4e584df2012-04-24 23:12:47 +0100492 * HitTestResult for hitting a HTML::a tag with src=http.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 */
494 public static final int SRC_ANCHOR_TYPE = 7;
495 /**
Steve Block4e584df2012-04-24 23:12:47 +0100496 * HitTestResult for hitting a HTML::a tag with src=http + HTML::img.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 */
498 public static final int SRC_IMAGE_ANCHOR_TYPE = 8;
499 /**
Steve Block4e584df2012-04-24 23:12:47 +0100500 * HitTestResult for hitting an edit text area.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 */
502 public static final int EDIT_TEXT_TYPE = 9;
503
504 private int mType;
505 private String mExtra;
506
Jonathan Dixon3c909522012-02-28 18:45:06 +0000507 /**
508 * @hide Only for use by WebViewProvider implementations
509 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000510 @SystemApi
Jonathan Dixon3c909522012-02-28 18:45:06 +0000511 public HitTestResult() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 mType = UNKNOWN_TYPE;
513 }
514
Jonathan Dixon3c909522012-02-28 18:45:06 +0000515 /**
516 * @hide Only for use by WebViewProvider implementations
517 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000518 @SystemApi
Jonathan Dixon3c909522012-02-28 18:45:06 +0000519 public void setType(int type) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 mType = type;
521 }
522
Jonathan Dixon3c909522012-02-28 18:45:06 +0000523 /**
524 * @hide Only for use by WebViewProvider implementations
525 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000526 @SystemApi
Jonathan Dixon3c909522012-02-28 18:45:06 +0000527 public void setExtra(String extra) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 mExtra = extra;
529 }
530
Jonathan Dixone230e542011-12-21 10:57:00 +0000531 /**
Steve Block4e584df2012-04-24 23:12:47 +0100532 * 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 Dixone230e542011-12-21 10:57:00 +0000536 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 public int getType() {
538 return mType;
539 }
540
Jonathan Dixone230e542011-12-21 10:57:00 +0000541 /**
Steve Block4e584df2012-04-24 23:12:47 +0100542 * Gets additional type-dependant information about the result. See
Nate Fischer0a6140d2017-09-05 12:37:49 -0700543 * {@link WebView#getHitTestResult()} for details. May either be {@code null}
Steve Block4e584df2012-04-24 23:12:47 +0100544 * or contain extra information about this result.
545 *
546 * @return additional type-dependant information about the result
Jonathan Dixone230e542011-12-21 10:57:00 +0000547 */
Nate Fischer3442c742017-09-08 17:02:00 -0700548 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 public String getExtra() {
550 return mExtra;
551 }
552 }
553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 /**
Nate Fischer674561a2017-10-26 12:28:01 -0700555 * Constructs a new WebView with an Activity Context object.
Steve Block4e584df2012-04-24 23:12:47 +0100556 *
Nate Fischer674561a2017-10-26 12:28:01 -0700557 * <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 Project9066cfe2009-03-03 19:31:44 -0800562 */
563 public WebView(Context context) {
564 this(context, null);
565 }
566
567 /**
Steve Block4e584df2012-04-24 23:12:47 +0100568 * Constructs a new WebView with layout parameters.
569 *
Nate Fischer674561a2017-10-26 12:28:01 -0700570 * @param context an Activity Context to access application assets
Steve Block4e584df2012-04-24 23:12:47 +0100571 * @param attrs an AttributeSet passed to our parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 */
573 public WebView(Context context, AttributeSet attrs) {
574 this(context, attrs, com.android.internal.R.attr.webViewStyle);
575 }
576
577 /**
Steve Block4e584df2012-04-24 23:12:47 +0100578 * Constructs a new WebView with layout parameters and a default style.
579 *
Nate Fischer674561a2017-10-26 12:28:01 -0700580 * @param context an Activity Context to access application assets
Steve Block4e584df2012-04-24 23:12:47 +0100581 * @param attrs an AttributeSet passed to our parent
Alan Viverette617feb92013-09-09 18:09:13 -0700582 * @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 Project9066cfe2009-03-03 19:31:44 -0800585 */
Alan Viverette617feb92013-09-09 18:09:13 -0700586 public WebView(Context context, AttributeSet attrs, int defStyleAttr) {
587 this(context, attrs, defStyleAttr, 0);
Elliott Slaughterf21d2e32010-07-14 18:08:54 -0700588 }
589
590 /**
Steve Block4e584df2012-04-24 23:12:47 +0100591 * Constructs a new WebView with layout parameters and a default style.
592 *
Nate Fischer674561a2017-10-26 12:28:01 -0700593 * @param context an Activity Context to access application assets
Steve Block4e584df2012-04-24 23:12:47 +0100594 * @param attrs an AttributeSet passed to our parent
Alan Viverette617feb92013-09-09 18:09:13 -0700595 * @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 Fischer674561a2017-10-26 12:28:01 -0700610 * @param context an Activity Context to access application assets
Alan Viverette617feb92013-09-09 18:09:13 -0700611 * @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 Block4e584df2012-04-24 23:12:47 +0100615 * @param privateBrowsing whether this WebView will be initialized in
616 * private mode
Kristian Monsen5cc23512012-08-09 15:33:08 -0400617 *
Selim Gurunb6aa97e2014-03-26 14:10:28 -0700618 * @deprecated Private browsing is no longer supported directly via
Kristian Monsen5cc23512012-08-09 15:33:08 -0400619 * 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 Slaughterf21d2e32010-07-14 18:08:54 -0700622 */
Kristian Monsen5cc23512012-08-09 15:33:08 -0400623 @Deprecated
Alan Viverette617feb92013-09-09 18:09:13 -0700624 public WebView(Context context, AttributeSet attrs, int defStyleAttr,
Elliott Slaughterf21d2e32010-07-14 18:08:54 -0700625 boolean privateBrowsing) {
Alan Viverette617feb92013-09-09 18:09:13 -0700626 this(context, attrs, defStyleAttr, 0, null, privateBrowsing);
Andrei Popescu4950b2b2009-09-03 13:56:07 +0100627 }
628
629 /**
Steve Block4e584df2012-04-24 23:12:47 +0100630 * Constructs a new WebView with layout parameters, a default style and a set
Nate Fischerffb81c42016-09-26 12:35:47 -0700631 * of custom JavaScript interfaces to be added to this WebView at initialization
Romain Guy01d0fbf2009-12-01 14:52:19 -0800632 * time. This guarantees that these interfaces will be available when the JS
Andrei Popescu4950b2b2009-09-03 13:56:07 +0100633 * context is initialized.
Steve Block4e584df2012-04-24 23:12:47 +0100634 *
Nate Fischer674561a2017-10-26 12:28:01 -0700635 * @param context an Activity Context to access application assets
Steve Block4e584df2012-04-24 23:12:47 +0100636 * @param attrs an AttributeSet passed to our parent
Alan Viverette617feb92013-09-09 18:09:13 -0700637 * @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 Block4e584df2012-04-24 23:12:47 +0100640 * @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 Fischerffb81c42016-09-26 12:35:47 -0700645 * @hide This is used internally by dumprendertree, as it requires the JavaScript interfaces to
Steve Block4e584df2012-04-24 23:12:47 +0100646 * be added synchronously, before a subsequent loadUrl call takes effect.
Andrei Popescu4950b2b2009-09-03 13:56:07 +0100647 */
Alan Viverette617feb92013-09-09 18:09:13 -0700648 protected WebView(Context context, AttributeSet attrs, int defStyleAttr,
Steve Block81f19ff2010-11-01 13:23:24 +0000649 Map<String, Object> javaScriptInterfaces, boolean privateBrowsing) {
Alan Viverette617feb92013-09-09 18:09:13 -0700650 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 Lemed04a6972017-03-02 12:56:18 -0800660
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 Monsen87af7312011-09-09 02:12:51 +0100666 if (context == null) {
667 throw new IllegalArgumentException("Invalid context argument");
668 }
Gustav Sennton01673692018-01-19 12:11:25 +0000669 if (mWebViewThread == null) {
670 throw new RuntimeException(
671 "WebView cannot be initialized on a thread that has no Looper.");
672 }
Kristian Monsenb5cd8c02013-04-09 17:57:37 -0700673 sEnforceThreadChecking = context.getApplicationInfo().targetSdkVersion >=
674 Build.VERSION_CODES.JELLY_BEAN_MR2;
Jonathan Dixon3c909522012-02-28 18:45:06 +0000675 checkThread();
Kristian Monsen87af7312011-09-09 02:12:51 +0100676
Jonathan Dixon3c909522012-02-28 18:45:06 +0000677 ensureProviderCreated();
678 mProvider.init(javaScriptInterfaces, privateBrowsing);
Jonathan Dixoneb9e8012013-10-03 11:03:31 +0100679 // Post condition of creating a webview is the CookieSyncManager.getInstance() is allowed.
680 CookieSyncManager.setGetInstanceIsAllowed();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
682
683 /**
Steve Block4e584df2012-04-24 23:12:47 +0100684 * Specifies whether the horizontal scrollbar has overlay style.
685 *
Paul Millerc8694a02015-06-05 11:23:03 -0700686 * @deprecated This method has no effect.
Nate Fischer0a6140d2017-09-05 12:37:49 -0700687 * @param overlay {@code true} if horizontal scrollbar should have overlay style
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 */
Paul Millerc8694a02015-06-05 11:23:03 -0700689 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 public void setHorizontalScrollbarOverlay(boolean overlay) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 }
692
693 /**
Steve Block4e584df2012-04-24 23:12:47 +0100694 * Specifies whether the vertical scrollbar has overlay style.
695 *
Paul Millerc8694a02015-06-05 11:23:03 -0700696 * @deprecated This method has no effect.
Nate Fischer0a6140d2017-09-05 12:37:49 -0700697 * @param overlay {@code true} if vertical scrollbar should have overlay style
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 */
Paul Millerc8694a02015-06-05 11:23:03 -0700699 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 public void setVerticalScrollbarOverlay(boolean overlay) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 }
702
703 /**
Steve Block4e584df2012-04-24 23:12:47 +0100704 * Gets whether horizontal scrollbar has overlay style.
705 *
Paul Millerc8694a02015-06-05 11:23:03 -0700706 * @deprecated This method is now obsolete.
Nate Fischer0a6140d2017-09-05 12:37:49 -0700707 * @return {@code true}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 */
Paul Millerc8694a02015-06-05 11:23:03 -0700709 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 public boolean overlayHorizontalScrollbar() {
Paul Millerc8694a02015-06-05 11:23:03 -0700711 // The old implementation defaulted to true, so return true for consistency
712 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
714
715 /**
Steve Block4e584df2012-04-24 23:12:47 +0100716 * Gets whether vertical scrollbar has overlay style.
717 *
Paul Millerc8694a02015-06-05 11:23:03 -0700718 * @deprecated This method is now obsolete.
Nate Fischer0a6140d2017-09-05 12:37:49 -0700719 * @return {@code false}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 */
Paul Millerc8694a02015-06-05 11:23:03 -0700721 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 public boolean overlayVerticalScrollbar() {
Paul Millerc8694a02015-06-05 11:23:03 -0700723 // The old implementation defaulted to false, so return false for consistency
724 return false;
Mike Reede8853fc2009-09-04 14:01:48 -0400725 }
726
Michael Kolbe54f6652011-03-16 09:11:51 -0700727 /**
Steve Block4e584df2012-04-24 23:12:47 +0100728 * Gets the visible height (in pixels) of the embedded title bar (if any).
Michael Kolb73980a92010-08-05 16:32:51 -0700729 *
Michael Kolb24e53b02011-03-16 12:52:04 -0700730 * @deprecated This method is now obsolete.
Kristian Monsenf4912582012-08-16 15:26:24 -0400731 * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
Mike Reede8853fc2009-09-04 14:01:48 -0400732 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700733 @Deprecated
Michael Kolb73980a92010-08-05 16:32:51 -0700734 public int getVisibleTitleHeight() {
Steve Block51b08912011-04-27 15:04:48 +0100735 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000736 return mProvider.getVisibleTitleHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 }
738
739 /**
Nate Fischer0a6140d2017-09-05 12:37:49 -0700740 * Gets the SSL certificate for the main top-level page or {@code null} if there is
Steve Block4e584df2012-04-24 23:12:47 +0100741 * no certificate (the site is not secure).
742 *
743 * @return the SSL certificate for the main top-level page
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 */
Nate Fischer3442c742017-09-08 17:02:00 -0700745 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 public SslCertificate getCertificate() {
Steve Block51b08912011-04-27 15:04:48 +0100747 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000748 return mProvider.getCertificate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 }
750
751 /**
752 * Sets the SSL certificate for the main top-level page.
Kristian Monsen5cc23512012-08-09 15:33:08 -0400753 *
754 * @deprecated Calling this function has no useful effect, and will be
755 * ignored in future releases.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 */
Kristian Monsen5cc23512012-08-09 15:33:08 -0400757 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 public void setCertificate(SslCertificate certificate) {
Steve Block51b08912011-04-27 15:04:48 +0100759 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000760 mProvider.setCertificate(certificate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 }
762
763 //-------------------------------------------------------------------------
764 // Methods called by activity
765 //-------------------------------------------------------------------------
766
767 /**
Steve Block32fe4102012-07-17 16:29:11 +0100768 * Sets a username and password pair for the specified host. This data is
Nate Fischerffb81c42016-09-26 12:35:47 -0700769 * used by the WebView to autocomplete username and password fields in web
Steve Block32fe4102012-07-17 16:29:11 +0100770 * forms. Note that this is unrelated to the credentials used for HTTP
771 * authentication.
Steve Block4e584df2012-04-24 23:12:47 +0100772 *
Selim Gurun38915fd2013-04-04 17:14:29 +0000773 * @param host the host that required the credentials
Steve Block4e584df2012-04-24 23:12:47 +0100774 * @param username the username for the given host
775 * @param password the password for the given host
Steve Block32fe4102012-07-17 16:29:11 +0100776 * @see WebViewDatabase#clearUsernamePassword
777 * @see WebViewDatabase#hasUsernamePassword
Jonathan Dixon835b1fc2013-02-25 12:29:33 -0800778 * @deprecated Saving passwords in WebView will not be supported in future versions.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 */
Jonathan Dixon835b1fc2013-02-25 12:29:33 -0800780 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 public void savePassword(String host, String username, String password) {
Steve Block51b08912011-04-27 15:04:48 +0100782 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000783 mProvider.savePassword(host, username, password);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 }
785
786 /**
Hui Shu433fb932016-08-29 11:49:46 -0700787 * Stores HTTP authentication credentials for a given host and realm to the {@link WebViewDatabase}
788 * instance.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 *
Steve Block46ce1db2012-07-17 16:43:00 +0100790 * @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 Block4e584df2012-04-24 23:12:47 +0100793 * @param password the password
Nate Fischerc7edfb02016-09-23 15:59:21 -0700794 * @deprecated Use {@link WebViewDatabase#setHttpAuthUsernamePassword} instead
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 */
Nate Fischerc7edfb02016-09-23 15:59:21 -0700796 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 public void setHttpAuthUsernamePassword(String host, String realm,
798 String username, String password) {
Steve Block51b08912011-04-27 15:04:48 +0100799 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000800 mProvider.setHttpAuthUsernamePassword(host, realm, username, password);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 }
802
803 /**
Hui Shu433fb932016-08-29 11:49:46 -0700804 * Retrieves HTTP authentication credentials for a given host and realm from the {@link
805 * WebViewDatabase} instance.
Steve Block46ce1db2012-07-17 16:43:00 +0100806 * @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 Fischer0a6140d2017-09-05 12:37:49 -0700809 * is the username and the second element is the password. {@code null} if
Steve Block46ce1db2012-07-17 16:43:00 +0100810 * no credentials are found.
Nate Fischerc7edfb02016-09-23 15:59:21 -0700811 * @deprecated Use {@link WebViewDatabase#getHttpAuthUsernamePassword} instead
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 */
Nate Fischerc7edfb02016-09-23 15:59:21 -0700813 @Deprecated
Nate Fischer3442c742017-09-08 17:02:00 -0700814 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 public String[] getHttpAuthUsernamePassword(String host, String realm) {
Steve Block51b08912011-04-27 15:04:48 +0100816 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000817 return mProvider.getHttpAuthUsernamePassword(host, realm);
Leon Scroggins05919f22010-09-14 17:22:36 -0400818 }
819
820 /**
Steve Block4e584df2012-04-24 23:12:47 +0100821 * 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 Project9066cfe2009-03-03 19:31:44 -0800824 */
825 public void destroy() {
Steve Block51b08912011-04-27 15:04:48 +0100826 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000827 mProvider.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 }
829
830 /**
831 * Enables platform notifications of data state and proxy changes.
Kristian Monsencbb59db2011-05-09 16:04:34 +0100832 * Notifications are enabled by default.
Kristian Monsenfc771652011-05-10 16:44:05 +0100833 *
834 * @deprecated This method is now obsolete.
Kristian Monsenf4912582012-08-16 15:26:24 -0400835 * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 */
Kristian Monsenfc771652011-05-10 16:44:05 +0100837 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 public static void enablePlatformNotifications() {
Kristian Monsen10ca8082014-04-29 11:02:42 -0700839 // noop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 }
841
842 /**
Kristian Monsencbb59db2011-05-09 16:04:34 +0100843 * Disables platform notifications of data state and proxy changes.
844 * Notifications are enabled by default.
Kristian Monsenfc771652011-05-10 16:44:05 +0100845 *
846 * @deprecated This method is now obsolete.
Kristian Monsenf4912582012-08-16 15:26:24 -0400847 * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 */
Kristian Monsenfc771652011-05-10 16:44:05 +0100849 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 public static void disablePlatformNotifications() {
Kristian Monsen10ca8082014-04-29 11:02:42 -0700851 // noop
Feng Qianb3081372009-06-29 15:55:18 -0700852 }
853
854 /**
Primiano Tucci24426752013-09-05 12:01:51 +0100855 * Used only by internal tests to free up memory.
856 *
857 * @hide
858 */
859 public static void freeMemoryForTests() {
Primiano Tucci24426752013-09-05 12:01:51 +0100860 getFactory().getStatics().freeMemoryForTests();
861 }
862
863 /**
Steve Block4e584df2012-04-24 23:12:47 +0100864 * Informs WebView of the network state. This is used to set
Steve Block81f19ff2010-11-01 13:23:24 +0000865 * the JavaScript property window.navigator.isOnline and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 * generates the online/offline event as specified in HTML5, sec. 5.7.7
Steve Block4e584df2012-04-24 23:12:47 +0100867 *
868 * @param networkUp a boolean indicating if network is available
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 */
870 public void setNetworkAvailable(boolean networkUp) {
Steve Block51b08912011-04-27 15:04:48 +0100871 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000872 mProvider.setNetworkAvailable(networkUp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 }
874
875 /**
Steve Block4e584df2012-04-24 23:12:47 +0100876 * Saves the state of this WebView used in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 * {@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 Monsenf4912582012-08-16 15:26:24 -0400880 * called.
Steve Block4e584df2012-04-24 23:12:47 +0100881 *
882 * @param outState the Bundle to store this WebView's state
Nate Fischer3442c742017-09-08 17:02:00 -0700883 * @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 Project9066cfe2009-03-03 19:31:44 -0800885 */
Nate Fischer3442c742017-09-08 17:02:00 -0700886 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 public WebBackForwardList saveState(Bundle outState) {
Steve Block51b08912011-04-27 15:04:48 +0100888 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000889 return mProvider.saveState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 }
891
892 /**
Steve Block4e584df2012-04-24 23:12:47 +0100893 * Saves the current display data to the Bundle given. Used in conjunction
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 * with {@link #saveState}.
Steve Block4e584df2012-04-24 23:12:47 +0100895 * @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 Project9066cfe2009-03-03 19:31:44 -0800897 * overwritten with this WebView's picture data.
Nate Fischer0a6140d2017-09-05 12:37:49 -0700898 * @return {@code true} if the picture was successfully saved
Kristian Monsenfc771652011-05-10 16:44:05 +0100899 * @deprecated This method is now obsolete.
Kristian Monsenf4912582012-08-16 15:26:24 -0400900 * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 */
Kristian Monsenfc771652011-05-10 16:44:05 +0100902 @Deprecated
Patrick Scottda9a22b2010-04-08 08:32:52 -0400903 public boolean savePicture(Bundle b, final File dest) {
Steve Block51b08912011-04-27 15:04:48 +0100904 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000905 return mProvider.savePicture(b, dest);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 }
907
908 /**
Steve Block4e584df2012-04-24 23:12:47 +0100909 * 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 Reck2df8f422011-09-22 19:50:41 -0700912 *
Steve Block4e584df2012-04-24 23:12:47 +0100913 * @param b a Bundle containing the saved display data
914 * @param src the file where the picture data was stored
Nate Fischer0a6140d2017-09-05 12:37:49 -0700915 * @return {@code true} if the picture was successfully restored
Kristian Monsenfc771652011-05-10 16:44:05 +0100916 * @deprecated This method is now obsolete.
Kristian Monsenf4912582012-08-16 15:26:24 -0400917 * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 */
Kristian Monsenfc771652011-05-10 16:44:05 +0100919 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 public boolean restorePicture(Bundle b, File src) {
Steve Block51b08912011-04-27 15:04:48 +0100921 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000922 return mProvider.restorePicture(b, src);
John Reck95b7d6f2011-06-03 15:23:43 -0700923 }
924
925 /**
Steve Block42499062012-07-17 15:34:46 +0100926 * 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 Block4e584df2012-04-24 23:12:47 +0100929 * it is called after this WebView has had a chance to build state (load
Cary Clarkd6982c92009-05-29 11:02:22 -0400930 * pages, create a back/forward list, etc.) there may be undesirable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 * side-effects. Please note that this method no longer restores the
Kristian Monsenf4912582012-08-16 15:26:24 -0400932 * display data for this WebView.
Steve Block4e584df2012-04-24 23:12:47 +0100933 *
934 * @param inState the incoming Bundle of state
Nate Fischer0a6140d2017-09-05 12:37:49 -0700935 * @return the restored back/forward list or {@code null} if restoreState failed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 */
Nate Fischer3442c742017-09-08 17:02:00 -0700937 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 public WebBackForwardList restoreState(Bundle inState) {
Steve Block51b08912011-04-27 15:04:48 +0100939 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000940 return mProvider.restoreState(inState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 }
942
943 /**
Steve Block4e584df2012-04-24 23:12:47 +0100944 * Loads the given URL with the specified additional HTTP headers.
Bo Liue29d3132016-04-22 16:23:22 -0700945 * <p>
946 * Also see compatibility note on {@link #evaluateJavascript}.
Steve Block4e584df2012-04-24 23:12:47 +0100947 *
948 * @param url the URL of the resource to load
949 * @param additionalHttpHeaders the additional headers to be used in the
Steve Blockf71dea02011-08-01 12:29:14 +0100950 * 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 Block4e584df2012-04-24 23:12:47 +0100952 * that are set by default by this WebView, such as those
Steve Blockf71dea02011-08-01 12:29:14 +0100953 * controlling caching, accept types or the User-Agent, their
Nate Fischerffb81c42016-09-26 12:35:47 -0700954 * values may be overridden by this WebView's defaults.
Grace Klobad0d9bc22010-01-26 18:08:28 -0800955 */
Steve Blockf71dea02011-08-01 12:29:14 +0100956 public void loadUrl(String url, Map<String, String> additionalHttpHeaders) {
Steve Block51b08912011-04-27 15:04:48 +0100957 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000958 mProvider.loadUrl(url, additionalHttpHeaders);
Grace Klobad0d9bc22010-01-26 18:08:28 -0800959 }
960
961 /**
Steve Block4e584df2012-04-24 23:12:47 +0100962 * Loads the given URL.
Bo Liue29d3132016-04-22 16:23:22 -0700963 * <p>
964 * Also see compatibility note on {@link #evaluateJavascript}.
Steve Block4e584df2012-04-24 23:12:47 +0100965 *
966 * @param url the URL of the resource to load
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 */
968 public void loadUrl(String url) {
Steve Block51b08912011-04-27 15:04:48 +0100969 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000970 mProvider.loadUrl(url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 }
972
973 /**
Steve Block4e584df2012-04-24 23:12:47 +0100974 * Loads the URL with postData using "POST" method into this WebView. If url
Hui Shuc60ad9c2014-02-05 09:30:36 -0800975 * is not a network URL, it will be loaded with {@link #loadUrl(String)}
976 * instead, ignoring the postData param.
Cary Clarkd6982c92009-05-29 11:02:22 -0400977 *
Steve Block4e584df2012-04-24 23:12:47 +0100978 * @param url the URL of the resource to load
Ben Murdoch851004a2013-08-16 16:43:22 +0100979 * @param postData the data will be passed to "POST" request, which must be
980 * be "application/x-www-form-urlencoded" encoded.
Grace Kloba57534302009-05-22 18:55:02 -0700981 */
982 public void postUrl(String url, byte[] postData) {
Steve Block51b08912011-04-27 15:04:48 +0100983 checkThread();
Hui Shuc60ad9c2014-02-05 09:30:36 -0800984 if (URLUtil.isNetworkUrl(url)) {
985 mProvider.postUrl(url, postData);
986 } else {
987 mProvider.loadUrl(url);
988 }
Grace Kloba57534302009-05-22 18:55:02 -0700989 }
990
991 /**
Steve Block4e584df2012-04-24 23:12:47 +0100992 * Loads the given data into this WebView using a 'data' scheme URL.
Steve Blockb28b22a2011-07-04 13:01:25 +0100993 * <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 Blockf95d4902011-06-09 11:53:26 +01001000 * <p>
Nate Fischer867d1092018-01-04 21:57:33 -08001001 * The {@code encoding} parameter specifies whether the data is base64 or URL
Steve Block27f3e322012-07-23 10:45:59 +01001002 * encoded. If the data is base64 encoded, the value of the encoding
Nate Fischer867d1092018-01-04 21:57:33 -08001003 * 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 * "&lt;html&gt;&lt;body&gt;'%28' is the code for '('&lt;/body&gt;&lt;/html&gt;";
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 Blockb19c7872011-10-10 14:09:44 +01001016 * <p>
James Wallace-Leed38c2e32017-12-11 16:11:15 -08001017 * 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 Blockb19c7872011-10-10 14:09:44 +01001021 * The 'data' scheme URL formed by this method uses the default US-ASCII
Torne (Richard Coles)97bf0be2018-05-16 10:51:56 -04001022 * charset. If you need to set a different charset, you should form a
Steve Block33f962b2011-10-11 14:49:47 +01001023 * '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.
Torne (Richard Coles)97bf0be2018-05-16 10:51:56 -04001027 * <p>
1028 * Content loaded using this method will have a {@code window.origin} value
1029 * of {@code "null"}. This must not be considered to be a trusted origin
1030 * by the application or by any JavaScript code running inside the WebView
1031 * (for example, event sources in DOM event handlers or web messages),
1032 * because malicious content can also create frames with a null origin. If
1033 * you need to identify the main frame's origin in a trustworthy way, you
1034 * should use {@link #loadDataWithBaseURL(String,String,String,String,String)
1035 * loadDataWithBaseURL()} with a valid HTTP or HTTPS base URL to set the
1036 * origin.
Steve Block4e584df2012-04-24 23:12:47 +01001037 *
1038 * @param data a String of data in the given encoding
James Wallace-Leed38c2e32017-12-11 16:11:15 -08001039 * @param mimeType the MIME type of the data, e.g. 'text/html'.
Steve Block4e584df2012-04-24 23:12:47 +01001040 * @param encoding the encoding of the data
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 */
Nate Fischer3442c742017-09-08 17:02:00 -07001042 public void loadData(String data, @Nullable String mimeType, @Nullable String encoding) {
Steve Block51b08912011-04-27 15:04:48 +01001043 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001044 mProvider.loadData(data, mimeType, encoding);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 }
1046
1047 /**
Steve Block4e584df2012-04-24 23:12:47 +01001048 * Loads the given data into this WebView, using baseUrl as the base URL for
Steve Blockb28b22a2011-07-04 13:01:25 +01001049 * the content. The base URL is used both to resolve relative URLs and when
1050 * applying JavaScript's same origin policy. The historyUrl is used for the
1051 * history entry.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 * <p>
James Wallace-Leed38c2e32017-12-11 16:11:15 -08001053 * The {@code mimeType} parameter specifies the format of the data.
1054 * If WebView can't handle the specified MIME type, it will download the data.
1055 * If {@code null}, defaults to 'text/html'.
1056 * <p>
Steve Blockae328572011-06-08 18:58:51 +01001057 * Note that content specified in this way can access local device files
1058 * (via 'file' scheme URLs) only if baseUrl specifies a scheme other than
1059 * 'http', 'https', 'ftp', 'ftps', 'about' or 'javascript'.
Steve Blocke482d892011-07-01 13:57:32 +01001060 * <p>
1061 * If the base URL uses the data scheme, this method is equivalent to
1062 * calling {@link #loadData(String,String,String) loadData()} and the
Ben Murdoch597256e2013-07-22 16:52:12 +01001063 * historyUrl is ignored, and the data will be treated as part of a data: URL.
1064 * If the base URL uses any other scheme, then the data will be loaded into
1065 * the WebView as a plain string (i.e. not part of a data URL) and any URL-encoded
1066 * entities in the string will not be decoded.
Mikhail Naganovb29e0f62015-09-16 13:05:53 -07001067 * <p>
1068 * Note that the baseUrl is sent in the 'Referer' HTTP header when
1069 * requesting subresources (images, etc.) of the page loaded using this method.
Torne (Richard Coles)97bf0be2018-05-16 10:51:56 -04001070 * <p>
1071 * If a valid HTTP or HTTPS base URL is not specified in {@code baseUrl}, then
1072 * content loaded using this method will have a {@code window.origin} value
1073 * of {@code "null"}. This must not be considered to be a trusted origin
1074 * by the application or by any JavaScript code running inside the WebView
1075 * (for example, event sources in DOM event handlers or web messages),
1076 * because malicious content can also create frames with a null origin. If
1077 * you need to identify the main frame's origin in a trustworthy way, you
1078 * should use a valid HTTP or HTTPS base URL to set the origin.
Steve Block4e584df2012-04-24 23:12:47 +01001079 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07001080 * @param baseUrl the URL to use as the page's base URL. If {@code null} defaults to
Steve Block4e584df2012-04-24 23:12:47 +01001081 * 'about:blank'.
1082 * @param data a String of data in the given encoding
James Wallace-Leed38c2e32017-12-11 16:11:15 -08001083 * @param mimeType the MIME type of the data, e.g. 'text/html'.
Steve Block4e584df2012-04-24 23:12:47 +01001084 * @param encoding the encoding of the data
Nate Fischer0a6140d2017-09-05 12:37:49 -07001085 * @param historyUrl the URL to use as the history entry. If {@code null} defaults
Ben Murdoch95afb3b2013-02-25 19:18:19 +00001086 * to 'about:blank'. If non-null, this must be a valid URL.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 */
Nate Fischer3442c742017-09-08 17:02:00 -07001088 public void loadDataWithBaseURL(@Nullable String baseUrl, String data,
1089 @Nullable String mimeType, @Nullable String encoding, @Nullable String historyUrl) {
Steve Block51b08912011-04-27 15:04:48 +01001090 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001091 mProvider.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
1093
1094 /**
Ben Murdocha5cdd512013-07-17 16:25:07 +01001095 * Asynchronously evaluates JavaScript in the context of the currently displayed page.
1096 * If non-null, |resultCallback| will be invoked with any result returned from that
1097 * execution. This method must be called on the UI thread and the callback will
1098 * be made on the UI thread.
Bo Liue29d3132016-04-22 16:23:22 -07001099 * <p>
1100 * Compatibility note. Applications targeting {@link android.os.Build.VERSION_CODES#N} or
1101 * later, JavaScript state from an empty WebView is no longer persisted across navigations like
1102 * {@link #loadUrl(String)}. For example, global variables and functions defined before calling
1103 * {@link #loadUrl(String)} will not exist in the loaded page. Applications should use
1104 * {@link #addJavascriptInterface} instead to persist JavaScript objects across navigations.
Ben Murdocha5cdd512013-07-17 16:25:07 +01001105 *
1106 * @param script the JavaScript to execute.
1107 * @param resultCallback A callback to be invoked when the script execution
1108 * completes with the result of the execution (if any).
Nate Fischer0a6140d2017-09-05 12:37:49 -07001109 * May be {@code null} if no notification of the result is required.
Ben Murdocha5cdd512013-07-17 16:25:07 +01001110 */
Nate Fischer3442c742017-09-08 17:02:00 -07001111 public void evaluateJavascript(String script, @Nullable ValueCallback<String> resultCallback) {
Ben Murdocha5cdd512013-07-17 16:25:07 +01001112 checkThread();
1113 mProvider.evaluateJavaScript(script, resultCallback);
1114 }
1115
1116 /**
Elliott Slaughterb48fdbe2010-06-30 11:39:52 -07001117 * Saves the current view as a web archive.
1118 *
Steve Block4e584df2012-04-24 23:12:47 +01001119 * @param filename the filename where the archive should be placed
Elliott Slaughterb48fdbe2010-06-30 11:39:52 -07001120 */
1121 public void saveWebArchive(String filename) {
Steve Block51b08912011-04-27 15:04:48 +01001122 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001123 mProvider.saveWebArchive(filename);
Elliott Slaughterb48fdbe2010-06-30 11:39:52 -07001124 }
1125
1126 /**
1127 * Saves the current view as a web archive.
1128 *
Steve Block4e584df2012-04-24 23:12:47 +01001129 * @param basename the filename where the archive should be placed
Nate Fischer0a6140d2017-09-05 12:37:49 -07001130 * @param autoname if {@code false}, takes basename to be a file. If {@code true}, basename
Elliott Slaughterb48fdbe2010-06-30 11:39:52 -07001131 * is assumed to be a directory in which a filename will be
Steve Block4e584df2012-04-24 23:12:47 +01001132 * chosen according to the URL of the current page.
1133 * @param callback called after the web archive has been saved. The
Elliott Slaughterb48fdbe2010-06-30 11:39:52 -07001134 * parameter for onReceiveValue will either be the filename
Nate Fischer0a6140d2017-09-05 12:37:49 -07001135 * under which the file was saved, or {@code null} if saving the
Elliott Slaughterb48fdbe2010-06-30 11:39:52 -07001136 * file failed.
1137 */
Nate Fischer3442c742017-09-08 17:02:00 -07001138 public void saveWebArchive(String basename, boolean autoname, @Nullable ValueCallback<String>
1139 callback) {
Steve Block51b08912011-04-27 15:04:48 +01001140 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001141 mProvider.saveWebArchive(basename, autoname, callback);
Elliott Slaughterb48fdbe2010-06-30 11:39:52 -07001142 }
1143
1144 /**
Steve Block4e584df2012-04-24 23:12:47 +01001145 * Stops the current load.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 */
1147 public void stopLoading() {
Steve Block51b08912011-04-27 15:04:48 +01001148 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001149 mProvider.stopLoading();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 }
1151
1152 /**
Steve Block4e584df2012-04-24 23:12:47 +01001153 * Reloads the current URL.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 */
1155 public void reload() {
Steve Block51b08912011-04-27 15:04:48 +01001156 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001157 mProvider.reload();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 }
1159
1160 /**
Steve Block4e584df2012-04-24 23:12:47 +01001161 * Gets whether this WebView has a back history item.
1162 *
Nate Fischer2be201e2017-10-26 10:43:00 -07001163 * @return {@code true} if this WebView has a back history item
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 */
1165 public boolean canGoBack() {
Steve Block51b08912011-04-27 15:04:48 +01001166 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001167 return mProvider.canGoBack();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 }
1169
1170 /**
Steve Block4e584df2012-04-24 23:12:47 +01001171 * Goes back in the history of this WebView.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 */
1173 public void goBack() {
Steve Block51b08912011-04-27 15:04:48 +01001174 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001175 mProvider.goBack();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 }
1177
1178 /**
Steve Block4e584df2012-04-24 23:12:47 +01001179 * Gets whether this WebView has a forward history item.
1180 *
Nate Fischer2be201e2017-10-26 10:43:00 -07001181 * @return {@code true} if this WebView has a forward history item
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 */
1183 public boolean canGoForward() {
Steve Block51b08912011-04-27 15:04:48 +01001184 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001185 return mProvider.canGoForward();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
1187
1188 /**
Steve Block4e584df2012-04-24 23:12:47 +01001189 * Goes forward in the history of this WebView.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 */
1191 public void goForward() {
Steve Block51b08912011-04-27 15:04:48 +01001192 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001193 mProvider.goForward();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
1195
1196 /**
Steve Block4e584df2012-04-24 23:12:47 +01001197 * Gets whether the page can go back or forward the given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 * number of steps.
Steve Block4e584df2012-04-24 23:12:47 +01001199 *
1200 * @param steps the negative or positive number of steps to move the
1201 * history
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 */
1203 public boolean canGoBackOrForward(int steps) {
Steve Block51b08912011-04-27 15:04:48 +01001204 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001205 return mProvider.canGoBackOrForward(steps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 }
1207
1208 /**
Steve Block4e584df2012-04-24 23:12:47 +01001209 * Goes to the history item that is the number of steps away from
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 * the current item. Steps is negative if backward and positive
1211 * if forward.
Steve Block4e584df2012-04-24 23:12:47 +01001212 *
1213 * @param steps the number of steps to take back or forward in the back
1214 * forward list
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 */
1216 public void goBackOrForward(int steps) {
Steve Block51b08912011-04-27 15:04:48 +01001217 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001218 mProvider.goBackOrForward(steps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 }
Cary Clarkd6982c92009-05-29 11:02:22 -04001220
Elliott Slaughterf21d2e32010-07-14 18:08:54 -07001221 /**
Steve Block4e584df2012-04-24 23:12:47 +01001222 * Gets whether private browsing is enabled in this WebView.
Elliott Slaughterf21d2e32010-07-14 18:08:54 -07001223 */
Elliott Slaughter7c2d1352010-08-20 15:57:18 -07001224 public boolean isPrivateBrowsingEnabled() {
Steve Block51b08912011-04-27 15:04:48 +01001225 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001226 return mProvider.isPrivateBrowsingEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 }
Cary Clarkd6982c92009-05-29 11:02:22 -04001228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
Steve Block4e584df2012-04-24 23:12:47 +01001230 * Scrolls the contents of this WebView up by half the view size.
1231 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07001232 * @param top {@code true} to jump to the top of the page
1233 * @return {@code true} if the page was scrolled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 */
1235 public boolean pageUp(boolean top) {
Steve Block51b08912011-04-27 15:04:48 +01001236 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001237 return mProvider.pageUp(top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 }
Cary Clarkd6982c92009-05-29 11:02:22 -04001239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 /**
Steve Block4e584df2012-04-24 23:12:47 +01001241 * Scrolls the contents of this WebView down by half the page size.
1242 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07001243 * @param bottom {@code true} to jump to bottom of page
1244 * @return {@code true} if the page was scrolled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 */
1246 public boolean pageDown(boolean bottom) {
Steve Block51b08912011-04-27 15:04:48 +01001247 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001248 return mProvider.pageDown(bottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 }
1250
1251 /**
Tobias Sargeantb3656042015-05-13 11:23:57 +01001252 * Posts a {@link VisualStateCallback}, which will be called when
1253 * the current state of the WebView is ready to be drawn.
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001254 *
Nate Fischerffb81c42016-09-26 12:35:47 -07001255 * <p>Because updates to the DOM are processed asynchronously, updates to the DOM may not
Tobias Sargeantb3656042015-05-13 11:23:57 +01001256 * immediately be reflected visually by subsequent {@link WebView#onDraw} invocations. The
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001257 * {@link VisualStateCallback} provides a mechanism to notify the caller when the contents of
Tobias Sargeantb3656042015-05-13 11:23:57 +01001258 * the DOM at the current time are ready to be drawn the next time the {@link WebView}
Nate Fischerf02f8462017-09-11 15:16:33 -07001259 * draws.
Tobias Sargeantb3656042015-05-13 11:23:57 +01001260 *
1261 * <p>The next draw after the callback completes is guaranteed to reflect all the updates to the
Nate Fischerffb81c42016-09-26 12:35:47 -07001262 * DOM up to the point at which the {@link VisualStateCallback} was posted, but it may also
Nate Fischerf02f8462017-09-11 15:16:33 -07001263 * contain updates applied after the callback was posted.
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001264 *
1265 * <p>The state of the DOM covered by this API includes the following:
1266 * <ul>
1267 * <li>primitive HTML elements (div, img, span, etc..)</li>
1268 * <li>images</li>
1269 * <li>CSS animations</li>
1270 * <li>WebGL</li>
1271 * <li>canvas</li>
1272 * </ul>
1273 * It does not include the state of:
1274 * <ul>
1275 * <li>the video tag</li>
Nate Fischerf02f8462017-09-11 15:16:33 -07001276 * </ul>
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001277 *
1278 * <p>To guarantee that the {@link WebView} will successfully render the first frame
1279 * after the {@link VisualStateCallback#onComplete} method has been called a set of conditions
1280 * must be met:
1281 * <ul>
1282 * <li>If the {@link WebView}'s visibility is set to {@link View#VISIBLE VISIBLE} then
1283 * the {@link WebView} must be attached to the view hierarchy.</li>
1284 * <li>If the {@link WebView}'s visibility is set to {@link View#INVISIBLE INVISIBLE}
1285 * then the {@link WebView} must be attached to the view hierarchy and must be made
1286 * {@link View#VISIBLE VISIBLE} from the {@link VisualStateCallback#onComplete} method.</li>
1287 * <li>If the {@link WebView}'s visibility is set to {@link View#GONE GONE} then the
1288 * {@link WebView} must be attached to the view hierarchy and its
1289 * {@link AbsoluteLayout.LayoutParams LayoutParams}'s width and height need to be set to fixed
1290 * values and must be made {@link View#VISIBLE VISIBLE} from the
1291 * {@link VisualStateCallback#onComplete} method.</li>
Nate Fischerf02f8462017-09-11 15:16:33 -07001292 * </ul>
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001293 *
Tobias Sargeantb3656042015-05-13 11:23:57 +01001294 * <p>When using this API it is also recommended to enable pre-rasterization if the {@link
Nate Fischerffb81c42016-09-26 12:35:47 -07001295 * WebView} is off screen to avoid flickering. See {@link WebSettings#setOffscreenPreRaster} for
Nate Fischerf02f8462017-09-11 15:16:33 -07001296 * more details and do consider its caveats.
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001297 *
Tobias Sargeantb3656042015-05-13 11:23:57 +01001298 * @param requestId An id that will be returned in the callback to allow callers to match
1299 * requests with callbacks.
1300 * @param callback The callback to be invoked.
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001301 */
Tobias Sargeantb3656042015-05-13 11:23:57 +01001302 public void postVisualStateCallback(long requestId, VisualStateCallback callback) {
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001303 checkThread();
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001304 mProvider.insertVisualStateCallback(requestId, callback);
1305 }
1306
1307 /**
Steve Block4e584df2012-04-24 23:12:47 +01001308 * Clears this WebView so that onDraw() will draw nothing but white background,
1309 * and onMeasure() will return 0 if MeasureSpec is not MeasureSpec.EXACTLY.
Jonathan Dixon835b1fc2013-02-25 12:29:33 -08001310 * @deprecated Use WebView.loadUrl("about:blank") to reliably reset the view state
1311 * and release page resources (including any running JavaScript).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 */
Jonathan Dixon835b1fc2013-02-25 12:29:33 -08001313 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 public void clearView() {
Steve Block51b08912011-04-27 15:04:48 +01001315 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001316 mProvider.clearView();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 }
Cary Clarkd6982c92009-05-29 11:02:22 -04001318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 /**
Steve Block72498ed2012-07-17 15:57:25 +01001320 * Gets a new picture that captures the current contents of this WebView.
1321 * The picture is of the entire document being displayed, and is not
1322 * limited to the area currently displayed by this WebView. Also, the
1323 * picture is a static copy and is unaffected by later changes to the
1324 * content being displayed.
1325 * <p>
1326 * Note that due to internal changes, for API levels between
1327 * {@link android.os.Build.VERSION_CODES#HONEYCOMB} and
1328 * {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH} inclusive, the
1329 * picture does not include fixed position elements or scrollable divs.
Jonathan Dixon5545d082013-08-31 22:43:11 -07001330 * <p>
1331 * Note that from {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} the returned picture
1332 * should only be drawn into bitmap-backed Canvas - using any other type of Canvas will involve
1333 * additional conversion at a cost in memory and performance. Also the
1334 * {@link android.graphics.Picture#createFromStream} and
1335 * {@link android.graphics.Picture#writeToStream} methods are not supported on the
1336 * returned object.
1337 *
1338 * @deprecated Use {@link #onDraw} to obtain a bitmap snapshot of the WebView, or
Jonathan Dixondf3859e2013-09-18 12:51:47 -07001339 * {@link #saveWebArchive} to save the content to a file.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 *
Steve Block72498ed2012-07-17 15:57:25 +01001341 * @return a picture that captures the current contents of this WebView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 */
Jonathan Dixon5545d082013-08-31 22:43:11 -07001343 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 public Picture capturePicture() {
Steve Block51b08912011-04-27 15:04:48 +01001345 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001346 return mProvider.capturePicture();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 }
1348
Cary Clarkd6982c92009-05-29 11:02:22 -04001349 /**
Selim Gurun23a1af62014-03-14 16:52:45 -07001350 * @deprecated Use {@link #createPrintDocumentAdapter(String)} which requires user
1351 * to provide a print document name.
1352 */
1353 @Deprecated
1354 public PrintDocumentAdapter createPrintDocumentAdapter() {
1355 checkThread();
Selim Gurun23a1af62014-03-14 16:52:45 -07001356 return mProvider.createPrintDocumentAdapter("default");
1357 }
1358
1359 /**
Nate Fischerffb81c42016-09-26 12:35:47 -07001360 * Creates a PrintDocumentAdapter that provides the content of this WebView for printing.
Selim Gurun2167c0b2013-08-05 15:23:09 -07001361 *
Nate Fischerffb81c42016-09-26 12:35:47 -07001362 * The adapter works by converting the WebView contents to a PDF stream. The WebView cannot
Selim Gurunda7d6422013-09-06 14:39:05 -07001363 * be drawn during the conversion process - any such draws are undefined. It is recommended
Nate Fischerffb81c42016-09-26 12:35:47 -07001364 * to use a dedicated off screen WebView for the printing. If necessary, an application may
Selim Gurunda7d6422013-09-06 14:39:05 -07001365 * temporarily hide a visible WebView by using a custom PrintDocumentAdapter instance
1366 * wrapped around the object returned and observing the onStart and onFinish methods. See
1367 * {@link android.print.PrintDocumentAdapter} for more information.
Selim Gurun23a1af62014-03-14 16:52:45 -07001368 *
1369 * @param documentName The user-facing name of the printed document. See
1370 * {@link android.print.PrintDocumentInfo}
Selim Gurun2167c0b2013-08-05 15:23:09 -07001371 */
Selim Gurun23a1af62014-03-14 16:52:45 -07001372 public PrintDocumentAdapter createPrintDocumentAdapter(String documentName) {
Selim Gurun2167c0b2013-08-05 15:23:09 -07001373 checkThread();
Selim Gurun23a1af62014-03-14 16:52:45 -07001374 return mProvider.createPrintDocumentAdapter(documentName);
Selim Gurun2167c0b2013-08-05 15:23:09 -07001375 }
1376
1377 /**
Steve Block4e584df2012-04-24 23:12:47 +01001378 * Gets the current scale of this WebView.
1379 *
1380 * @return the current scale
Kristian Monsen5cc23512012-08-09 15:33:08 -04001381 *
1382 * @deprecated This method is prone to inaccuracy due to race conditions
1383 * between the web rendering and UI threads; prefer
1384 * {@link WebViewClient#onScaleChanged}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 */
Kristian Monsen5cc23512012-08-09 15:33:08 -04001386 @Deprecated
John Reck926cf562012-06-14 10:00:31 -07001387 @ViewDebug.ExportedProperty(category = "webview")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 public float getScale() {
Steve Block51b08912011-04-27 15:04:48 +01001389 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001390 return mProvider.getScale();
Mangesh Ghiwareb5f9fc32011-08-31 17:49:07 -07001391 }
1392
1393 /**
Mikhail Naganov00303362013-09-02 10:57:04 +01001394 * Sets the initial scale for this WebView. 0 means default.
1395 * The behavior for the default scale depends on the state of
1396 * {@link WebSettings#getUseWideViewPort()} and
1397 * {@link WebSettings#getLoadWithOverviewMode()}.
1398 * If the content fits into the WebView control by width, then
Nate Fischerffb81c42016-09-26 12:35:47 -07001399 * the zoom is set to 100%. For wide content, the behavior
Mikhail Naganov00303362013-09-02 10:57:04 +01001400 * depends on the state of {@link WebSettings#getLoadWithOverviewMode()}.
Nate Fischer0a6140d2017-09-05 12:37:49 -07001401 * If its value is {@code true}, the content will be zoomed out to be fit
Mikhail Naganov00303362013-09-02 10:57:04 +01001402 * by width into the WebView control, otherwise not.
1403 *
1404 * If initial scale is greater than 0, WebView starts with this value
1405 * as initial scale.
Mangesh Ghiwaree832b632011-11-16 11:46:39 -08001406 * Please note that unlike the scale properties in the viewport meta tag,
1407 * this method doesn't take the screen density into account.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 *
Steve Block4e584df2012-04-24 23:12:47 +01001409 * @param scaleInPercent the initial scale in percent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 */
1411 public void setInitialScale(int scaleInPercent) {
Steve Block51b08912011-04-27 15:04:48 +01001412 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001413 mProvider.setInitialScale(scaleInPercent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 }
1415
1416 /**
Steve Block4e584df2012-04-24 23:12:47 +01001417 * Invokes the graphical zoom picker widget for this WebView. This will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 * result in the zoom widget appearing on the screen to control the zoom
1419 * level of this WebView.
1420 */
1421 public void invokeZoomPicker() {
Steve Block51b08912011-04-27 15:04:48 +01001422 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001423 mProvider.invokeZoomPicker();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 }
1425
1426 /**
Steve Block4e584df2012-04-24 23:12:47 +01001427 * Gets a HitTestResult based on the current cursor node. If a HTML::a
1428 * tag is found and the anchor has a non-JavaScript URL, the HitTestResult
1429 * type is set to SRC_ANCHOR_TYPE and the URL is set in the "extra" field.
1430 * If the anchor does not have a URL or if it is a JavaScript URL, the type
1431 * will be UNKNOWN_TYPE and the URL has to be retrieved through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 * {@link #requestFocusNodeHref} asynchronously. If a HTML::img tag is
Steve Block4e584df2012-04-24 23:12:47 +01001433 * found, the HitTestResult type is set to IMAGE_TYPE and the URL is set in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 * the "extra" field. A type of
Steve Block4e584df2012-04-24 23:12:47 +01001435 * SRC_IMAGE_ANCHOR_TYPE indicates an anchor with a URL that has an image as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 * a child node. If a phone number is found, the HitTestResult type is set
1437 * to PHONE_TYPE and the phone number is set in the "extra" field of
1438 * HitTestResult. If a map address is found, the HitTestResult type is set
1439 * to GEO_TYPE and the address is set in the "extra" field of HitTestResult.
1440 * If an email address is found, the HitTestResult type is set to EMAIL_TYPE
1441 * and the email is set in the "extra" field of HitTestResult. Otherwise,
1442 * HitTestResult type is set to UNKNOWN_TYPE.
1443 */
1444 public HitTestResult getHitTestResult() {
Steve Block51b08912011-04-27 15:04:48 +01001445 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001446 return mProvider.getHitTestResult();
Cary Clarkb8491342010-11-29 16:23:19 -05001447 }
1448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 /**
Steve Block4e584df2012-04-24 23:12:47 +01001450 * Requests the anchor or image element URL at the last tapped point.
Nate Fischer0a6140d2017-09-05 12:37:49 -07001451 * If hrefMsg is {@code null}, this method returns immediately and does not
Cary Clark861368a2010-12-15 11:24:37 -05001452 * dispatch hrefMsg to its target. If the tapped point hits an image,
1453 * an anchor, or an image in an anchor, the message associates
1454 * strings in named keys in its data. The value paired with the key
1455 * may be an empty string.
Cary Clarkd6982c92009-05-29 11:02:22 -04001456 *
Steve Block4e584df2012-04-24 23:12:47 +01001457 * @param hrefMsg the message to be dispatched with the result of the
1458 * request. The message data contains three keys. "url"
1459 * returns the anchor's href attribute. "title" returns the
1460 * anchor's text. "src" returns the image's src attribute.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 */
Nate Fischer3442c742017-09-08 17:02:00 -07001462 public void requestFocusNodeHref(@Nullable Message hrefMsg) {
Steve Block51b08912011-04-27 15:04:48 +01001463 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001464 mProvider.requestFocusNodeHref(hrefMsg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 }
Cary Clarkd6982c92009-05-29 11:02:22 -04001466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 /**
Steve Block4e584df2012-04-24 23:12:47 +01001468 * Requests the URL of the image last touched by the user. msg will be sent
1469 * to its target with a String representing the URL as its object.
Cary Clarkd6982c92009-05-29 11:02:22 -04001470 *
Steve Block4e584df2012-04-24 23:12:47 +01001471 * @param msg the message to be dispatched with the result of the request
Nate Fischer0a6140d2017-09-05 12:37:49 -07001472 * as the data member with "url" as key. The result can be {@code null}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 */
1474 public void requestImageRef(Message msg) {
Steve Block51b08912011-04-27 15:04:48 +01001475 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001476 mProvider.requestImageRef(msg);
Adam Powell637d3372010-08-25 14:37:03 -07001477 }
1478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 /**
Steve Block4e584df2012-04-24 23:12:47 +01001480 * Gets the URL for the current page. This is not always the same as the URL
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 * passed to WebViewClient.onPageStarted because although the load for
Steve Block4e584df2012-04-24 23:12:47 +01001482 * that URL has begun, the current page may not have changed.
1483 *
1484 * @return the URL for the current page
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 */
John Reck926cf562012-06-14 10:00:31 -07001486 @ViewDebug.ExportedProperty(category = "webview")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 public String getUrl() {
Steve Block51b08912011-04-27 15:04:48 +01001488 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001489 return mProvider.getUrl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
Cary Clarkd6982c92009-05-29 11:02:22 -04001491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 /**
Steve Block4e584df2012-04-24 23:12:47 +01001493 * Gets the original URL for the current page. This is not always the same
1494 * as the URL passed to WebViewClient.onPageStarted because although the
1495 * load for that URL has begun, the current page may not have changed.
1496 * Also, there may have been redirects resulting in a different URL to that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 * originally requested.
Steve Block4e584df2012-04-24 23:12:47 +01001498 *
1499 * @return the URL that was originally requested for the current page
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 */
John Reck926cf562012-06-14 10:00:31 -07001501 @ViewDebug.ExportedProperty(category = "webview")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 public String getOriginalUrl() {
Steve Block51b08912011-04-27 15:04:48 +01001503 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001504 return mProvider.getOriginalUrl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 }
1506
1507 /**
Steve Block4e584df2012-04-24 23:12:47 +01001508 * Gets the title for the current page. This is the title of the current page
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 * until WebViewClient.onReceivedTitle is called.
Steve Block4e584df2012-04-24 23:12:47 +01001510 *
1511 * @return the title for the current page
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 */
John Reck926cf562012-06-14 10:00:31 -07001513 @ViewDebug.ExportedProperty(category = "webview")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 public String getTitle() {
Steve Block51b08912011-04-27 15:04:48 +01001515 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001516 return mProvider.getTitle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 }
1518
1519 /**
Steve Block4e584df2012-04-24 23:12:47 +01001520 * Gets the favicon for the current page. This is the favicon of the current
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 * page until WebViewClient.onReceivedIcon is called.
Steve Block4e584df2012-04-24 23:12:47 +01001522 *
1523 * @return the favicon for the current page
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 */
1525 public Bitmap getFavicon() {
Steve Block51b08912011-04-27 15:04:48 +01001526 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001527 return mProvider.getFavicon();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 }
1529
1530 /**
Steve Block4e584df2012-04-24 23:12:47 +01001531 * Gets the touch icon URL for the apple-touch-icon <link> element, or
Ben Murdoch372dfc82010-07-01 15:56:01 +01001532 * a URL on this site's server pointing to the standard location of a
1533 * touch icon.
Steve Block4e584df2012-04-24 23:12:47 +01001534 *
Patrick Scott2ba12622009-08-04 13:20:05 -04001535 * @hide
1536 */
1537 public String getTouchIconUrl() {
Jonathan Dixon3c909522012-02-28 18:45:06 +00001538 return mProvider.getTouchIconUrl();
Patrick Scott2ba12622009-08-04 13:20:05 -04001539 }
1540
1541 /**
Steve Block4e584df2012-04-24 23:12:47 +01001542 * Gets the progress for the current page.
1543 *
1544 * @return the progress for the current page between 0 and 100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 */
1546 public int getProgress() {
Steve Block51b08912011-04-27 15:04:48 +01001547 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001548 return mProvider.getProgress();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 }
Cary Clarkd6982c92009-05-29 11:02:22 -04001550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 /**
Steve Block4e584df2012-04-24 23:12:47 +01001552 * Gets the height of the HTML content.
1553 *
1554 * @return the height of the HTML content
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 */
John Reck926cf562012-06-14 10:00:31 -07001556 @ViewDebug.ExportedProperty(category = "webview")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 public int getContentHeight() {
Steve Block51b08912011-04-27 15:04:48 +01001558 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001559 return mProvider.getContentHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 }
1561
1562 /**
Steve Block4e584df2012-04-24 23:12:47 +01001563 * Gets the width of the HTML content.
1564 *
1565 * @return the width of the HTML content
Leon Scrogginsea96d1e2009-09-23 13:41:01 -04001566 * @hide
1567 */
John Reck926cf562012-06-14 10:00:31 -07001568 @ViewDebug.ExportedProperty(category = "webview")
Leon Scrogginsea96d1e2009-09-23 13:41:01 -04001569 public int getContentWidth() {
Jonathan Dixon3c909522012-02-28 18:45:06 +00001570 return mProvider.getContentWidth();
John Reck9b90d552011-06-14 15:19:17 -07001571 }
1572
1573 /**
Steve Block4e584df2012-04-24 23:12:47 +01001574 * Pauses all layout, parsing, and JavaScript timers for all WebViews. This
1575 * is a global requests, not restricted to just this WebView. This can be
Mike Reedd205d5b2009-05-27 11:02:29 -04001576 * useful if the application has been paused.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 */
1578 public void pauseTimers() {
Steve Block51b08912011-04-27 15:04:48 +01001579 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001580 mProvider.pauseTimers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 }
1582
1583 /**
Steve Block4e584df2012-04-24 23:12:47 +01001584 * Resumes all layout, parsing, and JavaScript timers for all WebViews.
Mike Reedd205d5b2009-05-27 11:02:29 -04001585 * This will resume dispatching all timers.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 */
1587 public void resumeTimers() {
Steve Block51b08912011-04-27 15:04:48 +01001588 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001589 mProvider.resumeTimers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 }
1591
1592 /**
Selim Gurun922cb582015-07-31 11:49:22 -07001593 * Does a best-effort attempt to pause any processing that can be paused
1594 * safely, such as animations and geolocation. Note that this call
1595 * does not pause JavaScript. To pause JavaScript globally, use
1596 * {@link #pauseTimers}.
1597 *
1598 * To resume WebView, call {@link #onResume}.
Mike Reedd205d5b2009-05-27 11:02:29 -04001599 */
1600 public void onPause() {
Steve Block51b08912011-04-27 15:04:48 +01001601 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001602 mProvider.onPause();
Mike Reedd205d5b2009-05-27 11:02:29 -04001603 }
1604
1605 /**
Selim Gurun922cb582015-07-31 11:49:22 -07001606 * Resumes a WebView after a previous call to {@link #onPause}.
Mike Reedd205d5b2009-05-27 11:02:29 -04001607 */
1608 public void onResume() {
Steve Block51b08912011-04-27 15:04:48 +01001609 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001610 mProvider.onResume();
Mike Reedd205d5b2009-05-27 11:02:29 -04001611 }
1612
1613 /**
Steve Block4e584df2012-04-24 23:12:47 +01001614 * Gets whether this WebView is paused, meaning onPause() was called.
Nate Fischer0a6140d2017-09-05 12:37:49 -07001615 * Calling onResume() sets the paused state back to {@code false}.
Steve Block4e584df2012-04-24 23:12:47 +01001616 *
Mike Reedd205d5b2009-05-27 11:02:29 -04001617 * @hide
1618 */
1619 public boolean isPaused() {
Jonathan Dixon3c909522012-02-28 18:45:06 +00001620 return mProvider.isPaused();
Mike Reedd205d5b2009-05-27 11:02:29 -04001621 }
1622
1623 /**
Steve Block4e584df2012-04-24 23:12:47 +01001624 * Informs this WebView that memory is low so that it can free any available
1625 * memory.
Jonathan Dixon5545d082013-08-31 22:43:11 -07001626 * @deprecated Memory caches are automatically dropped when no longer needed, and in response
1627 * to system memory pressure.
Derek Sollenbergere0155e92009-06-10 15:35:45 -04001628 */
Jonathan Dixon5545d082013-08-31 22:43:11 -07001629 @Deprecated
Derek Sollenbergere0155e92009-06-10 15:35:45 -04001630 public void freeMemory() {
Steve Block51b08912011-04-27 15:04:48 +01001631 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001632 mProvider.freeMemory();
Derek Sollenbergere0155e92009-06-10 15:35:45 -04001633 }
1634
1635 /**
Steve Block4e584df2012-04-24 23:12:47 +01001636 * Clears the resource cache. Note that the cache is per-application, so
Mike Hearnadcd2ed2009-01-21 16:44:36 +01001637 * this will clear the cache for all WebViews used.
1638 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07001639 * @param includeDiskFiles if {@code false}, only the RAM cache is cleared
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 */
1641 public void clearCache(boolean includeDiskFiles) {
Steve Block51b08912011-04-27 15:04:48 +01001642 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001643 mProvider.clearCache(includeDiskFiles);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 }
1645
1646 /**
Steve Block219dfa42012-07-20 10:31:21 +01001647 * Removes the autocomplete popup from the currently focused form field, if
1648 * present. Note this only affects the display of the autocomplete popup,
1649 * it does not remove any saved form data from this WebView's store. To do
1650 * that, use {@link WebViewDatabase#clearFormData}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 */
1652 public void clearFormData() {
Steve Block51b08912011-04-27 15:04:48 +01001653 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001654 mProvider.clearFormData();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 }
1656
1657 /**
Steve Block4e584df2012-04-24 23:12:47 +01001658 * Tells this WebView to clear its internal back/forward list.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 */
1660 public void clearHistory() {
Steve Block51b08912011-04-27 15:04:48 +01001661 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001662 mProvider.clearHistory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 }
1664
1665 /**
Steve Block4e584df2012-04-24 23:12:47 +01001666 * Clears the SSL preferences table stored in response to proceeding with
1667 * SSL certificate errors.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 */
1669 public void clearSslPreferences() {
Steve Block51b08912011-04-27 15:04:48 +01001670 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001671 mProvider.clearSslPreferences();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 }
1673
1674 /**
Selim Gurunee89ab42014-06-13 10:53:55 -07001675 * Clears the client certificate preferences stored in response
Nate Fischerffb81c42016-09-26 12:35:47 -07001676 * to proceeding/cancelling client cert requests. Note that WebView
Selim Gurunb6aa97e2014-03-26 14:10:28 -07001677 * automatically clears these preferences when it receives a
Selim Gurunee89ab42014-06-13 10:53:55 -07001678 * {@link KeyChain#ACTION_STORAGE_CHANGED} intent. The preferences are
Nate Fischerffb81c42016-09-26 12:35:47 -07001679 * shared by all the WebViews that are created by the embedder application.
Selim Gurunb6aa97e2014-03-26 14:10:28 -07001680 *
Selim Gurune1c6c3a2014-04-28 14:21:21 -07001681 * @param onCleared A runnable to be invoked when client certs are cleared.
Nate Fischer3442c742017-09-08 17:02:00 -07001682 * The runnable will be called in UI thread.
Selim Gurunb6aa97e2014-03-26 14:10:28 -07001683 */
Nate Fischer3442c742017-09-08 17:02:00 -07001684 public static void clearClientCertPreferences(@Nullable Runnable onCleared) {
Selim Gurune1c6c3a2014-04-28 14:21:21 -07001685 getFactory().getStatics().clearClientCertPreferences(onCleared);
Selim Gurunb6aa97e2014-03-26 14:10:28 -07001686 }
1687
1688 /**
Nate Fischer2b108d82017-08-21 17:59:55 -07001689 * Starts Safe Browsing initialization.
Nate Fischer471891d2017-07-18 19:15:52 -07001690 * <p>
Nate Fischer2b108d82017-08-21 17:59:55 -07001691 * URL loads are not guaranteed to be protected by Safe Browsing until after {@code callback} is
1692 * invoked with {@code true}. Safe Browsing is not fully supported on all devices. For those
1693 * devices {@code callback} will receive {@code false}.
Nate Fischer471891d2017-07-18 19:15:52 -07001694 * <p>
Nate Fischerfb92ee12018-01-18 12:01:19 -08001695 * This should not be called if Safe Browsing has been disabled by manifest tag or {@link
1696 * WebSettings#setSafeBrowsingEnabled}. This prepares resources used for Safe Browsing.
Nate Fischer471891d2017-07-18 19:15:52 -07001697 * <p>
Nate Fischer2b108d82017-08-21 17:59:55 -07001698 * This should be called with the Application Context (and will always use the Application
1699 * context to do its work regardless).
Nate Fischer471891d2017-07-18 19:15:52 -07001700 *
Nate Fischer3ca196a2017-07-12 18:45:03 -07001701 * @param context Application Context.
Nate Fischer2b108d82017-08-21 17:59:55 -07001702 * @param callback will be called on the UI thread with {@code true} if initialization is
1703 * successful, {@code false} otherwise.
Nate Fischer0e72c182017-05-30 11:25:17 -07001704 */
Nate Fischer65e3be02018-03-01 00:55:35 -08001705 public static void startSafeBrowsing(@NonNull Context context,
Nate Fischer3442c742017-09-08 17:02:00 -07001706 @Nullable ValueCallback<Boolean> callback) {
Nate Fischer0e72c182017-05-30 11:25:17 -07001707 getFactory().getStatics().initSafeBrowsing(context, callback);
1708 }
1709
1710 /**
Nate Fischer5a6ae8c2018-02-21 15:27:01 -08001711 * Sets the list of hosts (domain names/IP addresses) that are exempt from SafeBrowsing checks.
1712 * The list is global for all the WebViews.
Selim Gurun89e93592017-07-28 10:33:58 -07001713 * <p>
1714 * Each rule should take one of these:
1715 * <table>
1716 * <tr><th> Rule </th> <th> Example </th> <th> Matches Subdomain</th> </tr>
Nate Fischerfbebfa92017-08-29 16:17:22 -07001717 * <tr><td> HOSTNAME </td> <td> example.com </td> <td> Yes </td> </tr>
1718 * <tr><td> .HOSTNAME </td> <td> .example.com </td> <td> No </td> </tr>
1719 * <tr><td> IPV4_LITERAL </td> <td> 192.168.1.1 </td> <td> No </td></tr>
1720 * <tr><td> IPV6_LITERAL_WITH_BRACKETS </td><td>[10:20:30:40:50:60:70:80]</td><td>No</td></tr>
Selim Gurun89e93592017-07-28 10:33:58 -07001721 * </table>
1722 * <p>
1723 * All other rules, including wildcards, are invalid.
Nate Fischer5a6ae8c2018-02-21 15:27:01 -08001724 * <p>
1725 * The correct syntax for hosts is defined by <a
1726 * href="https://tools.ietf.org/html/rfc3986#section-3.2.2">RFC 3986</a>.
Nate Fischerbf158ec2017-06-27 22:52:20 -07001727 *
Nate Fischer5a6ae8c2018-02-21 15:27:01 -08001728 * @param hosts the list of hosts
1729 * @param callback will be called with {@code true} if hosts are successfully added to the
1730 * whitelist. It will be called with {@code false} if any hosts are malformed. The callback
1731 * will be run on the UI thread
Nate Fischerbf158ec2017-06-27 22:52:20 -07001732 */
Nate Fischer5a6ae8c2018-02-21 15:27:01 -08001733 public static void setSafeBrowsingWhitelist(@NonNull List<String> hosts,
Nate Fischerd6385d32017-07-17 12:58:18 -07001734 @Nullable ValueCallback<Boolean> callback) {
Nate Fischer5a6ae8c2018-02-21 15:27:01 -08001735 getFactory().getStatics().setSafeBrowsingWhitelist(hosts, callback);
Nate Fischerbf158ec2017-06-27 22:52:20 -07001736 }
1737
1738 /**
Nate Fischer3898ac12017-08-09 23:02:36 -07001739 * Returns a URL pointing to the privacy policy for Safe Browsing reporting.
1740 *
1741 * @return the url pointing to a privacy policy document which can be displayed to users.
1742 */
1743 @NonNull
1744 public static Uri getSafeBrowsingPrivacyPolicyUrl() {
1745 return getFactory().getStatics().getSafeBrowsingPrivacyPolicyUrl();
1746 }
1747
1748 /**
Steve Block4e584df2012-04-24 23:12:47 +01001749 * Gets the WebBackForwardList for this WebView. This contains the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 * back/forward list for use in querying each item in the history stack.
1751 * This is a copy of the private WebBackForwardList so it contains only a
1752 * snapshot of the current state. Multiple calls to this method may return
1753 * different objects. The object returned from this method will not be
1754 * updated to reflect any new state.
1755 */
1756 public WebBackForwardList copyBackForwardList() {
Steve Block51b08912011-04-27 15:04:48 +01001757 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001758 return mProvider.copyBackForwardList();
1759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 }
1761
Victoria Leaseabeb6a72012-03-05 16:29:12 -08001762 /**
Steve Block4e584df2012-04-24 23:12:47 +01001763 * Registers the listener to be notified as find-on-page operations
1764 * progress. This will replace the current listener.
Victoria Leaseabeb6a72012-03-05 16:29:12 -08001765 *
Steve Block4e584df2012-04-24 23:12:47 +01001766 * @param listener an implementation of {@link FindListener}
Victoria Leaseabeb6a72012-03-05 16:29:12 -08001767 */
1768 public void setFindListener(FindListener listener) {
1769 checkThread();
Ben Murdoch52c9f7f2013-01-18 00:50:37 +00001770 setupFindListenerIfNeeded();
1771 mFindListener.mUserFindListener = listener;
Victoria Leaseabeb6a72012-03-05 16:29:12 -08001772 }
1773
1774 /**
Kristian Monsenf4912582012-08-16 15:26:24 -04001775 * Highlights and scrolls to the next match found by
Victoria Lease0b8413b2012-03-26 13:04:10 -07001776 * {@link #findAllAsync}, wrapping around page boundaries as necessary.
Kristian Monsenf4912582012-08-16 15:26:24 -04001777 * Notifies any registered {@link FindListener}. If {@link #findAllAsync(String)}
1778 * has not been called yet, or if {@link #clearMatches} has been called since the
1779 * last find operation, this function does nothing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 *
Steve Block4e584df2012-04-24 23:12:47 +01001781 * @param forward the direction to search
Victoria Lease0b8413b2012-03-26 13:04:10 -07001782 * @see #setFindListener
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 */
1784 public void findNext(boolean forward) {
Steve Block51b08912011-04-27 15:04:48 +01001785 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001786 mProvider.findNext(forward);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 }
1788
Victoria Leaseabeb6a72012-03-05 16:29:12 -08001789 /**
Steve Block4e584df2012-04-24 23:12:47 +01001790 * Finds all instances of find on the page and highlights them.
Victoria Lease0b8413b2012-03-26 13:04:10 -07001791 * Notifies any registered {@link FindListener}.
Victoria Leaseabeb6a72012-03-05 16:29:12 -08001792 *
Steve Block4e584df2012-04-24 23:12:47 +01001793 * @param find the string to find
Nate Fischerffb81c42016-09-26 12:35:47 -07001794 * @return the number of occurrences of the String "find" that were found
Victoria Lease0b8413b2012-03-26 13:04:10 -07001795 * @deprecated {@link #findAllAsync} is preferred.
1796 * @see #setFindListener
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 */
Jonathan Dixon9f21c1c2012-04-12 11:14:20 +01001798 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 public int findAll(String find) {
Steve Block51b08912011-04-27 15:04:48 +01001800 checkThread();
Jonathan Dixon9f21c1c2012-04-12 11:14:20 +01001801 StrictMode.noteSlowCall("findAll blocks UI: prefer findAllAsync");
Jonathan Dixon3c909522012-02-28 18:45:06 +00001802 return mProvider.findAll(find);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 }
1804
Cary Clark3403eb32010-03-03 10:05:16 -05001805 /**
Steve Block4e584df2012-04-24 23:12:47 +01001806 * Finds all instances of find on the page and highlights them,
Victoria Lease0b8413b2012-03-26 13:04:10 -07001807 * asynchronously. Notifies any registered {@link FindListener}.
Kristian Monsenf4912582012-08-16 15:26:24 -04001808 * Successive calls to this will cancel any pending searches.
Victoria Leaseabeb6a72012-03-05 16:29:12 -08001809 *
Steve Block4e584df2012-04-24 23:12:47 +01001810 * @param find the string to find.
Victoria Lease0b8413b2012-03-26 13:04:10 -07001811 * @see #setFindListener
Victoria Leaseabeb6a72012-03-05 16:29:12 -08001812 */
1813 public void findAllAsync(String find) {
1814 checkThread();
1815 mProvider.findAllAsync(find);
1816 }
1817
1818 /**
Steve Block4e584df2012-04-24 23:12:47 +01001819 * Starts an ActionMode for finding text in this WebView. Only works if this
1820 * WebView is attached to the view system.
1821 *
1822 * @param text if non-null, will be the initial text to search for.
Leon Scrogginsfe026bd2010-08-24 14:16:09 -04001823 * Otherwise, the last String searched for in this WebView will
1824 * be used to start.
Nate Fischer0a6140d2017-09-05 12:37:49 -07001825 * @param showIme if {@code true}, show the IME, assuming the user will begin typing.
1826 * If {@code false} and text is non-null, perform a find all.
1827 * @return {@code true} if the find dialog is shown, {@code false} otherwise
Jonathan Dixon835b1fc2013-02-25 12:29:33 -08001828 * @deprecated This method does not work reliably on all Android versions;
1829 * implementing a custom find dialog using WebView.findAllAsync()
1830 * provides a more robust solution.
Cary Clarkde023c12010-03-03 10:05:16 -05001831 */
Jonathan Dixon835b1fc2013-02-25 12:29:33 -08001832 @Deprecated
Nate Fischer3442c742017-09-08 17:02:00 -07001833 public boolean showFindDialog(@Nullable String text, boolean showIme) {
Steve Block51b08912011-04-27 15:04:48 +01001834 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001835 return mProvider.showFindDialog(text, showIme);
Leon Scrogginsfe026bd2010-08-24 14:16:09 -04001836 }
1837
1838 /**
Steve Block4e584df2012-04-24 23:12:47 +01001839 * Gets the first substring consisting of the address of a physical
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 * location. Currently, only addresses in the United States are detected,
1841 * and consist of:
Steve Block4e584df2012-04-24 23:12:47 +01001842 * <ul>
1843 * <li>a house number</li>
1844 * <li>a street name</li>
1845 * <li>a street type (Road, Circle, etc), either spelled out or
1846 * abbreviated</li>
1847 * <li>a city name</li>
1848 * <li>a state or territory, either spelled out or two-letter abbr</li>
1849 * <li>an optional 5 digit or 9 digit zip code</li>
1850 * </ul>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 * All names must be correctly capitalized, and the zip code, if present,
1852 * must be valid for the state. The street type must be a standard USPS
1853 * spelling or abbreviation. The state or territory must also be spelled
Cary Clarkd6982c92009-05-29 11:02:22 -04001854 * or abbreviated using USPS standards. The house number may not exceed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 * five digits.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 *
Steve Block4e584df2012-04-24 23:12:47 +01001857 * @param addr the string to search for addresses
Nate Fischer0a6140d2017-09-05 12:37:49 -07001858 * @return the address, or if no address is found, {@code null}
Jan Althaus003889a2018-04-04 15:29:10 +02001859 * @deprecated this method is superseded by {@link TextClassifier#generateLinks(
1860 * android.view.textclassifier.TextLinks.Request)}. Avoid using this method even when targeting
1861 * API levels where no alternative is available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 */
Nate Fischer3442c742017-09-08 17:02:00 -07001863 @Nullable
Jan Althaus003889a2018-04-04 15:29:10 +02001864 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 public static String findAddress(String addr) {
Tobias Sargeantf4d85d82018-01-05 14:34:38 +00001866 if (addr == null) {
1867 throw new NullPointerException("addr is null");
1868 }
1869 return FindAddress.findAddress(addr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 }
1871
Victoria Lease0b8413b2012-03-26 13:04:10 -07001872 /**
Selim Gurun17d1adb2014-08-27 11:22:26 -07001873 * For apps targeting the L release, WebView has a new default behavior that reduces
1874 * memory footprint and increases performance by intelligently choosing
1875 * the portion of the HTML document that needs to be drawn. These
1876 * optimizations are transparent to the developers. However, under certain
1877 * circumstances, an App developer may want to disable them:
Ben Murdoch574fa342015-01-07 14:20:45 +00001878 * <ol>
1879 * <li>When an app uses {@link #onDraw} to do own drawing and accesses portions
1880 * of the page that is way outside the visible portion of the page.</li>
1881 * <li>When an app uses {@link #capturePicture} to capture a very large HTML document.
1882 * Note that capturePicture is a deprecated API.</li>
1883 * </ol>
Selim Gurun17d1adb2014-08-27 11:22:26 -07001884 * Enabling drawing the entire HTML document has a significant performance
1885 * cost. This method should be called before any WebViews are created.
Bo Liu9f3ed852014-07-18 10:03:12 -07001886 */
1887 public static void enableSlowWholeDocumentDraw() {
1888 getFactory().getStatics().enableSlowWholeDocumentDraw();
1889 }
1890
1891 /**
Steve Block4e584df2012-04-24 23:12:47 +01001892 * Clears the highlighting surrounding text matches created by
Kristian Monsenf4912582012-08-16 15:26:24 -04001893 * {@link #findAllAsync}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 */
1895 public void clearMatches() {
Steve Block51b08912011-04-27 15:04:48 +01001896 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001897 mProvider.clearMatches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 }
1899
1900 /**
Steve Block4e584df2012-04-24 23:12:47 +01001901 * Queries the document to see if it contains any image references. The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 * message object will be dispatched with arg1 being set to 1 if images
1903 * were found and 0 if the document does not reference any images.
Steve Block4e584df2012-04-24 23:12:47 +01001904 *
1905 * @param response the message that will be dispatched with the result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 */
1907 public void documentHasImages(Message response) {
Steve Block51b08912011-04-27 15:04:48 +01001908 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001909 mProvider.documentHasImages(response);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 }
1911
1912 /**
Steve Block4e584df2012-04-24 23:12:47 +01001913 * Sets the WebViewClient that will receive various notifications and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 * requests. This will replace the current handler.
Steve Block4e584df2012-04-24 23:12:47 +01001915 *
1916 * @param client an implementation of WebViewClient
Nate Fischer6e585842016-09-29 18:30:58 -07001917 * @see #getWebViewClient
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 */
1919 public void setWebViewClient(WebViewClient client) {
Steve Block51b08912011-04-27 15:04:48 +01001920 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001921 mProvider.setWebViewClient(client);
Grace Kloba94ab3b62009-10-07 18:00:19 -07001922 }
1923
1924 /**
Nate Fischer6e585842016-09-29 18:30:58 -07001925 * Gets the WebViewClient.
1926 *
1927 * @return the WebViewClient, or a default client if not yet set
1928 * @see #setWebViewClient
1929 */
1930 public WebViewClient getWebViewClient() {
1931 checkThread();
1932 return mProvider.getWebViewClient();
1933 }
1934
1935 /**
Steve Block4e584df2012-04-24 23:12:47 +01001936 * Registers the interface to be used when content can not be handled by
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 * the rendering engine, and should be downloaded instead. This will replace
1938 * the current handler.
Steve Block4e584df2012-04-24 23:12:47 +01001939 *
1940 * @param listener an implementation of DownloadListener
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 */
1942 public void setDownloadListener(DownloadListener listener) {
Steve Block51b08912011-04-27 15:04:48 +01001943 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001944 mProvider.setDownloadListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 }
1946
1947 /**
Steve Block4e584df2012-04-24 23:12:47 +01001948 * Sets the chrome handler. This is an implementation of WebChromeClient for
Steve Block81f19ff2010-11-01 13:23:24 +00001949 * use in handling JavaScript dialogs, favicons, titles, and the progress.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 * This will replace the current handler.
Steve Block4e584df2012-04-24 23:12:47 +01001951 *
1952 * @param client an implementation of WebChromeClient
Nate Fischer6e585842016-09-29 18:30:58 -07001953 * @see #getWebChromeClient
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 */
1955 public void setWebChromeClient(WebChromeClient client) {
Steve Block51b08912011-04-27 15:04:48 +01001956 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001957 mProvider.setWebChromeClient(client);
Patrick Scott0b2e84b2010-03-02 08:58:44 -05001958 }
1959
1960 /**
Nate Fischer6e585842016-09-29 18:30:58 -07001961 * Gets the chrome handler.
1962 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07001963 * @return the WebChromeClient, or {@code null} if not yet set
Nate Fischer6e585842016-09-29 18:30:58 -07001964 * @see #setWebChromeClient
1965 */
Nate Fischer3442c742017-09-08 17:02:00 -07001966 @Nullable
Nate Fischer6e585842016-09-29 18:30:58 -07001967 public WebChromeClient getWebChromeClient() {
1968 checkThread();
1969 return mProvider.getWebChromeClient();
1970 }
1971
1972 /**
Steve Block4e584df2012-04-24 23:12:47 +01001973 * Sets the Picture listener. This is an interface used to receive
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 * notifications of a new Picture.
Steve Block4e584df2012-04-24 23:12:47 +01001975 *
1976 * @param listener an implementation of WebView.PictureListener
Kristian Monsenfc771652011-05-10 16:44:05 +01001977 * @deprecated This method is now obsolete.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 */
Kristian Monsenfc771652011-05-10 16:44:05 +01001979 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 public void setPictureListener(PictureListener listener) {
Steve Block51b08912011-04-27 15:04:48 +01001981 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001982 mProvider.setPictureListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 }
1984
1985 /**
Steve Block3aa800b2012-04-24 13:01:34 +01001986 * Injects the supplied Java object into this WebView. The object is
1987 * injected into the JavaScript context of the main frame, using the
Selim Gurune91d5be2012-09-11 16:11:22 -07001988 * supplied name. This allows the Java object's methods to be
Selim Gurunb4c02e62012-09-12 13:36:21 -07001989 * accessed from JavaScript. For applications targeted to API
1990 * level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
Selim Gurune91d5be2012-09-11 16:11:22 -07001991 * and above, only public methods that are annotated with
1992 * {@link android.webkit.JavascriptInterface} can be accessed from JavaScript.
Selim Gurunb4c02e62012-09-12 13:36:21 -07001993 * For applications targeted to API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN} or below,
Selim Gurune91d5be2012-09-11 16:11:22 -07001994 * all public methods (including the inherited ones) can be accessed, see the
Selim Gurunb4c02e62012-09-12 13:36:21 -07001995 * important security note below for implications.
Nate Fischer02e281a2017-08-21 12:03:58 -07001996 * <p> Note that injected objects will not appear in JavaScript until the page is next
1997 * (re)loaded. JavaScript should be enabled before injecting the object. For example:
Selim Gurune91d5be2012-09-11 16:11:22 -07001998 * <pre>
1999 * class JsObject {
2000 * {@literal @}JavascriptInterface
2001 * public String toString() { return "injectedObject"; }
2002 * }
Nate Fischer02e281a2017-08-21 12:03:58 -07002003 * webview.getSettings().setJavaScriptEnabled(true);
Selim Gurune91d5be2012-09-11 16:11:22 -07002004 * webView.addJavascriptInterface(new JsObject(), "injectedObject");
Steve Block4cd73c52011-11-09 13:06:52 +00002005 * webView.loadData("<!DOCTYPE html><title></title>", "text/html", null);
2006 * webView.loadUrl("javascript:alert(injectedObject.toString())");</pre>
Steve Block3aa800b2012-04-24 13:01:34 +01002007 * <p>
2008 * <strong>IMPORTANT:</strong>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 * <ul>
Steve Block3aa800b2012-04-24 13:01:34 +01002010 * <li> This method can be used to allow JavaScript to control the host
2011 * application. This is a powerful feature, but also presents a security
Scott Rowe607c17f2014-08-07 11:42:09 -07002012 * risk for apps targeting {@link android.os.Build.VERSION_CODES#JELLY_BEAN} or earlier.
2013 * Apps that target a version later than {@link android.os.Build.VERSION_CODES#JELLY_BEAN}
2014 * are still vulnerable if the app runs on a device running Android earlier than 4.2.
2015 * The most secure way to use this method is to target {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
2016 * and to ensure the method is called only when running on Android 4.2 or later.
2017 * With these older versions, JavaScript could use reflection to access an
Steve Block3aa800b2012-04-24 13:01:34 +01002018 * injected object's public fields. Use of this method in a WebView
2019 * containing untrusted content could allow an attacker to manipulate the
2020 * host application in unintended ways, executing Java code with the
2021 * permissions of the host application. Use extreme care when using this
2022 * method in a WebView which could contain untrusted content.</li>
Steve Block4cd73c52011-11-09 13:06:52 +00002023 * <li> JavaScript interacts with Java object on a private, background
Steve Block4e584df2012-04-24 23:12:47 +01002024 * thread of this WebView. Care is therefore required to maintain thread
Scott Rowe607c17f2014-08-07 11:42:09 -07002025 * safety.
2026 * </li>
Selim Gurune91d5be2012-09-11 16:11:22 -07002027 * <li> The Java object's fields are not accessible.</li>
Dianne Hackborn955d8d62014-10-07 20:17:19 -07002028 * <li> For applications targeted to API level {@link android.os.Build.VERSION_CODES#LOLLIPOP}
Mikhail Naganovd380b202014-04-10 18:07:42 +01002029 * and above, methods of injected Java objects are enumerable from
2030 * JavaScript.</li>
Steve Block3aa800b2012-04-24 13:01:34 +01002031 * </ul>
2032 *
2033 * @param object the Java object to inject into this WebView's JavaScript
Nate Fischer0a6140d2017-09-05 12:37:49 -07002034 * context. {@code null} values are ignored.
Steve Block3aa800b2012-04-24 13:01:34 +01002035 * @param name the name used to expose the object in JavaScript
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 */
Steve Block4cd73c52011-11-09 13:06:52 +00002037 public void addJavascriptInterface(Object object, String name) {
Steve Block51b08912011-04-27 15:04:48 +01002038 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002039 mProvider.addJavascriptInterface(object, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 }
2041
2042 /**
Steve Block3aa800b2012-04-24 13:01:34 +01002043 * Removes a previously injected Java object from this WebView. Note that
2044 * the removal will not be reflected in JavaScript until the page is next
2045 * (re)loaded. See {@link #addJavascriptInterface}.
2046 *
2047 * @param name the name used to expose the object in JavaScript
Steve Block689a3422010-12-07 18:18:26 +00002048 */
Nate Fischer3442c742017-09-08 17:02:00 -07002049 public void removeJavascriptInterface(@NonNull String name) {
Steve Block51b08912011-04-27 15:04:48 +01002050 checkThread();
Steve Block3aa800b2012-04-24 13:01:34 +01002051 mProvider.removeJavascriptInterface(name);
Steve Block689a3422010-12-07 18:18:26 +00002052 }
2053
2054 /**
Selim Gurun4c8093a2015-03-10 17:40:06 -07002055 * Creates a message channel to communicate with JS and returns the message
2056 * ports that represent the endpoints of this message channel. The HTML5 message
Selim Gurun0a814b62015-05-13 10:15:55 -07002057 * channel functionality is described
2058 * <a href="https://html.spec.whatwg.org/multipage/comms.html#messagechannel">here
2059 * </a>
Selim Gurun4c8093a2015-03-10 17:40:06 -07002060 *
Nate Fischerf02f8462017-09-11 15:16:33 -07002061 * <p>The returned message channels are entangled and already in started state.
Selim Gurun4c8093a2015-03-10 17:40:06 -07002062 *
2063 * @return the two message ports that form the message channel.
2064 */
2065 public WebMessagePort[] createWebMessageChannel() {
2066 checkThread();
Selim Gurun4c8093a2015-03-10 17:40:06 -07002067 return mProvider.createWebMessageChannel();
2068 }
2069
2070 /**
2071 * Post a message to main frame. The embedded application can restrict the
2072 * messages to a certain target origin. See
Selim Gurun0a814b62015-05-13 10:15:55 -07002073 * <a href="https://html.spec.whatwg.org/multipage/comms.html#posting-messages">
2074 * HTML5 spec</a> for how target origin can be used.
Selim Gurunb18fbfa2017-05-18 11:50:26 -07002075 * <p>
2076 * A target origin can be set as a wildcard ("*"). However this is not recommended.
2077 * See the page above for security issues.
Torne (Richard Coles)97bf0be2018-05-16 10:51:56 -04002078 * <p>
2079 * Content loaded via {@link #loadData(String,String,String)} will not have a
2080 * valid origin, and thus cannot be sent messages securely. If you need to send
2081 * messages using this function, you should use
2082 * {@link #loadDataWithBaseURL(String,String,String,String,String)} with a valid
2083 * HTTP or HTTPS {@code baseUrl} to define a valid origin that can be used for
2084 * messaging.
Selim Gurun4c8093a2015-03-10 17:40:06 -07002085 *
2086 * @param message the WebMessage
Selim Gurunb18fbfa2017-05-18 11:50:26 -07002087 * @param targetOrigin the target origin.
Selim Gurun4c8093a2015-03-10 17:40:06 -07002088 */
Selim Gurun26de90a2015-05-15 11:02:45 -07002089 public void postWebMessage(WebMessage message, Uri targetOrigin) {
Selim Gurun4c8093a2015-03-10 17:40:06 -07002090 checkThread();
Selim Gurun4c8093a2015-03-10 17:40:06 -07002091 mProvider.postMessageToMainFrame(message, targetOrigin);
2092 }
2093
2094 /**
Steve Block4e584df2012-04-24 23:12:47 +01002095 * Gets the WebSettings object used to control the settings for this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 * WebView.
Steve Block4e584df2012-04-24 23:12:47 +01002097 *
2098 * @return a WebSettings object that can be used to control this WebView's
2099 * settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 */
2101 public WebSettings getSettings() {
Steve Block51b08912011-04-27 15:04:48 +01002102 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002103 return mProvider.getSettings();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 }
2105
Jonathan Dixon3c909522012-02-28 18:45:06 +00002106 /**
Mikhail Naganov057989e2013-09-06 16:35:25 -07002107 * Enables debugging of web contents (HTML / CSS / JavaScript)
2108 * loaded into any WebViews of this application. This flag can be enabled
2109 * in order to facilitate debugging of web layouts and JavaScript
2110 * code running inside WebViews. Please refer to WebView documentation
2111 * for the debugging guide.
2112 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07002113 * The default is {@code false}.
Mikhail Naganov057989e2013-09-06 16:35:25 -07002114 *
2115 * @param enabled whether to enable web contents debugging
2116 */
2117 public static void setWebContentsDebuggingEnabled(boolean enabled) {
Mikhail Naganov057989e2013-09-06 16:35:25 -07002118 getFactory().getStatics().setWebContentsDebuggingEnabled(enabled);
2119 }
2120
2121 /**
Steve Block4e584df2012-04-24 23:12:47 +01002122 * Gets the list of currently loaded plugins.
Jonathan Dixon3c909522012-02-28 18:45:06 +00002123 *
Steve Block4e584df2012-04-24 23:12:47 +01002124 * @return the list of currently loaded plugins
Jonathan Dixon3c909522012-02-28 18:45:06 +00002125 * @deprecated This was used for Gears, which has been deprecated.
Steve Block4e584df2012-04-24 23:12:47 +01002126 * @hide
Jonathan Dixon3c909522012-02-28 18:45:06 +00002127 */
Andrei Popescu385df692009-08-13 11:59:57 +01002128 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 public static synchronized PluginList getPluginList() {
Grace Klobabb245ea2009-11-10 13:13:24 -08002130 return new PluginList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 }
2132
Jonathan Dixon3c909522012-02-28 18:45:06 +00002133 /**
Torne (Richard Coles)12f64da2017-10-24 14:59:07 -04002134 * Define the directory used to store WebView data for the current process.
2135 * The provided suffix will be used when constructing data and cache
2136 * directory paths. If this API is not called, no suffix will be used.
2137 * Each directory can be used by only one process in the application. If more
2138 * than one process in an app wishes to use WebView, only one process can use
2139 * the default directory, and other processes must call this API to define
2140 * a unique suffix.
2141 * <p>
Torne (Richard Coles)de956b72018-01-29 16:58:44 -05002142 * This means that different processes in the same application cannot directly
2143 * share WebView-related data, since the data directories must be distinct.
2144 * Applications that use this API may have to explicitly pass data between
2145 * processes. For example, login cookies may have to be copied from one
2146 * process's cookie jar to the other using {@link CookieManager} if both
2147 * processes' WebViews are intended to be logged in.
2148 * <p>
2149 * Most applications should simply ensure that all components of the app
2150 * that rely on WebView are in the same process, to avoid needing multiple
2151 * data directories. The {@link #disableWebView} method can be used to ensure
2152 * that the other processes do not use WebView by accident in this case.
2153 * <p>
Torne (Richard Coles)12f64da2017-10-24 14:59:07 -04002154 * This API must be called before any instances of WebView are created in
2155 * this process and before any other methods in the android.webkit package
2156 * are called by this process.
2157 *
2158 * @param suffix The directory name suffix to be used for the current
2159 * process. Must not contain a path separator.
2160 * @throws IllegalStateException if WebView has already been initialized
2161 * in the current process.
2162 * @throws IllegalArgumentException if the suffix contains a path separator.
2163 */
2164 public static void setDataDirectorySuffix(String suffix) {
2165 WebViewFactory.setDataDirectorySuffix(suffix);
2166 }
2167
2168 /**
2169 * Indicate that the current process does not intend to use WebView, and
2170 * that an exception should be thrown if a WebView is created or any other
2171 * methods in the android.webkit package are used.
2172 * <p>
2173 * Applications with multiple processes may wish to call this in processes
Torne (Richard Coles)de956b72018-01-29 16:58:44 -05002174 * that are not intended to use WebView to avoid accidentally incurring
2175 * the memory usage of initializing WebView in long-lived processes that
2176 * have no need for it, and to prevent potential data directory conflicts
2177 * (see {@link #setDataDirectorySuffix}).
2178 * <p>
2179 * For example, an audio player application with one process for its
2180 * activities and another process for its playback service may wish to call
2181 * this method in the playback service's {@link android.app.Service#onCreate}.
Torne (Richard Coles)12f64da2017-10-24 14:59:07 -04002182 *
2183 * @throws IllegalStateException if WebView has already been initialized
2184 * in the current process.
2185 */
2186 public static void disableWebView() {
2187 WebViewFactory.disableWebView();
2188 }
2189
2190
2191 /**
Jonathan Dixon3c909522012-02-28 18:45:06 +00002192 * @deprecated This was used for Gears, which has been deprecated.
Steve Block4e584df2012-04-24 23:12:47 +01002193 * @hide
Jonathan Dixon3c909522012-02-28 18:45:06 +00002194 */
Andrei Popescu385df692009-08-13 11:59:57 +01002195 @Deprecated
Steve Block51b08912011-04-27 15:04:48 +01002196 public void refreshPlugins(boolean reloadOpenPages) {
2197 checkThread();
2198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199
Cary Clark966641a2010-03-04 08:41:56 -05002200 /**
Steve Block4e584df2012-04-24 23:12:47 +01002201 * Puts this WebView into text selection mode. Do not rely on this
2202 * functionality; it will be deprecated in the future.
2203 *
Kristian Monsenfc771652011-05-10 16:44:05 +01002204 * @deprecated This method is now obsolete.
Kristian Monsenf4912582012-08-16 15:26:24 -04002205 * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
Cary Clark966641a2010-03-04 08:41:56 -05002206 */
Kristian Monsenfc771652011-05-10 16:44:05 +01002207 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 public void emulateShiftHeld() {
Steve Block51b08912011-04-27 15:04:48 +01002209 checkThread();
Steve Howard16bd9372010-04-12 14:46:09 -07002210 }
2211
Leon Scrogginsa57632f2009-11-16 10:51:12 -05002212 /**
2213 * @deprecated WebView no longer needs to implement
2214 * ViewGroup.OnHierarchyChangeListener. This method does nothing now.
2215 */
George Mountfab67a12012-01-05 09:58:53 -08002216 @Override
Jonathan Dixone230e542011-12-21 10:57:00 +00002217 // Cannot add @hide as this can always be accessed via the interface.
Leon Scrogginsa57632f2009-11-16 10:51:12 -05002218 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 public void onChildViewAdded(View parent, View child) {}
Cary Clarkd6982c92009-05-29 11:02:22 -04002220
Leon Scrogginsa57632f2009-11-16 10:51:12 -05002221 /**
2222 * @deprecated WebView no longer needs to implement
2223 * ViewGroup.OnHierarchyChangeListener. This method does nothing now.
2224 */
George Mountfab67a12012-01-05 09:58:53 -08002225 @Override
Jonathan Dixone230e542011-12-21 10:57:00 +00002226 // Cannot add @hide as this can always be accessed via the interface.
Leon Scrogginsa57632f2009-11-16 10:51:12 -05002227 @Deprecated
2228 public void onChildViewRemoved(View p, View child) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229
2230 /**
2231 * @deprecated WebView should not have implemented
Gilles Debunne0e7d652d2011-02-22 15:26:14 -08002232 * ViewTreeObserver.OnGlobalFocusChangeListener. This method does nothing now.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 */
George Mountfab67a12012-01-05 09:58:53 -08002234 @Override
Jonathan Dixone230e542011-12-21 10:57:00 +00002235 // Cannot add @hide as this can always be accessed via the interface.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 @Deprecated
2237 public void onGlobalFocusChanged(View oldFocus, View newFocus) {
2238 }
2239
Kristian Monsen5cc23512012-08-09 15:33:08 -04002240 /**
Nate Fischer0a6140d2017-09-05 12:37:49 -07002241 * @deprecated Only the default case, {@code true}, will be supported in a future version.
Kristian Monsen5cc23512012-08-09 15:33:08 -04002242 */
2243 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 public void setMapTrackballToArrowKeys(boolean setMap) {
Steve Block51b08912011-04-27 15:04:48 +01002245 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002246 mProvider.setMapTrackballToArrowKeys(setMap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 }
2248
Derek Sollenberger03e48912010-05-18 17:03:42 -04002249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 public void flingScroll(int vx, int vy) {
Steve Block51b08912011-04-27 15:04:48 +01002251 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002252 mProvider.flingScroll(vx, vy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 }
2254
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002255 /**
Steve Block4e584df2012-04-24 23:12:47 +01002256 * Gets the zoom controls for this WebView, as a separate View. The caller
2257 * is responsible for inserting this View into the layout hierarchy.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002258 * <p/>
Steve Blockb838aef2012-04-23 18:22:15 +01002259 * API level {@link android.os.Build.VERSION_CODES#CUPCAKE} introduced
2260 * built-in zoom mechanisms for the WebView, as opposed to these separate
2261 * zoom controls. The built-in mechanisms are preferred and can be enabled
2262 * using {@link WebSettings#setBuiltInZoomControls}.
The Android Open Source Project10592532009-03-18 17:39:46 -07002263 *
Steve Block4e584df2012-04-24 23:12:47 +01002264 * @deprecated the built-in zoom mechanisms are preferred
Steve Blockb838aef2012-04-23 18:22:15 +01002265 * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 */
The Android Open Source Project10592532009-03-18 17:39:46 -07002267 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 public View getZoomControls() {
Steve Block51b08912011-04-27 15:04:48 +01002269 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002270 return mProvider.getZoomControls();
Mangesh Ghiwarefaab93d2011-09-21 13:58:47 -07002271 }
2272
2273 /**
Steve Block4e584df2012-04-24 23:12:47 +01002274 * Gets whether this WebView can be zoomed in.
2275 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07002276 * @return {@code true} if this WebView can be zoomed in
Kristian Monsen5cc23512012-08-09 15:33:08 -04002277 *
2278 * @deprecated This method is prone to inaccuracy due to race conditions
2279 * between the web rendering and UI threads; prefer
2280 * {@link WebViewClient#onScaleChanged}.
Grace Kloba6164ef12010-06-01 15:59:13 -07002281 */
Kristian Monsen5cc23512012-08-09 15:33:08 -04002282 @Deprecated
Grace Kloba6164ef12010-06-01 15:59:13 -07002283 public boolean canZoomIn() {
Steve Block51b08912011-04-27 15:04:48 +01002284 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002285 return mProvider.canZoomIn();
Grace Kloba6164ef12010-06-01 15:59:13 -07002286 }
2287
2288 /**
Steve Block4e584df2012-04-24 23:12:47 +01002289 * Gets whether this WebView can be zoomed out.
2290 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07002291 * @return {@code true} if this WebView can be zoomed out
Kristian Monsen5cc23512012-08-09 15:33:08 -04002292 *
2293 * @deprecated This method is prone to inaccuracy due to race conditions
2294 * between the web rendering and UI threads; prefer
2295 * {@link WebViewClient#onScaleChanged}.
Grace Kloba6164ef12010-06-01 15:59:13 -07002296 */
Kristian Monsen5cc23512012-08-09 15:33:08 -04002297 @Deprecated
Grace Kloba6164ef12010-06-01 15:59:13 -07002298 public boolean canZoomOut() {
Steve Block51b08912011-04-27 15:04:48 +01002299 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002300 return mProvider.canZoomOut();
Grace Kloba6164ef12010-06-01 15:59:13 -07002301 }
2302
2303 /**
Marcin Kosiba3ee06ef2014-07-15 17:59:51 +01002304 * Performs a zoom operation in this WebView.
2305 *
Nate Fischerffb81c42016-09-26 12:35:47 -07002306 * @param zoomFactor the zoom factor to apply. The zoom factor will be clamped to the WebView's
Marcin Kosiba3ee06ef2014-07-15 17:59:51 +01002307 * zoom limits. This value must be in the range 0.01 to 100.0 inclusive.
Marcin Kosiba3ee06ef2014-07-15 17:59:51 +01002308 */
Martin Kosiba3a82acd2014-09-05 11:03:58 +01002309 public void zoomBy(float zoomFactor) {
Marcin Kosiba3ee06ef2014-07-15 17:59:51 +01002310 checkThread();
2311 if (zoomFactor < 0.01)
2312 throw new IllegalArgumentException("zoomFactor must be greater than 0.01.");
2313 if (zoomFactor > 100.0)
2314 throw new IllegalArgumentException("zoomFactor must be less than 100.");
Martin Kosiba3a82acd2014-09-05 11:03:58 +01002315 mProvider.zoomBy(zoomFactor);
Marcin Kosiba3ee06ef2014-07-15 17:59:51 +01002316 }
2317
2318 /**
Steve Block4e584df2012-04-24 23:12:47 +01002319 * Performs zoom in in this WebView.
2320 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07002321 * @return {@code true} if zoom in succeeds, {@code false} if no zoom changes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 */
2323 public boolean zoomIn() {
Steve Block51b08912011-04-27 15:04:48 +01002324 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002325 return mProvider.zoomIn();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 }
2327
2328 /**
Steve Block4e584df2012-04-24 23:12:47 +01002329 * Performs zoom out in this WebView.
2330 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07002331 * @return {@code true} if zoom out succeeds, {@code false} if no zoom changes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 */
2333 public boolean zoomOut() {
Steve Block51b08912011-04-27 15:04:48 +01002334 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002335 return mProvider.zoomOut();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 }
2337
Kristian Monsenfc771652011-05-10 16:44:05 +01002338 /**
2339 * @deprecated This method is now obsolete.
Kristian Monsenf4912582012-08-16 15:26:24 -04002340 * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
Kristian Monsenfc771652011-05-10 16:44:05 +01002341 */
2342 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 public void debugDump() {
Steve Block51b08912011-04-27 15:04:48 +01002344 checkThread();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 }
Cary Clarkd6982c92009-05-29 11:02:22 -04002346
John Reck926cf562012-06-14 10:00:31 -07002347 /**
John Reckf2361152012-06-14 15:23:22 -07002348 * See {@link ViewDebug.HierarchyHandler#dumpViewHierarchyWithProperties(BufferedWriter, int)}
John Reck926cf562012-06-14 10:00:31 -07002349 * @hide
2350 */
2351 @Override
2352 public void dumpViewHierarchyWithProperties(BufferedWriter out, int level) {
2353 mProvider.dumpViewHierarchyWithProperties(out, level);
2354 }
2355
2356 /**
2357 * See {@link ViewDebug.HierarchyHandler#findHierarchyView(String, int)}
2358 * @hide
2359 */
2360 @Override
2361 public View findHierarchyView(String className, int hashCode) {
2362 return mProvider.findHierarchyView(className, hashCode);
2363 }
2364
Tobias Sargeantd10e4af2017-01-19 14:03:09 +00002365 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -07002366 @IntDef(prefix = { "RENDERER_PRIORITY_" }, value = {
2367 RENDERER_PRIORITY_WAIVED,
2368 RENDERER_PRIORITY_BOUND,
2369 RENDERER_PRIORITY_IMPORTANT
Tobias Sargeantd10e4af2017-01-19 14:03:09 +00002370 })
2371 @Retention(RetentionPolicy.SOURCE)
2372 public @interface RendererPriority {}
2373
2374 /**
2375 * The renderer associated with this WebView is bound with
2376 * {@link Context#BIND_WAIVE_PRIORITY}. At this priority level
2377 * {@link WebView} renderers will be strong targets for out of memory
2378 * killing.
2379 *
2380 * Use with {@link #setRendererPriorityPolicy}.
2381 */
2382 public static final int RENDERER_PRIORITY_WAIVED = 0;
2383 /**
2384 * The renderer associated with this WebView is bound with
2385 * the default priority for services.
2386 *
2387 * Use with {@link #setRendererPriorityPolicy}.
2388 */
2389 public static final int RENDERER_PRIORITY_BOUND = 1;
2390 /**
2391 * The renderer associated with this WebView is bound with
2392 * {@link Context#BIND_IMPORTANT}.
2393 *
2394 * Use with {@link #setRendererPriorityPolicy}.
2395 */
2396 public static final int RENDERER_PRIORITY_IMPORTANT = 2;
2397
2398 /**
2399 * Set the renderer priority policy for this {@link WebView}. The
2400 * priority policy will be used to determine whether an out of
2401 * process renderer should be considered to be a target for OOM
2402 * killing.
2403 *
2404 * Because a renderer can be associated with more than one
2405 * WebView, the final priority it is computed as the maximum of
2406 * any attached WebViews. When a WebView is destroyed it will
2407 * cease to be considerered when calculating the renderer
2408 * priority. Once no WebViews remain associated with the renderer,
2409 * the priority of the renderer will be reduced to
2410 * {@link #RENDERER_PRIORITY_WAIVED}.
2411 *
2412 * The default policy is to set the priority to
2413 * {@link #RENDERER_PRIORITY_IMPORTANT} regardless of visibility,
2414 * and this should not be changed unless the caller also handles
2415 * renderer crashes with
2416 * {@link WebViewClient#onRenderProcessGone}. Any other setting
2417 * will result in WebView renderers being killed by the system
2418 * more aggressively than the application.
2419 *
2420 * @param rendererRequestedPriority the minimum priority at which
2421 * this WebView desires the renderer process to be bound.
Nate Fischer0a6140d2017-09-05 12:37:49 -07002422 * @param waivedWhenNotVisible if {@code true}, this flag specifies that
Tobias Sargeantd10e4af2017-01-19 14:03:09 +00002423 * when this WebView is not visible, it will be treated as
2424 * if it had requested a priority of
2425 * {@link #RENDERER_PRIORITY_WAIVED}.
2426 */
2427 public void setRendererPriorityPolicy(
2428 @RendererPriority int rendererRequestedPriority,
2429 boolean waivedWhenNotVisible) {
2430 mProvider.setRendererPriorityPolicy(rendererRequestedPriority, waivedWhenNotVisible);
2431 }
2432
2433 /**
2434 * Get the requested renderer priority for this WebView.
2435 *
2436 * @return the requested renderer priority policy.
2437 */
2438 @RendererPriority
2439 public int getRendererRequestedPriority() {
2440 return mProvider.getRendererRequestedPriority();
2441 }
2442
2443 /**
2444 * Return whether this WebView requests a priority of
2445 * {@link #RENDERER_PRIORITY_WAIVED} when not visible.
2446 *
2447 * @return whether this WebView requests a priority of
2448 * {@link #RENDERER_PRIORITY_WAIVED} when not visible.
2449 */
2450 public boolean getRendererPriorityWaivedWhenNotVisible() {
2451 return mProvider.getRendererPriorityWaivedWhenNotVisible();
2452 }
Tima Vaisburdcf49a232017-03-28 11:35:58 -07002453
2454 /**
2455 * Sets the {@link TextClassifier} for this WebView.
2456 */
2457 public void setTextClassifier(@Nullable TextClassifier textClassifier) {
2458 mProvider.setTextClassifier(textClassifier);
2459 }
2460
2461 /**
2462 * Returns the {@link TextClassifier} used by this WebView.
2463 * If no TextClassifier has been set, this WebView uses the default set by the system.
2464 */
2465 @NonNull
2466 public TextClassifier getTextClassifier() {
2467 return mProvider.getTextClassifier();
2468 }
2469
Gustav Sennton265afdb2018-01-19 11:56:55 +00002470 /**
2471 * Returns the {@link ClassLoader} used to load internal WebView classes.
2472 * This method is meant for use by the WebView Support Library, there is no reason to use this
2473 * method otherwise.
2474 */
2475 @NonNull
2476 public static ClassLoader getWebViewClassLoader() {
2477 return getFactory().getWebViewClassLoader();
2478 }
2479
Gustav Sennton01673692018-01-19 12:11:25 +00002480 /**
2481 * Returns the {@link Looper} corresponding to the thread on which WebView calls must be made.
2482 */
2483 @NonNull
Gustav Sennton6abde8b2018-03-15 13:56:03 +00002484 public Looper getWebViewLooper() {
2485 return mWebViewThread;
2486 }
2487
Jonathan Dixon3c909522012-02-28 18:45:06 +00002488 //-------------------------------------------------------------------------
2489 // Interface for WebView providers
2490 //-------------------------------------------------------------------------
2491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 /**
Steve Block4e584df2012-04-24 23:12:47 +01002493 * Gets the WebViewProvider. Used by providers to obtain the underlying
Nate Fischerffb81c42016-09-26 12:35:47 -07002494 * implementation, e.g. when the application responds to
Steve Block4e584df2012-04-24 23:12:47 +01002495 * WebViewClient.onCreateWindow() request.
Mike Reedefe2c722009-10-14 09:42:02 -04002496 *
Jonathan Dixon3c909522012-02-28 18:45:06 +00002497 * @hide WebViewProvider is not public API.
Mike Reedefe2c722009-10-14 09:42:02 -04002498 */
Ignacio Solla451e3382014-11-10 10:35:54 +00002499 @SystemApi
Jonathan Dixon3c909522012-02-28 18:45:06 +00002500 public WebViewProvider getWebViewProvider() {
2501 return mProvider;
Mike Reedefe2c722009-10-14 09:42:02 -04002502 }
2503
2504 /**
Jonathan Dixon3c909522012-02-28 18:45:06 +00002505 * Callback interface, allows the provider implementation to access non-public methods
2506 * and fields, and make super-class calls in this WebView instance.
2507 * @hide Only for use by WebViewProvider implementations
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -08002508 */
Ignacio Solla451e3382014-11-10 10:35:54 +00002509 @SystemApi
Jonathan Dixon3c909522012-02-28 18:45:06 +00002510 public class PrivateAccess {
2511 // ---- Access to super-class methods ----
2512 public int super_getScrollBarStyle() {
2513 return WebView.super.getScrollBarStyle();
2514 }
2515
2516 public void super_scrollTo(int scrollX, int scrollY) {
2517 WebView.super.scrollTo(scrollX, scrollY);
2518 }
2519
2520 public void super_computeScroll() {
2521 WebView.super.computeScroll();
2522 }
2523
alanveebebc92012-06-15 18:59:11 -07002524 public boolean super_onHoverEvent(MotionEvent event) {
2525 return WebView.super.onHoverEvent(event);
2526 }
2527
Marcin Kosiba3e853fb2015-01-08 12:45:40 +00002528 public boolean super_performAccessibilityAction(int action, Bundle arguments) {
Alan Viverettea54956a2015-01-07 16:05:02 -08002529 return WebView.super.performAccessibilityActionInternal(action, arguments);
alanv448902d2012-05-16 20:27:47 -07002530 }
2531
Jonathan Dixon3c909522012-02-28 18:45:06 +00002532 public boolean super_performLongClick() {
2533 return WebView.super.performLongClick();
2534 }
2535
2536 public boolean super_setFrame(int left, int top, int right, int bottom) {
2537 return WebView.super.setFrame(left, top, right, bottom);
2538 }
2539
2540 public boolean super_dispatchKeyEvent(KeyEvent event) {
2541 return WebView.super.dispatchKeyEvent(event);
2542 }
2543
2544 public boolean super_onGenericMotionEvent(MotionEvent event) {
2545 return WebView.super.onGenericMotionEvent(event);
2546 }
2547
2548 public boolean super_requestFocus(int direction, Rect previouslyFocusedRect) {
2549 return WebView.super.requestFocus(direction, previouslyFocusedRect);
2550 }
2551
2552 public void super_setLayoutParams(ViewGroup.LayoutParams params) {
2553 WebView.super.setLayoutParams(params);
2554 }
2555
Hui Shuf119c522015-10-08 10:07:13 -07002556 public void super_startActivityForResult(Intent intent, int requestCode) {
2557 WebView.super.startActivityForResult(intent, requestCode);
2558 }
2559
Jonathan Dixon3c909522012-02-28 18:45:06 +00002560 // ---- Access to non-public methods ----
2561 public void overScrollBy(int deltaX, int deltaY,
2562 int scrollX, int scrollY,
2563 int scrollRangeX, int scrollRangeY,
2564 int maxOverScrollX, int maxOverScrollY,
2565 boolean isTouchEvent) {
2566 WebView.this.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY,
2567 maxOverScrollX, maxOverScrollY, isTouchEvent);
2568 }
2569
2570 public void awakenScrollBars(int duration) {
2571 WebView.this.awakenScrollBars(duration);
2572 }
2573
2574 public void awakenScrollBars(int duration, boolean invalidate) {
2575 WebView.this.awakenScrollBars(duration, invalidate);
2576 }
2577
2578 public float getVerticalScrollFactor() {
2579 return WebView.this.getVerticalScrollFactor();
2580 }
2581
2582 public float getHorizontalScrollFactor() {
2583 return WebView.this.getHorizontalScrollFactor();
2584 }
2585
2586 public void setMeasuredDimension(int measuredWidth, int measuredHeight) {
2587 WebView.this.setMeasuredDimension(measuredWidth, measuredHeight);
2588 }
2589
2590 public void onScrollChanged(int l, int t, int oldl, int oldt) {
2591 WebView.this.onScrollChanged(l, t, oldl, oldt);
2592 }
2593
2594 public int getHorizontalScrollbarHeight() {
2595 return WebView.this.getHorizontalScrollbarHeight();
2596 }
2597
Martin Kosiba9a68f822013-08-08 14:02:41 +01002598 public void super_onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
2599 int l, int t, int r, int b) {
2600 WebView.super.onDrawVerticalScrollBar(canvas, scrollBar, l, t, r, b);
2601 }
2602
Jonathan Dixon3c909522012-02-28 18:45:06 +00002603 // ---- Access to (non-public) fields ----
2604 /** Raw setter for the scroll X value, without invoking onScrollChanged handlers etc. */
2605 public void setScrollXRaw(int scrollX) {
2606 WebView.this.mScrollX = scrollX;
2607 }
2608
2609 /** Raw setter for the scroll Y value, without invoking onScrollChanged handlers etc. */
2610 public void setScrollYRaw(int scrollY) {
2611 WebView.this.mScrollY = scrollY;
2612 }
2613
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -08002614 }
2615
Jonathan Dixon3c909522012-02-28 18:45:06 +00002616 //-------------------------------------------------------------------------
Ben Murdoch52c9f7f2013-01-18 00:50:37 +00002617 // Package-private internal stuff
2618 //-------------------------------------------------------------------------
2619
2620 // Only used by android.webkit.FindActionModeCallback.
2621 void setFindDialogFindListener(FindListener listener) {
2622 checkThread();
2623 setupFindListenerIfNeeded();
2624 mFindListener.mFindDialogFindListener = listener;
2625 }
2626
2627 // Only used by android.webkit.FindActionModeCallback.
2628 void notifyFindDialogDismissed() {
2629 checkThread();
2630 mProvider.notifyFindDialogDismissed();
2631 }
2632
2633 //-------------------------------------------------------------------------
Jonathan Dixon3c909522012-02-28 18:45:06 +00002634 // Private internal stuff
2635 //-------------------------------------------------------------------------
2636
Jonathan Dixon3c909522012-02-28 18:45:06 +00002637 private WebViewProvider mProvider;
2638
Ben Murdoch52c9f7f2013-01-18 00:50:37 +00002639 /**
2640 * In addition to the FindListener that the user may set via the WebView.setFindListener
2641 * API, FindActionModeCallback will register it's own FindListener. We keep them separate
Mark Dolinerd0646dc2014-08-27 16:04:02 -07002642 * via this class so that the two FindListeners can potentially exist at once.
Ben Murdoch52c9f7f2013-01-18 00:50:37 +00002643 */
2644 private class FindListenerDistributor implements FindListener {
2645 private FindListener mFindDialogFindListener;
2646 private FindListener mUserFindListener;
2647
2648 @Override
2649 public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
2650 boolean isDoneCounting) {
2651 if (mFindDialogFindListener != null) {
2652 mFindDialogFindListener.onFindResultReceived(activeMatchOrdinal, numberOfMatches,
2653 isDoneCounting);
2654 }
2655
2656 if (mUserFindListener != null) {
2657 mUserFindListener.onFindResultReceived(activeMatchOrdinal, numberOfMatches,
2658 isDoneCounting);
2659 }
2660 }
2661 }
2662 private FindListenerDistributor mFindListener;
2663
2664 private void setupFindListenerIfNeeded() {
2665 if (mFindListener == null) {
2666 mFindListener = new FindListenerDistributor();
2667 mProvider.setFindListener(mFindListener);
2668 }
2669 }
2670
Jonathan Dixon3c909522012-02-28 18:45:06 +00002671 private void ensureProviderCreated() {
2672 checkThread();
2673 if (mProvider == null) {
Jonathan Dixon3c909522012-02-28 18:45:06 +00002674 // As this can get called during the base class constructor chain, pass the minimum
2675 // number of dependencies here; the rest are deferred to init().
2676 mProvider = getFactory().createWebView(this, new PrivateAccess());
2677 }
Ben Murdochecbc65c2010-01-13 10:54:56 +00002678 }
2679
Torne (Richard Coles)59375a02016-07-18 18:34:02 +01002680 private static WebViewFactoryProvider getFactory() {
Jonathan Dixond3101b12012-04-12 20:51:51 +01002681 return WebViewFactory.getProvider();
Ben Murdoch1708ad52010-11-11 15:56:16 +00002682 }
2683
Jonathan Dixon517771b2013-10-08 13:32:11 -07002684 private final Looper mWebViewThread = Looper.myLooper();
2685
2686 private void checkThread() {
2687 // Ignore mWebViewThread == null because this can be called during in the super class
2688 // constructor, before this class's own constructor has even started.
2689 if (mWebViewThread != null && Looper.myLooper() != mWebViewThread) {
Steve Block7a01d942011-09-26 12:30:31 +01002690 Throwable throwable = new Throwable(
Jonathan Dixon517771b2013-10-08 13:32:11 -07002691 "A WebView method was called on thread '" +
Steve Block08d584c2011-05-17 19:05:03 +01002692 Thread.currentThread().getName() + "'. " +
Jonathan Dixon517771b2013-10-08 13:32:11 -07002693 "All WebView methods must be called on the same thread. " +
2694 "(Expected Looper " + mWebViewThread + " called on " + Looper.myLooper() +
2695 ", FYI main Looper is " + Looper.getMainLooper() + ")");
Steve Block7a01d942011-09-26 12:30:31 +01002696 Log.w(LOGTAG, Log.getStackTraceString(throwable));
2697 StrictMode.onWebViewMethodCalledOnWrongThread(throwable);
Kristian Monsenb5cd8c02013-04-09 17:57:37 -07002698
2699 if (sEnforceThreadChecking) {
2700 throw new RuntimeException(throwable);
2701 }
Steve Block51b08912011-04-27 15:04:48 +01002702 }
2703 }
2704
Jonathan Dixon3c909522012-02-28 18:45:06 +00002705 //-------------------------------------------------------------------------
2706 // Override View methods
2707 //-------------------------------------------------------------------------
2708
2709 // TODO: Add a test that enumerates all methods in ViewDelegte & ScrollDelegate, and ensures
2710 // there's a corresponding override (or better, caller) for each of them in here.
2711
2712 @Override
2713 protected void onAttachedToWindow() {
2714 super.onAttachedToWindow();
2715 mProvider.getViewDelegate().onAttachedToWindow();
Chris Craik555c55e2011-07-28 15:39:43 -07002716 }
2717
John Reck34dbce12014-03-06 12:52:24 -08002718 /** @hide */
Jonathan Dixon3c909522012-02-28 18:45:06 +00002719 @Override
John Reck34dbce12014-03-06 12:52:24 -08002720 protected void onDetachedFromWindowInternal() {
Jonathan Dixon3c909522012-02-28 18:45:06 +00002721 mProvider.getViewDelegate().onDetachedFromWindow();
John Reck34dbce12014-03-06 12:52:24 -08002722 super.onDetachedFromWindowInternal();
Chris Craik555c55e2011-07-28 15:39:43 -07002723 }
2724
Tima Vaisburd5544f6c2017-04-27 10:53:27 -07002725 /** @hide */
Jonathan Dixon3c909522012-02-28 18:45:06 +00002726 @Override
Tima Vaisburd134752b2017-04-06 15:50:05 -07002727 public void onMovedToDisplay(int displayId, Configuration config) {
2728 mProvider.getViewDelegate().onMovedToDisplay(displayId, config);
2729 }
2730
2731 @Override
Jonathan Dixon3c909522012-02-28 18:45:06 +00002732 public void setLayoutParams(ViewGroup.LayoutParams params) {
2733 mProvider.getViewDelegate().setLayoutParams(params);
Chris Craik445ab742011-07-13 13:19:37 -07002734 }
2735
Jonathan Dixon3c909522012-02-28 18:45:06 +00002736 @Override
2737 public void setOverScrollMode(int mode) {
2738 super.setOverScrollMode(mode);
Martin Kosiba5ee743e2012-12-21 12:39:50 +00002739 // This method may be called in the constructor chain, before the WebView provider is
2740 // created.
Jonathan Dixon3c909522012-02-28 18:45:06 +00002741 ensureProviderCreated();
2742 mProvider.getViewDelegate().setOverScrollMode(mode);
Chris Craik445ab742011-07-13 13:19:37 -07002743 }
2744
Jonathan Dixon3c909522012-02-28 18:45:06 +00002745 @Override
2746 public void setScrollBarStyle(int style) {
2747 mProvider.getViewDelegate().setScrollBarStyle(style);
2748 super.setScrollBarStyle(style);
George Mount3d095312012-01-10 11:24:02 -08002749 }
2750
Jonathan Dixon3c909522012-02-28 18:45:06 +00002751 @Override
2752 protected int computeHorizontalScrollRange() {
2753 return mProvider.getScrollDelegate().computeHorizontalScrollRange();
John Reck5528d7c2012-02-28 11:29:29 -08002754 }
2755
Jonathan Dixon3c909522012-02-28 18:45:06 +00002756 @Override
2757 protected int computeHorizontalScrollOffset() {
2758 return mProvider.getScrollDelegate().computeHorizontalScrollOffset();
2759 }
Grace Kloba8abd50b2010-07-08 15:02:14 -07002760
Jonathan Dixon3c909522012-02-28 18:45:06 +00002761 @Override
2762 protected int computeVerticalScrollRange() {
2763 return mProvider.getScrollDelegate().computeVerticalScrollRange();
2764 }
Patrick Scotta3ebcc92010-07-16 11:52:22 -04002765
Jonathan Dixon3c909522012-02-28 18:45:06 +00002766 @Override
2767 protected int computeVerticalScrollOffset() {
2768 return mProvider.getScrollDelegate().computeVerticalScrollOffset();
2769 }
Ben Murdochfa148f62011-02-01 20:51:27 +00002770
Jonathan Dixon3c909522012-02-28 18:45:06 +00002771 @Override
2772 protected int computeVerticalScrollExtent() {
2773 return mProvider.getScrollDelegate().computeVerticalScrollExtent();
2774 }
2775
2776 @Override
2777 public void computeScroll() {
2778 mProvider.getScrollDelegate().computeScroll();
2779 }
2780
2781 @Override
2782 public boolean onHoverEvent(MotionEvent event) {
2783 return mProvider.getViewDelegate().onHoverEvent(event);
2784 }
2785
2786 @Override
2787 public boolean onTouchEvent(MotionEvent event) {
2788 return mProvider.getViewDelegate().onTouchEvent(event);
2789 }
2790
2791 @Override
2792 public boolean onGenericMotionEvent(MotionEvent event) {
2793 return mProvider.getViewDelegate().onGenericMotionEvent(event);
2794 }
2795
2796 @Override
2797 public boolean onTrackballEvent(MotionEvent event) {
2798 return mProvider.getViewDelegate().onTrackballEvent(event);
2799 }
2800
2801 @Override
2802 public boolean onKeyDown(int keyCode, KeyEvent event) {
2803 return mProvider.getViewDelegate().onKeyDown(keyCode, event);
2804 }
2805
2806 @Override
2807 public boolean onKeyUp(int keyCode, KeyEvent event) {
2808 return mProvider.getViewDelegate().onKeyUp(keyCode, event);
2809 }
2810
2811 @Override
2812 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
2813 return mProvider.getViewDelegate().onKeyMultiple(keyCode, repeatCount, event);
2814 }
2815
2816 /*
2817 TODO: These are not currently implemented in WebViewClassic, but it seems inconsistent not
2818 to be delegating them too.
2819
2820 @Override
2821 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
2822 return mProvider.getViewDelegate().onKeyPreIme(keyCode, event);
2823 }
2824 @Override
2825 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
2826 return mProvider.getViewDelegate().onKeyLongPress(keyCode, event);
2827 }
2828 @Override
2829 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2830 return mProvider.getViewDelegate().onKeyShortcut(keyCode, event);
2831 }
2832 */
2833
Ben Murdoche3f90712013-06-05 14:19:48 +01002834 @Override
2835 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
2836 AccessibilityNodeProvider provider =
2837 mProvider.getViewDelegate().getAccessibilityNodeProvider();
2838 return provider == null ? super.getAccessibilityNodeProvider() : provider;
2839 }
2840
Jonathan Dixon3c909522012-02-28 18:45:06 +00002841 @Deprecated
2842 @Override
2843 public boolean shouldDelayChildPressedState() {
2844 return mProvider.getViewDelegate().shouldDelayChildPressedState();
2845 }
2846
Felipe Leme1ca634a2016-11-28 17:21:21 -08002847 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -08002848 public CharSequence getAccessibilityClassName() {
2849 return WebView.class.getName();
2850 }
2851
Selim Gurun1b650cb2015-04-06 20:13:37 -07002852 @Override
Dianne Hackborn49b043f2015-05-07 14:21:38 -07002853 public void onProvideVirtualStructure(ViewStructure structure) {
Selim Gurun50b26a52015-06-01 16:26:10 -07002854 mProvider.getViewDelegate().onProvideVirtualStructure(structure);
Selim Gurun1b650cb2015-04-06 20:13:37 -07002855 }
2856
Felipe Leme1a1e4682017-03-13 16:34:03 -07002857 /**
2858 * {@inheritDoc}
2859 *
2860 * <p>The {@link ViewStructure} traditionally represents a {@link View}, while for web pages
2861 * it represent HTML nodes. Hence, it's necessary to "map" the HTML properties in a way that is
2862 * understood by the {@link android.service.autofill.AutofillService} implementations:
2863 *
2864 * <ol>
Felipe Leme303b6092017-07-31 18:01:15 -07002865 * <li>Only the HTML nodes inside a {@code FORM} are generated.
2866 * <li>The source of the HTML is set using {@link ViewStructure#setWebDomain(String)} in the
2867 * node representing the WebView.
2868 * <li>If a web page has multiple {@code FORM}s, only the data for the current form is
2869 * represented&mdash;if the user taps a field from another form, then the current autofill
2870 * context is canceled (by calling {@link android.view.autofill.AutofillManager#cancel()} and
2871 * a new context is created for that {@code FORM}.
2872 * <li>Similarly, if the page has {@code IFRAME} nodes, they are not initially represented in
2873 * the view structure until the user taps a field from a {@code FORM} inside the
2874 * {@code IFRAME}, in which case it would be treated the same way as multiple forms described
2875 * above, except that the {@link ViewStructure#setWebDomain(String) web domain} of the
2876 * {@code FORM} contains the {@code src} attribute from the {@code IFRAME} node.
Felipe Leme1a1e4682017-03-13 16:34:03 -07002877 * <li>The W3C autofill field ({@code autocomplete} tag attribute) maps to
Felipe Leme58390fe2017-09-15 15:16:28 -07002878 * {@link ViewStructure#setAutofillHints(String[])}.
2879 * <li>If the view is editable, the {@link ViewStructure#setAutofillType(int)} and
Felipe Leme6dcec872017-05-25 11:24:23 -07002880 * {@link ViewStructure#setAutofillValue(AutofillValue)} must be set.
Felipe Leme1a1e4682017-03-13 16:34:03 -07002881 * <li>The {@code placeholder} attribute maps to {@link ViewStructure#setHint(CharSequence)}.
Felipe Leme25bf7872017-03-28 15:32:29 -07002882 * <li>Other HTML attributes can be represented through
2883 * {@link ViewStructure#setHtmlInfo(android.view.ViewStructure.HtmlInfo)}.
Felipe Leme1a1e4682017-03-13 16:34:03 -07002884 * </ol>
2885 *
Felipe Leme58390fe2017-09-15 15:16:28 -07002886 * <p>If the WebView implementation can determine that the value of a field was set statically
2887 * (for example, not through Javascript), it should also call
2888 * {@code structure.setDataIsSensitive(false)}.
Felipe Leme6dcec872017-05-25 11:24:23 -07002889 *
Felipe Leme58390fe2017-09-15 15:16:28 -07002890 * <p>For example, an HTML form with 2 fields for username and password:
Felipe Leme1a1e4682017-03-13 16:34:03 -07002891 *
2892 * <pre class="prettyprint">
Felipe Lemef485f892017-11-22 08:15:59 -08002893 * &lt;label&gt;Username:&lt;/label&gt;
Felipe Leme6dcec872017-05-25 11:24:23 -07002894 * &lt;input type="text" name="username" id="user" value="Type your username" autocomplete="username" placeholder="Email or username"&gt;
Felipe Lemef485f892017-11-22 08:15:59 -08002895 * &lt;label&gt;Password:&lt;/label&gt;
Felipe Leme6dcec872017-05-25 11:24:23 -07002896 * &lt;input type="password" name="password" id="pass" autocomplete="current-password" placeholder="Password"&gt;
Felipe Leme1a1e4682017-03-13 16:34:03 -07002897 * </pre>
2898 *
2899 * <p>Would map to:
2900 *
2901 * <pre class="prettyprint">
Felipe Leme25bf7872017-03-28 15:32:29 -07002902 * int index = structure.addChildCount(2);
2903 * ViewStructure username = structure.newChild(index);
Felipe Leme6dcec872017-05-25 11:24:23 -07002904 * username.setAutofillId(structure.getAutofillId(), 1); // id 1 - first child
Felipe Leme1a1e4682017-03-13 16:34:03 -07002905 * username.setAutofillHints("username");
Felipe Leme6dcec872017-05-25 11:24:23 -07002906 * username.setHtmlInfo(username.newHtmlInfoBuilder("input")
2907 * .addAttribute("type", "text")
Felipe Leme25bf7872017-03-28 15:32:29 -07002908 * .addAttribute("name", "username")
Felipe Lemef485f892017-11-22 08:15:59 -08002909 * .addAttribute("label", "Username:")
Felipe Leme25bf7872017-03-28 15:32:29 -07002910 * .build());
Felipe Leme1a1e4682017-03-13 16:34:03 -07002911 * username.setHint("Email or username");
2912 * username.setAutofillType(View.AUTOFILL_TYPE_TEXT);
Felipe Leme6dcec872017-05-25 11:24:23 -07002913 * username.setAutofillValue(AutofillValue.forText("Type your username"));
Felipe Leme58390fe2017-09-15 15:16:28 -07002914 * // Value of the field is not sensitive because it was created statically and not changed.
Felipe Leme6dcec872017-05-25 11:24:23 -07002915 * username.setDataIsSensitive(false);
Felipe Leme1a1e4682017-03-13 16:34:03 -07002916 *
Felipe Leme25bf7872017-03-28 15:32:29 -07002917 * ViewStructure password = structure.newChild(index + 1);
2918 * username.setAutofillId(structure, 2); // id 2 - second child
Felipe Leme1a1e4682017-03-13 16:34:03 -07002919 * password.setAutofillHints("current-password");
Felipe Leme6dcec872017-05-25 11:24:23 -07002920 * password.setHtmlInfo(password.newHtmlInfoBuilder("input")
2921 * .addAttribute("type", "password")
Felipe Leme25bf7872017-03-28 15:32:29 -07002922 * .addAttribute("name", "password")
Felipe Lemef485f892017-11-22 08:15:59 -08002923 * .addAttribute("label", "Password:")
Felipe Leme25bf7872017-03-28 15:32:29 -07002924 * .build());
Felipe Leme1a1e4682017-03-13 16:34:03 -07002925 * password.setHint("Password");
2926 * password.setAutofillType(View.AUTOFILL_TYPE_TEXT);
Felipe Leme1a1e4682017-03-13 16:34:03 -07002927 * </pre>
Felipe Leme1a1e4682017-03-13 16:34:03 -07002928 */
Felipe Leme1ca634a2016-11-28 17:21:21 -08002929 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -08002930 public void onProvideAutofillVirtualStructure(ViewStructure structure, int flags) {
2931 mProvider.getViewDelegate().onProvideAutofillVirtualStructure(structure, flags);
Felipe Leme1ca634a2016-11-28 17:21:21 -08002932 }
2933
Tao Baia5717332017-03-30 14:58:53 -07002934 @Override
2935 public void autofill(SparseArray<AutofillValue>values) {
2936 mProvider.getViewDelegate().autofill(values);
2937 }
2938
Felipe Lemeef335262018-03-07 11:49:44 -08002939 @Override
2940 public boolean isVisibleToUserForAutofill(int virtualId) {
2941 return mProvider.getViewDelegate().isVisibleToUserForAutofill(virtualId);
2942 }
2943
Alan Viverettea54956a2015-01-07 16:05:02 -08002944 /** @hide */
Jonathan Dixon3c909522012-02-28 18:45:06 +00002945 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -08002946 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
2947 super.onInitializeAccessibilityNodeInfoInternal(info);
Marcin Kosiba3e853fb2015-01-08 12:45:40 +00002948 mProvider.getViewDelegate().onInitializeAccessibilityNodeInfo(info);
Jonathan Dixon3c909522012-02-28 18:45:06 +00002949 }
2950
Alan Viverettea54956a2015-01-07 16:05:02 -08002951 /** @hide */
Jonathan Dixon3c909522012-02-28 18:45:06 +00002952 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -08002953 public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
2954 super.onInitializeAccessibilityEventInternal(event);
Marcin Kosiba3e853fb2015-01-08 12:45:40 +00002955 mProvider.getViewDelegate().onInitializeAccessibilityEvent(event);
Jonathan Dixon3c909522012-02-28 18:45:06 +00002956 }
2957
Alan Viverettea54956a2015-01-07 16:05:02 -08002958 /** @hide */
alanv448902d2012-05-16 20:27:47 -07002959 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -08002960 public boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Marcin Kosiba3e853fb2015-01-08 12:45:40 +00002961 return mProvider.getViewDelegate().performAccessibilityAction(action, arguments);
alanv448902d2012-05-16 20:27:47 -07002962 }
2963
Jonathan Dixon3c909522012-02-28 18:45:06 +00002964 /** @hide */
2965 @Override
2966 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
2967 int l, int t, int r, int b) {
2968 mProvider.getViewDelegate().onDrawVerticalScrollBar(canvas, scrollBar, l, t, r, b);
2969 }
2970
2971 @Override
2972 protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
2973 mProvider.getViewDelegate().onOverScrolled(scrollX, scrollY, clampedX, clampedY);
2974 }
2975
2976 @Override
2977 protected void onWindowVisibilityChanged(int visibility) {
2978 super.onWindowVisibilityChanged(visibility);
2979 mProvider.getViewDelegate().onWindowVisibilityChanged(visibility);
2980 }
2981
2982 @Override
Jonathan Dixon3c909522012-02-28 18:45:06 +00002983 protected void onDraw(Canvas canvas) {
2984 mProvider.getViewDelegate().onDraw(canvas);
2985 }
2986
2987 @Override
2988 public boolean performLongClick() {
2989 return mProvider.getViewDelegate().performLongClick();
2990 }
2991
2992 @Override
2993 protected void onConfigurationChanged(Configuration newConfig) {
2994 mProvider.getViewDelegate().onConfigurationChanged(newConfig);
2995 }
2996
Selim Gurunc92080b2016-10-20 11:53:14 -07002997 /**
2998 * Creates a new InputConnection for an InputMethod to interact with the WebView.
2999 * This is similar to {@link View#onCreateInputConnection} but note that WebView
3000 * calls InputConnection methods on a thread other than the UI thread.
3001 * If these methods are overridden, then the overriding methods should respect
3002 * thread restrictions when calling View methods or accessing data.
3003 */
Jonathan Dixon3c909522012-02-28 18:45:06 +00003004 @Override
3005 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
3006 return mProvider.getViewDelegate().onCreateInputConnection(outAttrs);
3007 }
3008
3009 @Override
Hui Shuecdce962016-02-04 15:00:19 -08003010 public boolean onDragEvent(DragEvent event) {
3011 return mProvider.getViewDelegate().onDragEvent(event);
3012 }
3013
3014 @Override
Jonathan Dixon3c909522012-02-28 18:45:06 +00003015 protected void onVisibilityChanged(View changedView, int visibility) {
3016 super.onVisibilityChanged(changedView, visibility);
Martin Kosiba5ee743e2012-12-21 12:39:50 +00003017 // This method may be called in the constructor chain, before the WebView provider is
3018 // created.
3019 ensureProviderCreated();
Jonathan Dixon3c909522012-02-28 18:45:06 +00003020 mProvider.getViewDelegate().onVisibilityChanged(changedView, visibility);
3021 }
3022
3023 @Override
3024 public void onWindowFocusChanged(boolean hasWindowFocus) {
3025 mProvider.getViewDelegate().onWindowFocusChanged(hasWindowFocus);
3026 super.onWindowFocusChanged(hasWindowFocus);
3027 }
3028
3029 @Override
3030 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
3031 mProvider.getViewDelegate().onFocusChanged(focused, direction, previouslyFocusedRect);
3032 super.onFocusChanged(focused, direction, previouslyFocusedRect);
3033 }
3034
3035 /** @hide */
3036 @Override
3037 protected boolean setFrame(int left, int top, int right, int bottom) {
3038 return mProvider.getViewDelegate().setFrame(left, top, right, bottom);
3039 }
3040
3041 @Override
3042 protected void onSizeChanged(int w, int h, int ow, int oh) {
3043 super.onSizeChanged(w, h, ow, oh);
3044 mProvider.getViewDelegate().onSizeChanged(w, h, ow, oh);
3045 }
3046
3047 @Override
3048 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
3049 super.onScrollChanged(l, t, oldl, oldt);
3050 mProvider.getViewDelegate().onScrollChanged(l, t, oldl, oldt);
3051 }
3052
3053 @Override
3054 public boolean dispatchKeyEvent(KeyEvent event) {
3055 return mProvider.getViewDelegate().dispatchKeyEvent(event);
3056 }
3057
3058 @Override
3059 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
3060 return mProvider.getViewDelegate().requestFocus(direction, previouslyFocusedRect);
3061 }
3062
Jonathan Dixon3c909522012-02-28 18:45:06 +00003063 @Override
3064 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
3065 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
3066 mProvider.getViewDelegate().onMeasure(widthMeasureSpec, heightMeasureSpec);
3067 }
3068
3069 @Override
3070 public boolean requestChildRectangleOnScreen(View child, Rect rect, boolean immediate) {
3071 return mProvider.getViewDelegate().requestChildRectangleOnScreen(child, rect, immediate);
3072 }
3073
3074 @Override
3075 public void setBackgroundColor(int color) {
3076 mProvider.getViewDelegate().setBackgroundColor(color);
3077 }
John Recka5408e62012-03-16 14:18:44 -07003078
3079 @Override
3080 public void setLayerType(int layerType, Paint paint) {
3081 super.setLayerType(layerType, paint);
3082 mProvider.getViewDelegate().setLayerType(layerType, paint);
3083 }
Ben Murdoche623e242012-09-07 20:47:07 +01003084
3085 @Override
3086 protected void dispatchDraw(Canvas canvas) {
3087 mProvider.getViewDelegate().preDispatchDraw(canvas);
3088 super.dispatchDraw(canvas);
3089 }
Ben Murdoch7302f4f2014-07-03 14:47:44 +01003090
3091 @Override
3092 public void onStartTemporaryDetach() {
3093 super.onStartTemporaryDetach();
3094 mProvider.getViewDelegate().onStartTemporaryDetach();
3095 }
3096
3097 @Override
3098 public void onFinishTemporaryDetach() {
3099 super.onFinishTemporaryDetach();
3100 mProvider.getViewDelegate().onFinishTemporaryDetach();
3101 }
Siva Velusamy94a6d152015-05-05 15:07:00 -07003102
Selim Gurune319dad2016-03-17 01:40:40 +00003103 @Override
3104 public Handler getHandler() {
3105 return mProvider.getViewDelegate().getHandler(super.getHandler());
3106 }
3107
3108 @Override
3109 public View findFocus() {
3110 return mProvider.getViewDelegate().findFocus(super.findFocus());
3111 }
3112
Hui Shuf119c522015-10-08 10:07:13 -07003113 /**
Gustav Senntonbf683e02016-09-15 14:42:50 +01003114 * If WebView has already been loaded into the current process this method will return the
3115 * package that was used to load it. Otherwise, the package that would be used if the WebView
3116 * was loaded right now will be returned; this does not cause WebView to be loaded, so this
3117 * information may become outdated at any time.
Gustav Sennton4cbfe712017-03-07 14:22:01 +00003118 * The WebView package changes either when the current WebView package is updated, disabled, or
3119 * uninstalled. It can also be changed through a Developer Setting.
3120 * If the WebView package changes, any app process that has loaded WebView will be killed. The
3121 * next time the app starts and loads WebView it will use the new WebView package instead.
Nate Fischer0a6140d2017-09-05 12:37:49 -07003122 * @return the current WebView package, or {@code null} if there is none.
Gustav Senntonbf683e02016-09-15 14:42:50 +01003123 */
Nate Fischer3442c742017-09-08 17:02:00 -07003124 @Nullable
Gustav Senntonbf683e02016-09-15 14:42:50 +01003125 public static PackageInfo getCurrentWebViewPackage() {
3126 PackageInfo webviewPackage = WebViewFactory.getLoadedPackageInfo();
3127 if (webviewPackage != null) {
3128 return webviewPackage;
3129 }
3130
Torne (Richard Coles)ff937ac2017-10-02 17:09:32 -04003131 IWebViewUpdateService service = WebViewFactory.getUpdateService();
3132 if (service == null) {
3133 return null;
3134 }
Gustav Senntonbf683e02016-09-15 14:42:50 +01003135 try {
Torne (Richard Coles)ff937ac2017-10-02 17:09:32 -04003136 return service.getCurrentWebViewPackage();
Gustav Senntonbf683e02016-09-15 14:42:50 +01003137 } catch (RemoteException e) {
3138 throw e.rethrowFromSystemServer();
3139 }
3140 }
3141
3142 /**
Hui Shuf119c522015-10-08 10:07:13 -07003143 * Receive the result from a previous call to {@link #startActivityForResult(Intent, int)}.
3144 *
3145 * @param requestCode The integer request code originally supplied to
3146 * startActivityForResult(), allowing you to identify who this
3147 * result came from.
3148 * @param resultCode The integer result code returned by the child activity
3149 * through its setResult().
3150 * @param data An Intent, which can return result data to the caller
3151 * (various data can be attached to Intent "extras").
3152 * @hide
3153 */
3154 @Override
3155 public void onActivityResult(int requestCode, int resultCode, Intent data) {
3156 mProvider.getViewDelegate().onActivityResult(requestCode, resultCode, data);
3157 }
3158
Changwan Ryu15e9afc2018-03-06 15:55:19 -08003159 @Override
3160 public boolean onCheckIsTextEditor() {
3161 return mProvider.getViewDelegate().onCheckIsTextEditor();
3162 }
3163
Siva Velusamy94a6d152015-05-05 15:07:00 -07003164 /** @hide */
3165 @Override
3166 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
3167 super.encodeProperties(encoder);
3168
3169 checkThread();
3170 encoder.addProperty("webview:contentHeight", mProvider.getContentHeight());
3171 encoder.addProperty("webview:contentWidth", mProvider.getContentWidth());
3172 encoder.addProperty("webview:scale", mProvider.getScale());
3173 encoder.addProperty("webview:title", mProvider.getTitle());
3174 encoder.addProperty("webview:url", mProvider.getUrl());
3175 encoder.addProperty("webview:originalUrl", mProvider.getOriginalUrl());
3176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177}