Skip to content

Commit

Permalink
Issue nodejs#168 bugfix for links not being clickable, adds autoLink …
Browse files Browse the repository at this point in the history
…feature including web, phone, map and email
  • Loading branch information
kenodoggy authored and niftynei committed Sep 10, 2016
1 parent b47e223 commit 3898a33
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateUtils;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zulip.android.models.Person;
import com.j256.ormlite.stmt.UpdateBuilder;
import com.zulip.android.models.Stream;
import com.zulip.android.networking.AsyncPointerUpdate;

import com.squareup.picasso.Picasso;
import com.zulip.android.R;
import com.zulip.android.ZulipApp;
Expand Down Expand Up @@ -362,6 +359,7 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MessageHolder messageHolder = ((MessageHolder) holder);
final Message message = ((Message) items.get(position));
messageHolder.contentView.setText(message.getFormattedContent(zulipApp));
messageHolder.contentView.setMovementMethod(LinkMovementMethod.getInstance());

if (message.getType() == MessageType.STREAM_MESSAGE) {
messageHolder.senderName.setText(message.getSender().getName());
Expand Down
42 changes: 23 additions & 19 deletions app/src/main/java/com/zulip/android/models/Message.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
package com.zulip.android.models;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.ccil.cowan.tagsoup.HTMLSchema;
import org.ccil.cowan.tagsoup.Parser;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
Expand All @@ -25,16 +8,36 @@
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.util.Linkify;
import android.util.Log;

import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.misc.TransactionManager;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.table.DatabaseTable;
import com.zulip.android.ZulipApp;
import com.zulip.android.util.CustomHtmlToSpannedConverter;
import com.zulip.android.util.ZLog;
import com.zulip.android.ZulipApp;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.ccil.cowan.tagsoup.HTMLSchema;
import org.ccil.cowan.tagsoup.Parser;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import static com.zulip.android.util.CustomHtmlToSpannedConverter.linkifySpanned;

@DatabaseTable(tableName = "messages")
public class Message {
Expand Down Expand Up @@ -531,7 +534,8 @@ public Drawable getDrawable(String source) {

CustomHtmlToSpannedConverter converter = new CustomHtmlToSpannedConverter(
source, null, null, parser, emojiGetter, app.getServerURI());
return converter.convert();

return linkifySpanned(converter.convert(), Linkify.ALL);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.text.Html;
import android.text.Layout;
import android.text.Spannable;
Expand All @@ -40,6 +41,8 @@
import android.text.style.TypefaceSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.text.util.Linkify;
import android.util.Pair;

import org.ccil.cowan.tagsoup.Parser;
import org.xml.sax.Attributes;
Expand All @@ -51,7 +54,9 @@

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CustomHtmlToSpannedConverter implements ContentHandler {

Expand Down Expand Up @@ -687,4 +692,31 @@ private static int convertValueToInt(CharSequence charSeq,

return Integer.parseInt(nm.substring(index), base) * sign;
}

/**
* Parses Spanned text for existing html links and reapplies them.
* @see <a href="https://developer.android.com/reference/android/text/util/Linkify.html">Linkify</a>
*
* @param spann
* @param mask bitmask to define which kinds of links will be searched and applied (e.g. <a href="https://developer.android.com/reference/android/text/util/Linkify.html#ALL">Linkify.ALL</a>)
* @return
*/
public static Spanned linkifySpanned(@NonNull final Spanned spann, final int mask) {
URLSpan[] existingSpans = spann.getSpans(0, spann.length(), URLSpan.class);
List<Pair<Integer, Integer>> links = new ArrayList<>();

for (URLSpan urlSpan : existingSpans) {
links.add(new Pair<>(spann.getSpanStart(urlSpan), spann.getSpanEnd(urlSpan)));
}

Linkify.addLinks((Spannable) spann, mask);

// add the links back in
for (int i = 0; i < existingSpans.length; i++) {
((Spannable) spann).setSpan(existingSpans[i], links.get(i).first, links.get(i).second, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

return spann;
}

}

0 comments on commit 3898a33

Please sign in to comment.