1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.webkit; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SystemApi; 22 import android.content.Intent; 23 import android.content.res.Configuration; 24 import android.graphics.Bitmap; 25 import android.graphics.Canvas; 26 import android.graphics.Paint; 27 import android.graphics.Picture; 28 import android.graphics.Rect; 29 import android.graphics.drawable.Drawable; 30 import android.net.Uri; 31 import android.net.http.SslCertificate; 32 import android.os.Bundle; 33 import android.os.Handler; 34 import android.os.Message; 35 import android.print.PrintDocumentAdapter; 36 import android.util.SparseArray; 37 import android.view.DragEvent; 38 import android.view.KeyEvent; 39 import android.view.MotionEvent; 40 import android.view.View; 41 import android.view.ViewGroup.LayoutParams; 42 import android.view.accessibility.AccessibilityEvent; 43 import android.view.accessibility.AccessibilityNodeInfo; 44 import android.view.accessibility.AccessibilityNodeProvider; 45 import android.view.autofill.AutofillValue; 46 import android.view.inputmethod.EditorInfo; 47 import android.view.inputmethod.InputConnection; 48 import android.view.textclassifier.TextClassifier; 49 import android.webkit.WebView.HitTestResult; 50 import android.webkit.WebView.PictureListener; 51 import android.webkit.WebView.VisualStateCallback; 52 53 54 import java.io.BufferedWriter; 55 import java.io.File; 56 import java.util.Map; 57 import java.util.concurrent.Executor; 58 59 /** 60 * WebView backend provider interface: this interface is the abstract backend to a WebView 61 * instance; each WebView object is bound to exactly one WebViewProvider object which implements 62 * the runtime behavior of that WebView. 63 * 64 * All methods must behave as per their namesake in {@link WebView}, unless otherwise noted. 65 * 66 * @hide Not part of the public API; only required by system implementors. 67 */ 68 @SystemApi 69 public interface WebViewProvider { 70 //------------------------------------------------------------------------- 71 // Main interface for backend provider of the WebView class. 72 //------------------------------------------------------------------------- 73 /** 74 * Initialize this WebViewProvider instance. Called after the WebView has fully constructed. 75 * @param javaScriptInterfaces is a Map of interface names, as keys, and 76 * object implementing those interfaces, as values. 77 * @param privateBrowsing If {@code true} the web view will be initialized in private / 78 * incognito mode. 79 */ 80 public void init(Map<String, Object> javaScriptInterfaces, 81 boolean privateBrowsing); 82 83 // Deprecated - should never be called 84 public void setHorizontalScrollbarOverlay(boolean overlay); 85 86 // Deprecated - should never be called 87 public void setVerticalScrollbarOverlay(boolean overlay); 88 89 // Deprecated - should never be called 90 public boolean overlayHorizontalScrollbar(); 91 92 // Deprecated - should never be called 93 public boolean overlayVerticalScrollbar(); 94 95 public int getVisibleTitleHeight(); 96 97 public SslCertificate getCertificate(); 98 99 public void setCertificate(SslCertificate certificate); 100 101 public void savePassword(String host, String username, String password); 102 103 public void setHttpAuthUsernamePassword(String host, String realm, 104 String username, String password); 105 106 public String[] getHttpAuthUsernamePassword(String host, String realm); 107 108 /** 109 * See {@link WebView#destroy()}. 110 * As well as releasing the internal state and resources held by the implementation, 111 * the provider should null all references it holds on the WebView proxy class, and ensure 112 * no further method calls are made to it. 113 */ 114 public void destroy(); 115 116 public void setNetworkAvailable(boolean networkUp); 117 118 public WebBackForwardList saveState(Bundle outState); 119 120 public boolean savePicture(Bundle b, final File dest); 121 122 public boolean restorePicture(Bundle b, File src); 123 124 public WebBackForwardList restoreState(Bundle inState); 125 126 public void loadUrl(String url, Map<String, String> additionalHttpHeaders); 127 128 public void loadUrl(String url); 129 130 public void postUrl(String url, byte[] postData); 131 132 public void loadData(String data, String mimeType, String encoding); 133 134 public void loadDataWithBaseURL(String baseUrl, String data, 135 String mimeType, String encoding, String historyUrl); 136 137 public void evaluateJavaScript(String script, ValueCallback<String> resultCallback); 138 139 public void saveWebArchive(String filename); 140 141 public void saveWebArchive(String basename, boolean autoname, ValueCallback<String> callback); 142 143 public void stopLoading(); 144 145 public void reload(); 146 147 public boolean canGoBack(); 148 149 public void goBack(); 150 151 public boolean canGoForward(); 152 153 public void goForward(); 154 155 public boolean canGoBackOrForward(int steps); 156 157 public void goBackOrForward(int steps); 158 159 public boolean isPrivateBrowsingEnabled(); 160 161 public boolean pageUp(boolean top); 162 163 public boolean pageDown(boolean bottom); 164 165 public void insertVisualStateCallback(long requestId, VisualStateCallback callback); 166 167 public void clearView(); 168 169 public Picture capturePicture(); 170 171 public PrintDocumentAdapter createPrintDocumentAdapter(String documentName); 172 173 public float getScale(); 174 175 public void setInitialScale(int scaleInPercent); 176 177 public void invokeZoomPicker(); 178 179 public HitTestResult getHitTestResult(); 180 181 public void requestFocusNodeHref(Message hrefMsg); 182 183 public void requestImageRef(Message msg); 184 185 public String getUrl(); 186 187 public String getOriginalUrl(); 188 189 public String getTitle(); 190 191 public Bitmap getFavicon(); 192 193 public String getTouchIconUrl(); 194 195 public int getProgress(); 196 197 public int getContentHeight(); 198 199 public int getContentWidth(); 200 201 public void pauseTimers(); 202 203 public void resumeTimers(); 204 205 public void onPause(); 206 207 public void onResume(); 208 209 public boolean isPaused(); 210 211 public void freeMemory(); 212 213 public void clearCache(boolean includeDiskFiles); 214 215 public void clearFormData(); 216 217 public void clearHistory(); 218 219 public void clearSslPreferences(); 220 221 public WebBackForwardList copyBackForwardList(); 222 223 public void setFindListener(WebView.FindListener listener); 224 225 public void findNext(boolean forward); 226 227 public int findAll(String find); 228 229 public void findAllAsync(String find); 230 231 public boolean showFindDialog(String text, boolean showIme); 232 233 public void clearMatches(); 234 235 public void documentHasImages(Message response); 236 237 public void setWebViewClient(WebViewClient client); 238 239 public WebViewClient getWebViewClient(); 240 241 @Nullable 242 public WebViewRenderProcess getWebViewRenderProcess(); 243 244 public void setWebViewRenderProcessClient( 245 @Nullable Executor executor, 246 @Nullable WebViewRenderProcessClient client); 247 248 @Nullable 249 public WebViewRenderProcessClient getWebViewRenderProcessClient(); 250 251 public void setDownloadListener(DownloadListener listener); 252 253 public void setWebChromeClient(WebChromeClient client); 254 255 public WebChromeClient getWebChromeClient(); 256 257 public void setPictureListener(PictureListener listener); 258 259 public void addJavascriptInterface(Object obj, String interfaceName); 260 261 public void removeJavascriptInterface(String interfaceName); 262 263 public WebMessagePort[] createWebMessageChannel(); 264 265 public void postMessageToMainFrame(WebMessage message, Uri targetOrigin); 266 267 public WebSettings getSettings(); 268 269 public void setMapTrackballToArrowKeys(boolean setMap); 270 271 public void flingScroll(int vx, int vy); 272 273 public View getZoomControls(); 274 275 public boolean canZoomIn(); 276 277 public boolean canZoomOut(); 278 279 public boolean zoomBy(float zoomFactor); 280 281 public boolean zoomIn(); 282 283 public boolean zoomOut(); 284 285 public void dumpViewHierarchyWithProperties(BufferedWriter out, int level); 286 287 public View findHierarchyView(String className, int hashCode); 288 289 public void setRendererPriorityPolicy(int rendererRequestedPriority, boolean waivedWhenNotVisible); 290 291 public int getRendererRequestedPriority(); 292 293 public boolean getRendererPriorityWaivedWhenNotVisible(); 294 295 @SuppressWarnings("unused") 296 public default void setTextClassifier(@Nullable TextClassifier textClassifier) {} 297 298 @NonNull 299 public default TextClassifier getTextClassifier() { return TextClassifier.NO_OP; } 300 301 //------------------------------------------------------------------------- 302 // Provider internal methods 303 //------------------------------------------------------------------------- 304 305 /** 306 * @return the ViewDelegate implementation. This provides the functionality to back all of 307 * the name-sake functions from the View and ViewGroup base classes of WebView. 308 */ 309 /* package */ ViewDelegate getViewDelegate(); 310 311 /** 312 * @return a ScrollDelegate implementation. Normally this would be same object as is 313 * returned by getViewDelegate(). 314 */ 315 /* package */ ScrollDelegate getScrollDelegate(); 316 317 /** 318 * Only used by FindActionModeCallback to inform providers that the find dialog has 319 * been dismissed. 320 */ 321 public void notifyFindDialogDismissed(); 322 323 //------------------------------------------------------------------------- 324 // View / ViewGroup delegation methods 325 //------------------------------------------------------------------------- 326 327 /** 328 * Provides mechanism for the name-sake methods declared in View and ViewGroup to be delegated 329 * into the WebViewProvider instance. 330 * NOTE: For many of these methods, the WebView will provide a super.Foo() call before or after 331 * making the call into the provider instance. This is done for convenience in the common case 332 * of maintaining backward compatibility. For remaining super class calls (e.g. where the 333 * provider may need to only conditionally make the call based on some internal state) see the 334 * {@link WebView.PrivateAccess} callback class. 335 */ 336 // TODO: See if the pattern of the super-class calls can be rationalized at all, and document 337 // the remainder on the methods below. 338 interface ViewDelegate { 339 public boolean shouldDelayChildPressedState(); 340 341 public void onProvideVirtualStructure(android.view.ViewStructure structure); 342 343 default void onProvideAutofillVirtualStructure( 344 @SuppressWarnings("unused") android.view.ViewStructure structure, 345 @SuppressWarnings("unused") int flags) { 346 } 347 348 default void autofill(@SuppressWarnings("unused") SparseArray<AutofillValue> values) { 349 } 350 351 default boolean isVisibleToUserForAutofill(@SuppressWarnings("unused") int virtualId) { 352 return true; // true is the default value returned by View.isVisibleToUserForAutofill() 353 } 354 355 default void onProvideContentCaptureStructure( 356 @NonNull @SuppressWarnings("unused") android.view.ViewStructure structure, 357 @SuppressWarnings("unused") int flags) { 358 } 359 360 public AccessibilityNodeProvider getAccessibilityNodeProvider(); 361 362 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info); 363 364 public void onInitializeAccessibilityEvent(AccessibilityEvent event); 365 366 public boolean performAccessibilityAction(int action, Bundle arguments); 367 368 public void setOverScrollMode(int mode); 369 370 public void setScrollBarStyle(int style); 371 372 public void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar, int l, int t, 373 int r, int b); 374 375 public void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY); 376 377 public void onWindowVisibilityChanged(int visibility); 378 379 public void onDraw(Canvas canvas); 380 381 public void setLayoutParams(LayoutParams layoutParams); 382 383 public boolean performLongClick(); 384 385 public void onConfigurationChanged(Configuration newConfig); 386 387 public InputConnection onCreateInputConnection(EditorInfo outAttrs); 388 389 public boolean onDragEvent(DragEvent event); 390 391 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event); 392 393 public boolean onKeyDown(int keyCode, KeyEvent event); 394 395 public boolean onKeyUp(int keyCode, KeyEvent event); 396 397 public void onAttachedToWindow(); 398 399 public void onDetachedFromWindow(); 400 401 public default void onMovedToDisplay(int displayId, Configuration config) {} 402 403 public void onVisibilityChanged(View changedView, int visibility); 404 405 public void onWindowFocusChanged(boolean hasWindowFocus); 406 407 public void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect); 408 409 public boolean setFrame(int left, int top, int right, int bottom); 410 411 public void onSizeChanged(int w, int h, int ow, int oh); 412 413 public void onScrollChanged(int l, int t, int oldl, int oldt); 414 415 public boolean dispatchKeyEvent(KeyEvent event); 416 417 public boolean onTouchEvent(MotionEvent ev); 418 419 public boolean onHoverEvent(MotionEvent event); 420 421 public boolean onGenericMotionEvent(MotionEvent event); 422 423 public boolean onTrackballEvent(MotionEvent ev); 424 425 public boolean requestFocus(int direction, Rect previouslyFocusedRect); 426 427 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec); 428 429 public boolean requestChildRectangleOnScreen(View child, Rect rect, boolean immediate); 430 431 public void setBackgroundColor(int color); 432 433 public void setLayerType(int layerType, Paint paint); 434 435 public void preDispatchDraw(Canvas canvas); 436 437 public void onStartTemporaryDetach(); 438 439 public void onFinishTemporaryDetach(); 440 441 public void onActivityResult(int requestCode, int resultCode, Intent data); 442 443 public Handler getHandler(Handler originalHandler); 444 445 public View findFocus(View originalFocusedView); 446 447 @SuppressWarnings("unused") 448 default boolean onCheckIsTextEditor() { 449 return false; 450 } 451 } 452 453 interface ScrollDelegate { 454 // These methods are declared protected in the ViewGroup base class. This interface 455 // exists to promote them to public so they may be called by the WebView proxy class. 456 // TODO: Combine into ViewDelegate? 457 /** 458 * See {@link android.webkit.WebView#computeHorizontalScrollRange} 459 */ 460 public int computeHorizontalScrollRange(); 461 462 /** 463 * See {@link android.webkit.WebView#computeHorizontalScrollOffset} 464 */ 465 public int computeHorizontalScrollOffset(); 466 467 /** 468 * See {@link android.webkit.WebView#computeVerticalScrollRange} 469 */ 470 public int computeVerticalScrollRange(); 471 472 /** 473 * See {@link android.webkit.WebView#computeVerticalScrollOffset} 474 */ 475 public int computeVerticalScrollOffset(); 476 477 /** 478 * See {@link android.webkit.WebView#computeVerticalScrollExtent} 479 */ 480 public int computeVerticalScrollExtent(); 481 482 /** 483 * See {@link android.webkit.WebView#computeScroll} 484 */ 485 public void computeScroll(); 486 } 487 } 488