From 16a05cf208dfbdc67ecd6f9f4bdf242d3bb32906 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 13 Sep 2012 15:53:14 +0200 Subject: [PATCH] Links in text messages are clickable and shorten if possible --- res/layout/chat_bubble_alt_incoming.xml | 2 + res/layout/chat_bubble_alt_outgoing.xml | 2 + res/layout/chat_bubble_incoming.xml | 2 + res/layout/chat_bubble_outgoing.xml | 2 + src/org/linphone/ui/BubbleChat.java | 49 ++++++++++++++++++------- 5 files changed, 44 insertions(+), 13 deletions(-) diff --git a/res/layout/chat_bubble_alt_incoming.xml b/res/layout/chat_bubble_alt_incoming.xml index bef5cdc20..8080c0c0f 100644 --- a/res/layout/chat_bubble_alt_incoming.xml +++ b/res/layout/chat_bubble_alt_incoming.xml @@ -19,6 +19,8 @@ android:gravity="bottom" android:singleLine="true" android:paddingLeft="5dp" + android:linksClickable="true" + android:autoLink="web" android:layout_width="wrap_content" android:layout_height="match_parent" /> diff --git a/res/layout/chat_bubble_alt_outgoing.xml b/res/layout/chat_bubble_alt_outgoing.xml index 8a0f994b4..44a75fc29 100644 --- a/res/layout/chat_bubble_alt_outgoing.xml +++ b/res/layout/chat_bubble_alt_outgoing.xml @@ -24,6 +24,8 @@ diff --git a/res/layout/chat_bubble_incoming.xml b/res/layout/chat_bubble_incoming.xml index c61b49af6..cb18f054e 100644 --- a/res/layout/chat_bubble_incoming.xml +++ b/res/layout/chat_bubble_incoming.xml @@ -19,6 +19,8 @@ android:textSize="12dp" android:singleLine="true" android:paddingLeft="5dp" + android:linksClickable="true" + android:autoLink="web" android:layout_width="wrap_content" android:layout_height="wrap_content" /> diff --git a/res/layout/chat_bubble_outgoing.xml b/res/layout/chat_bubble_outgoing.xml index 0cd48d992..52027dcc9 100644 --- a/res/layout/chat_bubble_outgoing.xml +++ b/res/layout/chat_bubble_outgoing.xml @@ -10,6 +10,8 @@ android:id="@+id/message" android:layout_gravity="right" android:textColor="@android:color/black" + android:linksClickable="true" + android:autoLink="web" android:layout_width="wrap_content" android:layout_height="wrap_content" /> diff --git a/src/org/linphone/ui/BubbleChat.java b/src/org/linphone/ui/BubbleChat.java index 0e69cb28e..e2bf3cf7b 100644 --- a/src/org/linphone/ui/BubbleChat.java +++ b/src/org/linphone/ui/BubbleChat.java @@ -28,8 +28,11 @@ import org.linphone.core.LinphoneChatMessage; import android.content.Context; import android.graphics.Color; +import android.text.Html; import android.text.Spannable; import android.text.SpannableStringBuilder; +import android.text.Spanned; +import android.text.method.LinkMovementMethod; import android.text.style.ImageSpan; import android.view.LayoutInflater; import android.view.View; @@ -62,7 +65,7 @@ public class BubbleChat { emoticons.put(":-*", R.drawable.emo_im_kissing); emoticons.put(":*", R.drawable.emo_im_kissing); emoticons.put(":-/", R.drawable.emo_im_undecided); - emoticons.put(":/", R.drawable.emo_im_undecided); + emoticons.put(":/ ", R.drawable.emo_im_undecided); emoticons.put(":-\\", R.drawable.emo_im_undecided); emoticons.put(":\\", R.drawable.emo_im_undecided); emoticons.put(":-O", R.drawable.emo_im_surprised); @@ -100,6 +103,13 @@ public class BubbleChat { layoutParams.setMargins(0, LinphoneUtils.pixelsToDpi(context.getResources(), 10), 0, 0); view.setLayoutParams(layoutParams); + Spanned text; + if (context.getResources().getBoolean(R.bool.emoticons_in_messages)) { + text = getSmiledText(context, getTextWithHttpLinks(message)); + } else { + text = getTextWithHttpLinks(message); + } + if (context.getResources().getBoolean(R.bool.display_messages_time)) { LinearLayout layout; if (context.getResources().getBoolean(R.bool.display_time_aside)) { @@ -117,11 +127,8 @@ public class BubbleChat { } TextView msgView = (TextView) layout.findViewById(R.id.message); - if (context.getResources().getBoolean(R.bool.emoticons_in_messages)) { - msgView.setText(getSmiledText(context, message)); - } else { - msgView.setText(message); - } + msgView.setText(text); + msgView.setMovementMethod(LinkMovementMethod.getInstance()); TextView timeView = (TextView) layout.findViewById(R.id.time); timeView.setText(timestampToHumanDate(context, time)); @@ -143,12 +150,9 @@ public class BubbleChat { messageView.setId(id); messageView.setTextColor(Color.BLACK); messageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); - - if (context.getResources().getBoolean(R.bool.emoticons_in_messages)) { - messageView.setText(getSmiledText(context, message)); - } else { - messageView.setText(message); - } + messageView.setText(text); + messageView.setLinksClickable(true); + messageView.setMovementMethod(LinkMovementMethod.getInstance()); view.addView(messageView); } @@ -205,7 +209,7 @@ public class BubbleChat { cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)); } - public static Spannable getSmiledText(Context context, String text) { + public static Spannable getSmiledText(Context context, Spanned text) { SpannableStringBuilder builder = new SpannableStringBuilder(text); int index; @@ -224,4 +228,23 @@ public class BubbleChat { } return builder; } + + public static Spanned getTextWithHttpLinks(String text) { + if (text.contains("http://")) { + int indexHttp = text.indexOf("http://"); + int indexFinHttp = text.indexOf(" ", indexHttp) == -1 ? text.length() : text.indexOf(" ", indexHttp); + String link = text.substring(indexHttp, indexFinHttp); + String linkWithoutScheme = link.replace("http://", ""); + text = text.replaceFirst(link, "" + linkWithoutScheme + ""); + } + if (text.contains("https://")) { + int indexHttp = text.indexOf("https://"); + int indexFinHttp = text.indexOf(" ", indexHttp) == -1 ? text.length() : text.indexOf(" ", indexHttp); + String link = text.substring(indexHttp, indexFinHttp); + String linkWithoutScheme = link.replace("https://", ""); + text = text.replaceFirst(link, "" + linkWithoutScheme + ""); + } + + return Html.fromHtml(text); + } }