Example usage for android.text StaticLayout getOffsetForHorizontal

List of usage examples for android.text StaticLayout getOffsetForHorizontal

Introduction

In this page you can find the example usage for android.text StaticLayout getOffsetForHorizontal.

Prototype

public int getOffsetForHorizontal(int line, float horiz) 

Source Link

Document

Get the character offset on the specified line whose position is closest to the specified horizontal position.

Usage

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;
    }/*from ww  w  .  j a  v  a2  s  . c o m*/
    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;
}