List of usage examples for android.text StaticLayout getSelectionPath
public void getSelectionPath(int start, int end, Path dest)
From source file:org.telegram.ui.Cells.SharedLinkCell.java
@Override public boolean onTouchEvent(MotionEvent event) { boolean result = false; if (message != null && !linkLayout.isEmpty() && delegate != null && delegate.canPerformActions()) { if (event.getAction() == MotionEvent.ACTION_DOWN || linkPreviewPressed && event.getAction() == MotionEvent.ACTION_UP) { int x = (int) event.getX(); int y = (int) event.getY(); int offset = 0; boolean ok = false; for (int a = 0; a < linkLayout.size(); a++) { StaticLayout layout = linkLayout.get(a); if (layout.getLineCount() > 0) { int height = layout.getLineBottom(layout.getLineCount() - 1); int linkPosX = AndroidUtilities .dp(LocaleController.isRTL ? 8 : AndroidUtilities.leftBaseline); if (x >= linkPosX + layout.getLineLeft(0) && x <= linkPosX + layout.getLineWidth(0) && y >= linkY + offset && y <= linkY + offset + height) { ok = true;/*from w ww.jav a 2 s . co m*/ if (event.getAction() == MotionEvent.ACTION_DOWN) { resetPressedLink(); pressedLink = a; linkPreviewPressed = true; try { urlPath.setCurrentLayout(layout, 0, 0); layout.getSelectionPath(0, layout.getText().length(), urlPath); } catch (Exception e) { FileLog.e("tmessages", e); } result = true; } else if (linkPreviewPressed) { try { TLRPC.WebPage webPage = pressedLink == 0 && message.messageOwner.media != null ? message.messageOwner.media.webpage : null; if (webPage != null && Build.VERSION.SDK_INT >= 16 && webPage.embed_url != null && webPage.embed_url.length() != 0) { delegate.needOpenWebView(webPage); } else { Browser.openUrl(getContext(), links.get(pressedLink)); } } catch (Exception e) { FileLog.e("tmessages", e); } resetPressedLink(); result = true; } break; } offset += height; } } if (!ok) { resetPressedLink(); } } else if (event.getAction() == MotionEvent.ACTION_CANCEL) { resetPressedLink(); } } else { resetPressedLink(); } return result || super.onTouchEvent(event); }
From source file:org.telegram.ui.ArticleViewer.java
private boolean checkLayoutForLinks(MotionEvent event, View parentView, StaticLayout layout, int layoutX, int layoutY) { if (parentView == null || layout == null) { return false; }/*www . ja v a 2 s . com*/ CharSequence text = layout.getText(); if (!(text instanceof Spannable)) { return false; } int x = (int) event.getX(); int y = (int) event.getY(); boolean removeLink = false; if (event.getAction() == MotionEvent.ACTION_DOWN) { if (x >= layoutX && x <= layoutX + layout.getWidth() && y >= layoutY && y <= layoutY + layout.getHeight()) { try { int checkX = x - layoutX; int checkY = y - layoutY; final int line = layout.getLineForVertical(checkY); final int off = layout.getOffsetForHorizontal(line, checkX); final float left = layout.getLineLeft(line); if (left <= checkX && left + layout.getLineWidth(line) >= checkX) { Spannable buffer = (Spannable) layout.getText(); TextPaintUrlSpan[] link = buffer.getSpans(off, off, TextPaintUrlSpan.class); if (link != null && link.length > 0) { pressedLink = link[0]; int pressedStart = buffer.getSpanStart(pressedLink); int pressedEnd = buffer.getSpanEnd(pressedLink); for (int a = 1; a < link.length; a++) { TextPaintUrlSpan span = link[a]; int start = buffer.getSpanStart(span); int end = buffer.getSpanEnd(span); if (pressedStart > start || end > pressedEnd) { pressedLink = span; pressedStart = start; pressedEnd = end; } } pressedLinkOwnerLayout = layout; pressedLinkOwnerView = parentView; try { urlPath.setCurrentLayout(layout, pressedStart, 0); layout.getSelectionPath(pressedStart, pressedEnd, urlPath); parentView.invalidate(); } catch (Exception e) { FileLog.e(e); } } } } catch (Exception e) { FileLog.e(e); } } } else if (event.getAction() == MotionEvent.ACTION_UP) { if (pressedLink != null) { removeLink = true; String url = pressedLink.getUrl(); if (url != null) { int index; boolean isAnchor = false; final String anchor; if ((index = url.lastIndexOf('#')) != -1) { anchor = url.substring(index + 1); if (url.toLowerCase().contains(currentPage.url.toLowerCase())) { Integer row = anchors.get(anchor); if (row != null) { layoutManager.scrollToPositionWithOffset(row, 0); isAnchor = true; } } } else { anchor = null; } if (!isAnchor) { if (openUrlReqId == 0) { showProgressView(true); final TLRPC.TL_messages_getWebPage req = new TLRPC.TL_messages_getWebPage(); req.url = pressedLink.getUrl(); req.hash = 0; openUrlReqId = ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() { @Override public void run(final TLObject response, TLRPC.TL_error error) { AndroidUtilities.runOnUIThread(new Runnable() { @Override public void run() { if (openUrlReqId == 0) { return; } openUrlReqId = 0; showProgressView(false); if (isVisible) { if (response instanceof TLRPC.TL_webPage && ((TLRPC.TL_webPage) response).cached_page instanceof TLRPC.TL_pageFull) { addPageToStack((TLRPC.TL_webPage) response, anchor); } else { Browser.openUrl(parentActivity, req.url); } } } }); } }); } } } } } else if (event.getAction() == MotionEvent.ACTION_CANCEL) { removeLink = true; } if (removeLink && pressedLink != null) { pressedLink = null; pressedLinkOwnerLayout = null; pressedLinkOwnerView = null; parentView.invalidate(); } if (pressedLink != null && event.getAction() == MotionEvent.ACTION_DOWN) { startCheckLongPress(); } if (event.getAction() != MotionEvent.ACTION_DOWN && event.getAction() != MotionEvent.ACTION_MOVE) { cancelCheckLongPress(); } return pressedLink != null; }