/*
 * Copyright 2015 Hippo Seven
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hippo.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.Layout;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;

public class LinkifyTextView extends TextView {

    private ClickableSpan mCurrentSpan;

    public LinkifyTextView(Context context) {
        super(context);
    }

    public LinkifyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LinkifyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ClickableSpan getCurrentSpan() {
        return mCurrentSpan;
    }

    public void clearCurrentSpan() {
        mCurrentSpan = null;
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        // Let the parent or grandparent of TextView to handles click aciton.
        // Otherwise click effect like ripple will not work, and if touch area
        // do not contain a url, the TextView will still get MotionEvent.
        // onTouchEven must be called with MotionEvent.ACTION_DOWN for each touch
        // action on it, so we analyze touched url here.
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mCurrentSpan = null;

            if (getText() instanceof Spanned) {
                // Get this code from android.text.method.LinkMovementMethod.
                // Work fine !
                int x = (int) event.getX();
                int y = (int) event.getY();

                x -= getTotalPaddingLeft();
                y -= getTotalPaddingTop();

                x += getScrollX();
                y += getScrollY();

                Layout layout = getLayout();
                int line = layout.getLineForVertical(y);
                int off = layout.getOffsetForHorizontal(line, x);

                ClickableSpan[] spans = ((Spanned)getText()).getSpans(off, off, ClickableSpan.class);

                if (spans.length > 0) {
                    mCurrentSpan = spans[0];
                }
            }
        }

        return super.onTouchEvent(event);
    }
}

@@ -2435,24 +2435,66 @@
 etLayout();%0A
+                if (null != layout) %7B%0A    
             
@@ -2538,16 +2538,20 @@
 cal(y);%0A
+    
         
@@ -2605,24 +2605,28 @@
 (line, x);%0A%0A
+    
             
@@ -2728,24 +2728,28 @@
             
+    
 if (spans.le
@@ -2756,24 +2756,28 @@
 ngth %3E 0) %7B%0A
+    
             
@@ -2809,16 +2809,38 @@
 ans%5B0%5D;%0A
+                    %7D%0A
         

Check layout null in LinkifyTextView