package cl.monsoon.s1next.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.View;
import android.webkit.URLUtil;

import org.xml.sax.XMLReader;

import cl.monsoon.s1next.activity.GalleryActivity;

/**
 * Make ImageSpan clickable.
 */
public final class ImageTagHandler implements Html.TagHandler {

    private final Context mContext;

    public ImageTagHandler(Context context) {
        this.mContext = context;
    }

    @Override
    public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
        if (tag.toLowerCase().equals("img")) {
            // get all ImageSpans
            int length = output.length();
            // ImageSpan's length = 1
            ImageSpan imageSpan = output.getSpans(length - 1, length, ImageSpan.class)[0];

            String url = imageSpan.getSource();
            // Emoji url don't have domain
            // skip Emoji url because we don't want Emoji clickable
            if (URLUtil.isNetworkUrl(url)) {

                // make this ImageSpan clickable
                output.setSpan(
                        new ImageClickableSpan(mContext, imageSpan.getDrawable(), url),
                        length - 1,
                        length,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }

    public static class ImageClickableSpan extends ImageSpan implements View.OnClickListener {

        private final Context mContext;

        ImageClickableSpan(Context context, Drawable d, String source) {
            super(d, source);

            this.mContext = context;
        }

        public Context getContext() {
            return mContext;
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getContext(), GalleryActivity.class);
            intent.putExtra(GalleryActivity.ARG_IMAGE_URL, getSource());

            getContext().startActivity(intent);
        }
    }
}

@@ -747,81 +747,319 @@
 if (
-tag.toLowerCase().equals(%22img%22)) %7B%0A            // get all ImageSpans%0A    
+!opening && tag.equalsIgnoreCase(%22img%22)) %7B%0A            handleStartImg(output);%0A        %7D%0A    %7D%0A%0A    /**%0A     * see android.text.HtmlToSpannedConverter#startImg(android.text.SpannableStringBuilder, org.xml.sax.Attributes, android.text.Html.ImageGetter)%0A     */%0A    private void handleStartImg(Editable output) %7B%0A
     
@@ -1066,22 +1066,19 @@
     int 
-l
 en
-gth
+d
  = outpu
@@ -1089,28 +1089,25 @@
 ngth();%0A
-    
+%0A
         
 // Image
@@ -1102,38 +1102,37 @@
     
-// ImageSpan's length = 1%0A    
+int len = %22%5CuFFFC%22.length();%0A
     
@@ -1177,26 +1177,22 @@
 ans(
-l
 en
-gth - 1
+d - len
 , 
-l
 en
-gth
+d
 , Im
@@ -1211,20 +1211,16 @@
 s)%5B0%5D;%0A%0A
-    
         
@@ -1255,24 +1255,205 @@
 urce();%0A    
+    // replace %5CuFFFC (OBJECT REPLACEMENT CHARACTER) to its ImageSpan's source%0A        // in order to support url copy (when selected)%0A        output.replace(end - len, end, url);%0A%0A
         // E
@@ -1487,20 +1487,16 @@
         
-  
-  
 // skip 
@@ -1547,28 +1547,24 @@
 ble%0A        
-    
 if (URLUtil.
@@ -1585,24 +1585,62 @@
 rl)) %7B%0A%0A    
+        output.removeSpan(imageSpan);%0A
             
@@ -1680,28 +1680,24 @@
             
-    
 output.setSp
@@ -1700,20 +1700,16 @@
 etSpan(%0A
-    
         
@@ -1808,22 +1808,17 @@
     
-    length - 1
+end - len
 ,%0A  
@@ -1839,24 +1839,25 @@
     
-    length,%0A    
+output.length(),%0A
     
@@ -1911,22 +1911,8 @@
 E);%0A
-            %7D%0A
     

Tweak `ImageTagHandler` and support image url copy when selected