Code example for WebView

Methods: capturePicturegetSettingssetWebChromeClientsetWebViewClient, getHeight, getVerticalScrollbarWidth, getWidth

0
      task.setVideoFrameRecords(mVideoFrameGrabber.getFrameRecords());
      mVideoFrameGrabber = null;
    } 
 
    task.setTimings(mWebViewClient.getTimings());
    mWebView.setWebViewClient(null);
    mWebView.setWebChromeClient(null);
    mWebView.getSettings().setLoadWithOverviewMode(false);
    mWebView.getSettings().setUseWideViewPort(false);
    if (mCondition != null) {
      mCondition.open();
    } 
  } 
 
  public Bitmap captureScreenshot() {
    Log.d(TAG, "captureScreenshot()");
    // It's not clear if this is UI-thread-safe or not. 
    return PhoneUtils.captureScreenshot(mWebView);
  } 
 
  private class JavascriptTimingHelper { 
    volatile long onLoadTimeMs = 0;
    volatile long onContentLoadTimeMs = 0;
 
    /** Called by javascript. */ 
    @SuppressWarnings("unused") 
    public void onLoad() { 
      onLoadTimeMs = System.currentTimeMillis();
    } 
 
    /** Called by javascript. */ 
    public void onDOMContentLoaded() { 
      onContentLoadTimeMs = System.currentTimeMillis();
    } 
  } 
 
  private class WebViewClientForTiming extends WebViewClient {
    final JavascriptTimingHelper jsTimings;
 
    volatile long startTimeMs = 0;
    volatile long endTimeMs = 0;
 
    WebViewClientForTiming(JavascriptTimingHelper jsTimings) {
      this.jsTimings = jsTimings;
    } 
 
    public void onStart() { 
      startTimeMs = System.currentTimeMillis();
    } 
 
    @Override 
    public void onPageFinished(WebView view, String url) {
      if (endTimeMs == 0) {
        endTimeMs = System.currentTimeMillis();
      } 
      if (mCondition != null) {
        mCondition.open();
      } 
    } 
 
    public Map<String, String> getTimings() {
      long onLoadTimeMs = -1;
      long onContentLoadMs = -1;
 
      if (startTimeMs > 0) {
        if (jsTimings.onLoadTimeMs > startTimeMs) {
          onLoadTimeMs = jsTimings.onLoadTimeMs - startTimeMs;
        } else if (endTimeMs > startTimeMs) {
          onLoadTimeMs = endTimeMs - startTimeMs;
        } 
 
        if (jsTimings.onContentLoadTimeMs > startTimeMs) {
          onContentLoadMs = jsTimings.onContentLoadTimeMs - startTimeMs;
        } 
      } 
 
      HashMap<String, String> timings = new HashMap<String, String>();
      timings.put("navigationStartTime", Long.toString(startTimeMs));
      timings.put("onLoad", Long.toString(onLoadTimeMs));
      timings.put("onContentLoad", Long.toString(onContentLoadMs));
      return timings;
    } 
  } 
 
  private class WebChromeClientForTimings extends WebChromeClient {
    final JavascriptTimingHelper jsTimings;
    boolean registeredOnloadHandler = false;
 
    WebChromeClientForTimings(JavascriptTimingHelper jsTimings) {
      this.jsTimings = jsTimings;
    } 
 
    @Override 
    public void onReceivedTitle (WebView view, String title) {
      registerOnLoadHandler(view);
    } 
 
    private void registerOnLoadHandler(WebView view) {
      if (registeredOnloadHandler) {
        return; 
      } 
 
      // Sets a default DOMContentLoad time. This is needed because, if a page is small and fast, 
      // registerOnLoadHandler() is often called after the DOMContentLoaded event is fired. In that 
      // case, we call jsTimings.onDOMContentLoaded() here to set DOMContentLoaded time to now, 
      // which is not really accurate, but better than not catching a DOMContentLoaded time. 
      jsTimings.onDOMContentLoaded();
 
      registeredOnloadHandler = true;
      view.loadUrl("javascript:" +
        "window.addEventListener('DOMContentLoaded', function() {" + 
            TIMING_JS_INTERFACE_NAME + ".onDOMContentLoaded();});" +
        "window.addEventListener('DOMContent', function() {" + 
            TIMING_JS_INTERFACE_NAME + ".onDOMContentLoaded();});" +
        "window.addEventListener('load', function() {" + 
            TIMING_JS_INTERFACE_NAME + ".onLoad();});");
    } 
 
    @Override 
    public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,
                              JsPromptResult result) {
      // When a web page calls prompt, dismiss the blocking dialog with the default value. 
      result.confirm(defaultValue);
      return true; 
    } 
 
    @Override 
    public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
      // When a web page calls confirm, dismiss the blocking dialog box. 
      result.confirm();
      return true; 
    } 
 
    @Override 
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
      // When a web page calls alert, dismiss the blocking dialog box. 
      result.confirm();
      return true; 
    } 
 
    @Override 
    public void onProgressChanged (WebView view, int newProgress) {
      // "30%" is chosen arbitrarily. The intention is to make sure registerOnLoadHandler() is 
      // called early (but not too early, not before DOM exists) in a page load. 
      if (newProgress > 30) {
        registerOnLoadHandler(view);
      } 
    } 
  } 
} 
Contextual code suggestions in your IDE  Get Codota for Java