Java Code Examples for android.webkit.WebView#HitTestResult

The following examples show how to use android.webkit.WebView#HitTestResult . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebViewEx.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onLongClick(View view) {
    WebView.HitTestResult result = ((WebView) view).getHitTestResult();
    if (result.getType() == WebView.HitTestResult.IMAGE_TYPE ||
            result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
        Log.i("Long press url=" + result.getExtra());

        Uri uri = Uri.parse(result.getExtra());
        if ("cid".equals(uri.getScheme()) || "data".equals(uri.getScheme()))
            return false;

        Helper.view(getContext(), uri, true);

        return true;
    }
    return false;
}
 
Example 2
Source File: WebBrowserActivity.java    From Dashchan with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
	WebView.HitTestResult hitTestResult = webView.getHitTestResult();
	switch (hitTestResult.getType()) {
		case WebView.HitTestResult.IMAGE_TYPE:
		case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE: {
			final Uri uri = Uri.parse(hitTestResult.getExtra());
			ChanLocator locator = ChanLocator.getDefault();
			if (locator.isWebScheme(uri) && locator.isImageExtension(uri.getPath())) {
				menu.add(R.string.action_view).setOnMenuItemClickListener(item -> {
					NavigationUtils.openImageVideo(WebBrowserActivity.this, uri, false);
					return true;
				});
			}
			break;
		}
	}
}
 
Example 3
Source File: FirstActivity3.java    From YCWebView with Apache License 2.0 6 votes vote down vote up
@Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //判断重定向的方式一
            WebView.HitTestResult hitTestResult = view.getHitTestResult();
            if(hitTestResult == null) {
                return false;
            }
            if(hitTestResult.getType() == WebView.HitTestResult.UNKNOWN_TYPE) {
                //X5LogUtils.i("-------重定向-------");
                return false;
            }

            if (webView.isTouchByUser()){
                X5LogUtils.i("-------点击事件-------");
            }
            view.loadUrl(url);
            X5LogUtils.i("-----shouldOverrideUrlLoading------loadUrl-------");
//            running++;
            return true;
            //return super.shouldOverrideUrlLoading(view, url);
        }
 
Example 4
Source File: FirstActivity1.java    From YCWebView with Apache License 2.0 6 votes vote down vote up
@Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //判断重定向的方式一
            X5LogUtils.i("-----shouldOverrideUrlLoading---------"+url);
            WebView.HitTestResult hitTestResult = view.getHitTestResult();
            if(hitTestResult == null) {
                return false;
            }
            if(hitTestResult.getType() == WebView.HitTestResult.UNKNOWN_TYPE) {
                //X5LogUtils.i("-------重定向-------");
                return false;
            }
            view.loadUrl(url);
            //X5LogUtils.i("-----shouldOverrideUrlLoading-----2----"+url);
//            running++;
            return true;
            //return super.shouldOverrideUrlLoading(view, url);
        }
 
Example 5
Source File: FirstActivity2.java    From YCWebView with Apache License 2.0 6 votes vote down vote up
@Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //判断重定向的方式一
            WebView.HitTestResult hitTestResult = view.getHitTestResult();
            if(hitTestResult == null) {
                return false;
            }
            if(hitTestResult.getType() == WebView.HitTestResult.UNKNOWN_TYPE) {
                //X5LogUtils.i("-------重定向-------");
                return false;
            }

            if (webView.isTouchByUser()){
                X5LogUtils.i("-------点击事件-------");
            }
            view.loadUrl(url);
            X5LogUtils.i("-----shouldOverrideUrlLoading------loadUrl-------");
//            running++;
            return true;
            //return super.shouldOverrideUrlLoading(view, url);
        }
 
Example 6
Source File: AdvWebView.java    From 4pdaClient-plus with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(android.view.MotionEvent event) {
    boolean b = super.onTouchEvent(event);
    try {

        if (mOnScrollChangedCallback == null) return b;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                m_MotionDown = new Point((int) event.getX(), (int) event.getY());
                m_InTouch = true;
                final WebView.HitTestResult hitTestResult = getHitTestResult();
                if (hitTestResult == null) {
                    startClickTime = Calendar.getInstance().getTimeInMillis();
                } else
                    switch (hitTestResult.getType()) {
                        case WebView.HitTestResult.UNKNOWN_TYPE:
                        case WebView.HitTestResult.EDIT_TEXT_TYPE:
                            startClickTime = Calendar.getInstance().getTimeInMillis();
                    }

                break;
            }
            case MotionEvent.ACTION_UP: {
                m_InTouch = false;
                long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
                if (clickDuration < MAX_CLICK_DURATION &&m_MotionDown!=null&& Math.abs(m_MotionDown.y - event.getY()) < MAX_TOUCH__Y_DISTANCE) {
                    mOnScrollChangedCallback.onTouch();
                }
            }
        }
    } catch (Throwable ex) {
        AppLog.e(getContext(), ex);
    }finally {
        m_LastMotionEvent = new Point((int) event.getX(), (int) event.getY());
    }

    return b;

}
 
Example 7
Source File: WebViewActivity.java    From CloudReader with Apache License 2.0 5 votes vote down vote up
/**
     * 长按图片事件处理
     */
    private boolean handleLongImage() {
        final WebView.HitTestResult hitTestResult = webView.getHitTestResult();
        // 如果是图片类型或者是带有图片链接的类型
        if (hitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
                hitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
            // 弹出保存图片的对话框
            new AlertDialog.Builder(WebViewActivity.this)
                    .setItems(new String[]{"发送给朋友", "保存到相册"}, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String picUrl = hitTestResult.getExtra();
                            //获取图片
//                            Log.e("picUrl", picUrl);
                            switch (which) {
                                case 0:
                                    ShareUtils.shareNetImage(WebViewActivity.this, picUrl);
                                    break;
                                case 1:
                                    if (!PermissionHandler.isHandlePermission(WebViewActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                                        return;
                                    }
                                    RxSaveImage.saveImageToGallery(WebViewActivity.this, picUrl, picUrl);
                                    break;
                                default:
                                    break;
                            }
                        }
                    })
                    .show();
            return true;
        }
        return false;
    }
 
Example 8
Source File: ClickableWebView.java    From ClickableWebView with Apache License 2.0 5 votes vote down vote up
@Override
public void onClick(View view) {
    WebView.HitTestResult hr = getHitTestResult();
    try {
        if (listener != null && hr.getType() == IMAGE_TYPE) {
            listener.onClick(hr.getExtra());
        }
    } catch (Exception e) {
        e.getStackTrace();
    }
}
 
Example 9
Source File: WebViewActivity.java    From ByWebView with Apache License 2.0 5 votes vote down vote up
/**
 * 长按图片事件处理
 */
private boolean handleLongImage() {
    final WebView.HitTestResult hitTestResult = webView.getHitTestResult();
    // 如果是图片类型或者是带有图片链接的类型
    if (hitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
            hitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
        // 弹出保存图片的对话框
        new AlertDialog.Builder(WebViewActivity.this)
                .setItems(new String[]{"查看大图", "保存图片到相册"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String picUrl = hitTestResult.getExtra();
                        //获取图片
                        Log.e("picUrl", picUrl);
                        switch (which) {
                            case 0:
                                break;
                            case 1:
                                break;
                            default:
                                break;
                        }
                    }
                })
                .show();
        return true;
    }
    return false;
}
 
Example 10
Source File: MainActivity.java    From FaceSlim with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
    WebView.HitTestResult result = webView.getHitTestResult();
    if (result != null) {
        int type = result.getType();

        if (type == WebView.HitTestResult.IMAGE_TYPE || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
            showLongPressedImageMenu(menu, result.getExtra());
        }
    }
}
 
Example 11
Source File: BaseWebViewLoadPresenter.java    From YiZhi with Apache License 2.0 5 votes vote down vote up
@Override
public void imageLongClicked(WebView.HitTestResult hitTestResult) {
    if (null == hitTestResult)
        return;

    int type = hitTestResult.getType();
    switch (type) {
        case WebView.HitTestResult.UNKNOWN_TYPE:
            return;
        case WebView.HitTestResult.PHONE_TYPE: // 处理拨号
            break;
        case WebView.HitTestResult.EMAIL_TYPE: // 处理Email
            break;
        case WebView.HitTestResult.GEO_TYPE:
            break;
        case WebView.HitTestResult.SRC_ANCHOR_TYPE: // 超链接
            break;
        case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
            break;
        case WebView.HitTestResult.IMAGE_TYPE: // 处理长按图片的菜单项
            if (mIView != null)
                mIView.showPopupWindow();
            break;
        default:
            break;
    }
}
 
Example 12
Source File: PageDetailActivity.java    From MaoWanAndoidClient with Apache License 2.0 5 votes vote down vote up
/**
 * 长按图片事件处理
 */
private boolean handleLongImage() {
    final WebView.HitTestResult hitTestResult = webView.getHitTestResult();
    // 如果是图片类型或者是带有图片链接的类型
    if (hitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
            hitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
        // 弹出保存图片的对话框
        new AlertDialog.Builder(PageDetailActivity.this)
                .setItems(new String[]{"查看大图", "保存图片到相册"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String picUrl = hitTestResult.getExtra();
                        //获取图片
                        Log.e("picUrl", picUrl);
                        switch (which) {
                            case 0:
                                break;
                            case 1:
                                break;
                            default:
                                break;
                        }
                    }
                })
                .show();
        return true;
    }
    return false;
}
 
Example 13
Source File: BrowserWindow.java    From Beedio with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean onLongClick(View v) {
    final WebView.HitTestResult hit = page.getHitTestResult();
    if (hit.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
        if (hit.getExtra() != null) {
            View point = new View(getActivity());
            point.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams
                    .WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            if (view != null) {
                ((ViewGroup) view).addView(point);
            }
            point.getLayoutParams().height = 10;
            point.getLayoutParams().width = 10;
            point.setX(page.getClickX());
            point.setY(page.getClickY());
            PopupMenu menu = new PopupMenu(getActivity(), point);
            menu.getMenu().add("Open in new window");
            menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    getLMvdActivity().getBrowserManager().newWindow(hit.getExtra());
                    return true;
                }
            });
            menu.show();
        }
    }
    return true;
}
 
Example 14
Source File: FragmentWebView.java    From Pixiv-Shaft with MIT License 5 votes vote down vote up
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    WebView.HitTestResult result = mWebView.getHitTestResult();
    mIntentUrl = result.getExtra();
    menu.setHeaderView(new ContextMenuTitleView(mContext, mIntentUrl));

    if (result.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
        mIntentUrl = result.getExtra();
        //menu.setHeaderTitle(mIntentUrl);
        menu.add(Menu.NONE, WebViewClickHandler.OPEN_IN_BROWSER, 0, R.string.webview_handler_open_in_browser).setOnMenuItemClickListener(handler);
        menu.add(Menu.NONE, WebViewClickHandler.COPY_LINK_ADDRESS, 1, R.string.webview_handler_copy_link_addr).setOnMenuItemClickListener(handler);
        menu.add(Menu.NONE, WebViewClickHandler.COPY_LINK_TEXT, 1, R.string.webview_handler_copy_link_text).setOnMenuItemClickListener(handler);
        menu.add(Menu.NONE, WebViewClickHandler.DOWNLOAD_LINK, 1, R.string.webview_handler_download_link).setOnMenuItemClickListener(handler);
        menu.add(Menu.NONE, WebViewClickHandler.SHARE_LINK, 1, R.string.webview_handler_share).setOnMenuItemClickListener(handler);
    }

    if (result.getType() == WebView.HitTestResult.IMAGE_TYPE ||
            result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {

        mIntentUrl = result.getExtra();
        //menu.setHeaderTitle(mIntentUrl);
        menu.add(Menu.NONE, WebViewClickHandler.OPEN_IN_BROWSER, 0, R.string.webview_handler_open_in_browser).setOnMenuItemClickListener(handler);
        menu.add(Menu.NONE, WebViewClickHandler.OPEN_IMAGE, 1, R.string.webview_handler_open_image).setOnMenuItemClickListener(handler);
        menu.add(Menu.NONE, WebViewClickHandler.DOWNLOAD_LINK, 2, R.string.webview_handler_download_link).setOnMenuItemClickListener(handler);
        menu.add(Menu.NONE, WebViewClickHandler.SEARCH_GOOGLE, 2, R.string.webview_handler_search_with_ggl).setOnMenuItemClickListener(handler);
        menu.add(Menu.NONE, WebViewClickHandler.SHARE_LINK, 2, R.string.webview_handler_share).setOnMenuItemClickListener(handler);

    }
}
 
Example 15
Source File: CodeWebView.java    From OpenHub with GNU General Public License v3.0 4 votes vote down vote up
private boolean hitLinkResult(WebView.HitTestResult result) {
    return result.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE ||
            result.getType() == HitTestResult.IMAGE_TYPE ||
            result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE;
}
 
Example 16
Source File: PrettifyWebView.java    From mvvm-template with GNU General Public License v3.0 4 votes vote down vote up
private boolean hitLinkResult(WebView.HitTestResult result) {
    return result.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE || result.getType() == HitTestResult.IMAGE_TYPE ||
            result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE;
}
 
Example 17
Source File: LinkHandler.java    From firefox-echo-show with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public boolean onLongClick(View v) {
    if (callback == null) {
        return false;
    }

    final WebView.HitTestResult hitTestResult = webView.getHitTestResult();

    switch (hitTestResult.getType()) {
        case WebView.HitTestResult.SRC_ANCHOR_TYPE:
            final String linkURL = hitTestResult.getExtra();
            callback.onLongPress(new IWebView.HitTarget(true, linkURL, false, null));
            return true;

        case WebView.HitTestResult.IMAGE_TYPE:
            final String imageURL = hitTestResult.getExtra();
            callback.onLongPress(new IWebView.HitTarget(false, null, true, imageURL));
            return true;

        case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
            // hitTestResult.getExtra() contains only the image URL, and not the link
            // URL. Internally, WebView's HitTestData contains both, but they only
            // make it available via requestFocusNodeHref...
            final Message message = new Message();
            message.setTarget(new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    final Bundle data = msg.getData();
                    final String url = data.getString("url");
                    final String src = data.getString("src");

                    if (url == null || src == null) {
                        throw new IllegalStateException("WebView did not supply url or src for image link");
                    }

                    if (callback != null) {
                        callback.onLongPress(new IWebView.HitTarget(true, url, true, src));
                    }
                }
            });

            webView.requestFocusNodeHref(message);
            return true;

        default:
            return false;
    }
}
 
Example 18
Source File: LinkHandler.java    From focus-android with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public boolean onLongClick(View v) {
    if (callback == null) {
        return false;
    }

    final WebView.HitTestResult hitTestResult = webView.getHitTestResult();

    switch (hitTestResult.getType()) {
        case WebView.HitTestResult.SRC_ANCHOR_TYPE:
            final String linkURL = hitTestResult.getExtra();
            callback.onLongPress(new IWebView.HitTarget(true, linkURL, false, null));
            return true;

        case WebView.HitTestResult.IMAGE_TYPE:
            final String imageURL = hitTestResult.getExtra();
            callback.onLongPress(new IWebView.HitTarget(false, null, true, imageURL));
            return true;

        case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
            // hitTestResult.getExtra() contains only the image URL, and not the link
            // URL. Internally, WebView's HitTestData contains both, but they only
            // make it available via requestFocusNodeHref...
            final Message message = new Message();
            message.setTarget(new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    final Bundle data = msg.getData();
                    final String url = data.getString("url");
                    final String src = data.getString("src");

                    if (url == null || src == null) {
                        throw new IllegalStateException("WebView did not supply url or src for image link");
                    }

                    if (callback != null) {
                        callback.onLongPress(new IWebView.HitTarget(true, url, true, src));
                    }
                }
            });

            webView.requestFocusNodeHref(message);
            return true;

        default:
            return false;
    }
}
 
Example 19
Source File: BaseWebViewLoadContract.java    From YiZhi with Apache License 2.0 2 votes vote down vote up
/**
 * 图片长按事件
 *
 * @param hitTestResult {@link WebView.HitTestResult}
 */
public abstract void imageLongClicked(WebView.HitTestResult hitTestResult);
 
Example 20
Source File: WebViewAssist.java    From DevUtils with Apache License 2.0 2 votes vote down vote up
/**
 * 获取长按事件类型
 * <pre>
 *     WebView.HitTestResult.UNKNOWN_TYPE // 未知类型
 *     WebView.HitTestResult.PHONE_TYPE // 电话类型
 *     WebView.HitTestResult.EMAIL_TYPE // 电子邮件类型
 *     WebView.HitTestResult.GEO_TYPE // 地图类型
 *     WebView.HitTestResult.SRC_ANCHOR_TYPE // 超链接类型
 *     WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE // 带有链接的图片类型
 *     WebView.HitTestResult.IMAGE_TYPE // 单纯的图片类型
 *     WebView.HitTestResult.EDIT_TEXT_TYPE // 选中的文字类型
 *     <p></p>
 *     mWebView.setOnLongClickListener(new View.OnLongClickListener() {
 *          @Override
 *          public boolean onLongClick(View view) {
 *              WebView.HitTestResult result = webViewAssist.getHitTestResult();
 *              if(result != null) {
 *                  switch (result.getType()) {
 *                      case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
 *                          String imgUrl = result.getExtra();
 *                          return true;
 *                  }
 *              }
 *              return false;
 *          }
 *      });
 *      <p></p>
 *      HitTestResult.getType() 获取所选中目标的类型, 可以是图片、超链接、邮件、电话等等
 *      HitTestResult.getExtra() 获取额外的信息
 * </pre>
 * @return 长按事件类型
 */
public WebView.HitTestResult getHitTestResult() {
    return isWebViewNotEmpty() ? mWebView.getHitTestResult() : null;
}