blob: 2c51ee940039ae7a7552c7823c332f9e6d6ad18f [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>
254 * <p>The {@code hdpi.css} stylesheet is only used for devices with a screen pixel ration 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>
330 * If Safe Browsing is enabled, WebView will block malicious URLs and present a warning UI to the
331 * user to allow them to navigate back safely or proceed to the malicious page.
Nate Fischer471891d2017-07-18 19:15:52 -0700332 * <p>
Nate Fischer20635082017-11-08 13:39:08 -0800333 * The recommended way for apps to enable the feature is putting the following tag in the manifest's
334 * {@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;
341 * android:value=&quot;true&quot; /&gt;
342 * &lt;/application&gt;
343 * &lt;/manifest&gt;
Nate Fischer471891d2017-07-18 19:15:52 -0700344 * </pre>
Nate Fischer471891d2017-07-18 19:15:52 -0700345 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 */
Steve Block406aeb22012-04-23 18:17:19 +0100347// Implementation notes.
348// The WebView is a thin API class that delegates its public API to a backend WebViewProvider
349// class instance. WebView extends {@link AbsoluteLayout} for backward compatibility reasons.
350// Methods are delegated to the provider implementation: all public API methods introduced in this
351// file are fully delegated, whereas public and protected methods from the View base classes are
352// only delegated where a specific need exists for them to do so.
Raphael30df2372010-03-06 10:09:54 -0800353@Widget
Cary Clarkd6982c92009-05-29 11:02:22 -0400354public class WebView extends AbsoluteLayout
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 implements ViewTreeObserver.OnGlobalFocusChangeListener,
John Reck926cf562012-06-14 10:00:31 -0700356 ViewGroup.OnHierarchyChangeListener, ViewDebug.HierarchyHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357
Jonathan Dixon69f9f932013-08-28 15:54:29 -0700358 private static final String LOGTAG = "WebView";
Nicolas Roard12c18e62010-10-13 20:14:31 -0700359
Kristian Monsenb5cd8c02013-04-09 17:57:37 -0700360 // Throwing an exception for incorrect thread usage if the
361 // build target is JB MR2 or newer. Defaults to false, and is
362 // set in the WebView constructor.
Jonathan Dixon0d6a1452013-08-21 13:09:36 -0700363 private static volatile boolean sEnforceThreadChecking = false;
Kristian Monsenb5cd8c02013-04-09 17:57:37 -0700364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 /**
366 * Transportation object for returning WebView across thread boundaries.
367 */
368 public class WebViewTransport {
369 private WebView mWebview;
370
371 /**
Steve Block4e584df2012-04-24 23:12:47 +0100372 * Sets the WebView to the transportation object.
373 *
374 * @param webview the WebView to transport
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 */
376 public synchronized void setWebView(WebView webview) {
377 mWebview = webview;
378 }
379
380 /**
Steve Block4e584df2012-04-24 23:12:47 +0100381 * Gets the WebView object.
382 *
383 * @return the transported WebView object
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 */
385 public synchronized WebView getWebView() {
386 return mWebview;
387 }
388 }
389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 /**
Steve Block4e584df2012-04-24 23:12:47 +0100391 * URI scheme for telephone number.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 */
393 public static final String SCHEME_TEL = "tel:";
394 /**
Steve Block4e584df2012-04-24 23:12:47 +0100395 * URI scheme for email address.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 */
397 public static final String SCHEME_MAILTO = "mailto:";
398 /**
Steve Block4e584df2012-04-24 23:12:47 +0100399 * URI scheme for map address.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 */
401 public static final String SCHEME_GEO = "geo:0,0?q=";
Cary Clarkd6982c92009-05-29 11:02:22 -0400402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 /**
Victoria Leased405a432012-03-22 15:53:48 -0700404 * Interface to listen for find results.
Victoria Leased405a432012-03-22 15:53:48 -0700405 */
406 public interface FindListener {
407 /**
Steve Block4e584df2012-04-24 23:12:47 +0100408 * Notifies the listener about progress made by a find operation.
Victoria Leased405a432012-03-22 15:53:48 -0700409 *
Steve Block4e584df2012-04-24 23:12:47 +0100410 * @param activeMatchOrdinal the zero-based ordinal of the currently selected match
Selim Gurun92b81a32012-08-21 17:32:35 -0700411 * @param numberOfMatches how many matches have been found
Steve Block4e584df2012-04-24 23:12:47 +0100412 * @param isDoneCounting whether the find operation has actually completed. The listener
413 * may be notified multiple times while the
414 * operation is underway, and the numberOfMatches
415 * value should not be considered final unless
Nate Fischer0a6140d2017-09-05 12:37:49 -0700416 * isDoneCounting is {@code true}.
Victoria Leased405a432012-03-22 15:53:48 -0700417 */
Selim Gurun92b81a32012-08-21 17:32:35 -0700418 public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
Victoria Leased405a432012-03-22 15:53:48 -0700419 boolean isDoneCounting);
420 }
421
422 /**
Tobias Sargeantb3656042015-05-13 11:23:57 +0100423 * Callback interface supplied to {@link #postVisualStateCallback} for receiving
Tobias Sargeanta8352f42015-03-03 18:07:40 +0000424 * notifications about the visual state.
425 */
426 public static abstract class VisualStateCallback {
427 /**
428 * Invoked when the visual state is ready to be drawn in the next {@link #onDraw}.
429 *
Tobias Sargeantb3656042015-05-13 11:23:57 +0100430 * @param requestId The identifier passed to {@link #postVisualStateCallback} when this
431 * callback was posted.
Tobias Sargeanta8352f42015-03-03 18:07:40 +0000432 */
433 public abstract void onComplete(long requestId);
434 }
435
436 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 * Interface to listen for new pictures as they change.
Steve Block4e584df2012-04-24 23:12:47 +0100438 *
Kristian Monsenfc771652011-05-10 16:44:05 +0100439 * @deprecated This interface is now obsolete.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 */
Kristian Monsenfc771652011-05-10 16:44:05 +0100441 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 public interface PictureListener {
443 /**
Steve Block72498ed2012-07-17 15:57:25 +0100444 * Used to provide notification that the WebView's picture has changed.
445 * See {@link WebView#capturePicture} for details of the picture.
Steve Block4e584df2012-04-24 23:12:47 +0100446 *
447 * @param view the WebView that owns the picture
Ben Murdoch52643e02013-02-26 12:01:00 +0000448 * @param picture the new picture. Applications targeting
449 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2} or above
Nate Fischer0a6140d2017-09-05 12:37:49 -0700450 * will always receive a {@code null} Picture.
Steve Block72498ed2012-07-17 15:57:25 +0100451 * @deprecated Deprecated due to internal changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 */
Kristian Monsenfc771652011-05-10 16:44:05 +0100453 @Deprecated
Nate Fischer3442c742017-09-08 17:02:00 -0700454 void onNewPicture(WebView view, @Nullable Picture picture);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 }
456
Jonathan Dixon19644b62011-12-21 14:21:36 +0000457 public static class HitTestResult {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 /**
Steve Block4e584df2012-04-24 23:12:47 +0100459 * Default HitTestResult, where the target is unknown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 */
461 public static final int UNKNOWN_TYPE = 0;
462 /**
Steve Block1854ddb2011-04-19 12:18:19 +0100463 * @deprecated This type is no longer used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 */
Steve Block1854ddb2011-04-19 12:18:19 +0100465 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 public static final int ANCHOR_TYPE = 1;
467 /**
Steve Block4e584df2012-04-24 23:12:47 +0100468 * HitTestResult for hitting a phone number.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 */
470 public static final int PHONE_TYPE = 2;
471 /**
Steve Block4e584df2012-04-24 23:12:47 +0100472 * HitTestResult for hitting a map address.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 */
474 public static final int GEO_TYPE = 3;
475 /**
Steve Block4e584df2012-04-24 23:12:47 +0100476 * HitTestResult for hitting an email address.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 */
478 public static final int EMAIL_TYPE = 4;
479 /**
Steve Block4e584df2012-04-24 23:12:47 +0100480 * HitTestResult for hitting an HTML::img tag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 */
482 public static final int IMAGE_TYPE = 5;
483 /**
Steve Block1854ddb2011-04-19 12:18:19 +0100484 * @deprecated This type is no longer used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 */
Steve Block1854ddb2011-04-19 12:18:19 +0100486 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 public static final int IMAGE_ANCHOR_TYPE = 6;
488 /**
Steve Block4e584df2012-04-24 23:12:47 +0100489 * HitTestResult for hitting a HTML::a tag with src=http.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 */
491 public static final int SRC_ANCHOR_TYPE = 7;
492 /**
Steve Block4e584df2012-04-24 23:12:47 +0100493 * HitTestResult for hitting a HTML::a tag with src=http + HTML::img.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 */
495 public static final int SRC_IMAGE_ANCHOR_TYPE = 8;
496 /**
Steve Block4e584df2012-04-24 23:12:47 +0100497 * HitTestResult for hitting an edit text area.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 */
499 public static final int EDIT_TEXT_TYPE = 9;
500
501 private int mType;
502 private String mExtra;
503
Jonathan Dixon3c909522012-02-28 18:45:06 +0000504 /**
505 * @hide Only for use by WebViewProvider implementations
506 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000507 @SystemApi
Jonathan Dixon3c909522012-02-28 18:45:06 +0000508 public HitTestResult() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 mType = UNKNOWN_TYPE;
510 }
511
Jonathan Dixon3c909522012-02-28 18:45:06 +0000512 /**
513 * @hide Only for use by WebViewProvider implementations
514 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000515 @SystemApi
Jonathan Dixon3c909522012-02-28 18:45:06 +0000516 public void setType(int type) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 mType = type;
518 }
519
Jonathan Dixon3c909522012-02-28 18:45:06 +0000520 /**
521 * @hide Only for use by WebViewProvider implementations
522 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000523 @SystemApi
Jonathan Dixon3c909522012-02-28 18:45:06 +0000524 public void setExtra(String extra) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 mExtra = extra;
526 }
527
Jonathan Dixone230e542011-12-21 10:57:00 +0000528 /**
Steve Block4e584df2012-04-24 23:12:47 +0100529 * Gets the type of the hit test result. See the XXX_TYPE constants
530 * defined in this class.
531 *
532 * @return the type of the hit test result
Jonathan Dixone230e542011-12-21 10:57:00 +0000533 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 public int getType() {
535 return mType;
536 }
537
Jonathan Dixone230e542011-12-21 10:57:00 +0000538 /**
Steve Block4e584df2012-04-24 23:12:47 +0100539 * Gets additional type-dependant information about the result. See
Nate Fischer0a6140d2017-09-05 12:37:49 -0700540 * {@link WebView#getHitTestResult()} for details. May either be {@code null}
Steve Block4e584df2012-04-24 23:12:47 +0100541 * or contain extra information about this result.
542 *
543 * @return additional type-dependant information about the result
Jonathan Dixone230e542011-12-21 10:57:00 +0000544 */
Nate Fischer3442c742017-09-08 17:02:00 -0700545 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 public String getExtra() {
547 return mExtra;
548 }
549 }
550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 /**
Nate Fischer674561a2017-10-26 12:28:01 -0700552 * Constructs a new WebView with an Activity Context object.
Steve Block4e584df2012-04-24 23:12:47 +0100553 *
Nate Fischer674561a2017-10-26 12:28:01 -0700554 * <p class="note"><b>Note:</b> WebView should always be instantiated with an Activity Context.
555 * If instantiated with an Application Context, WebView will be unable to provide several
556 * features, such as JavaScript dialogs and autofill.
557 *
558 * @param context an Activity Context to access application assets
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 */
560 public WebView(Context context) {
561 this(context, null);
562 }
563
564 /**
Steve Block4e584df2012-04-24 23:12:47 +0100565 * Constructs a new WebView with layout parameters.
566 *
Nate Fischer674561a2017-10-26 12:28:01 -0700567 * @param context an Activity Context to access application assets
Steve Block4e584df2012-04-24 23:12:47 +0100568 * @param attrs an AttributeSet passed to our parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 */
570 public WebView(Context context, AttributeSet attrs) {
571 this(context, attrs, com.android.internal.R.attr.webViewStyle);
572 }
573
574 /**
Steve Block4e584df2012-04-24 23:12:47 +0100575 * Constructs a new WebView with layout parameters and a default style.
576 *
Nate Fischer674561a2017-10-26 12:28:01 -0700577 * @param context an Activity Context to access application assets
Steve Block4e584df2012-04-24 23:12:47 +0100578 * @param attrs an AttributeSet passed to our parent
Alan Viverette617feb92013-09-09 18:09:13 -0700579 * @param defStyleAttr an attribute in the current theme that contains a
580 * reference to a style resource that supplies default values for
581 * the view. Can be 0 to not look for defaults.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 */
Alan Viverette617feb92013-09-09 18:09:13 -0700583 public WebView(Context context, AttributeSet attrs, int defStyleAttr) {
584 this(context, attrs, defStyleAttr, 0);
Elliott Slaughterf21d2e32010-07-14 18:08:54 -0700585 }
586
587 /**
Steve Block4e584df2012-04-24 23:12:47 +0100588 * Constructs a new WebView with layout parameters and a default style.
589 *
Nate Fischer674561a2017-10-26 12:28:01 -0700590 * @param context an Activity Context to access application assets
Steve Block4e584df2012-04-24 23:12:47 +0100591 * @param attrs an AttributeSet passed to our parent
Alan Viverette617feb92013-09-09 18:09:13 -0700592 * @param defStyleAttr an attribute in the current theme that contains a
593 * reference to a style resource that supplies default values for
594 * the view. Can be 0 to not look for defaults.
595 * @param defStyleRes a resource identifier of a style resource that
596 * supplies default values for the view, used only if
597 * defStyleAttr is 0 or can not be found in the theme. Can be 0
598 * to not look for defaults.
599 */
600 public WebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
601 this(context, attrs, defStyleAttr, defStyleRes, null, false);
602 }
603
604 /**
605 * Constructs a new WebView with layout parameters and a default style.
606 *
Nate Fischer674561a2017-10-26 12:28:01 -0700607 * @param context an Activity Context to access application assets
Alan Viverette617feb92013-09-09 18:09:13 -0700608 * @param attrs an AttributeSet passed to our parent
609 * @param defStyleAttr an attribute in the current theme that contains a
610 * reference to a style resource that supplies default values for
611 * the view. Can be 0 to not look for defaults.
Steve Block4e584df2012-04-24 23:12:47 +0100612 * @param privateBrowsing whether this WebView will be initialized in
613 * private mode
Kristian Monsen5cc23512012-08-09 15:33:08 -0400614 *
Selim Gurunb6aa97e2014-03-26 14:10:28 -0700615 * @deprecated Private browsing is no longer supported directly via
Kristian Monsen5cc23512012-08-09 15:33:08 -0400616 * WebView and will be removed in a future release. Prefer using
617 * {@link WebSettings}, {@link WebViewDatabase}, {@link CookieManager}
618 * and {@link WebStorage} for fine-grained control of privacy data.
Elliott Slaughterf21d2e32010-07-14 18:08:54 -0700619 */
Kristian Monsen5cc23512012-08-09 15:33:08 -0400620 @Deprecated
Alan Viverette617feb92013-09-09 18:09:13 -0700621 public WebView(Context context, AttributeSet attrs, int defStyleAttr,
Elliott Slaughterf21d2e32010-07-14 18:08:54 -0700622 boolean privateBrowsing) {
Alan Viverette617feb92013-09-09 18:09:13 -0700623 this(context, attrs, defStyleAttr, 0, null, privateBrowsing);
Andrei Popescu4950b2b2009-09-03 13:56:07 +0100624 }
625
626 /**
Steve Block4e584df2012-04-24 23:12:47 +0100627 * Constructs a new WebView with layout parameters, a default style and a set
Nate Fischerffb81c42016-09-26 12:35:47 -0700628 * of custom JavaScript interfaces to be added to this WebView at initialization
Romain Guy01d0fbf2009-12-01 14:52:19 -0800629 * time. This guarantees that these interfaces will be available when the JS
Andrei Popescu4950b2b2009-09-03 13:56:07 +0100630 * context is initialized.
Steve Block4e584df2012-04-24 23:12:47 +0100631 *
Nate Fischer674561a2017-10-26 12:28:01 -0700632 * @param context an Activity Context to access application assets
Steve Block4e584df2012-04-24 23:12:47 +0100633 * @param attrs an AttributeSet passed to our parent
Alan Viverette617feb92013-09-09 18:09:13 -0700634 * @param defStyleAttr an attribute in the current theme that contains a
635 * reference to a style resource that supplies default values for
636 * the view. Can be 0 to not look for defaults.
Steve Block4e584df2012-04-24 23:12:47 +0100637 * @param javaScriptInterfaces a Map of interface names, as keys, and
638 * object implementing those interfaces, as
639 * values
640 * @param privateBrowsing whether this WebView will be initialized in
641 * private mode
Nate Fischerffb81c42016-09-26 12:35:47 -0700642 * @hide This is used internally by dumprendertree, as it requires the JavaScript interfaces to
Steve Block4e584df2012-04-24 23:12:47 +0100643 * be added synchronously, before a subsequent loadUrl call takes effect.
Andrei Popescu4950b2b2009-09-03 13:56:07 +0100644 */
Alan Viverette617feb92013-09-09 18:09:13 -0700645 protected WebView(Context context, AttributeSet attrs, int defStyleAttr,
Steve Block81f19ff2010-11-01 13:23:24 +0000646 Map<String, Object> javaScriptInterfaces, boolean privateBrowsing) {
Alan Viverette617feb92013-09-09 18:09:13 -0700647 this(context, attrs, defStyleAttr, 0, javaScriptInterfaces, privateBrowsing);
648 }
649
650 /**
651 * @hide
652 */
653 @SuppressWarnings("deprecation") // for super() call into deprecated base class constructor.
654 protected WebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes,
655 Map<String, Object> javaScriptInterfaces, boolean privateBrowsing) {
656 super(context, attrs, defStyleAttr, defStyleRes);
Felipe Lemed04a6972017-03-02 12:56:18 -0800657
658 // WebView is important by default, unless app developer overrode attribute.
659 if (getImportantForAutofill() == IMPORTANT_FOR_AUTOFILL_AUTO) {
660 setImportantForAutofill(IMPORTANT_FOR_AUTOFILL_YES);
661 }
662
Kristian Monsen87af7312011-09-09 02:12:51 +0100663 if (context == null) {
664 throw new IllegalArgumentException("Invalid context argument");
665 }
Gustav Sennton01673692018-01-19 12:11:25 +0000666 if (mWebViewThread == null) {
667 throw new RuntimeException(
668 "WebView cannot be initialized on a thread that has no Looper.");
669 }
Kristian Monsenb5cd8c02013-04-09 17:57:37 -0700670 sEnforceThreadChecking = context.getApplicationInfo().targetSdkVersion >=
671 Build.VERSION_CODES.JELLY_BEAN_MR2;
Jonathan Dixon3c909522012-02-28 18:45:06 +0000672 checkThread();
Kristian Monsen87af7312011-09-09 02:12:51 +0100673
Jonathan Dixon3c909522012-02-28 18:45:06 +0000674 ensureProviderCreated();
675 mProvider.init(javaScriptInterfaces, privateBrowsing);
Jonathan Dixoneb9e8012013-10-03 11:03:31 +0100676 // Post condition of creating a webview is the CookieSyncManager.getInstance() is allowed.
677 CookieSyncManager.setGetInstanceIsAllowed();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 }
679
680 /**
Steve Block4e584df2012-04-24 23:12:47 +0100681 * Specifies whether the horizontal scrollbar has overlay style.
682 *
Paul Millerc8694a02015-06-05 11:23:03 -0700683 * @deprecated This method has no effect.
Nate Fischer0a6140d2017-09-05 12:37:49 -0700684 * @param overlay {@code true} if horizontal scrollbar should have overlay style
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 */
Paul Millerc8694a02015-06-05 11:23:03 -0700686 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 public void setHorizontalScrollbarOverlay(boolean overlay) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 }
689
690 /**
Steve Block4e584df2012-04-24 23:12:47 +0100691 * Specifies whether the vertical scrollbar has overlay style.
692 *
Paul Millerc8694a02015-06-05 11:23:03 -0700693 * @deprecated This method has no effect.
Nate Fischer0a6140d2017-09-05 12:37:49 -0700694 * @param overlay {@code true} if vertical scrollbar should have overlay style
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 */
Paul Millerc8694a02015-06-05 11:23:03 -0700696 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 public void setVerticalScrollbarOverlay(boolean overlay) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 }
699
700 /**
Steve Block4e584df2012-04-24 23:12:47 +0100701 * Gets whether horizontal scrollbar has overlay style.
702 *
Paul Millerc8694a02015-06-05 11:23:03 -0700703 * @deprecated This method is now obsolete.
Nate Fischer0a6140d2017-09-05 12:37:49 -0700704 * @return {@code true}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 */
Paul Millerc8694a02015-06-05 11:23:03 -0700706 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 public boolean overlayHorizontalScrollbar() {
Paul Millerc8694a02015-06-05 11:23:03 -0700708 // The old implementation defaulted to true, so return true for consistency
709 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
711
712 /**
Steve Block4e584df2012-04-24 23:12:47 +0100713 * Gets whether vertical scrollbar has overlay style.
714 *
Paul Millerc8694a02015-06-05 11:23:03 -0700715 * @deprecated This method is now obsolete.
Nate Fischer0a6140d2017-09-05 12:37:49 -0700716 * @return {@code false}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 */
Paul Millerc8694a02015-06-05 11:23:03 -0700718 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 public boolean overlayVerticalScrollbar() {
Paul Millerc8694a02015-06-05 11:23:03 -0700720 // The old implementation defaulted to false, so return false for consistency
721 return false;
Mike Reede8853fc2009-09-04 14:01:48 -0400722 }
723
Michael Kolbe54f6652011-03-16 09:11:51 -0700724 /**
Steve Block4e584df2012-04-24 23:12:47 +0100725 * Gets the visible height (in pixels) of the embedded title bar (if any).
Michael Kolb73980a92010-08-05 16:32:51 -0700726 *
Michael Kolb24e53b02011-03-16 12:52:04 -0700727 * @deprecated This method is now obsolete.
Kristian Monsenf4912582012-08-16 15:26:24 -0400728 * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
Mike Reede8853fc2009-09-04 14:01:48 -0400729 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700730 @Deprecated
Michael Kolb73980a92010-08-05 16:32:51 -0700731 public int getVisibleTitleHeight() {
Steve Block51b08912011-04-27 15:04:48 +0100732 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000733 return mProvider.getVisibleTitleHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 }
735
736 /**
Nate Fischer0a6140d2017-09-05 12:37:49 -0700737 * Gets the SSL certificate for the main top-level page or {@code null} if there is
Steve Block4e584df2012-04-24 23:12:47 +0100738 * no certificate (the site is not secure).
739 *
740 * @return the SSL certificate for the main top-level page
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 */
Nate Fischer3442c742017-09-08 17:02:00 -0700742 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 public SslCertificate getCertificate() {
Steve Block51b08912011-04-27 15:04:48 +0100744 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000745 return mProvider.getCertificate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
747
748 /**
749 * Sets the SSL certificate for the main top-level page.
Kristian Monsen5cc23512012-08-09 15:33:08 -0400750 *
751 * @deprecated Calling this function has no useful effect, and will be
752 * ignored in future releases.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 */
Kristian Monsen5cc23512012-08-09 15:33:08 -0400754 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 public void setCertificate(SslCertificate certificate) {
Steve Block51b08912011-04-27 15:04:48 +0100756 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000757 mProvider.setCertificate(certificate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759
760 //-------------------------------------------------------------------------
761 // Methods called by activity
762 //-------------------------------------------------------------------------
763
764 /**
Steve Block32fe4102012-07-17 16:29:11 +0100765 * Sets a username and password pair for the specified host. This data is
Nate Fischerffb81c42016-09-26 12:35:47 -0700766 * used by the WebView to autocomplete username and password fields in web
Steve Block32fe4102012-07-17 16:29:11 +0100767 * forms. Note that this is unrelated to the credentials used for HTTP
768 * authentication.
Steve Block4e584df2012-04-24 23:12:47 +0100769 *
Selim Gurun38915fd2013-04-04 17:14:29 +0000770 * @param host the host that required the credentials
Steve Block4e584df2012-04-24 23:12:47 +0100771 * @param username the username for the given host
772 * @param password the password for the given host
Steve Block32fe4102012-07-17 16:29:11 +0100773 * @see WebViewDatabase#clearUsernamePassword
774 * @see WebViewDatabase#hasUsernamePassword
Jonathan Dixon835b1fc2013-02-25 12:29:33 -0800775 * @deprecated Saving passwords in WebView will not be supported in future versions.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 */
Jonathan Dixon835b1fc2013-02-25 12:29:33 -0800777 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 public void savePassword(String host, String username, String password) {
Steve Block51b08912011-04-27 15:04:48 +0100779 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000780 mProvider.savePassword(host, username, password);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 }
782
783 /**
Hui Shu433fb932016-08-29 11:49:46 -0700784 * Stores HTTP authentication credentials for a given host and realm to the {@link WebViewDatabase}
785 * instance.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 *
Steve Block46ce1db2012-07-17 16:43:00 +0100787 * @param host the host to which the credentials apply
788 * @param realm the realm to which the credentials apply
789 * @param username the username
Steve Block4e584df2012-04-24 23:12:47 +0100790 * @param password the password
Nate Fischerc7edfb02016-09-23 15:59:21 -0700791 * @deprecated Use {@link WebViewDatabase#setHttpAuthUsernamePassword} instead
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 */
Nate Fischerc7edfb02016-09-23 15:59:21 -0700793 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 public void setHttpAuthUsernamePassword(String host, String realm,
795 String username, String password) {
Steve Block51b08912011-04-27 15:04:48 +0100796 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000797 mProvider.setHttpAuthUsernamePassword(host, realm, username, password);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
799
800 /**
Hui Shu433fb932016-08-29 11:49:46 -0700801 * Retrieves HTTP authentication credentials for a given host and realm from the {@link
802 * WebViewDatabase} instance.
Steve Block46ce1db2012-07-17 16:43:00 +0100803 * @param host the host to which the credentials apply
804 * @param realm the realm to which the credentials apply
805 * @return the credentials as a String array, if found. The first element
Nate Fischer0a6140d2017-09-05 12:37:49 -0700806 * is the username and the second element is the password. {@code null} if
Steve Block46ce1db2012-07-17 16:43:00 +0100807 * no credentials are found.
Nate Fischerc7edfb02016-09-23 15:59:21 -0700808 * @deprecated Use {@link WebViewDatabase#getHttpAuthUsernamePassword} instead
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 */
Nate Fischerc7edfb02016-09-23 15:59:21 -0700810 @Deprecated
Nate Fischer3442c742017-09-08 17:02:00 -0700811 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 public String[] getHttpAuthUsernamePassword(String host, String realm) {
Steve Block51b08912011-04-27 15:04:48 +0100813 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000814 return mProvider.getHttpAuthUsernamePassword(host, realm);
Leon Scroggins05919f22010-09-14 17:22:36 -0400815 }
816
817 /**
Steve Block4e584df2012-04-24 23:12:47 +0100818 * Destroys the internal state of this WebView. This method should be called
819 * after this WebView has been removed from the view system. No other
820 * methods may be called on this WebView after destroy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 */
822 public void destroy() {
Steve Block51b08912011-04-27 15:04:48 +0100823 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000824 mProvider.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 }
826
827 /**
828 * Enables platform notifications of data state and proxy changes.
Kristian Monsencbb59db2011-05-09 16:04:34 +0100829 * Notifications are enabled by default.
Kristian Monsenfc771652011-05-10 16:44:05 +0100830 *
831 * @deprecated This method is now obsolete.
Kristian Monsenf4912582012-08-16 15:26:24 -0400832 * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 */
Kristian Monsenfc771652011-05-10 16:44:05 +0100834 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 public static void enablePlatformNotifications() {
Kristian Monsen10ca8082014-04-29 11:02:42 -0700836 // noop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 }
838
839 /**
Kristian Monsencbb59db2011-05-09 16:04:34 +0100840 * Disables platform notifications of data state and proxy changes.
841 * Notifications are enabled by default.
Kristian Monsenfc771652011-05-10 16:44:05 +0100842 *
843 * @deprecated This method is now obsolete.
Kristian Monsenf4912582012-08-16 15:26:24 -0400844 * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 */
Kristian Monsenfc771652011-05-10 16:44:05 +0100846 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 public static void disablePlatformNotifications() {
Kristian Monsen10ca8082014-04-29 11:02:42 -0700848 // noop
Feng Qianb3081372009-06-29 15:55:18 -0700849 }
850
851 /**
Primiano Tucci24426752013-09-05 12:01:51 +0100852 * Used only by internal tests to free up memory.
853 *
854 * @hide
855 */
856 public static void freeMemoryForTests() {
Primiano Tucci24426752013-09-05 12:01:51 +0100857 getFactory().getStatics().freeMemoryForTests();
858 }
859
860 /**
Steve Block4e584df2012-04-24 23:12:47 +0100861 * Informs WebView of the network state. This is used to set
Steve Block81f19ff2010-11-01 13:23:24 +0000862 * the JavaScript property window.navigator.isOnline and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 * generates the online/offline event as specified in HTML5, sec. 5.7.7
Steve Block4e584df2012-04-24 23:12:47 +0100864 *
865 * @param networkUp a boolean indicating if network is available
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 */
867 public void setNetworkAvailable(boolean networkUp) {
Steve Block51b08912011-04-27 15:04:48 +0100868 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000869 mProvider.setNetworkAvailable(networkUp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 }
871
872 /**
Steve Block4e584df2012-04-24 23:12:47 +0100873 * Saves the state of this WebView used in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 * {@link android.app.Activity#onSaveInstanceState}. Please note that this
875 * method no longer stores the display data for this WebView. The previous
876 * behavior could potentially leak files if {@link #restoreState} was never
Kristian Monsenf4912582012-08-16 15:26:24 -0400877 * called.
Steve Block4e584df2012-04-24 23:12:47 +0100878 *
879 * @param outState the Bundle to store this WebView's state
Nate Fischer3442c742017-09-08 17:02:00 -0700880 * @return the same copy of the back/forward list used to save the state, {@code null} if the
881 * method fails.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 */
Nate Fischer3442c742017-09-08 17:02:00 -0700883 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 public WebBackForwardList saveState(Bundle outState) {
Steve Block51b08912011-04-27 15:04:48 +0100885 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000886 return mProvider.saveState(outState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 }
888
889 /**
Steve Block4e584df2012-04-24 23:12:47 +0100890 * Saves the current display data to the Bundle given. Used in conjunction
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 * with {@link #saveState}.
Steve Block4e584df2012-04-24 23:12:47 +0100892 * @param b a Bundle to store the display data
893 * @param dest the file to store the serialized picture data. Will be
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 * overwritten with this WebView's picture data.
Nate Fischer0a6140d2017-09-05 12:37:49 -0700895 * @return {@code true} if the picture was successfully saved
Kristian Monsenfc771652011-05-10 16:44:05 +0100896 * @deprecated This method is now obsolete.
Kristian Monsenf4912582012-08-16 15:26:24 -0400897 * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 */
Kristian Monsenfc771652011-05-10 16:44:05 +0100899 @Deprecated
Patrick Scottda9a22b2010-04-08 08:32:52 -0400900 public boolean savePicture(Bundle b, final File dest) {
Steve Block51b08912011-04-27 15:04:48 +0100901 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000902 return mProvider.savePicture(b, dest);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 }
904
905 /**
Steve Block4e584df2012-04-24 23:12:47 +0100906 * Restores the display data that was saved in {@link #savePicture}. Used in
907 * conjunction with {@link #restoreState}. Note that this will not work if
908 * this WebView is hardware accelerated.
John Reck2df8f422011-09-22 19:50:41 -0700909 *
Steve Block4e584df2012-04-24 23:12:47 +0100910 * @param b a Bundle containing the saved display data
911 * @param src the file where the picture data was stored
Nate Fischer0a6140d2017-09-05 12:37:49 -0700912 * @return {@code true} if the picture was successfully restored
Kristian Monsenfc771652011-05-10 16:44:05 +0100913 * @deprecated This method is now obsolete.
Kristian Monsenf4912582012-08-16 15:26:24 -0400914 * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 */
Kristian Monsenfc771652011-05-10 16:44:05 +0100916 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 public boolean restorePicture(Bundle b, File src) {
Steve Block51b08912011-04-27 15:04:48 +0100918 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000919 return mProvider.restorePicture(b, src);
John Reck95b7d6f2011-06-03 15:23:43 -0700920 }
921
922 /**
Steve Block42499062012-07-17 15:34:46 +0100923 * Restores the state of this WebView from the given Bundle. This method is
924 * intended for use in {@link android.app.Activity#onRestoreInstanceState}
925 * and should be called to restore the state of this WebView. If
Steve Block4e584df2012-04-24 23:12:47 +0100926 * it is called after this WebView has had a chance to build state (load
Cary Clarkd6982c92009-05-29 11:02:22 -0400927 * pages, create a back/forward list, etc.) there may be undesirable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 * side-effects. Please note that this method no longer restores the
Kristian Monsenf4912582012-08-16 15:26:24 -0400929 * display data for this WebView.
Steve Block4e584df2012-04-24 23:12:47 +0100930 *
931 * @param inState the incoming Bundle of state
Nate Fischer0a6140d2017-09-05 12:37:49 -0700932 * @return the restored back/forward list or {@code null} if restoreState failed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 */
Nate Fischer3442c742017-09-08 17:02:00 -0700934 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 public WebBackForwardList restoreState(Bundle inState) {
Steve Block51b08912011-04-27 15:04:48 +0100936 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000937 return mProvider.restoreState(inState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 }
939
940 /**
Steve Block4e584df2012-04-24 23:12:47 +0100941 * Loads the given URL with the specified additional HTTP headers.
Bo Liue29d3132016-04-22 16:23:22 -0700942 * <p>
943 * Also see compatibility note on {@link #evaluateJavascript}.
Steve Block4e584df2012-04-24 23:12:47 +0100944 *
945 * @param url the URL of the resource to load
946 * @param additionalHttpHeaders the additional headers to be used in the
Steve Blockf71dea02011-08-01 12:29:14 +0100947 * HTTP request for this URL, specified as a map from name to
948 * value. Note that if this map contains any of the headers
Steve Block4e584df2012-04-24 23:12:47 +0100949 * that are set by default by this WebView, such as those
Steve Blockf71dea02011-08-01 12:29:14 +0100950 * controlling caching, accept types or the User-Agent, their
Nate Fischerffb81c42016-09-26 12:35:47 -0700951 * values may be overridden by this WebView's defaults.
Grace Klobad0d9bc22010-01-26 18:08:28 -0800952 */
Steve Blockf71dea02011-08-01 12:29:14 +0100953 public void loadUrl(String url, Map<String, String> additionalHttpHeaders) {
Steve Block51b08912011-04-27 15:04:48 +0100954 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000955 mProvider.loadUrl(url, additionalHttpHeaders);
Grace Klobad0d9bc22010-01-26 18:08:28 -0800956 }
957
958 /**
Steve Block4e584df2012-04-24 23:12:47 +0100959 * Loads the given URL.
Bo Liue29d3132016-04-22 16:23:22 -0700960 * <p>
961 * Also see compatibility note on {@link #evaluateJavascript}.
Steve Block4e584df2012-04-24 23:12:47 +0100962 *
963 * @param url the URL of the resource to load
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 */
965 public void loadUrl(String url) {
Steve Block51b08912011-04-27 15:04:48 +0100966 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +0000967 mProvider.loadUrl(url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 }
969
970 /**
Steve Block4e584df2012-04-24 23:12:47 +0100971 * Loads the URL with postData using "POST" method into this WebView. If url
Hui Shuc60ad9c2014-02-05 09:30:36 -0800972 * is not a network URL, it will be loaded with {@link #loadUrl(String)}
973 * instead, ignoring the postData param.
Cary Clarkd6982c92009-05-29 11:02:22 -0400974 *
Steve Block4e584df2012-04-24 23:12:47 +0100975 * @param url the URL of the resource to load
Ben Murdoch851004a2013-08-16 16:43:22 +0100976 * @param postData the data will be passed to "POST" request, which must be
977 * be "application/x-www-form-urlencoded" encoded.
Grace Kloba57534302009-05-22 18:55:02 -0700978 */
979 public void postUrl(String url, byte[] postData) {
Steve Block51b08912011-04-27 15:04:48 +0100980 checkThread();
Hui Shuc60ad9c2014-02-05 09:30:36 -0800981 if (URLUtil.isNetworkUrl(url)) {
982 mProvider.postUrl(url, postData);
983 } else {
984 mProvider.loadUrl(url);
985 }
Grace Kloba57534302009-05-22 18:55:02 -0700986 }
987
988 /**
Steve Block4e584df2012-04-24 23:12:47 +0100989 * Loads the given data into this WebView using a 'data' scheme URL.
Steve Blockb28b22a2011-07-04 13:01:25 +0100990 * <p>
991 * Note that JavaScript's same origin policy means that script running in a
992 * page loaded using this method will be unable to access content loaded
993 * using any scheme other than 'data', including 'http(s)'. To avoid this
994 * restriction, use {@link
995 * #loadDataWithBaseURL(String,String,String,String,String)
996 * loadDataWithBaseURL()} with an appropriate base URL.
Steve Blockf95d4902011-06-09 11:53:26 +0100997 * <p>
Nate Fischer867d1092018-01-04 21:57:33 -0800998 * The {@code encoding} parameter specifies whether the data is base64 or URL
Steve Block27f3e322012-07-23 10:45:59 +0100999 * encoded. If the data is base64 encoded, the value of the encoding
Nate Fischer867d1092018-01-04 21:57:33 -08001000 * parameter must be 'base64'. HTML can be encoded with {@link
1001 * android.util.Base64#encodeToString(byte[],int)} like so:
1002 * <pre>
1003 * String unencodedHtml =
1004 * "&lt;html&gt;&lt;body&gt;'%28' is the code for '('&lt;/body&gt;&lt;/html&gt;";
1005 * String encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(), Base64.NO_PADDING);
1006 * webView.loadData(encodedHtml, "text/html", "base64");
1007 * </pre>
1008 * <p>
1009 * For all other values of {@code encoding} (including {@code null}) it is assumed that the
1010 * data uses ASCII encoding for octets inside the range of safe URL characters and use the
1011 * standard %xx hex encoding of URLs for octets outside that range. See <a
1012 * href="https://tools.ietf.org/html/rfc3986#section-2.2">RFC 3986</a> for more information.
Steve Blockb19c7872011-10-10 14:09:44 +01001013 * <p>
James Wallace-Leed38c2e32017-12-11 16:11:15 -08001014 * The {@code mimeType} parameter specifies the format of the data.
1015 * If WebView can't handle the specified MIME type, it will download the data.
1016 * If {@code null}, defaults to 'text/html'.
1017 * <p>
Steve Blockb19c7872011-10-10 14:09:44 +01001018 * The 'data' scheme URL formed by this method uses the default US-ASCII
1019 * charset. If you need need to set a different charset, you should form a
Steve Block33f962b2011-10-11 14:49:47 +01001020 * 'data' scheme URL which explicitly specifies a charset parameter in the
1021 * mediatype portion of the URL and call {@link #loadUrl(String)} instead.
1022 * Note that the charset obtained from the mediatype portion of a data URL
1023 * always overrides that specified in the HTML or XML document itself.
Steve Block4e584df2012-04-24 23:12:47 +01001024 *
1025 * @param data a String of data in the given encoding
James Wallace-Leed38c2e32017-12-11 16:11:15 -08001026 * @param mimeType the MIME type of the data, e.g. 'text/html'.
Steve Block4e584df2012-04-24 23:12:47 +01001027 * @param encoding the encoding of the data
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 */
Nate Fischer3442c742017-09-08 17:02:00 -07001029 public void loadData(String data, @Nullable String mimeType, @Nullable String encoding) {
Steve Block51b08912011-04-27 15:04:48 +01001030 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001031 mProvider.loadData(data, mimeType, encoding);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 }
1033
1034 /**
Steve Block4e584df2012-04-24 23:12:47 +01001035 * Loads the given data into this WebView, using baseUrl as the base URL for
Steve Blockb28b22a2011-07-04 13:01:25 +01001036 * the content. The base URL is used both to resolve relative URLs and when
1037 * applying JavaScript's same origin policy. The historyUrl is used for the
1038 * history entry.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 * <p>
James Wallace-Leed38c2e32017-12-11 16:11:15 -08001040 * The {@code mimeType} parameter specifies the format of the data.
1041 * If WebView can't handle the specified MIME type, it will download the data.
1042 * If {@code null}, defaults to 'text/html'.
1043 * <p>
Steve Blockae328572011-06-08 18:58:51 +01001044 * Note that content specified in this way can access local device files
1045 * (via 'file' scheme URLs) only if baseUrl specifies a scheme other than
1046 * 'http', 'https', 'ftp', 'ftps', 'about' or 'javascript'.
Steve Blocke482d892011-07-01 13:57:32 +01001047 * <p>
1048 * If the base URL uses the data scheme, this method is equivalent to
1049 * calling {@link #loadData(String,String,String) loadData()} and the
Ben Murdoch597256e2013-07-22 16:52:12 +01001050 * historyUrl is ignored, and the data will be treated as part of a data: URL.
1051 * If the base URL uses any other scheme, then the data will be loaded into
1052 * the WebView as a plain string (i.e. not part of a data URL) and any URL-encoded
1053 * entities in the string will not be decoded.
Mikhail Naganovb29e0f62015-09-16 13:05:53 -07001054 * <p>
1055 * Note that the baseUrl is sent in the 'Referer' HTTP header when
1056 * requesting subresources (images, etc.) of the page loaded using this method.
Steve Block4e584df2012-04-24 23:12:47 +01001057 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07001058 * @param baseUrl the URL to use as the page's base URL. If {@code null} defaults to
Steve Block4e584df2012-04-24 23:12:47 +01001059 * 'about:blank'.
1060 * @param data a String of data in the given encoding
James Wallace-Leed38c2e32017-12-11 16:11:15 -08001061 * @param mimeType the MIME type of the data, e.g. 'text/html'.
Steve Block4e584df2012-04-24 23:12:47 +01001062 * @param encoding the encoding of the data
Nate Fischer0a6140d2017-09-05 12:37:49 -07001063 * @param historyUrl the URL to use as the history entry. If {@code null} defaults
Ben Murdoch95afb3b2013-02-25 19:18:19 +00001064 * to 'about:blank'. If non-null, this must be a valid URL.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 */
Nate Fischer3442c742017-09-08 17:02:00 -07001066 public void loadDataWithBaseURL(@Nullable String baseUrl, String data,
1067 @Nullable String mimeType, @Nullable String encoding, @Nullable String historyUrl) {
Steve Block51b08912011-04-27 15:04:48 +01001068 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001069 mProvider.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 }
1071
1072 /**
Ben Murdocha5cdd512013-07-17 16:25:07 +01001073 * Asynchronously evaluates JavaScript in the context of the currently displayed page.
1074 * If non-null, |resultCallback| will be invoked with any result returned from that
1075 * execution. This method must be called on the UI thread and the callback will
1076 * be made on the UI thread.
Bo Liue29d3132016-04-22 16:23:22 -07001077 * <p>
1078 * Compatibility note. Applications targeting {@link android.os.Build.VERSION_CODES#N} or
1079 * later, JavaScript state from an empty WebView is no longer persisted across navigations like
1080 * {@link #loadUrl(String)}. For example, global variables and functions defined before calling
1081 * {@link #loadUrl(String)} will not exist in the loaded page. Applications should use
1082 * {@link #addJavascriptInterface} instead to persist JavaScript objects across navigations.
Ben Murdocha5cdd512013-07-17 16:25:07 +01001083 *
1084 * @param script the JavaScript to execute.
1085 * @param resultCallback A callback to be invoked when the script execution
1086 * completes with the result of the execution (if any).
Nate Fischer0a6140d2017-09-05 12:37:49 -07001087 * May be {@code null} if no notification of the result is required.
Ben Murdocha5cdd512013-07-17 16:25:07 +01001088 */
Nate Fischer3442c742017-09-08 17:02:00 -07001089 public void evaluateJavascript(String script, @Nullable ValueCallback<String> resultCallback) {
Ben Murdocha5cdd512013-07-17 16:25:07 +01001090 checkThread();
1091 mProvider.evaluateJavaScript(script, resultCallback);
1092 }
1093
1094 /**
Elliott Slaughterb48fdbe2010-06-30 11:39:52 -07001095 * Saves the current view as a web archive.
1096 *
Steve Block4e584df2012-04-24 23:12:47 +01001097 * @param filename the filename where the archive should be placed
Elliott Slaughterb48fdbe2010-06-30 11:39:52 -07001098 */
1099 public void saveWebArchive(String filename) {
Steve Block51b08912011-04-27 15:04:48 +01001100 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001101 mProvider.saveWebArchive(filename);
Elliott Slaughterb48fdbe2010-06-30 11:39:52 -07001102 }
1103
1104 /**
1105 * Saves the current view as a web archive.
1106 *
Steve Block4e584df2012-04-24 23:12:47 +01001107 * @param basename the filename where the archive should be placed
Nate Fischer0a6140d2017-09-05 12:37:49 -07001108 * @param autoname if {@code false}, takes basename to be a file. If {@code true}, basename
Elliott Slaughterb48fdbe2010-06-30 11:39:52 -07001109 * is assumed to be a directory in which a filename will be
Steve Block4e584df2012-04-24 23:12:47 +01001110 * chosen according to the URL of the current page.
1111 * @param callback called after the web archive has been saved. The
Elliott Slaughterb48fdbe2010-06-30 11:39:52 -07001112 * parameter for onReceiveValue will either be the filename
Nate Fischer0a6140d2017-09-05 12:37:49 -07001113 * under which the file was saved, or {@code null} if saving the
Elliott Slaughterb48fdbe2010-06-30 11:39:52 -07001114 * file failed.
1115 */
Nate Fischer3442c742017-09-08 17:02:00 -07001116 public void saveWebArchive(String basename, boolean autoname, @Nullable ValueCallback<String>
1117 callback) {
Steve Block51b08912011-04-27 15:04:48 +01001118 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001119 mProvider.saveWebArchive(basename, autoname, callback);
Elliott Slaughterb48fdbe2010-06-30 11:39:52 -07001120 }
1121
1122 /**
Steve Block4e584df2012-04-24 23:12:47 +01001123 * Stops the current load.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 */
1125 public void stopLoading() {
Steve Block51b08912011-04-27 15:04:48 +01001126 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001127 mProvider.stopLoading();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
1129
1130 /**
Steve Block4e584df2012-04-24 23:12:47 +01001131 * Reloads the current URL.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 */
1133 public void reload() {
Steve Block51b08912011-04-27 15:04:48 +01001134 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001135 mProvider.reload();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 }
1137
1138 /**
Steve Block4e584df2012-04-24 23:12:47 +01001139 * Gets whether this WebView has a back history item.
1140 *
Nate Fischer2be201e2017-10-26 10:43:00 -07001141 * @return {@code true} if this WebView has a back history item
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 */
1143 public boolean canGoBack() {
Steve Block51b08912011-04-27 15:04:48 +01001144 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001145 return mProvider.canGoBack();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 }
1147
1148 /**
Steve Block4e584df2012-04-24 23:12:47 +01001149 * Goes back in the history of this WebView.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 */
1151 public void goBack() {
Steve Block51b08912011-04-27 15:04:48 +01001152 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001153 mProvider.goBack();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 }
1155
1156 /**
Steve Block4e584df2012-04-24 23:12:47 +01001157 * Gets whether this WebView has a forward history item.
1158 *
Nate Fischer2be201e2017-10-26 10:43:00 -07001159 * @return {@code true} if this WebView has a forward history item
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 */
1161 public boolean canGoForward() {
Steve Block51b08912011-04-27 15:04:48 +01001162 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001163 return mProvider.canGoForward();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
1165
1166 /**
Steve Block4e584df2012-04-24 23:12:47 +01001167 * Goes forward in the history of this WebView.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 */
1169 public void goForward() {
Steve Block51b08912011-04-27 15:04:48 +01001170 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001171 mProvider.goForward();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 }
1173
1174 /**
Steve Block4e584df2012-04-24 23:12:47 +01001175 * Gets whether the page can go back or forward the given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 * number of steps.
Steve Block4e584df2012-04-24 23:12:47 +01001177 *
1178 * @param steps the negative or positive number of steps to move the
1179 * history
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 */
1181 public boolean canGoBackOrForward(int steps) {
Steve Block51b08912011-04-27 15:04:48 +01001182 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001183 return mProvider.canGoBackOrForward(steps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 }
1185
1186 /**
Steve Block4e584df2012-04-24 23:12:47 +01001187 * Goes to the history item that is the number of steps away from
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 * the current item. Steps is negative if backward and positive
1189 * if forward.
Steve Block4e584df2012-04-24 23:12:47 +01001190 *
1191 * @param steps the number of steps to take back or forward in the back
1192 * forward list
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 */
1194 public void goBackOrForward(int steps) {
Steve Block51b08912011-04-27 15:04:48 +01001195 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001196 mProvider.goBackOrForward(steps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 }
Cary Clarkd6982c92009-05-29 11:02:22 -04001198
Elliott Slaughterf21d2e32010-07-14 18:08:54 -07001199 /**
Steve Block4e584df2012-04-24 23:12:47 +01001200 * Gets whether private browsing is enabled in this WebView.
Elliott Slaughterf21d2e32010-07-14 18:08:54 -07001201 */
Elliott Slaughter7c2d1352010-08-20 15:57:18 -07001202 public boolean isPrivateBrowsingEnabled() {
Steve Block51b08912011-04-27 15:04:48 +01001203 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001204 return mProvider.isPrivateBrowsingEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 }
Cary Clarkd6982c92009-05-29 11:02:22 -04001206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 /**
Steve Block4e584df2012-04-24 23:12:47 +01001208 * Scrolls the contents of this WebView up by half the view size.
1209 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07001210 * @param top {@code true} to jump to the top of the page
1211 * @return {@code true} if the page was scrolled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 */
1213 public boolean pageUp(boolean top) {
Steve Block51b08912011-04-27 15:04:48 +01001214 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001215 return mProvider.pageUp(top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 }
Cary Clarkd6982c92009-05-29 11:02:22 -04001217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 /**
Steve Block4e584df2012-04-24 23:12:47 +01001219 * Scrolls the contents of this WebView down by half the page size.
1220 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07001221 * @param bottom {@code true} to jump to bottom of page
1222 * @return {@code true} if the page was scrolled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 */
1224 public boolean pageDown(boolean bottom) {
Steve Block51b08912011-04-27 15:04:48 +01001225 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001226 return mProvider.pageDown(bottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 }
1228
1229 /**
Tobias Sargeantb3656042015-05-13 11:23:57 +01001230 * Posts a {@link VisualStateCallback}, which will be called when
1231 * the current state of the WebView is ready to be drawn.
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001232 *
Nate Fischerffb81c42016-09-26 12:35:47 -07001233 * <p>Because updates to the DOM are processed asynchronously, updates to the DOM may not
Tobias Sargeantb3656042015-05-13 11:23:57 +01001234 * immediately be reflected visually by subsequent {@link WebView#onDraw} invocations. The
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001235 * {@link VisualStateCallback} provides a mechanism to notify the caller when the contents of
Tobias Sargeantb3656042015-05-13 11:23:57 +01001236 * the DOM at the current time are ready to be drawn the next time the {@link WebView}
Nate Fischerf02f8462017-09-11 15:16:33 -07001237 * draws.
Tobias Sargeantb3656042015-05-13 11:23:57 +01001238 *
1239 * <p>The next draw after the callback completes is guaranteed to reflect all the updates to the
Nate Fischerffb81c42016-09-26 12:35:47 -07001240 * DOM up to the point at which the {@link VisualStateCallback} was posted, but it may also
Nate Fischerf02f8462017-09-11 15:16:33 -07001241 * contain updates applied after the callback was posted.
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001242 *
1243 * <p>The state of the DOM covered by this API includes the following:
1244 * <ul>
1245 * <li>primitive HTML elements (div, img, span, etc..)</li>
1246 * <li>images</li>
1247 * <li>CSS animations</li>
1248 * <li>WebGL</li>
1249 * <li>canvas</li>
1250 * </ul>
1251 * It does not include the state of:
1252 * <ul>
1253 * <li>the video tag</li>
Nate Fischerf02f8462017-09-11 15:16:33 -07001254 * </ul>
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001255 *
1256 * <p>To guarantee that the {@link WebView} will successfully render the first frame
1257 * after the {@link VisualStateCallback#onComplete} method has been called a set of conditions
1258 * must be met:
1259 * <ul>
1260 * <li>If the {@link WebView}'s visibility is set to {@link View#VISIBLE VISIBLE} then
1261 * the {@link WebView} must be attached to the view hierarchy.</li>
1262 * <li>If the {@link WebView}'s visibility is set to {@link View#INVISIBLE INVISIBLE}
1263 * then the {@link WebView} must be attached to the view hierarchy and must be made
1264 * {@link View#VISIBLE VISIBLE} from the {@link VisualStateCallback#onComplete} method.</li>
1265 * <li>If the {@link WebView}'s visibility is set to {@link View#GONE GONE} then the
1266 * {@link WebView} must be attached to the view hierarchy and its
1267 * {@link AbsoluteLayout.LayoutParams LayoutParams}'s width and height need to be set to fixed
1268 * values and must be made {@link View#VISIBLE VISIBLE} from the
1269 * {@link VisualStateCallback#onComplete} method.</li>
Nate Fischerf02f8462017-09-11 15:16:33 -07001270 * </ul>
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001271 *
Tobias Sargeantb3656042015-05-13 11:23:57 +01001272 * <p>When using this API it is also recommended to enable pre-rasterization if the {@link
Nate Fischerffb81c42016-09-26 12:35:47 -07001273 * WebView} is off screen to avoid flickering. See {@link WebSettings#setOffscreenPreRaster} for
Nate Fischerf02f8462017-09-11 15:16:33 -07001274 * more details and do consider its caveats.
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001275 *
Tobias Sargeantb3656042015-05-13 11:23:57 +01001276 * @param requestId An id that will be returned in the callback to allow callers to match
1277 * requests with callbacks.
1278 * @param callback The callback to be invoked.
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001279 */
Tobias Sargeantb3656042015-05-13 11:23:57 +01001280 public void postVisualStateCallback(long requestId, VisualStateCallback callback) {
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001281 checkThread();
Tobias Sargeanta8352f42015-03-03 18:07:40 +00001282 mProvider.insertVisualStateCallback(requestId, callback);
1283 }
1284
1285 /**
Steve Block4e584df2012-04-24 23:12:47 +01001286 * Clears this WebView so that onDraw() will draw nothing but white background,
1287 * and onMeasure() will return 0 if MeasureSpec is not MeasureSpec.EXACTLY.
Jonathan Dixon835b1fc2013-02-25 12:29:33 -08001288 * @deprecated Use WebView.loadUrl("about:blank") to reliably reset the view state
1289 * and release page resources (including any running JavaScript).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 */
Jonathan Dixon835b1fc2013-02-25 12:29:33 -08001291 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 public void clearView() {
Steve Block51b08912011-04-27 15:04:48 +01001293 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001294 mProvider.clearView();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 }
Cary Clarkd6982c92009-05-29 11:02:22 -04001296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 /**
Steve Block72498ed2012-07-17 15:57:25 +01001298 * Gets a new picture that captures the current contents of this WebView.
1299 * The picture is of the entire document being displayed, and is not
1300 * limited to the area currently displayed by this WebView. Also, the
1301 * picture is a static copy and is unaffected by later changes to the
1302 * content being displayed.
1303 * <p>
1304 * Note that due to internal changes, for API levels between
1305 * {@link android.os.Build.VERSION_CODES#HONEYCOMB} and
1306 * {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH} inclusive, the
1307 * picture does not include fixed position elements or scrollable divs.
Jonathan Dixon5545d082013-08-31 22:43:11 -07001308 * <p>
1309 * Note that from {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} the returned picture
1310 * should only be drawn into bitmap-backed Canvas - using any other type of Canvas will involve
1311 * additional conversion at a cost in memory and performance. Also the
1312 * {@link android.graphics.Picture#createFromStream} and
1313 * {@link android.graphics.Picture#writeToStream} methods are not supported on the
1314 * returned object.
1315 *
1316 * @deprecated Use {@link #onDraw} to obtain a bitmap snapshot of the WebView, or
Jonathan Dixondf3859e2013-09-18 12:51:47 -07001317 * {@link #saveWebArchive} to save the content to a file.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 *
Steve Block72498ed2012-07-17 15:57:25 +01001319 * @return a picture that captures the current contents of this WebView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 */
Jonathan Dixon5545d082013-08-31 22:43:11 -07001321 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 public Picture capturePicture() {
Steve Block51b08912011-04-27 15:04:48 +01001323 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001324 return mProvider.capturePicture();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 }
1326
Cary Clarkd6982c92009-05-29 11:02:22 -04001327 /**
Selim Gurun23a1af62014-03-14 16:52:45 -07001328 * @deprecated Use {@link #createPrintDocumentAdapter(String)} which requires user
1329 * to provide a print document name.
1330 */
1331 @Deprecated
1332 public PrintDocumentAdapter createPrintDocumentAdapter() {
1333 checkThread();
Selim Gurun23a1af62014-03-14 16:52:45 -07001334 return mProvider.createPrintDocumentAdapter("default");
1335 }
1336
1337 /**
Nate Fischerffb81c42016-09-26 12:35:47 -07001338 * Creates a PrintDocumentAdapter that provides the content of this WebView for printing.
Selim Gurun2167c0b2013-08-05 15:23:09 -07001339 *
Nate Fischerffb81c42016-09-26 12:35:47 -07001340 * The adapter works by converting the WebView contents to a PDF stream. The WebView cannot
Selim Gurunda7d6422013-09-06 14:39:05 -07001341 * be drawn during the conversion process - any such draws are undefined. It is recommended
Nate Fischerffb81c42016-09-26 12:35:47 -07001342 * to use a dedicated off screen WebView for the printing. If necessary, an application may
Selim Gurunda7d6422013-09-06 14:39:05 -07001343 * temporarily hide a visible WebView by using a custom PrintDocumentAdapter instance
1344 * wrapped around the object returned and observing the onStart and onFinish methods. See
1345 * {@link android.print.PrintDocumentAdapter} for more information.
Selim Gurun23a1af62014-03-14 16:52:45 -07001346 *
1347 * @param documentName The user-facing name of the printed document. See
1348 * {@link android.print.PrintDocumentInfo}
Selim Gurun2167c0b2013-08-05 15:23:09 -07001349 */
Selim Gurun23a1af62014-03-14 16:52:45 -07001350 public PrintDocumentAdapter createPrintDocumentAdapter(String documentName) {
Selim Gurun2167c0b2013-08-05 15:23:09 -07001351 checkThread();
Selim Gurun23a1af62014-03-14 16:52:45 -07001352 return mProvider.createPrintDocumentAdapter(documentName);
Selim Gurun2167c0b2013-08-05 15:23:09 -07001353 }
1354
1355 /**
Steve Block4e584df2012-04-24 23:12:47 +01001356 * Gets the current scale of this WebView.
1357 *
1358 * @return the current scale
Kristian Monsen5cc23512012-08-09 15:33:08 -04001359 *
1360 * @deprecated This method is prone to inaccuracy due to race conditions
1361 * between the web rendering and UI threads; prefer
1362 * {@link WebViewClient#onScaleChanged}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 */
Kristian Monsen5cc23512012-08-09 15:33:08 -04001364 @Deprecated
John Reck926cf562012-06-14 10:00:31 -07001365 @ViewDebug.ExportedProperty(category = "webview")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 public float getScale() {
Steve Block51b08912011-04-27 15:04:48 +01001367 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001368 return mProvider.getScale();
Mangesh Ghiwareb5f9fc32011-08-31 17:49:07 -07001369 }
1370
1371 /**
Mikhail Naganov00303362013-09-02 10:57:04 +01001372 * Sets the initial scale for this WebView. 0 means default.
1373 * The behavior for the default scale depends on the state of
1374 * {@link WebSettings#getUseWideViewPort()} and
1375 * {@link WebSettings#getLoadWithOverviewMode()}.
1376 * If the content fits into the WebView control by width, then
Nate Fischerffb81c42016-09-26 12:35:47 -07001377 * the zoom is set to 100%. For wide content, the behavior
Mikhail Naganov00303362013-09-02 10:57:04 +01001378 * depends on the state of {@link WebSettings#getLoadWithOverviewMode()}.
Nate Fischer0a6140d2017-09-05 12:37:49 -07001379 * If its value is {@code true}, the content will be zoomed out to be fit
Mikhail Naganov00303362013-09-02 10:57:04 +01001380 * by width into the WebView control, otherwise not.
1381 *
1382 * If initial scale is greater than 0, WebView starts with this value
1383 * as initial scale.
Mangesh Ghiwaree832b632011-11-16 11:46:39 -08001384 * Please note that unlike the scale properties in the viewport meta tag,
1385 * this method doesn't take the screen density into account.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 *
Steve Block4e584df2012-04-24 23:12:47 +01001387 * @param scaleInPercent the initial scale in percent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 */
1389 public void setInitialScale(int scaleInPercent) {
Steve Block51b08912011-04-27 15:04:48 +01001390 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001391 mProvider.setInitialScale(scaleInPercent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 }
1393
1394 /**
Steve Block4e584df2012-04-24 23:12:47 +01001395 * Invokes the graphical zoom picker widget for this WebView. This will
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 * result in the zoom widget appearing on the screen to control the zoom
1397 * level of this WebView.
1398 */
1399 public void invokeZoomPicker() {
Steve Block51b08912011-04-27 15:04:48 +01001400 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001401 mProvider.invokeZoomPicker();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 }
1403
1404 /**
Steve Block4e584df2012-04-24 23:12:47 +01001405 * Gets a HitTestResult based on the current cursor node. If a HTML::a
1406 * tag is found and the anchor has a non-JavaScript URL, the HitTestResult
1407 * type is set to SRC_ANCHOR_TYPE and the URL is set in the "extra" field.
1408 * If the anchor does not have a URL or if it is a JavaScript URL, the type
1409 * will be UNKNOWN_TYPE and the URL has to be retrieved through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 * {@link #requestFocusNodeHref} asynchronously. If a HTML::img tag is
Steve Block4e584df2012-04-24 23:12:47 +01001411 * 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 -08001412 * the "extra" field. A type of
Steve Block4e584df2012-04-24 23:12:47 +01001413 * 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 -08001414 * a child node. If a phone number is found, the HitTestResult type is set
1415 * to PHONE_TYPE and the phone number is set in the "extra" field of
1416 * HitTestResult. If a map address is found, the HitTestResult type is set
1417 * to GEO_TYPE and the address is set in the "extra" field of HitTestResult.
1418 * If an email address is found, the HitTestResult type is set to EMAIL_TYPE
1419 * and the email is set in the "extra" field of HitTestResult. Otherwise,
1420 * HitTestResult type is set to UNKNOWN_TYPE.
1421 */
1422 public HitTestResult getHitTestResult() {
Steve Block51b08912011-04-27 15:04:48 +01001423 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001424 return mProvider.getHitTestResult();
Cary Clarkb8491342010-11-29 16:23:19 -05001425 }
1426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 /**
Steve Block4e584df2012-04-24 23:12:47 +01001428 * Requests the anchor or image element URL at the last tapped point.
Nate Fischer0a6140d2017-09-05 12:37:49 -07001429 * If hrefMsg is {@code null}, this method returns immediately and does not
Cary Clark861368a2010-12-15 11:24:37 -05001430 * dispatch hrefMsg to its target. If the tapped point hits an image,
1431 * an anchor, or an image in an anchor, the message associates
1432 * strings in named keys in its data. The value paired with the key
1433 * may be an empty string.
Cary Clarkd6982c92009-05-29 11:02:22 -04001434 *
Steve Block4e584df2012-04-24 23:12:47 +01001435 * @param hrefMsg the message to be dispatched with the result of the
1436 * request. The message data contains three keys. "url"
1437 * returns the anchor's href attribute. "title" returns the
1438 * anchor's text. "src" returns the image's src attribute.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 */
Nate Fischer3442c742017-09-08 17:02:00 -07001440 public void requestFocusNodeHref(@Nullable Message hrefMsg) {
Steve Block51b08912011-04-27 15:04:48 +01001441 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001442 mProvider.requestFocusNodeHref(hrefMsg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 }
Cary Clarkd6982c92009-05-29 11:02:22 -04001444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 /**
Steve Block4e584df2012-04-24 23:12:47 +01001446 * Requests the URL of the image last touched by the user. msg will be sent
1447 * to its target with a String representing the URL as its object.
Cary Clarkd6982c92009-05-29 11:02:22 -04001448 *
Steve Block4e584df2012-04-24 23:12:47 +01001449 * @param msg the message to be dispatched with the result of the request
Nate Fischer0a6140d2017-09-05 12:37:49 -07001450 * as the data member with "url" as key. The result can be {@code null}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 */
1452 public void requestImageRef(Message msg) {
Steve Block51b08912011-04-27 15:04:48 +01001453 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001454 mProvider.requestImageRef(msg);
Adam Powell637d3372010-08-25 14:37:03 -07001455 }
1456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 /**
Steve Block4e584df2012-04-24 23:12:47 +01001458 * 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 -08001459 * passed to WebViewClient.onPageStarted because although the load for
Steve Block4e584df2012-04-24 23:12:47 +01001460 * that URL has begun, the current page may not have changed.
1461 *
1462 * @return the URL for the current page
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 */
John Reck926cf562012-06-14 10:00:31 -07001464 @ViewDebug.ExportedProperty(category = "webview")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 public String getUrl() {
Steve Block51b08912011-04-27 15:04:48 +01001466 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001467 return mProvider.getUrl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 }
Cary Clarkd6982c92009-05-29 11:02:22 -04001469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 /**
Steve Block4e584df2012-04-24 23:12:47 +01001471 * Gets the original URL for the current page. This is not always the same
1472 * as the URL passed to WebViewClient.onPageStarted because although the
1473 * load for that URL has begun, the current page may not have changed.
1474 * Also, there may have been redirects resulting in a different URL to that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 * originally requested.
Steve Block4e584df2012-04-24 23:12:47 +01001476 *
1477 * @return the URL that was originally requested for the current page
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 */
John Reck926cf562012-06-14 10:00:31 -07001479 @ViewDebug.ExportedProperty(category = "webview")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 public String getOriginalUrl() {
Steve Block51b08912011-04-27 15:04:48 +01001481 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001482 return mProvider.getOriginalUrl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 }
1484
1485 /**
Steve Block4e584df2012-04-24 23:12:47 +01001486 * 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 -08001487 * until WebViewClient.onReceivedTitle is called.
Steve Block4e584df2012-04-24 23:12:47 +01001488 *
1489 * @return the title for the current page
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 */
John Reck926cf562012-06-14 10:00:31 -07001491 @ViewDebug.ExportedProperty(category = "webview")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 public String getTitle() {
Steve Block51b08912011-04-27 15:04:48 +01001493 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001494 return mProvider.getTitle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 }
1496
1497 /**
Steve Block4e584df2012-04-24 23:12:47 +01001498 * Gets the favicon for the current page. This is the favicon of the current
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 * page until WebViewClient.onReceivedIcon is called.
Steve Block4e584df2012-04-24 23:12:47 +01001500 *
1501 * @return the favicon for the current page
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 */
1503 public Bitmap getFavicon() {
Steve Block51b08912011-04-27 15:04:48 +01001504 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001505 return mProvider.getFavicon();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 }
1507
1508 /**
Steve Block4e584df2012-04-24 23:12:47 +01001509 * Gets the touch icon URL for the apple-touch-icon <link> element, or
Ben Murdoch372dfc82010-07-01 15:56:01 +01001510 * a URL on this site's server pointing to the standard location of a
1511 * touch icon.
Steve Block4e584df2012-04-24 23:12:47 +01001512 *
Patrick Scott2ba12622009-08-04 13:20:05 -04001513 * @hide
1514 */
1515 public String getTouchIconUrl() {
Jonathan Dixon3c909522012-02-28 18:45:06 +00001516 return mProvider.getTouchIconUrl();
Patrick Scott2ba12622009-08-04 13:20:05 -04001517 }
1518
1519 /**
Steve Block4e584df2012-04-24 23:12:47 +01001520 * Gets the progress for the current page.
1521 *
1522 * @return the progress for the current page between 0 and 100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 */
1524 public int getProgress() {
Steve Block51b08912011-04-27 15:04:48 +01001525 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001526 return mProvider.getProgress();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 }
Cary Clarkd6982c92009-05-29 11:02:22 -04001528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 /**
Steve Block4e584df2012-04-24 23:12:47 +01001530 * Gets the height of the HTML content.
1531 *
1532 * @return the height of the HTML content
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 */
John Reck926cf562012-06-14 10:00:31 -07001534 @ViewDebug.ExportedProperty(category = "webview")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 public int getContentHeight() {
Steve Block51b08912011-04-27 15:04:48 +01001536 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001537 return mProvider.getContentHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 }
1539
1540 /**
Steve Block4e584df2012-04-24 23:12:47 +01001541 * Gets the width of the HTML content.
1542 *
1543 * @return the width of the HTML content
Leon Scrogginsea96d1e2009-09-23 13:41:01 -04001544 * @hide
1545 */
John Reck926cf562012-06-14 10:00:31 -07001546 @ViewDebug.ExportedProperty(category = "webview")
Leon Scrogginsea96d1e2009-09-23 13:41:01 -04001547 public int getContentWidth() {
Jonathan Dixon3c909522012-02-28 18:45:06 +00001548 return mProvider.getContentWidth();
John Reck9b90d552011-06-14 15:19:17 -07001549 }
1550
1551 /**
Steve Block4e584df2012-04-24 23:12:47 +01001552 * Pauses all layout, parsing, and JavaScript timers for all WebViews. This
1553 * is a global requests, not restricted to just this WebView. This can be
Mike Reedd205d5b2009-05-27 11:02:29 -04001554 * useful if the application has been paused.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 */
1556 public void pauseTimers() {
Steve Block51b08912011-04-27 15:04:48 +01001557 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001558 mProvider.pauseTimers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 }
1560
1561 /**
Steve Block4e584df2012-04-24 23:12:47 +01001562 * Resumes all layout, parsing, and JavaScript timers for all WebViews.
Mike Reedd205d5b2009-05-27 11:02:29 -04001563 * This will resume dispatching all timers.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 */
1565 public void resumeTimers() {
Steve Block51b08912011-04-27 15:04:48 +01001566 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001567 mProvider.resumeTimers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 }
1569
1570 /**
Selim Gurun922cb582015-07-31 11:49:22 -07001571 * Does a best-effort attempt to pause any processing that can be paused
1572 * safely, such as animations and geolocation. Note that this call
1573 * does not pause JavaScript. To pause JavaScript globally, use
1574 * {@link #pauseTimers}.
1575 *
1576 * To resume WebView, call {@link #onResume}.
Mike Reedd205d5b2009-05-27 11:02:29 -04001577 */
1578 public void onPause() {
Steve Block51b08912011-04-27 15:04:48 +01001579 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001580 mProvider.onPause();
Mike Reedd205d5b2009-05-27 11:02:29 -04001581 }
1582
1583 /**
Selim Gurun922cb582015-07-31 11:49:22 -07001584 * Resumes a WebView after a previous call to {@link #onPause}.
Mike Reedd205d5b2009-05-27 11:02:29 -04001585 */
1586 public void onResume() {
Steve Block51b08912011-04-27 15:04:48 +01001587 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001588 mProvider.onResume();
Mike Reedd205d5b2009-05-27 11:02:29 -04001589 }
1590
1591 /**
Steve Block4e584df2012-04-24 23:12:47 +01001592 * Gets whether this WebView is paused, meaning onPause() was called.
Nate Fischer0a6140d2017-09-05 12:37:49 -07001593 * Calling onResume() sets the paused state back to {@code false}.
Steve Block4e584df2012-04-24 23:12:47 +01001594 *
Mike Reedd205d5b2009-05-27 11:02:29 -04001595 * @hide
1596 */
1597 public boolean isPaused() {
Jonathan Dixon3c909522012-02-28 18:45:06 +00001598 return mProvider.isPaused();
Mike Reedd205d5b2009-05-27 11:02:29 -04001599 }
1600
1601 /**
Steve Block4e584df2012-04-24 23:12:47 +01001602 * Informs this WebView that memory is low so that it can free any available
1603 * memory.
Jonathan Dixon5545d082013-08-31 22:43:11 -07001604 * @deprecated Memory caches are automatically dropped when no longer needed, and in response
1605 * to system memory pressure.
Derek Sollenbergere0155e92009-06-10 15:35:45 -04001606 */
Jonathan Dixon5545d082013-08-31 22:43:11 -07001607 @Deprecated
Derek Sollenbergere0155e92009-06-10 15:35:45 -04001608 public void freeMemory() {
Steve Block51b08912011-04-27 15:04:48 +01001609 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001610 mProvider.freeMemory();
Derek Sollenbergere0155e92009-06-10 15:35:45 -04001611 }
1612
1613 /**
Steve Block4e584df2012-04-24 23:12:47 +01001614 * Clears the resource cache. Note that the cache is per-application, so
Mike Hearnadcd2ed2009-01-21 16:44:36 +01001615 * this will clear the cache for all WebViews used.
1616 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07001617 * @param includeDiskFiles if {@code false}, only the RAM cache is cleared
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 */
1619 public void clearCache(boolean includeDiskFiles) {
Steve Block51b08912011-04-27 15:04:48 +01001620 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001621 mProvider.clearCache(includeDiskFiles);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 }
1623
1624 /**
Steve Block219dfa42012-07-20 10:31:21 +01001625 * Removes the autocomplete popup from the currently focused form field, if
1626 * present. Note this only affects the display of the autocomplete popup,
1627 * it does not remove any saved form data from this WebView's store. To do
1628 * that, use {@link WebViewDatabase#clearFormData}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 */
1630 public void clearFormData() {
Steve Block51b08912011-04-27 15:04:48 +01001631 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001632 mProvider.clearFormData();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 }
1634
1635 /**
Steve Block4e584df2012-04-24 23:12:47 +01001636 * Tells this WebView to clear its internal back/forward list.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 */
1638 public void clearHistory() {
Steve Block51b08912011-04-27 15:04:48 +01001639 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001640 mProvider.clearHistory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 }
1642
1643 /**
Steve Block4e584df2012-04-24 23:12:47 +01001644 * Clears the SSL preferences table stored in response to proceeding with
1645 * SSL certificate errors.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 */
1647 public void clearSslPreferences() {
Steve Block51b08912011-04-27 15:04:48 +01001648 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001649 mProvider.clearSslPreferences();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 }
1651
1652 /**
Selim Gurunee89ab42014-06-13 10:53:55 -07001653 * Clears the client certificate preferences stored in response
Nate Fischerffb81c42016-09-26 12:35:47 -07001654 * to proceeding/cancelling client cert requests. Note that WebView
Selim Gurunb6aa97e2014-03-26 14:10:28 -07001655 * automatically clears these preferences when it receives a
Selim Gurunee89ab42014-06-13 10:53:55 -07001656 * {@link KeyChain#ACTION_STORAGE_CHANGED} intent. The preferences are
Nate Fischerffb81c42016-09-26 12:35:47 -07001657 * shared by all the WebViews that are created by the embedder application.
Selim Gurunb6aa97e2014-03-26 14:10:28 -07001658 *
Selim Gurune1c6c3a2014-04-28 14:21:21 -07001659 * @param onCleared A runnable to be invoked when client certs are cleared.
Nate Fischer3442c742017-09-08 17:02:00 -07001660 * The runnable will be called in UI thread.
Selim Gurunb6aa97e2014-03-26 14:10:28 -07001661 */
Nate Fischer3442c742017-09-08 17:02:00 -07001662 public static void clearClientCertPreferences(@Nullable Runnable onCleared) {
Selim Gurune1c6c3a2014-04-28 14:21:21 -07001663 getFactory().getStatics().clearClientCertPreferences(onCleared);
Selim Gurunb6aa97e2014-03-26 14:10:28 -07001664 }
1665
1666 /**
Nate Fischer2b108d82017-08-21 17:59:55 -07001667 * Starts Safe Browsing initialization.
Nate Fischer471891d2017-07-18 19:15:52 -07001668 * <p>
Nate Fischer2b108d82017-08-21 17:59:55 -07001669 * URL loads are not guaranteed to be protected by Safe Browsing until after {@code callback} is
1670 * invoked with {@code true}. Safe Browsing is not fully supported on all devices. For those
1671 * devices {@code callback} will receive {@code false}.
Nate Fischer471891d2017-07-18 19:15:52 -07001672 * <p>
Nate Fischer2b108d82017-08-21 17:59:55 -07001673 * This does not enable the Safe Browsing feature itself, and should only be called if Safe
1674 * Browsing is enabled by the manifest tag or {@link WebSettings#setSafeBrowsingEnabled}. This
1675 * prepares resources used for Safe Browsing.
Nate Fischer471891d2017-07-18 19:15:52 -07001676 * <p>
Nate Fischer2b108d82017-08-21 17:59:55 -07001677 * This should be called with the Application Context (and will always use the Application
1678 * context to do its work regardless).
Nate Fischer471891d2017-07-18 19:15:52 -07001679 *
Nate Fischer3ca196a2017-07-12 18:45:03 -07001680 * @param context Application Context.
Nate Fischer2b108d82017-08-21 17:59:55 -07001681 * @param callback will be called on the UI thread with {@code true} if initialization is
1682 * successful, {@code false} otherwise.
Nate Fischer0e72c182017-05-30 11:25:17 -07001683 */
Nate Fischer3442c742017-09-08 17:02:00 -07001684 public static void startSafeBrowsing(Context context,
1685 @Nullable ValueCallback<Boolean> callback) {
Nate Fischer0e72c182017-05-30 11:25:17 -07001686 getFactory().getStatics().initSafeBrowsing(context, callback);
1687 }
1688
1689 /**
Nate Fischerbf158ec2017-06-27 22:52:20 -07001690 * Sets the list of domains that are exempt from SafeBrowsing checks. The list is
1691 * global for all the WebViews.
Selim Gurun89e93592017-07-28 10:33:58 -07001692 * <p>
1693 * Each rule should take one of these:
1694 * <table>
1695 * <tr><th> Rule </th> <th> Example </th> <th> Matches Subdomain</th> </tr>
Nate Fischerfbebfa92017-08-29 16:17:22 -07001696 * <tr><td> HOSTNAME </td> <td> example.com </td> <td> Yes </td> </tr>
1697 * <tr><td> .HOSTNAME </td> <td> .example.com </td> <td> No </td> </tr>
1698 * <tr><td> IPV4_LITERAL </td> <td> 192.168.1.1 </td> <td> No </td></tr>
1699 * <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 -07001700 * </table>
1701 * <p>
1702 * All other rules, including wildcards, are invalid.
Nate Fischerbf158ec2017-06-27 22:52:20 -07001703 *
1704 * @param urls the list of URLs
Nate Fischer0a6140d2017-09-05 12:37:49 -07001705 * @param callback will be called with {@code true} if URLs are successfully added to the
1706 * whitelist. It will be called with {@code false} if any URLs are malformed. The callback will
1707 * be run on the UI thread
Nate Fischerbf158ec2017-06-27 22:52:20 -07001708 */
Nate Fischerc5ec7452017-07-18 16:08:42 -07001709 public static void setSafeBrowsingWhitelist(@NonNull List<String> urls,
Nate Fischerd6385d32017-07-17 12:58:18 -07001710 @Nullable ValueCallback<Boolean> callback) {
1711 getFactory().getStatics().setSafeBrowsingWhitelist(urls, callback);
Nate Fischerbf158ec2017-06-27 22:52:20 -07001712 }
1713
1714 /**
Nate Fischer3898ac12017-08-09 23:02:36 -07001715 * Returns a URL pointing to the privacy policy for Safe Browsing reporting.
1716 *
1717 * @return the url pointing to a privacy policy document which can be displayed to users.
1718 */
1719 @NonNull
1720 public static Uri getSafeBrowsingPrivacyPolicyUrl() {
1721 return getFactory().getStatics().getSafeBrowsingPrivacyPolicyUrl();
1722 }
1723
1724 /**
Steve Block4e584df2012-04-24 23:12:47 +01001725 * Gets the WebBackForwardList for this WebView. This contains the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 * back/forward list for use in querying each item in the history stack.
1727 * This is a copy of the private WebBackForwardList so it contains only a
1728 * snapshot of the current state. Multiple calls to this method may return
1729 * different objects. The object returned from this method will not be
1730 * updated to reflect any new state.
1731 */
1732 public WebBackForwardList copyBackForwardList() {
Steve Block51b08912011-04-27 15:04:48 +01001733 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001734 return mProvider.copyBackForwardList();
1735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 }
1737
Victoria Leaseabeb6a72012-03-05 16:29:12 -08001738 /**
Steve Block4e584df2012-04-24 23:12:47 +01001739 * Registers the listener to be notified as find-on-page operations
1740 * progress. This will replace the current listener.
Victoria Leaseabeb6a72012-03-05 16:29:12 -08001741 *
Steve Block4e584df2012-04-24 23:12:47 +01001742 * @param listener an implementation of {@link FindListener}
Victoria Leaseabeb6a72012-03-05 16:29:12 -08001743 */
1744 public void setFindListener(FindListener listener) {
1745 checkThread();
Ben Murdoch52c9f7f2013-01-18 00:50:37 +00001746 setupFindListenerIfNeeded();
1747 mFindListener.mUserFindListener = listener;
Victoria Leaseabeb6a72012-03-05 16:29:12 -08001748 }
1749
1750 /**
Kristian Monsenf4912582012-08-16 15:26:24 -04001751 * Highlights and scrolls to the next match found by
Victoria Lease0b8413b2012-03-26 13:04:10 -07001752 * {@link #findAllAsync}, wrapping around page boundaries as necessary.
Kristian Monsenf4912582012-08-16 15:26:24 -04001753 * Notifies any registered {@link FindListener}. If {@link #findAllAsync(String)}
1754 * has not been called yet, or if {@link #clearMatches} has been called since the
1755 * last find operation, this function does nothing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 *
Steve Block4e584df2012-04-24 23:12:47 +01001757 * @param forward the direction to search
Victoria Lease0b8413b2012-03-26 13:04:10 -07001758 * @see #setFindListener
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 */
1760 public void findNext(boolean forward) {
Steve Block51b08912011-04-27 15:04:48 +01001761 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001762 mProvider.findNext(forward);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 }
1764
Victoria Leaseabeb6a72012-03-05 16:29:12 -08001765 /**
Steve Block4e584df2012-04-24 23:12:47 +01001766 * Finds all instances of find on the page and highlights them.
Victoria Lease0b8413b2012-03-26 13:04:10 -07001767 * Notifies any registered {@link FindListener}.
Victoria Leaseabeb6a72012-03-05 16:29:12 -08001768 *
Steve Block4e584df2012-04-24 23:12:47 +01001769 * @param find the string to find
Nate Fischerffb81c42016-09-26 12:35:47 -07001770 * @return the number of occurrences of the String "find" that were found
Victoria Lease0b8413b2012-03-26 13:04:10 -07001771 * @deprecated {@link #findAllAsync} is preferred.
1772 * @see #setFindListener
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 */
Jonathan Dixon9f21c1c2012-04-12 11:14:20 +01001774 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 public int findAll(String find) {
Steve Block51b08912011-04-27 15:04:48 +01001776 checkThread();
Jonathan Dixon9f21c1c2012-04-12 11:14:20 +01001777 StrictMode.noteSlowCall("findAll blocks UI: prefer findAllAsync");
Jonathan Dixon3c909522012-02-28 18:45:06 +00001778 return mProvider.findAll(find);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 }
1780
Cary Clark3403eb32010-03-03 10:05:16 -05001781 /**
Steve Block4e584df2012-04-24 23:12:47 +01001782 * Finds all instances of find on the page and highlights them,
Victoria Lease0b8413b2012-03-26 13:04:10 -07001783 * asynchronously. Notifies any registered {@link FindListener}.
Kristian Monsenf4912582012-08-16 15:26:24 -04001784 * Successive calls to this will cancel any pending searches.
Victoria Leaseabeb6a72012-03-05 16:29:12 -08001785 *
Steve Block4e584df2012-04-24 23:12:47 +01001786 * @param find the string to find.
Victoria Lease0b8413b2012-03-26 13:04:10 -07001787 * @see #setFindListener
Victoria Leaseabeb6a72012-03-05 16:29:12 -08001788 */
1789 public void findAllAsync(String find) {
1790 checkThread();
1791 mProvider.findAllAsync(find);
1792 }
1793
1794 /**
Steve Block4e584df2012-04-24 23:12:47 +01001795 * Starts an ActionMode for finding text in this WebView. Only works if this
1796 * WebView is attached to the view system.
1797 *
1798 * @param text if non-null, will be the initial text to search for.
Leon Scrogginsfe026bd2010-08-24 14:16:09 -04001799 * Otherwise, the last String searched for in this WebView will
1800 * be used to start.
Nate Fischer0a6140d2017-09-05 12:37:49 -07001801 * @param showIme if {@code true}, show the IME, assuming the user will begin typing.
1802 * If {@code false} and text is non-null, perform a find all.
1803 * @return {@code true} if the find dialog is shown, {@code false} otherwise
Jonathan Dixon835b1fc2013-02-25 12:29:33 -08001804 * @deprecated This method does not work reliably on all Android versions;
1805 * implementing a custom find dialog using WebView.findAllAsync()
1806 * provides a more robust solution.
Cary Clarkde023c12010-03-03 10:05:16 -05001807 */
Jonathan Dixon835b1fc2013-02-25 12:29:33 -08001808 @Deprecated
Nate Fischer3442c742017-09-08 17:02:00 -07001809 public boolean showFindDialog(@Nullable String text, boolean showIme) {
Steve Block51b08912011-04-27 15:04:48 +01001810 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001811 return mProvider.showFindDialog(text, showIme);
Leon Scrogginsfe026bd2010-08-24 14:16:09 -04001812 }
1813
1814 /**
Steve Block4e584df2012-04-24 23:12:47 +01001815 * Gets the first substring consisting of the address of a physical
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 * location. Currently, only addresses in the United States are detected,
1817 * and consist of:
Steve Block4e584df2012-04-24 23:12:47 +01001818 * <ul>
1819 * <li>a house number</li>
1820 * <li>a street name</li>
1821 * <li>a street type (Road, Circle, etc), either spelled out or
1822 * abbreviated</li>
1823 * <li>a city name</li>
1824 * <li>a state or territory, either spelled out or two-letter abbr</li>
1825 * <li>an optional 5 digit or 9 digit zip code</li>
1826 * </ul>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 * All names must be correctly capitalized, and the zip code, if present,
1828 * must be valid for the state. The street type must be a standard USPS
1829 * spelling or abbreviation. The state or territory must also be spelled
Cary Clarkd6982c92009-05-29 11:02:22 -04001830 * or abbreviated using USPS standards. The house number may not exceed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 * five digits.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 *
Steve Block4e584df2012-04-24 23:12:47 +01001833 * @param addr the string to search for addresses
Nate Fischer0a6140d2017-09-05 12:37:49 -07001834 * @return the address, or if no address is found, {@code null}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 */
Nate Fischer3442c742017-09-08 17:02:00 -07001836 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 public static String findAddress(String addr) {
Tobias Sargeantf4d85d82018-01-05 14:34:38 +00001838 if (addr == null) {
1839 throw new NullPointerException("addr is null");
1840 }
1841 return FindAddress.findAddress(addr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 }
1843
Victoria Lease0b8413b2012-03-26 13:04:10 -07001844 /**
Selim Gurun17d1adb2014-08-27 11:22:26 -07001845 * For apps targeting the L release, WebView has a new default behavior that reduces
1846 * memory footprint and increases performance by intelligently choosing
1847 * the portion of the HTML document that needs to be drawn. These
1848 * optimizations are transparent to the developers. However, under certain
1849 * circumstances, an App developer may want to disable them:
Ben Murdoch574fa342015-01-07 14:20:45 +00001850 * <ol>
1851 * <li>When an app uses {@link #onDraw} to do own drawing and accesses portions
1852 * of the page that is way outside the visible portion of the page.</li>
1853 * <li>When an app uses {@link #capturePicture} to capture a very large HTML document.
1854 * Note that capturePicture is a deprecated API.</li>
1855 * </ol>
Selim Gurun17d1adb2014-08-27 11:22:26 -07001856 * Enabling drawing the entire HTML document has a significant performance
1857 * cost. This method should be called before any WebViews are created.
Bo Liu9f3ed852014-07-18 10:03:12 -07001858 */
1859 public static void enableSlowWholeDocumentDraw() {
1860 getFactory().getStatics().enableSlowWholeDocumentDraw();
1861 }
1862
1863 /**
Steve Block4e584df2012-04-24 23:12:47 +01001864 * Clears the highlighting surrounding text matches created by
Kristian Monsenf4912582012-08-16 15:26:24 -04001865 * {@link #findAllAsync}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 */
1867 public void clearMatches() {
Steve Block51b08912011-04-27 15:04:48 +01001868 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001869 mProvider.clearMatches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 }
1871
1872 /**
Steve Block4e584df2012-04-24 23:12:47 +01001873 * Queries the document to see if it contains any image references. The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 * message object will be dispatched with arg1 being set to 1 if images
1875 * were found and 0 if the document does not reference any images.
Steve Block4e584df2012-04-24 23:12:47 +01001876 *
1877 * @param response the message that will be dispatched with the result
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 */
1879 public void documentHasImages(Message response) {
Steve Block51b08912011-04-27 15:04:48 +01001880 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001881 mProvider.documentHasImages(response);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 }
1883
1884 /**
Steve Block4e584df2012-04-24 23:12:47 +01001885 * Sets the WebViewClient that will receive various notifications and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 * requests. This will replace the current handler.
Steve Block4e584df2012-04-24 23:12:47 +01001887 *
1888 * @param client an implementation of WebViewClient
Nate Fischer6e585842016-09-29 18:30:58 -07001889 * @see #getWebViewClient
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 */
1891 public void setWebViewClient(WebViewClient client) {
Steve Block51b08912011-04-27 15:04:48 +01001892 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001893 mProvider.setWebViewClient(client);
Grace Kloba94ab3b62009-10-07 18:00:19 -07001894 }
1895
1896 /**
Nate Fischer6e585842016-09-29 18:30:58 -07001897 * Gets the WebViewClient.
1898 *
1899 * @return the WebViewClient, or a default client if not yet set
1900 * @see #setWebViewClient
1901 */
1902 public WebViewClient getWebViewClient() {
1903 checkThread();
1904 return mProvider.getWebViewClient();
1905 }
1906
1907 /**
Steve Block4e584df2012-04-24 23:12:47 +01001908 * Registers the interface to be used when content can not be handled by
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 * the rendering engine, and should be downloaded instead. This will replace
1910 * the current handler.
Steve Block4e584df2012-04-24 23:12:47 +01001911 *
1912 * @param listener an implementation of DownloadListener
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 */
1914 public void setDownloadListener(DownloadListener listener) {
Steve Block51b08912011-04-27 15:04:48 +01001915 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001916 mProvider.setDownloadListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 }
1918
1919 /**
Steve Block4e584df2012-04-24 23:12:47 +01001920 * Sets the chrome handler. This is an implementation of WebChromeClient for
Steve Block81f19ff2010-11-01 13:23:24 +00001921 * use in handling JavaScript dialogs, favicons, titles, and the progress.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 * This will replace the current handler.
Steve Block4e584df2012-04-24 23:12:47 +01001923 *
1924 * @param client an implementation of WebChromeClient
Nate Fischer6e585842016-09-29 18:30:58 -07001925 * @see #getWebChromeClient
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 */
1927 public void setWebChromeClient(WebChromeClient client) {
Steve Block51b08912011-04-27 15:04:48 +01001928 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001929 mProvider.setWebChromeClient(client);
Patrick Scott0b2e84b2010-03-02 08:58:44 -05001930 }
1931
1932 /**
Nate Fischer6e585842016-09-29 18:30:58 -07001933 * Gets the chrome handler.
1934 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07001935 * @return the WebChromeClient, or {@code null} if not yet set
Nate Fischer6e585842016-09-29 18:30:58 -07001936 * @see #setWebChromeClient
1937 */
Nate Fischer3442c742017-09-08 17:02:00 -07001938 @Nullable
Nate Fischer6e585842016-09-29 18:30:58 -07001939 public WebChromeClient getWebChromeClient() {
1940 checkThread();
1941 return mProvider.getWebChromeClient();
1942 }
1943
1944 /**
Steve Block4e584df2012-04-24 23:12:47 +01001945 * Sets the Picture listener. This is an interface used to receive
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 * notifications of a new Picture.
Steve Block4e584df2012-04-24 23:12:47 +01001947 *
1948 * @param listener an implementation of WebView.PictureListener
Kristian Monsenfc771652011-05-10 16:44:05 +01001949 * @deprecated This method is now obsolete.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 */
Kristian Monsenfc771652011-05-10 16:44:05 +01001951 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 public void setPictureListener(PictureListener listener) {
Steve Block51b08912011-04-27 15:04:48 +01001953 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00001954 mProvider.setPictureListener(listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 }
1956
1957 /**
Steve Block3aa800b2012-04-24 13:01:34 +01001958 * Injects the supplied Java object into this WebView. The object is
1959 * injected into the JavaScript context of the main frame, using the
Selim Gurune91d5be2012-09-11 16:11:22 -07001960 * supplied name. This allows the Java object's methods to be
Selim Gurunb4c02e62012-09-12 13:36:21 -07001961 * accessed from JavaScript. For applications targeted to API
1962 * level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
Selim Gurune91d5be2012-09-11 16:11:22 -07001963 * and above, only public methods that are annotated with
1964 * {@link android.webkit.JavascriptInterface} can be accessed from JavaScript.
Selim Gurunb4c02e62012-09-12 13:36:21 -07001965 * For applications targeted to API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN} or below,
Selim Gurune91d5be2012-09-11 16:11:22 -07001966 * all public methods (including the inherited ones) can be accessed, see the
Selim Gurunb4c02e62012-09-12 13:36:21 -07001967 * important security note below for implications.
Nate Fischer02e281a2017-08-21 12:03:58 -07001968 * <p> Note that injected objects will not appear in JavaScript until the page is next
1969 * (re)loaded. JavaScript should be enabled before injecting the object. For example:
Selim Gurune91d5be2012-09-11 16:11:22 -07001970 * <pre>
1971 * class JsObject {
1972 * {@literal @}JavascriptInterface
1973 * public String toString() { return "injectedObject"; }
1974 * }
Nate Fischer02e281a2017-08-21 12:03:58 -07001975 * webview.getSettings().setJavaScriptEnabled(true);
Selim Gurune91d5be2012-09-11 16:11:22 -07001976 * webView.addJavascriptInterface(new JsObject(), "injectedObject");
Steve Block4cd73c52011-11-09 13:06:52 +00001977 * webView.loadData("<!DOCTYPE html><title></title>", "text/html", null);
1978 * webView.loadUrl("javascript:alert(injectedObject.toString())");</pre>
Steve Block3aa800b2012-04-24 13:01:34 +01001979 * <p>
1980 * <strong>IMPORTANT:</strong>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 * <ul>
Steve Block3aa800b2012-04-24 13:01:34 +01001982 * <li> This method can be used to allow JavaScript to control the host
1983 * application. This is a powerful feature, but also presents a security
Scott Rowe607c17f2014-08-07 11:42:09 -07001984 * risk for apps targeting {@link android.os.Build.VERSION_CODES#JELLY_BEAN} or earlier.
1985 * Apps that target a version later than {@link android.os.Build.VERSION_CODES#JELLY_BEAN}
1986 * are still vulnerable if the app runs on a device running Android earlier than 4.2.
1987 * The most secure way to use this method is to target {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
1988 * and to ensure the method is called only when running on Android 4.2 or later.
1989 * With these older versions, JavaScript could use reflection to access an
Steve Block3aa800b2012-04-24 13:01:34 +01001990 * injected object's public fields. Use of this method in a WebView
1991 * containing untrusted content could allow an attacker to manipulate the
1992 * host application in unintended ways, executing Java code with the
1993 * permissions of the host application. Use extreme care when using this
1994 * method in a WebView which could contain untrusted content.</li>
Steve Block4cd73c52011-11-09 13:06:52 +00001995 * <li> JavaScript interacts with Java object on a private, background
Steve Block4e584df2012-04-24 23:12:47 +01001996 * thread of this WebView. Care is therefore required to maintain thread
Scott Rowe607c17f2014-08-07 11:42:09 -07001997 * safety.
1998 * </li>
Selim Gurune91d5be2012-09-11 16:11:22 -07001999 * <li> The Java object's fields are not accessible.</li>
Dianne Hackborn955d8d62014-10-07 20:17:19 -07002000 * <li> For applications targeted to API level {@link android.os.Build.VERSION_CODES#LOLLIPOP}
Mikhail Naganovd380b202014-04-10 18:07:42 +01002001 * and above, methods of injected Java objects are enumerable from
2002 * JavaScript.</li>
Steve Block3aa800b2012-04-24 13:01:34 +01002003 * </ul>
2004 *
2005 * @param object the Java object to inject into this WebView's JavaScript
Nate Fischer0a6140d2017-09-05 12:37:49 -07002006 * context. {@code null} values are ignored.
Steve Block3aa800b2012-04-24 13:01:34 +01002007 * @param name the name used to expose the object in JavaScript
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 */
Steve Block4cd73c52011-11-09 13:06:52 +00002009 public void addJavascriptInterface(Object object, String name) {
Steve Block51b08912011-04-27 15:04:48 +01002010 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002011 mProvider.addJavascriptInterface(object, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 }
2013
2014 /**
Steve Block3aa800b2012-04-24 13:01:34 +01002015 * Removes a previously injected Java object from this WebView. Note that
2016 * the removal will not be reflected in JavaScript until the page is next
2017 * (re)loaded. See {@link #addJavascriptInterface}.
2018 *
2019 * @param name the name used to expose the object in JavaScript
Steve Block689a3422010-12-07 18:18:26 +00002020 */
Nate Fischer3442c742017-09-08 17:02:00 -07002021 public void removeJavascriptInterface(@NonNull String name) {
Steve Block51b08912011-04-27 15:04:48 +01002022 checkThread();
Steve Block3aa800b2012-04-24 13:01:34 +01002023 mProvider.removeJavascriptInterface(name);
Steve Block689a3422010-12-07 18:18:26 +00002024 }
2025
2026 /**
Selim Gurun4c8093a2015-03-10 17:40:06 -07002027 * Creates a message channel to communicate with JS and returns the message
2028 * ports that represent the endpoints of this message channel. The HTML5 message
Selim Gurun0a814b62015-05-13 10:15:55 -07002029 * channel functionality is described
2030 * <a href="https://html.spec.whatwg.org/multipage/comms.html#messagechannel">here
2031 * </a>
Selim Gurun4c8093a2015-03-10 17:40:06 -07002032 *
Nate Fischerf02f8462017-09-11 15:16:33 -07002033 * <p>The returned message channels are entangled and already in started state.
Selim Gurun4c8093a2015-03-10 17:40:06 -07002034 *
2035 * @return the two message ports that form the message channel.
2036 */
2037 public WebMessagePort[] createWebMessageChannel() {
2038 checkThread();
Selim Gurun4c8093a2015-03-10 17:40:06 -07002039 return mProvider.createWebMessageChannel();
2040 }
2041
2042 /**
2043 * Post a message to main frame. The embedded application can restrict the
2044 * messages to a certain target origin. See
Selim Gurun0a814b62015-05-13 10:15:55 -07002045 * <a href="https://html.spec.whatwg.org/multipage/comms.html#posting-messages">
2046 * HTML5 spec</a> for how target origin can be used.
Selim Gurunb18fbfa2017-05-18 11:50:26 -07002047 * <p>
2048 * A target origin can be set as a wildcard ("*"). However this is not recommended.
2049 * See the page above for security issues.
Selim Gurun4c8093a2015-03-10 17:40:06 -07002050 *
2051 * @param message the WebMessage
Selim Gurunb18fbfa2017-05-18 11:50:26 -07002052 * @param targetOrigin the target origin.
Selim Gurun4c8093a2015-03-10 17:40:06 -07002053 */
Selim Gurun26de90a2015-05-15 11:02:45 -07002054 public void postWebMessage(WebMessage message, Uri targetOrigin) {
Selim Gurun4c8093a2015-03-10 17:40:06 -07002055 checkThread();
Selim Gurun4c8093a2015-03-10 17:40:06 -07002056 mProvider.postMessageToMainFrame(message, targetOrigin);
2057 }
2058
2059 /**
Steve Block4e584df2012-04-24 23:12:47 +01002060 * Gets the WebSettings object used to control the settings for this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 * WebView.
Steve Block4e584df2012-04-24 23:12:47 +01002062 *
2063 * @return a WebSettings object that can be used to control this WebView's
2064 * settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 */
2066 public WebSettings getSettings() {
Steve Block51b08912011-04-27 15:04:48 +01002067 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002068 return mProvider.getSettings();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 }
2070
Jonathan Dixon3c909522012-02-28 18:45:06 +00002071 /**
Mikhail Naganov057989e2013-09-06 16:35:25 -07002072 * Enables debugging of web contents (HTML / CSS / JavaScript)
2073 * loaded into any WebViews of this application. This flag can be enabled
2074 * in order to facilitate debugging of web layouts and JavaScript
2075 * code running inside WebViews. Please refer to WebView documentation
2076 * for the debugging guide.
2077 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07002078 * The default is {@code false}.
Mikhail Naganov057989e2013-09-06 16:35:25 -07002079 *
2080 * @param enabled whether to enable web contents debugging
2081 */
2082 public static void setWebContentsDebuggingEnabled(boolean enabled) {
Mikhail Naganov057989e2013-09-06 16:35:25 -07002083 getFactory().getStatics().setWebContentsDebuggingEnabled(enabled);
2084 }
2085
2086 /**
Steve Block4e584df2012-04-24 23:12:47 +01002087 * Gets the list of currently loaded plugins.
Jonathan Dixon3c909522012-02-28 18:45:06 +00002088 *
Steve Block4e584df2012-04-24 23:12:47 +01002089 * @return the list of currently loaded plugins
Jonathan Dixon3c909522012-02-28 18:45:06 +00002090 * @deprecated This was used for Gears, which has been deprecated.
Steve Block4e584df2012-04-24 23:12:47 +01002091 * @hide
Jonathan Dixon3c909522012-02-28 18:45:06 +00002092 */
Andrei Popescu385df692009-08-13 11:59:57 +01002093 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 public static synchronized PluginList getPluginList() {
Grace Klobabb245ea2009-11-10 13:13:24 -08002095 return new PluginList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 }
2097
Jonathan Dixon3c909522012-02-28 18:45:06 +00002098 /**
Torne (Richard Coles)12f64da2017-10-24 14:59:07 -04002099 * Define the directory used to store WebView data for the current process.
2100 * The provided suffix will be used when constructing data and cache
2101 * directory paths. If this API is not called, no suffix will be used.
2102 * Each directory can be used by only one process in the application. If more
2103 * than one process in an app wishes to use WebView, only one process can use
2104 * the default directory, and other processes must call this API to define
2105 * a unique suffix.
2106 * <p>
2107 * This API must be called before any instances of WebView are created in
2108 * this process and before any other methods in the android.webkit package
2109 * are called by this process.
2110 *
2111 * @param suffix The directory name suffix to be used for the current
2112 * process. Must not contain a path separator.
2113 * @throws IllegalStateException if WebView has already been initialized
2114 * in the current process.
2115 * @throws IllegalArgumentException if the suffix contains a path separator.
2116 */
2117 public static void setDataDirectorySuffix(String suffix) {
2118 WebViewFactory.setDataDirectorySuffix(suffix);
2119 }
2120
2121 /**
2122 * Indicate that the current process does not intend to use WebView, and
2123 * that an exception should be thrown if a WebView is created or any other
2124 * methods in the android.webkit package are used.
2125 * <p>
2126 * Applications with multiple processes may wish to call this in processes
2127 * which are not intended to use WebView to prevent potential data directory
2128 * conflicts (see {@link #setDataDirectorySuffix}) and to avoid accidentally
2129 * incurring the memory usage of initializing WebView in long-lived
2130 * processes which have no need for it.
2131 *
2132 * @throws IllegalStateException if WebView has already been initialized
2133 * in the current process.
2134 */
2135 public static void disableWebView() {
2136 WebViewFactory.disableWebView();
2137 }
2138
2139
2140 /**
Jonathan Dixon3c909522012-02-28 18:45:06 +00002141 * @deprecated This was used for Gears, which has been deprecated.
Steve Block4e584df2012-04-24 23:12:47 +01002142 * @hide
Jonathan Dixon3c909522012-02-28 18:45:06 +00002143 */
Andrei Popescu385df692009-08-13 11:59:57 +01002144 @Deprecated
Steve Block51b08912011-04-27 15:04:48 +01002145 public void refreshPlugins(boolean reloadOpenPages) {
2146 checkThread();
2147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148
Cary Clark966641a2010-03-04 08:41:56 -05002149 /**
Steve Block4e584df2012-04-24 23:12:47 +01002150 * Puts this WebView into text selection mode. Do not rely on this
2151 * functionality; it will be deprecated in the future.
2152 *
Kristian Monsenfc771652011-05-10 16:44:05 +01002153 * @deprecated This method is now obsolete.
Kristian Monsenf4912582012-08-16 15:26:24 -04002154 * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
Cary Clark966641a2010-03-04 08:41:56 -05002155 */
Kristian Monsenfc771652011-05-10 16:44:05 +01002156 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 public void emulateShiftHeld() {
Steve Block51b08912011-04-27 15:04:48 +01002158 checkThread();
Steve Howard16bd9372010-04-12 14:46:09 -07002159 }
2160
Leon Scrogginsa57632f2009-11-16 10:51:12 -05002161 /**
2162 * @deprecated WebView no longer needs to implement
2163 * ViewGroup.OnHierarchyChangeListener. This method does nothing now.
2164 */
George Mountfab67a12012-01-05 09:58:53 -08002165 @Override
Jonathan Dixone230e542011-12-21 10:57:00 +00002166 // Cannot add @hide as this can always be accessed via the interface.
Leon Scrogginsa57632f2009-11-16 10:51:12 -05002167 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 public void onChildViewAdded(View parent, View child) {}
Cary Clarkd6982c92009-05-29 11:02:22 -04002169
Leon Scrogginsa57632f2009-11-16 10:51:12 -05002170 /**
2171 * @deprecated WebView no longer needs to implement
2172 * ViewGroup.OnHierarchyChangeListener. This method does nothing now.
2173 */
George Mountfab67a12012-01-05 09:58:53 -08002174 @Override
Jonathan Dixone230e542011-12-21 10:57:00 +00002175 // Cannot add @hide as this can always be accessed via the interface.
Leon Scrogginsa57632f2009-11-16 10:51:12 -05002176 @Deprecated
2177 public void onChildViewRemoved(View p, View child) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178
2179 /**
2180 * @deprecated WebView should not have implemented
Gilles Debunne0e7d652d2011-02-22 15:26:14 -08002181 * ViewTreeObserver.OnGlobalFocusChangeListener. This method does nothing now.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 */
George Mountfab67a12012-01-05 09:58:53 -08002183 @Override
Jonathan Dixone230e542011-12-21 10:57:00 +00002184 // Cannot add @hide as this can always be accessed via the interface.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 @Deprecated
2186 public void onGlobalFocusChanged(View oldFocus, View newFocus) {
2187 }
2188
Kristian Monsen5cc23512012-08-09 15:33:08 -04002189 /**
Nate Fischer0a6140d2017-09-05 12:37:49 -07002190 * @deprecated Only the default case, {@code true}, will be supported in a future version.
Kristian Monsen5cc23512012-08-09 15:33:08 -04002191 */
2192 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 public void setMapTrackballToArrowKeys(boolean setMap) {
Steve Block51b08912011-04-27 15:04:48 +01002194 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002195 mProvider.setMapTrackballToArrowKeys(setMap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 }
2197
Derek Sollenberger03e48912010-05-18 17:03:42 -04002198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 public void flingScroll(int vx, int vy) {
Steve Block51b08912011-04-27 15:04:48 +01002200 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002201 mProvider.flingScroll(vx, vy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 }
2203
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002204 /**
Steve Block4e584df2012-04-24 23:12:47 +01002205 * Gets the zoom controls for this WebView, as a separate View. The caller
2206 * is responsible for inserting this View into the layout hierarchy.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002207 * <p/>
Steve Blockb838aef2012-04-23 18:22:15 +01002208 * API level {@link android.os.Build.VERSION_CODES#CUPCAKE} introduced
2209 * built-in zoom mechanisms for the WebView, as opposed to these separate
2210 * zoom controls. The built-in mechanisms are preferred and can be enabled
2211 * using {@link WebSettings#setBuiltInZoomControls}.
The Android Open Source Project10592532009-03-18 17:39:46 -07002212 *
Steve Block4e584df2012-04-24 23:12:47 +01002213 * @deprecated the built-in zoom mechanisms are preferred
Steve Blockb838aef2012-04-23 18:22:15 +01002214 * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 */
The Android Open Source Project10592532009-03-18 17:39:46 -07002216 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 public View getZoomControls() {
Steve Block51b08912011-04-27 15:04:48 +01002218 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002219 return mProvider.getZoomControls();
Mangesh Ghiwarefaab93d2011-09-21 13:58:47 -07002220 }
2221
2222 /**
Steve Block4e584df2012-04-24 23:12:47 +01002223 * Gets whether this WebView can be zoomed in.
2224 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07002225 * @return {@code true} if this WebView can be zoomed in
Kristian Monsen5cc23512012-08-09 15:33:08 -04002226 *
2227 * @deprecated This method is prone to inaccuracy due to race conditions
2228 * between the web rendering and UI threads; prefer
2229 * {@link WebViewClient#onScaleChanged}.
Grace Kloba6164ef12010-06-01 15:59:13 -07002230 */
Kristian Monsen5cc23512012-08-09 15:33:08 -04002231 @Deprecated
Grace Kloba6164ef12010-06-01 15:59:13 -07002232 public boolean canZoomIn() {
Steve Block51b08912011-04-27 15:04:48 +01002233 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002234 return mProvider.canZoomIn();
Grace Kloba6164ef12010-06-01 15:59:13 -07002235 }
2236
2237 /**
Steve Block4e584df2012-04-24 23:12:47 +01002238 * Gets whether this WebView can be zoomed out.
2239 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07002240 * @return {@code true} if this WebView can be zoomed out
Kristian Monsen5cc23512012-08-09 15:33:08 -04002241 *
2242 * @deprecated This method is prone to inaccuracy due to race conditions
2243 * between the web rendering and UI threads; prefer
2244 * {@link WebViewClient#onScaleChanged}.
Grace Kloba6164ef12010-06-01 15:59:13 -07002245 */
Kristian Monsen5cc23512012-08-09 15:33:08 -04002246 @Deprecated
Grace Kloba6164ef12010-06-01 15:59:13 -07002247 public boolean canZoomOut() {
Steve Block51b08912011-04-27 15:04:48 +01002248 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002249 return mProvider.canZoomOut();
Grace Kloba6164ef12010-06-01 15:59:13 -07002250 }
2251
2252 /**
Marcin Kosiba3ee06ef2014-07-15 17:59:51 +01002253 * Performs a zoom operation in this WebView.
2254 *
Nate Fischerffb81c42016-09-26 12:35:47 -07002255 * @param zoomFactor the zoom factor to apply. The zoom factor will be clamped to the WebView's
Marcin Kosiba3ee06ef2014-07-15 17:59:51 +01002256 * zoom limits. This value must be in the range 0.01 to 100.0 inclusive.
Marcin Kosiba3ee06ef2014-07-15 17:59:51 +01002257 */
Martin Kosiba3a82acd2014-09-05 11:03:58 +01002258 public void zoomBy(float zoomFactor) {
Marcin Kosiba3ee06ef2014-07-15 17:59:51 +01002259 checkThread();
2260 if (zoomFactor < 0.01)
2261 throw new IllegalArgumentException("zoomFactor must be greater than 0.01.");
2262 if (zoomFactor > 100.0)
2263 throw new IllegalArgumentException("zoomFactor must be less than 100.");
Martin Kosiba3a82acd2014-09-05 11:03:58 +01002264 mProvider.zoomBy(zoomFactor);
Marcin Kosiba3ee06ef2014-07-15 17:59:51 +01002265 }
2266
2267 /**
Steve Block4e584df2012-04-24 23:12:47 +01002268 * Performs zoom in in this WebView.
2269 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07002270 * @return {@code true} if zoom in succeeds, {@code false} if no zoom changes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 */
2272 public boolean zoomIn() {
Steve Block51b08912011-04-27 15:04:48 +01002273 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002274 return mProvider.zoomIn();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 }
2276
2277 /**
Steve Block4e584df2012-04-24 23:12:47 +01002278 * Performs zoom out in this WebView.
2279 *
Nate Fischer0a6140d2017-09-05 12:37:49 -07002280 * @return {@code true} if zoom out succeeds, {@code false} if no zoom changes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 */
2282 public boolean zoomOut() {
Steve Block51b08912011-04-27 15:04:48 +01002283 checkThread();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002284 return mProvider.zoomOut();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 }
2286
Kristian Monsenfc771652011-05-10 16:44:05 +01002287 /**
2288 * @deprecated This method is now obsolete.
Kristian Monsenf4912582012-08-16 15:26:24 -04002289 * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
Kristian Monsenfc771652011-05-10 16:44:05 +01002290 */
2291 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 public void debugDump() {
Steve Block51b08912011-04-27 15:04:48 +01002293 checkThread();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 }
Cary Clarkd6982c92009-05-29 11:02:22 -04002295
John Reck926cf562012-06-14 10:00:31 -07002296 /**
John Reckf2361152012-06-14 15:23:22 -07002297 * See {@link ViewDebug.HierarchyHandler#dumpViewHierarchyWithProperties(BufferedWriter, int)}
John Reck926cf562012-06-14 10:00:31 -07002298 * @hide
2299 */
2300 @Override
2301 public void dumpViewHierarchyWithProperties(BufferedWriter out, int level) {
2302 mProvider.dumpViewHierarchyWithProperties(out, level);
2303 }
2304
2305 /**
2306 * See {@link ViewDebug.HierarchyHandler#findHierarchyView(String, int)}
2307 * @hide
2308 */
2309 @Override
2310 public View findHierarchyView(String className, int hashCode) {
2311 return mProvider.findHierarchyView(className, hashCode);
2312 }
2313
Tobias Sargeantd10e4af2017-01-19 14:03:09 +00002314 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -07002315 @IntDef(prefix = { "RENDERER_PRIORITY_" }, value = {
2316 RENDERER_PRIORITY_WAIVED,
2317 RENDERER_PRIORITY_BOUND,
2318 RENDERER_PRIORITY_IMPORTANT
Tobias Sargeantd10e4af2017-01-19 14:03:09 +00002319 })
2320 @Retention(RetentionPolicy.SOURCE)
2321 public @interface RendererPriority {}
2322
2323 /**
2324 * The renderer associated with this WebView is bound with
2325 * {@link Context#BIND_WAIVE_PRIORITY}. At this priority level
2326 * {@link WebView} renderers will be strong targets for out of memory
2327 * killing.
2328 *
2329 * Use with {@link #setRendererPriorityPolicy}.
2330 */
2331 public static final int RENDERER_PRIORITY_WAIVED = 0;
2332 /**
2333 * The renderer associated with this WebView is bound with
2334 * the default priority for services.
2335 *
2336 * Use with {@link #setRendererPriorityPolicy}.
2337 */
2338 public static final int RENDERER_PRIORITY_BOUND = 1;
2339 /**
2340 * The renderer associated with this WebView is bound with
2341 * {@link Context#BIND_IMPORTANT}.
2342 *
2343 * Use with {@link #setRendererPriorityPolicy}.
2344 */
2345 public static final int RENDERER_PRIORITY_IMPORTANT = 2;
2346
2347 /**
2348 * Set the renderer priority policy for this {@link WebView}. The
2349 * priority policy will be used to determine whether an out of
2350 * process renderer should be considered to be a target for OOM
2351 * killing.
2352 *
2353 * Because a renderer can be associated with more than one
2354 * WebView, the final priority it is computed as the maximum of
2355 * any attached WebViews. When a WebView is destroyed it will
2356 * cease to be considerered when calculating the renderer
2357 * priority. Once no WebViews remain associated with the renderer,
2358 * the priority of the renderer will be reduced to
2359 * {@link #RENDERER_PRIORITY_WAIVED}.
2360 *
2361 * The default policy is to set the priority to
2362 * {@link #RENDERER_PRIORITY_IMPORTANT} regardless of visibility,
2363 * and this should not be changed unless the caller also handles
2364 * renderer crashes with
2365 * {@link WebViewClient#onRenderProcessGone}. Any other setting
2366 * will result in WebView renderers being killed by the system
2367 * more aggressively than the application.
2368 *
2369 * @param rendererRequestedPriority the minimum priority at which
2370 * this WebView desires the renderer process to be bound.
Nate Fischer0a6140d2017-09-05 12:37:49 -07002371 * @param waivedWhenNotVisible if {@code true}, this flag specifies that
Tobias Sargeantd10e4af2017-01-19 14:03:09 +00002372 * when this WebView is not visible, it will be treated as
2373 * if it had requested a priority of
2374 * {@link #RENDERER_PRIORITY_WAIVED}.
2375 */
2376 public void setRendererPriorityPolicy(
2377 @RendererPriority int rendererRequestedPriority,
2378 boolean waivedWhenNotVisible) {
2379 mProvider.setRendererPriorityPolicy(rendererRequestedPriority, waivedWhenNotVisible);
2380 }
2381
2382 /**
2383 * Get the requested renderer priority for this WebView.
2384 *
2385 * @return the requested renderer priority policy.
2386 */
2387 @RendererPriority
2388 public int getRendererRequestedPriority() {
2389 return mProvider.getRendererRequestedPriority();
2390 }
2391
2392 /**
2393 * Return whether this WebView requests a priority of
2394 * {@link #RENDERER_PRIORITY_WAIVED} when not visible.
2395 *
2396 * @return whether this WebView requests a priority of
2397 * {@link #RENDERER_PRIORITY_WAIVED} when not visible.
2398 */
2399 public boolean getRendererPriorityWaivedWhenNotVisible() {
2400 return mProvider.getRendererPriorityWaivedWhenNotVisible();
2401 }
Tima Vaisburdcf49a232017-03-28 11:35:58 -07002402
2403 /**
2404 * Sets the {@link TextClassifier} for this WebView.
2405 */
2406 public void setTextClassifier(@Nullable TextClassifier textClassifier) {
2407 mProvider.setTextClassifier(textClassifier);
2408 }
2409
2410 /**
2411 * Returns the {@link TextClassifier} used by this WebView.
2412 * If no TextClassifier has been set, this WebView uses the default set by the system.
2413 */
2414 @NonNull
2415 public TextClassifier getTextClassifier() {
2416 return mProvider.getTextClassifier();
2417 }
2418
Gustav Sennton265afdb2018-01-19 11:56:55 +00002419 /**
2420 * Returns the {@link ClassLoader} used to load internal WebView classes.
2421 * This method is meant for use by the WebView Support Library, there is no reason to use this
2422 * method otherwise.
2423 */
2424 @NonNull
2425 public static ClassLoader getWebViewClassLoader() {
2426 return getFactory().getWebViewClassLoader();
2427 }
2428
Gustav Sennton01673692018-01-19 12:11:25 +00002429 /**
2430 * Returns the {@link Looper} corresponding to the thread on which WebView calls must be made.
2431 */
2432 @NonNull
2433 public Looper getLooper() {
2434 return mWebViewThread;
2435 }
2436
Jonathan Dixon3c909522012-02-28 18:45:06 +00002437 //-------------------------------------------------------------------------
2438 // Interface for WebView providers
2439 //-------------------------------------------------------------------------
2440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 /**
Steve Block4e584df2012-04-24 23:12:47 +01002442 * Gets the WebViewProvider. Used by providers to obtain the underlying
Nate Fischerffb81c42016-09-26 12:35:47 -07002443 * implementation, e.g. when the application responds to
Steve Block4e584df2012-04-24 23:12:47 +01002444 * WebViewClient.onCreateWindow() request.
Mike Reedefe2c722009-10-14 09:42:02 -04002445 *
Jonathan Dixon3c909522012-02-28 18:45:06 +00002446 * @hide WebViewProvider is not public API.
Mike Reedefe2c722009-10-14 09:42:02 -04002447 */
Ignacio Solla451e3382014-11-10 10:35:54 +00002448 @SystemApi
Jonathan Dixon3c909522012-02-28 18:45:06 +00002449 public WebViewProvider getWebViewProvider() {
2450 return mProvider;
Mike Reedefe2c722009-10-14 09:42:02 -04002451 }
2452
2453 /**
Jonathan Dixon3c909522012-02-28 18:45:06 +00002454 * Callback interface, allows the provider implementation to access non-public methods
2455 * and fields, and make super-class calls in this WebView instance.
2456 * @hide Only for use by WebViewProvider implementations
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -08002457 */
Ignacio Solla451e3382014-11-10 10:35:54 +00002458 @SystemApi
Jonathan Dixon3c909522012-02-28 18:45:06 +00002459 public class PrivateAccess {
2460 // ---- Access to super-class methods ----
2461 public int super_getScrollBarStyle() {
2462 return WebView.super.getScrollBarStyle();
2463 }
2464
2465 public void super_scrollTo(int scrollX, int scrollY) {
2466 WebView.super.scrollTo(scrollX, scrollY);
2467 }
2468
2469 public void super_computeScroll() {
2470 WebView.super.computeScroll();
2471 }
2472
alanveebebc92012-06-15 18:59:11 -07002473 public boolean super_onHoverEvent(MotionEvent event) {
2474 return WebView.super.onHoverEvent(event);
2475 }
2476
Marcin Kosiba3e853fb2015-01-08 12:45:40 +00002477 public boolean super_performAccessibilityAction(int action, Bundle arguments) {
Alan Viverettea54956a2015-01-07 16:05:02 -08002478 return WebView.super.performAccessibilityActionInternal(action, arguments);
alanv448902d2012-05-16 20:27:47 -07002479 }
2480
Jonathan Dixon3c909522012-02-28 18:45:06 +00002481 public boolean super_performLongClick() {
2482 return WebView.super.performLongClick();
2483 }
2484
2485 public boolean super_setFrame(int left, int top, int right, int bottom) {
2486 return WebView.super.setFrame(left, top, right, bottom);
2487 }
2488
2489 public boolean super_dispatchKeyEvent(KeyEvent event) {
2490 return WebView.super.dispatchKeyEvent(event);
2491 }
2492
2493 public boolean super_onGenericMotionEvent(MotionEvent event) {
2494 return WebView.super.onGenericMotionEvent(event);
2495 }
2496
2497 public boolean super_requestFocus(int direction, Rect previouslyFocusedRect) {
2498 return WebView.super.requestFocus(direction, previouslyFocusedRect);
2499 }
2500
2501 public void super_setLayoutParams(ViewGroup.LayoutParams params) {
2502 WebView.super.setLayoutParams(params);
2503 }
2504
Hui Shuf119c522015-10-08 10:07:13 -07002505 public void super_startActivityForResult(Intent intent, int requestCode) {
2506 WebView.super.startActivityForResult(intent, requestCode);
2507 }
2508
Jonathan Dixon3c909522012-02-28 18:45:06 +00002509 // ---- Access to non-public methods ----
2510 public void overScrollBy(int deltaX, int deltaY,
2511 int scrollX, int scrollY,
2512 int scrollRangeX, int scrollRangeY,
2513 int maxOverScrollX, int maxOverScrollY,
2514 boolean isTouchEvent) {
2515 WebView.this.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY,
2516 maxOverScrollX, maxOverScrollY, isTouchEvent);
2517 }
2518
2519 public void awakenScrollBars(int duration) {
2520 WebView.this.awakenScrollBars(duration);
2521 }
2522
2523 public void awakenScrollBars(int duration, boolean invalidate) {
2524 WebView.this.awakenScrollBars(duration, invalidate);
2525 }
2526
2527 public float getVerticalScrollFactor() {
2528 return WebView.this.getVerticalScrollFactor();
2529 }
2530
2531 public float getHorizontalScrollFactor() {
2532 return WebView.this.getHorizontalScrollFactor();
2533 }
2534
2535 public void setMeasuredDimension(int measuredWidth, int measuredHeight) {
2536 WebView.this.setMeasuredDimension(measuredWidth, measuredHeight);
2537 }
2538
2539 public void onScrollChanged(int l, int t, int oldl, int oldt) {
2540 WebView.this.onScrollChanged(l, t, oldl, oldt);
2541 }
2542
2543 public int getHorizontalScrollbarHeight() {
2544 return WebView.this.getHorizontalScrollbarHeight();
2545 }
2546
Martin Kosiba9a68f822013-08-08 14:02:41 +01002547 public void super_onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
2548 int l, int t, int r, int b) {
2549 WebView.super.onDrawVerticalScrollBar(canvas, scrollBar, l, t, r, b);
2550 }
2551
Jonathan Dixon3c909522012-02-28 18:45:06 +00002552 // ---- Access to (non-public) fields ----
2553 /** Raw setter for the scroll X value, without invoking onScrollChanged handlers etc. */
2554 public void setScrollXRaw(int scrollX) {
2555 WebView.this.mScrollX = scrollX;
2556 }
2557
2558 /** Raw setter for the scroll Y value, without invoking onScrollChanged handlers etc. */
2559 public void setScrollYRaw(int scrollY) {
2560 WebView.this.mScrollY = scrollY;
2561 }
2562
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -08002563 }
2564
Jonathan Dixon3c909522012-02-28 18:45:06 +00002565 //-------------------------------------------------------------------------
Ben Murdoch52c9f7f2013-01-18 00:50:37 +00002566 // Package-private internal stuff
2567 //-------------------------------------------------------------------------
2568
2569 // Only used by android.webkit.FindActionModeCallback.
2570 void setFindDialogFindListener(FindListener listener) {
2571 checkThread();
2572 setupFindListenerIfNeeded();
2573 mFindListener.mFindDialogFindListener = listener;
2574 }
2575
2576 // Only used by android.webkit.FindActionModeCallback.
2577 void notifyFindDialogDismissed() {
2578 checkThread();
2579 mProvider.notifyFindDialogDismissed();
2580 }
2581
2582 //-------------------------------------------------------------------------
Jonathan Dixon3c909522012-02-28 18:45:06 +00002583 // Private internal stuff
2584 //-------------------------------------------------------------------------
2585
Jonathan Dixon3c909522012-02-28 18:45:06 +00002586 private WebViewProvider mProvider;
2587
Ben Murdoch52c9f7f2013-01-18 00:50:37 +00002588 /**
2589 * In addition to the FindListener that the user may set via the WebView.setFindListener
2590 * API, FindActionModeCallback will register it's own FindListener. We keep them separate
Mark Dolinerd0646dc2014-08-27 16:04:02 -07002591 * via this class so that the two FindListeners can potentially exist at once.
Ben Murdoch52c9f7f2013-01-18 00:50:37 +00002592 */
2593 private class FindListenerDistributor implements FindListener {
2594 private FindListener mFindDialogFindListener;
2595 private FindListener mUserFindListener;
2596
2597 @Override
2598 public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
2599 boolean isDoneCounting) {
2600 if (mFindDialogFindListener != null) {
2601 mFindDialogFindListener.onFindResultReceived(activeMatchOrdinal, numberOfMatches,
2602 isDoneCounting);
2603 }
2604
2605 if (mUserFindListener != null) {
2606 mUserFindListener.onFindResultReceived(activeMatchOrdinal, numberOfMatches,
2607 isDoneCounting);
2608 }
2609 }
2610 }
2611 private FindListenerDistributor mFindListener;
2612
2613 private void setupFindListenerIfNeeded() {
2614 if (mFindListener == null) {
2615 mFindListener = new FindListenerDistributor();
2616 mProvider.setFindListener(mFindListener);
2617 }
2618 }
2619
Jonathan Dixon3c909522012-02-28 18:45:06 +00002620 private void ensureProviderCreated() {
2621 checkThread();
2622 if (mProvider == null) {
Jonathan Dixon3c909522012-02-28 18:45:06 +00002623 // As this can get called during the base class constructor chain, pass the minimum
2624 // number of dependencies here; the rest are deferred to init().
2625 mProvider = getFactory().createWebView(this, new PrivateAccess());
2626 }
Ben Murdochecbc65c2010-01-13 10:54:56 +00002627 }
2628
Torne (Richard Coles)59375a02016-07-18 18:34:02 +01002629 private static WebViewFactoryProvider getFactory() {
Jonathan Dixond3101b12012-04-12 20:51:51 +01002630 return WebViewFactory.getProvider();
Ben Murdoch1708ad52010-11-11 15:56:16 +00002631 }
2632
Jonathan Dixon517771b2013-10-08 13:32:11 -07002633 private final Looper mWebViewThread = Looper.myLooper();
2634
2635 private void checkThread() {
2636 // Ignore mWebViewThread == null because this can be called during in the super class
2637 // constructor, before this class's own constructor has even started.
2638 if (mWebViewThread != null && Looper.myLooper() != mWebViewThread) {
Steve Block7a01d942011-09-26 12:30:31 +01002639 Throwable throwable = new Throwable(
Jonathan Dixon517771b2013-10-08 13:32:11 -07002640 "A WebView method was called on thread '" +
Steve Block08d584c2011-05-17 19:05:03 +01002641 Thread.currentThread().getName() + "'. " +
Jonathan Dixon517771b2013-10-08 13:32:11 -07002642 "All WebView methods must be called on the same thread. " +
2643 "(Expected Looper " + mWebViewThread + " called on " + Looper.myLooper() +
2644 ", FYI main Looper is " + Looper.getMainLooper() + ")");
Steve Block7a01d942011-09-26 12:30:31 +01002645 Log.w(LOGTAG, Log.getStackTraceString(throwable));
2646 StrictMode.onWebViewMethodCalledOnWrongThread(throwable);
Kristian Monsenb5cd8c02013-04-09 17:57:37 -07002647
2648 if (sEnforceThreadChecking) {
2649 throw new RuntimeException(throwable);
2650 }
Steve Block51b08912011-04-27 15:04:48 +01002651 }
2652 }
2653
Jonathan Dixon3c909522012-02-28 18:45:06 +00002654 //-------------------------------------------------------------------------
2655 // Override View methods
2656 //-------------------------------------------------------------------------
2657
2658 // TODO: Add a test that enumerates all methods in ViewDelegte & ScrollDelegate, and ensures
2659 // there's a corresponding override (or better, caller) for each of them in here.
2660
2661 @Override
2662 protected void onAttachedToWindow() {
2663 super.onAttachedToWindow();
2664 mProvider.getViewDelegate().onAttachedToWindow();
Chris Craik555c55e2011-07-28 15:39:43 -07002665 }
2666
John Reck34dbce12014-03-06 12:52:24 -08002667 /** @hide */
Jonathan Dixon3c909522012-02-28 18:45:06 +00002668 @Override
John Reck34dbce12014-03-06 12:52:24 -08002669 protected void onDetachedFromWindowInternal() {
Jonathan Dixon3c909522012-02-28 18:45:06 +00002670 mProvider.getViewDelegate().onDetachedFromWindow();
John Reck34dbce12014-03-06 12:52:24 -08002671 super.onDetachedFromWindowInternal();
Chris Craik555c55e2011-07-28 15:39:43 -07002672 }
2673
Tima Vaisburd5544f6c2017-04-27 10:53:27 -07002674 /** @hide */
Jonathan Dixon3c909522012-02-28 18:45:06 +00002675 @Override
Tima Vaisburd134752b2017-04-06 15:50:05 -07002676 public void onMovedToDisplay(int displayId, Configuration config) {
2677 mProvider.getViewDelegate().onMovedToDisplay(displayId, config);
2678 }
2679
2680 @Override
Jonathan Dixon3c909522012-02-28 18:45:06 +00002681 public void setLayoutParams(ViewGroup.LayoutParams params) {
2682 mProvider.getViewDelegate().setLayoutParams(params);
Chris Craik445ab742011-07-13 13:19:37 -07002683 }
2684
Jonathan Dixon3c909522012-02-28 18:45:06 +00002685 @Override
2686 public void setOverScrollMode(int mode) {
2687 super.setOverScrollMode(mode);
Martin Kosiba5ee743e2012-12-21 12:39:50 +00002688 // This method may be called in the constructor chain, before the WebView provider is
2689 // created.
Jonathan Dixon3c909522012-02-28 18:45:06 +00002690 ensureProviderCreated();
2691 mProvider.getViewDelegate().setOverScrollMode(mode);
Chris Craik445ab742011-07-13 13:19:37 -07002692 }
2693
Jonathan Dixon3c909522012-02-28 18:45:06 +00002694 @Override
2695 public void setScrollBarStyle(int style) {
2696 mProvider.getViewDelegate().setScrollBarStyle(style);
2697 super.setScrollBarStyle(style);
George Mount3d095312012-01-10 11:24:02 -08002698 }
2699
Jonathan Dixon3c909522012-02-28 18:45:06 +00002700 @Override
2701 protected int computeHorizontalScrollRange() {
2702 return mProvider.getScrollDelegate().computeHorizontalScrollRange();
John Reck5528d7c2012-02-28 11:29:29 -08002703 }
2704
Jonathan Dixon3c909522012-02-28 18:45:06 +00002705 @Override
2706 protected int computeHorizontalScrollOffset() {
2707 return mProvider.getScrollDelegate().computeHorizontalScrollOffset();
2708 }
Grace Kloba8abd50b2010-07-08 15:02:14 -07002709
Jonathan Dixon3c909522012-02-28 18:45:06 +00002710 @Override
2711 protected int computeVerticalScrollRange() {
2712 return mProvider.getScrollDelegate().computeVerticalScrollRange();
2713 }
Patrick Scotta3ebcc92010-07-16 11:52:22 -04002714
Jonathan Dixon3c909522012-02-28 18:45:06 +00002715 @Override
2716 protected int computeVerticalScrollOffset() {
2717 return mProvider.getScrollDelegate().computeVerticalScrollOffset();
2718 }
Ben Murdochfa148f62011-02-01 20:51:27 +00002719
Jonathan Dixon3c909522012-02-28 18:45:06 +00002720 @Override
2721 protected int computeVerticalScrollExtent() {
2722 return mProvider.getScrollDelegate().computeVerticalScrollExtent();
2723 }
2724
2725 @Override
2726 public void computeScroll() {
2727 mProvider.getScrollDelegate().computeScroll();
2728 }
2729
2730 @Override
2731 public boolean onHoverEvent(MotionEvent event) {
2732 return mProvider.getViewDelegate().onHoverEvent(event);
2733 }
2734
2735 @Override
2736 public boolean onTouchEvent(MotionEvent event) {
2737 return mProvider.getViewDelegate().onTouchEvent(event);
2738 }
2739
2740 @Override
2741 public boolean onGenericMotionEvent(MotionEvent event) {
2742 return mProvider.getViewDelegate().onGenericMotionEvent(event);
2743 }
2744
2745 @Override
2746 public boolean onTrackballEvent(MotionEvent event) {
2747 return mProvider.getViewDelegate().onTrackballEvent(event);
2748 }
2749
2750 @Override
2751 public boolean onKeyDown(int keyCode, KeyEvent event) {
2752 return mProvider.getViewDelegate().onKeyDown(keyCode, event);
2753 }
2754
2755 @Override
2756 public boolean onKeyUp(int keyCode, KeyEvent event) {
2757 return mProvider.getViewDelegate().onKeyUp(keyCode, event);
2758 }
2759
2760 @Override
2761 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
2762 return mProvider.getViewDelegate().onKeyMultiple(keyCode, repeatCount, event);
2763 }
2764
2765 /*
2766 TODO: These are not currently implemented in WebViewClassic, but it seems inconsistent not
2767 to be delegating them too.
2768
2769 @Override
2770 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
2771 return mProvider.getViewDelegate().onKeyPreIme(keyCode, event);
2772 }
2773 @Override
2774 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
2775 return mProvider.getViewDelegate().onKeyLongPress(keyCode, event);
2776 }
2777 @Override
2778 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
2779 return mProvider.getViewDelegate().onKeyShortcut(keyCode, event);
2780 }
2781 */
2782
Ben Murdoche3f90712013-06-05 14:19:48 +01002783 @Override
2784 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
2785 AccessibilityNodeProvider provider =
2786 mProvider.getViewDelegate().getAccessibilityNodeProvider();
2787 return provider == null ? super.getAccessibilityNodeProvider() : provider;
2788 }
2789
Jonathan Dixon3c909522012-02-28 18:45:06 +00002790 @Deprecated
2791 @Override
2792 public boolean shouldDelayChildPressedState() {
2793 return mProvider.getViewDelegate().shouldDelayChildPressedState();
2794 }
2795
Felipe Leme1ca634a2016-11-28 17:21:21 -08002796 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -08002797 public CharSequence getAccessibilityClassName() {
2798 return WebView.class.getName();
2799 }
2800
Selim Gurun1b650cb2015-04-06 20:13:37 -07002801 @Override
Dianne Hackborn49b043f2015-05-07 14:21:38 -07002802 public void onProvideVirtualStructure(ViewStructure structure) {
Selim Gurun50b26a52015-06-01 16:26:10 -07002803 mProvider.getViewDelegate().onProvideVirtualStructure(structure);
Selim Gurun1b650cb2015-04-06 20:13:37 -07002804 }
2805
Felipe Leme1a1e4682017-03-13 16:34:03 -07002806 /**
2807 * {@inheritDoc}
2808 *
2809 * <p>The {@link ViewStructure} traditionally represents a {@link View}, while for web pages
2810 * it represent HTML nodes. Hence, it's necessary to "map" the HTML properties in a way that is
2811 * understood by the {@link android.service.autofill.AutofillService} implementations:
2812 *
2813 * <ol>
Felipe Leme303b6092017-07-31 18:01:15 -07002814 * <li>Only the HTML nodes inside a {@code FORM} are generated.
2815 * <li>The source of the HTML is set using {@link ViewStructure#setWebDomain(String)} in the
2816 * node representing the WebView.
2817 * <li>If a web page has multiple {@code FORM}s, only the data for the current form is
2818 * represented&mdash;if the user taps a field from another form, then the current autofill
2819 * context is canceled (by calling {@link android.view.autofill.AutofillManager#cancel()} and
2820 * a new context is created for that {@code FORM}.
2821 * <li>Similarly, if the page has {@code IFRAME} nodes, they are not initially represented in
2822 * the view structure until the user taps a field from a {@code FORM} inside the
2823 * {@code IFRAME}, in which case it would be treated the same way as multiple forms described
2824 * above, except that the {@link ViewStructure#setWebDomain(String) web domain} of the
2825 * {@code FORM} contains the {@code src} attribute from the {@code IFRAME} node.
Felipe Leme1a1e4682017-03-13 16:34:03 -07002826 * <li>The W3C autofill field ({@code autocomplete} tag attribute) maps to
Felipe Leme58390fe2017-09-15 15:16:28 -07002827 * {@link ViewStructure#setAutofillHints(String[])}.
2828 * <li>If the view is editable, the {@link ViewStructure#setAutofillType(int)} and
Felipe Leme6dcec872017-05-25 11:24:23 -07002829 * {@link ViewStructure#setAutofillValue(AutofillValue)} must be set.
Felipe Leme1a1e4682017-03-13 16:34:03 -07002830 * <li>The {@code placeholder} attribute maps to {@link ViewStructure#setHint(CharSequence)}.
Felipe Leme25bf7872017-03-28 15:32:29 -07002831 * <li>Other HTML attributes can be represented through
2832 * {@link ViewStructure#setHtmlInfo(android.view.ViewStructure.HtmlInfo)}.
Felipe Leme1a1e4682017-03-13 16:34:03 -07002833 * </ol>
2834 *
Felipe Leme58390fe2017-09-15 15:16:28 -07002835 * <p>If the WebView implementation can determine that the value of a field was set statically
2836 * (for example, not through Javascript), it should also call
2837 * {@code structure.setDataIsSensitive(false)}.
Felipe Leme6dcec872017-05-25 11:24:23 -07002838 *
Felipe Leme58390fe2017-09-15 15:16:28 -07002839 * <p>For example, an HTML form with 2 fields for username and password:
Felipe Leme1a1e4682017-03-13 16:34:03 -07002840 *
2841 * <pre class="prettyprint">
Felipe Lemef485f892017-11-22 08:15:59 -08002842 * &lt;label&gt;Username:&lt;/label&gt;
Felipe Leme6dcec872017-05-25 11:24:23 -07002843 * &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 -08002844 * &lt;label&gt;Password:&lt;/label&gt;
Felipe Leme6dcec872017-05-25 11:24:23 -07002845 * &lt;input type="password" name="password" id="pass" autocomplete="current-password" placeholder="Password"&gt;
Felipe Leme1a1e4682017-03-13 16:34:03 -07002846 * </pre>
2847 *
2848 * <p>Would map to:
2849 *
2850 * <pre class="prettyprint">
Felipe Leme25bf7872017-03-28 15:32:29 -07002851 * int index = structure.addChildCount(2);
2852 * ViewStructure username = structure.newChild(index);
Felipe Leme6dcec872017-05-25 11:24:23 -07002853 * username.setAutofillId(structure.getAutofillId(), 1); // id 1 - first child
Felipe Leme1a1e4682017-03-13 16:34:03 -07002854 * username.setAutofillHints("username");
Felipe Leme6dcec872017-05-25 11:24:23 -07002855 * username.setHtmlInfo(username.newHtmlInfoBuilder("input")
2856 * .addAttribute("type", "text")
Felipe Leme25bf7872017-03-28 15:32:29 -07002857 * .addAttribute("name", "username")
Felipe Lemef485f892017-11-22 08:15:59 -08002858 * .addAttribute("label", "Username:")
Felipe Leme25bf7872017-03-28 15:32:29 -07002859 * .build());
Felipe Leme1a1e4682017-03-13 16:34:03 -07002860 * username.setHint("Email or username");
2861 * username.setAutofillType(View.AUTOFILL_TYPE_TEXT);
Felipe Leme6dcec872017-05-25 11:24:23 -07002862 * username.setAutofillValue(AutofillValue.forText("Type your username"));
Felipe Leme58390fe2017-09-15 15:16:28 -07002863 * // Value of the field is not sensitive because it was created statically and not changed.
Felipe Leme6dcec872017-05-25 11:24:23 -07002864 * username.setDataIsSensitive(false);
Felipe Leme1a1e4682017-03-13 16:34:03 -07002865 *
Felipe Leme25bf7872017-03-28 15:32:29 -07002866 * ViewStructure password = structure.newChild(index + 1);
2867 * username.setAutofillId(structure, 2); // id 2 - second child
Felipe Leme1a1e4682017-03-13 16:34:03 -07002868 * password.setAutofillHints("current-password");
Felipe Leme6dcec872017-05-25 11:24:23 -07002869 * password.setHtmlInfo(password.newHtmlInfoBuilder("input")
2870 * .addAttribute("type", "password")
Felipe Leme25bf7872017-03-28 15:32:29 -07002871 * .addAttribute("name", "password")
Felipe Lemef485f892017-11-22 08:15:59 -08002872 * .addAttribute("label", "Password:")
Felipe Leme25bf7872017-03-28 15:32:29 -07002873 * .build());
Felipe Leme1a1e4682017-03-13 16:34:03 -07002874 * password.setHint("Password");
2875 * password.setAutofillType(View.AUTOFILL_TYPE_TEXT);
Felipe Leme1a1e4682017-03-13 16:34:03 -07002876 * </pre>
Felipe Leme1a1e4682017-03-13 16:34:03 -07002877 */
Felipe Leme1ca634a2016-11-28 17:21:21 -08002878 @Override
Felipe Leme640f30a2017-03-06 15:44:06 -08002879 public void onProvideAutofillVirtualStructure(ViewStructure structure, int flags) {
2880 mProvider.getViewDelegate().onProvideAutofillVirtualStructure(structure, flags);
Felipe Leme1ca634a2016-11-28 17:21:21 -08002881 }
2882
Tao Baia5717332017-03-30 14:58:53 -07002883 @Override
2884 public void autofill(SparseArray<AutofillValue>values) {
2885 mProvider.getViewDelegate().autofill(values);
2886 }
2887
Alan Viverettea54956a2015-01-07 16:05:02 -08002888 /** @hide */
Jonathan Dixon3c909522012-02-28 18:45:06 +00002889 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -08002890 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
2891 super.onInitializeAccessibilityNodeInfoInternal(info);
Marcin Kosiba3e853fb2015-01-08 12:45:40 +00002892 mProvider.getViewDelegate().onInitializeAccessibilityNodeInfo(info);
Jonathan Dixon3c909522012-02-28 18:45:06 +00002893 }
2894
Alan Viverettea54956a2015-01-07 16:05:02 -08002895 /** @hide */
Jonathan Dixon3c909522012-02-28 18:45:06 +00002896 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -08002897 public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
2898 super.onInitializeAccessibilityEventInternal(event);
Marcin Kosiba3e853fb2015-01-08 12:45:40 +00002899 mProvider.getViewDelegate().onInitializeAccessibilityEvent(event);
Jonathan Dixon3c909522012-02-28 18:45:06 +00002900 }
2901
Alan Viverettea54956a2015-01-07 16:05:02 -08002902 /** @hide */
alanv448902d2012-05-16 20:27:47 -07002903 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -08002904 public boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Marcin Kosiba3e853fb2015-01-08 12:45:40 +00002905 return mProvider.getViewDelegate().performAccessibilityAction(action, arguments);
alanv448902d2012-05-16 20:27:47 -07002906 }
2907
Jonathan Dixon3c909522012-02-28 18:45:06 +00002908 /** @hide */
2909 @Override
2910 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
2911 int l, int t, int r, int b) {
2912 mProvider.getViewDelegate().onDrawVerticalScrollBar(canvas, scrollBar, l, t, r, b);
2913 }
2914
2915 @Override
2916 protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
2917 mProvider.getViewDelegate().onOverScrolled(scrollX, scrollY, clampedX, clampedY);
2918 }
2919
2920 @Override
2921 protected void onWindowVisibilityChanged(int visibility) {
2922 super.onWindowVisibilityChanged(visibility);
2923 mProvider.getViewDelegate().onWindowVisibilityChanged(visibility);
2924 }
2925
2926 @Override
Jonathan Dixon3c909522012-02-28 18:45:06 +00002927 protected void onDraw(Canvas canvas) {
2928 mProvider.getViewDelegate().onDraw(canvas);
2929 }
2930
2931 @Override
2932 public boolean performLongClick() {
2933 return mProvider.getViewDelegate().performLongClick();
2934 }
2935
2936 @Override
2937 protected void onConfigurationChanged(Configuration newConfig) {
2938 mProvider.getViewDelegate().onConfigurationChanged(newConfig);
2939 }
2940
Selim Gurunc92080b2016-10-20 11:53:14 -07002941 /**
2942 * Creates a new InputConnection for an InputMethod to interact with the WebView.
2943 * This is similar to {@link View#onCreateInputConnection} but note that WebView
2944 * calls InputConnection methods on a thread other than the UI thread.
2945 * If these methods are overridden, then the overriding methods should respect
2946 * thread restrictions when calling View methods or accessing data.
2947 */
Jonathan Dixon3c909522012-02-28 18:45:06 +00002948 @Override
2949 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
2950 return mProvider.getViewDelegate().onCreateInputConnection(outAttrs);
2951 }
2952
2953 @Override
Hui Shuecdce962016-02-04 15:00:19 -08002954 public boolean onDragEvent(DragEvent event) {
2955 return mProvider.getViewDelegate().onDragEvent(event);
2956 }
2957
2958 @Override
Jonathan Dixon3c909522012-02-28 18:45:06 +00002959 protected void onVisibilityChanged(View changedView, int visibility) {
2960 super.onVisibilityChanged(changedView, visibility);
Martin Kosiba5ee743e2012-12-21 12:39:50 +00002961 // This method may be called in the constructor chain, before the WebView provider is
2962 // created.
2963 ensureProviderCreated();
Jonathan Dixon3c909522012-02-28 18:45:06 +00002964 mProvider.getViewDelegate().onVisibilityChanged(changedView, visibility);
2965 }
2966
2967 @Override
2968 public void onWindowFocusChanged(boolean hasWindowFocus) {
2969 mProvider.getViewDelegate().onWindowFocusChanged(hasWindowFocus);
2970 super.onWindowFocusChanged(hasWindowFocus);
2971 }
2972
2973 @Override
2974 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
2975 mProvider.getViewDelegate().onFocusChanged(focused, direction, previouslyFocusedRect);
2976 super.onFocusChanged(focused, direction, previouslyFocusedRect);
2977 }
2978
2979 /** @hide */
2980 @Override
2981 protected boolean setFrame(int left, int top, int right, int bottom) {
2982 return mProvider.getViewDelegate().setFrame(left, top, right, bottom);
2983 }
2984
2985 @Override
2986 protected void onSizeChanged(int w, int h, int ow, int oh) {
2987 super.onSizeChanged(w, h, ow, oh);
2988 mProvider.getViewDelegate().onSizeChanged(w, h, ow, oh);
2989 }
2990
2991 @Override
2992 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
2993 super.onScrollChanged(l, t, oldl, oldt);
2994 mProvider.getViewDelegate().onScrollChanged(l, t, oldl, oldt);
2995 }
2996
2997 @Override
2998 public boolean dispatchKeyEvent(KeyEvent event) {
2999 return mProvider.getViewDelegate().dispatchKeyEvent(event);
3000 }
3001
3002 @Override
3003 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
3004 return mProvider.getViewDelegate().requestFocus(direction, previouslyFocusedRect);
3005 }
3006
Jonathan Dixon3c909522012-02-28 18:45:06 +00003007 @Override
3008 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
3009 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
3010 mProvider.getViewDelegate().onMeasure(widthMeasureSpec, heightMeasureSpec);
3011 }
3012
3013 @Override
3014 public boolean requestChildRectangleOnScreen(View child, Rect rect, boolean immediate) {
3015 return mProvider.getViewDelegate().requestChildRectangleOnScreen(child, rect, immediate);
3016 }
3017
3018 @Override
3019 public void setBackgroundColor(int color) {
3020 mProvider.getViewDelegate().setBackgroundColor(color);
3021 }
John Recka5408e62012-03-16 14:18:44 -07003022
3023 @Override
3024 public void setLayerType(int layerType, Paint paint) {
3025 super.setLayerType(layerType, paint);
3026 mProvider.getViewDelegate().setLayerType(layerType, paint);
3027 }
Ben Murdoche623e242012-09-07 20:47:07 +01003028
3029 @Override
3030 protected void dispatchDraw(Canvas canvas) {
3031 mProvider.getViewDelegate().preDispatchDraw(canvas);
3032 super.dispatchDraw(canvas);
3033 }
Ben Murdoch7302f4f2014-07-03 14:47:44 +01003034
3035 @Override
3036 public void onStartTemporaryDetach() {
3037 super.onStartTemporaryDetach();
3038 mProvider.getViewDelegate().onStartTemporaryDetach();
3039 }
3040
3041 @Override
3042 public void onFinishTemporaryDetach() {
3043 super.onFinishTemporaryDetach();
3044 mProvider.getViewDelegate().onFinishTemporaryDetach();
3045 }
Siva Velusamy94a6d152015-05-05 15:07:00 -07003046
Selim Gurune319dad2016-03-17 01:40:40 +00003047 @Override
3048 public Handler getHandler() {
3049 return mProvider.getViewDelegate().getHandler(super.getHandler());
3050 }
3051
3052 @Override
3053 public View findFocus() {
3054 return mProvider.getViewDelegate().findFocus(super.findFocus());
3055 }
3056
Hui Shuf119c522015-10-08 10:07:13 -07003057 /**
Gustav Senntonbf683e02016-09-15 14:42:50 +01003058 * If WebView has already been loaded into the current process this method will return the
3059 * package that was used to load it. Otherwise, the package that would be used if the WebView
3060 * was loaded right now will be returned; this does not cause WebView to be loaded, so this
3061 * information may become outdated at any time.
Gustav Sennton4cbfe712017-03-07 14:22:01 +00003062 * The WebView package changes either when the current WebView package is updated, disabled, or
3063 * uninstalled. It can also be changed through a Developer Setting.
3064 * If the WebView package changes, any app process that has loaded WebView will be killed. The
3065 * next time the app starts and loads WebView it will use the new WebView package instead.
Nate Fischer0a6140d2017-09-05 12:37:49 -07003066 * @return the current WebView package, or {@code null} if there is none.
Gustav Senntonbf683e02016-09-15 14:42:50 +01003067 */
Nate Fischer3442c742017-09-08 17:02:00 -07003068 @Nullable
Gustav Senntonbf683e02016-09-15 14:42:50 +01003069 public static PackageInfo getCurrentWebViewPackage() {
3070 PackageInfo webviewPackage = WebViewFactory.getLoadedPackageInfo();
3071 if (webviewPackage != null) {
3072 return webviewPackage;
3073 }
3074
Torne (Richard Coles)ff937ac2017-10-02 17:09:32 -04003075 IWebViewUpdateService service = WebViewFactory.getUpdateService();
3076 if (service == null) {
3077 return null;
3078 }
Gustav Senntonbf683e02016-09-15 14:42:50 +01003079 try {
Torne (Richard Coles)ff937ac2017-10-02 17:09:32 -04003080 return service.getCurrentWebViewPackage();
Gustav Senntonbf683e02016-09-15 14:42:50 +01003081 } catch (RemoteException e) {
3082 throw e.rethrowFromSystemServer();
3083 }
3084 }
3085
3086 /**
Hui Shuf119c522015-10-08 10:07:13 -07003087 * Receive the result from a previous call to {@link #startActivityForResult(Intent, int)}.
3088 *
3089 * @param requestCode The integer request code originally supplied to
3090 * startActivityForResult(), allowing you to identify who this
3091 * result came from.
3092 * @param resultCode The integer result code returned by the child activity
3093 * through its setResult().
3094 * @param data An Intent, which can return result data to the caller
3095 * (various data can be attached to Intent "extras").
3096 * @hide
3097 */
3098 @Override
3099 public void onActivityResult(int requestCode, int resultCode, Intent data) {
3100 mProvider.getViewDelegate().onActivityResult(requestCode, resultCode, data);
3101 }
3102
Siva Velusamy94a6d152015-05-05 15:07:00 -07003103 /** @hide */
3104 @Override
3105 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) {
3106 super.encodeProperties(encoder);
3107
3108 checkThread();
3109 encoder.addProperty("webview:contentHeight", mProvider.getContentHeight());
3110 encoder.addProperty("webview:contentWidth", mProvider.getContentWidth());
3111 encoder.addProperty("webview:scale", mProvider.getScale());
3112 encoder.addProperty("webview:title", mProvider.getTitle());
3113 encoder.addProperty("webview:url", mProvider.getUrl());
3114 encoder.addProperty("webview:originalUrl", mProvider.getOriginalUrl());
3115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116}