Even better solution to properly handle links in messages

This commit is contained in:
Sylvain Berfini 2021-01-27 10:13:38 +01:00
parent 8f116a84e4
commit 3b28d1e57a
2 changed files with 3 additions and 53 deletions

View file

@ -28,8 +28,8 @@ import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.CountDownTimer; import android.os.CountDownTimer;
import android.text.Spanned;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
@ -221,9 +221,8 @@ public class ChatMessageViewHolder extends RecyclerView.ViewHolder implements Vi
} }
if (message.hasTextContent()) { if (message.hasTextContent()) {
String msg = message.getTextContent(); messageText.setText(message.getTextContent());
Spanned text = LinphoneUtils.getTextWithHttpLinks(msg); Linkify.addLinks(messageText, Linkify.ALL);
messageText.setText(text);
messageText.setMovementMethod(LinkMovementMethod.getInstance()); messageText.setMovementMethod(LinkMovementMethod.getInstance());
messageText.setVisibility(View.VISIBLE); messageText.setVisibility(View.VISIBLE);
} }

View file

@ -29,8 +29,6 @@ import android.net.NetworkInfo;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.text.Html;
import android.text.Spanned;
import android.view.View; import android.view.View;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
@ -41,7 +39,6 @@ import androidx.core.content.ContextCompat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Pattern;
import org.linphone.LinphoneContext; import org.linphone.LinphoneContext;
import org.linphone.LinphoneManager; import org.linphone.LinphoneManager;
import org.linphone.R; import org.linphone.R;
@ -302,52 +299,6 @@ public final class LinphoneUtils {
} }
} }
public static Spanned getTextWithHttpLinks(String text) {
if (text == null) return null;
if (text.contains("<")) {
text = text.replace("<", "&lt;");
}
if (text.contains(">")) {
text = text.replace(">", "&gt;");
}
if (text.contains("\n")) {
text = text.replace("\n", "<br>");
}
if (text.contains("http://")) {
int indexHttp = text.indexOf("http://");
int indexFinHttp =
text.indexOf(" ", indexHttp) == -1
? (text.indexOf("<br>", indexHttp) == -1
? text.length()
: text.indexOf("<br>", indexHttp))
: text.indexOf(" ", indexHttp);
String link = text.substring(indexHttp, indexFinHttp);
String linkWithoutScheme = link.replace("http://", "");
text =
text.replaceFirst(
Pattern.quote(link),
"<a href=\"" + link + "\">" + linkWithoutScheme + "</a>");
}
if (text.contains("https://")) {
int indexHttp = text.indexOf("https://");
int indexFinHttp =
text.indexOf(" ", indexHttp) == -1
? (text.indexOf("<br>", indexHttp) == -1
? text.length()
: text.indexOf("<br>", indexHttp))
: text.indexOf(" ", indexHttp);
String link = text.substring(indexHttp, indexFinHttp);
String linkWithoutScheme = link.replace("https://", "");
text =
text.replaceFirst(
Pattern.quote(link),
"<a href=\"" + link + "\">" + linkWithoutScheme + "</a>");
}
return Html.fromHtml(text);
}
public static void showTrustDeniedDialog(Context context) { public static void showTrustDeniedDialog(Context context) {
final Dialog dialog = new Dialog(context); final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);