diff options
Diffstat (limited to 'src/main/java')
8 files changed, 301 insertions, 37 deletions
diff --git a/src/main/java/de/tzur/conversations/Settings.java b/src/main/java/de/tzur/conversations/Settings.java new file mode 100644 index 00000000..25079ea1 --- /dev/null +++ b/src/main/java/de/tzur/conversations/Settings.java @@ -0,0 +1,55 @@ +package de.tzur.conversations; + +import android.content.SharedPreferences; +import android.util.Log; + +/** + * This class is used to provide access to settings which have to be accessed frequently. + * Every setting in this class has to be updated using @see SettingsActivity#onSharedPreferenceChanged. + */ +public final class Settings { + + /** + * Initializes the settings provided via this static class. + * @param preferences the shared preferences of the app. + */ + public static void initSettingsClassWithPreferences(SharedPreferences preferences) { + Log.d("SETTING", "Initializing settings"); + String[] preferenceNames = { "parse_emoticons", "send_button_status" }; + for (String name : preferenceNames) { + Settings.synchronizeSettingsClassWithPreferences(preferences, name); + } + } + + /** + * Synchronizes the setting value in this class on settings update in SettingsActivity. + * @param preferences the shared preferences of the app. + * @param name the name of the setting to synchronize. + */ + public static void synchronizeSettingsClassWithPreferences(SharedPreferences preferences, String name) { + Log.d("SETTING", "Synchronizing settings"); + switch (name) { + case "parse_emoticons": + Settings.PARSE_EMOTICONS = preferences.getBoolean(name, Settings.PARSE_EMOTICONS); + break; + case "send_button_status": + Settings.SHOW_ONLINE_STATUS = preferences.getBoolean(name, Settings.SHOW_ONLINE_STATUS); + break; + } + } + /** + * Boolean if emoticons should be parsed to emoticons or not. + */ + public static boolean PARSE_EMOTICONS = false; + /** + * Boolean if online status should be shown or not. + */ + public static boolean SHOW_ONLINE_STATUS = false; + + /** + * This is a utility class - private constructor avoids any instantiation. + */ + private Settings() { + // Private constructor to avoid instantiation + } +} diff --git a/src/main/java/eu/siacs/conversations/Config.java b/src/main/java/eu/siacs/conversations/Config.java index f38bcbfc..fa9345a2 100644 --- a/src/main/java/eu/siacs/conversations/Config.java +++ b/src/main/java/eu/siacs/conversations/Config.java @@ -20,6 +20,8 @@ public final class Config { public static final int MESSAGE_MERGE_WINDOW = 20; + public static final boolean UTF8_EMOTICONS = false; + public static final int PAGE_SIZE = 50; public static final int MAX_NUM_PAGES = 3; diff --git a/src/main/java/eu/siacs/conversations/parser/AbstractParser.java b/src/main/java/eu/siacs/conversations/parser/AbstractParser.java index bfe84440..977062e2 100644 --- a/src/main/java/eu/siacs/conversations/parser/AbstractParser.java +++ b/src/main/java/eu/siacs/conversations/parser/AbstractParser.java @@ -1,11 +1,13 @@ package eu.siacs.conversations.parser; - import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.datatype.DatatypeFactory; + import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.services.XmppConnectionService; @@ -20,6 +22,15 @@ public abstract class AbstractParser { this.mXmppConnectionService = service; } + /** + * Gets the timestamp from the 'delay' element. + * Refer to XEP-0203: Delayed Delivery for details. @link{http://xmpp.org/extensions/xep-0203.html} + * @param packet the element to find the child element 'delay' in. + * @return the time in milli seconds of the attribute 'stamp' of the + * element 'delay'. In case there is no 'delay' element or no 'stamp' + * attribute or the current time is less than the value of the 'stamp' + * attribute the current time is returned. + */ protected long getTimestamp(Element packet) { long now = System.currentTimeMillis(); Element delay = packet.findChild("delay"); @@ -30,30 +41,34 @@ public abstract class AbstractParser { if (stamp == null) { return now; } - try { - long time = parseTimestamp(stamp).getTime(); - return now < time ? now : time; - } catch (ParseException e) { - return now; - } + long time = parseTimestamp(stamp).getTime(); + return now < time ? now : time; } - public static Date parseTimestamp(String timestamp) throws ParseException { - timestamp = timestamp.replace("Z", "+0000"); - SimpleDateFormat dateFormat; - timestamp = timestamp.substring(0,19)+timestamp.substring(timestamp.length() -5,timestamp.length()); - dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ",Locale.US); - return dateFormat.parse(timestamp); + /** + * Parses the timestamp according to XEP-0082: XMPP Date and Time Profiles. + * @link{http://xmpp.org/extensions/xep-0082.html} + * + * @param timestamp the timestamp to parse + * @return Date + * @throws ParseException + */ + public static Date parseTimestamp(String timestamp) { + try { + return DatatypeFactory.newInstance().newXMLGregorianCalendar(timestamp).toGregorianCalendar().getTime(); + } catch (DatatypeConfigurationException e) { + return new Date(); + } } protected void updateLastseen(final Element packet, final Account account, final boolean presenceOverwrite) { final Jid from = packet.getAttributeAsJid("from"); - updateLastseen(packet, account, from, presenceOverwrite); - } + updateLastseen(packet, account, from, presenceOverwrite); + } - protected void updateLastseen(final Element packet, final Account account, final Jid from, - final boolean presenceOverwrite) { + protected void updateLastseen(final Element packet, final Account account, final Jid from, + final boolean presenceOverwrite) { final String presence = from == null || from.isBareJid() ? "" : from.getResourcepart(); final Contact contact = account.getRoster().getContact(from); final long timestamp = getTimestamp(packet); diff --git a/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java b/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java index 39e215f2..1c1ff3b9 100644 --- a/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Locale; +import de.tzur.conversations.Settings; import eu.siacs.conversations.entities.Account; import android.content.SharedPreferences; @@ -58,28 +59,33 @@ public class SettingsActivity extends XmppActivity implements @Override public void onSharedPreferenceChanged(SharedPreferences preferences, String name) { - if (name.equals("resource")) { - String resource = preferences.getString("resource", "mobile") - .toLowerCase(Locale.US); - if (xmppConnectionServiceBound) { - for (Account account : xmppConnectionService.getAccounts()) { - account.setResource(resource); - if (!account.isOptionSet(Account.OPTION_DISABLED)) { - xmppConnectionService.reconnectAccountInBackground(account); + switch (name) { + case "resource": + String resource = preferences.getString("resource", "mobile") + .toLowerCase(Locale.US); + if (xmppConnectionServiceBound) { + for (Account account : xmppConnectionService.getAccounts()) { + account.setResource(resource); + if (!account.isOptionSet(Account.OPTION_DISABLED)) { + xmppConnectionService.reconnectAccountInBackground(account); + } + } + } + break; + case "keep_foreground_service": + xmppConnectionService.toggleForegroundService(); + break; + case "confirm_messages": + if (xmppConnectionServiceBound) { + for (Account account : xmppConnectionService.getAccounts()) { + if (!account.isOptionSet(Account.OPTION_DISABLED)) { + xmppConnectionService.sendPresence(account); + } } } - } - } else if (name.equals("keep_foreground_service")) { - xmppConnectionService.toggleForegroundService(); - } else if (name.equals("confirm_messages")) { - if (xmppConnectionServiceBound) { - for (Account account : xmppConnectionService.getAccounts()) { - if (!account.isOptionSet(Account.OPTION_DISABLED)) { - xmppConnectionService.sendPresence(account); - } - } - } - } + break; + } + Settings.synchronizeSettingsClassWithPreferences(getPreferences(), name); } } diff --git a/src/main/java/eu/siacs/conversations/ui/XmppActivity.java b/src/main/java/eu/siacs/conversations/ui/XmppActivity.java index 7eaec10c..62f62b9a 100644 --- a/src/main/java/eu/siacs/conversations/ui/XmppActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/XmppActivity.java @@ -63,6 +63,7 @@ import java.util.Hashtable; import java.util.List; import java.util.concurrent.RejectedExecutionException; +import de.tzur.conversations.Settings; import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Account; @@ -336,6 +337,9 @@ public abstract class XmppActivity extends Activity { setTheme(this.mTheme); this.mUsingEnterKey = usingEnterKey(); mUseSubject = getPreferences().getBoolean("use_subject", true); + + Settings.initSettingsClassWithPreferences(getPreferences()); + final ActionBar ab = getActionBar(); if (ab!=null) { ab.setDisplayHomeAsUpEnabled(true); diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java index a48f6ae4..c4a446e8 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java @@ -20,13 +20,27 @@ import java.lang.ref.WeakReference; import java.util.List; import java.util.concurrent.RejectedExecutionException; +import de.tzur.conversations.Settings; +import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Downloadable; import eu.siacs.conversations.entities.Message; +import eu.siacs.conversations.entities.Presences; import eu.siacs.conversations.ui.ConversationActivity; import eu.siacs.conversations.ui.XmppActivity; import eu.siacs.conversations.utils.UIHelper; +import android.content.Context; +import android.graphics.Color; +import android.graphics.Typeface; +import android.preference.PreferenceManager; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; public class ConversationAdapter extends ArrayAdapter<Conversation> { @@ -68,6 +82,26 @@ public class ConversationAdapter extends ArrayAdapter<Conversation> { TextView mTimestamp = (TextView) view.findViewById(R.id.conversation_lastupdate); ImageView imagePreview = (ImageView) view.findViewById(R.id.conversation_lastimage); + if (Settings.SHOW_ONLINE_STATUS) { + TextView status = (TextView) view.findViewById(R.id.status); + + String color = "#000000"; + switch (conversation.getContact().getMostAvailableStatus()) { + case Presences.ONLINE: + case Presences.CHAT: + color = "#259B23"; + break; + case Presences.AWAY: + case Presences.XA: + color = "#FF9800"; + break; + case Presences.DND: + color = "#E51C23"; + break; + } + status.setBackgroundColor(Color.parseColor(color)); + } + Message message = conversation.getLatestMessage(); if (!conversation.isRead()) { diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index da92fb18..c3736f0b 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -5,6 +5,7 @@ import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.graphics.Typeface; import android.net.Uri; +import android.preference.PreferenceManager; import android.text.Spannable; import android.text.SpannableString; import android.text.style.ForegroundColorSpan; @@ -24,6 +25,7 @@ import android.widget.Toast; import java.util.List; +import de.tzur.conversations.Settings; import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Account; @@ -224,6 +226,10 @@ public class MessageAdapter extends ArrayAdapter<Message> { final String formattedBody = message.getMergedBody().replaceAll("^" + Message.ME_COMMAND, nick + " "); if (message.getType() != Message.TYPE_PRIVATE) { + boolean parseEmoticons = Settings.PARSE_EMOTICONS; + viewHolder.messageBody.setText(parseEmoticons ? UIHelper + .transformAsciiEmoticons(getContext(), message.getMergedBody()) + : message.getMergedBody()); if (message.hasMeCommand()) { final Spannable span = new SpannableString(formattedBody); span.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 0, nick.length(), diff --git a/src/main/java/eu/siacs/conversations/utils/UIHelper.java b/src/main/java/eu/siacs/conversations/utils/UIHelper.java index c3195d86..0ddf606f 100644 --- a/src/main/java/eu/siacs/conversations/utils/UIHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/UIHelper.java @@ -1,9 +1,18 @@ package eu.siacs.conversations.utils; +import java.util.ArrayList; import java.net.URLConnection; import java.util.Calendar; import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Map.Entry; +import java.util.regex.Pattern; +import java.util.regex.Matcher; +import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Conversation; @@ -14,6 +23,9 @@ import eu.siacs.conversations.xmpp.jid.Jid; import android.content.Context; import android.text.format.DateFormat; import android.text.format.DateUtils; +import android.text.Spannable.Factory; +import android.text.style.ImageSpan; +import android.text.Spannable; import android.util.Pair; public class UIHelper { @@ -101,6 +113,136 @@ public class UIHelper { } } + public static final Map<Pattern, Integer> ANDROID_EMOTICONS = new HashMap<Pattern, Integer>(); + + private static final Factory spannableFactory = Spannable.Factory + .getInstance(); + + static { + addPattern(ANDROID_EMOTICONS, ":)", R.drawable.emo_im_happy); + addPattern(ANDROID_EMOTICONS, ":-)", R.drawable.emo_im_happy); + addPattern(ANDROID_EMOTICONS, ":(", R.drawable.emo_im_sad); + addPattern(ANDROID_EMOTICONS, ":-(", R.drawable.emo_im_sad); + addPattern(ANDROID_EMOTICONS, ";)", R.drawable.emo_im_winking); + addPattern(ANDROID_EMOTICONS, ";-)", R.drawable.emo_im_winking); + addPattern(ANDROID_EMOTICONS, ":P", + R.drawable.emo_im_tongue_sticking_out); + addPattern(ANDROID_EMOTICONS, ":-P", + R.drawable.emo_im_tongue_sticking_out); + addPattern(ANDROID_EMOTICONS, "=-O", R.drawable.emo_im_surprised); + addPattern(ANDROID_EMOTICONS, ":*", R.drawable.emo_im_kissing); + addPattern(ANDROID_EMOTICONS, ":-*", R.drawable.emo_im_kissing); + addPattern(ANDROID_EMOTICONS, ":O", R.drawable.emo_im_wtf); + addPattern(ANDROID_EMOTICONS, ":-O", R.drawable.emo_im_wtf); + addPattern(ANDROID_EMOTICONS, "B)", R.drawable.emo_im_cool); + addPattern(ANDROID_EMOTICONS, "B-)", R.drawable.emo_im_cool); + addPattern(ANDROID_EMOTICONS, "8)", R.drawable.emo_im_cool); + addPattern(ANDROID_EMOTICONS, "8-)", R.drawable.emo_im_cool); + addPattern(ANDROID_EMOTICONS, ":$", R.drawable.emo_im_money_mouth); + addPattern(ANDROID_EMOTICONS, ":-$", R.drawable.emo_im_money_mouth); + addPattern(ANDROID_EMOTICONS, ":-!", R.drawable.emo_im_foot_in_mouth); + addPattern(ANDROID_EMOTICONS, ":-[", R.drawable.emo_im_embarrassed); + addPattern(ANDROID_EMOTICONS, "O:)", R.drawable.emo_im_angel); + addPattern(ANDROID_EMOTICONS, "O:-)", R.drawable.emo_im_angel); + addPattern(ANDROID_EMOTICONS, ":\\", R.drawable.emo_im_undecided); + addPattern(ANDROID_EMOTICONS, ":-\\", R.drawable.emo_im_undecided); + addPattern(ANDROID_EMOTICONS, ":'(", R.drawable.emo_im_crying); + addPattern(ANDROID_EMOTICONS, ":D", R.drawable.emo_im_laughing); + addPattern(ANDROID_EMOTICONS, ":-D", R.drawable.emo_im_laughing); + addPattern(ANDROID_EMOTICONS, "O_o", R.drawable.emo_im_wtf); + addPattern(ANDROID_EMOTICONS, "o_O", R.drawable.emo_im_wtf); + addPattern(ANDROID_EMOTICONS, ">:O", R.drawable.emo_im_yelling); + addPattern(ANDROID_EMOTICONS, ">:0", R.drawable.emo_im_yelling); + addPattern(ANDROID_EMOTICONS, ":S", R.drawable.emo_im_lips_are_sealed); + addPattern(ANDROID_EMOTICONS, ":-S", R.drawable.emo_im_lips_are_sealed); + addPattern(ANDROID_EMOTICONS, "<3", R.drawable.emo_im_heart); + } + + private static void addPattern(Map<Pattern, Integer> map, String smile, + int resource) { + map.put(Pattern.compile(Pattern.quote(smile)), resource); + } + + private static boolean getSmiledText(Context context, Spannable spannable) { + boolean hasChanges = false; + Map<Pattern, Integer> emoticons = ANDROID_EMOTICONS; + for (Entry<Pattern, Integer> entry : emoticons.entrySet()) { + Matcher matcher = entry.getKey().matcher(spannable); + while (matcher.find()) { + boolean set = true; + for (ImageSpan span : spannable.getSpans(matcher.start(), + matcher.end(), ImageSpan.class)) + if (spannable.getSpanStart(span) >= matcher.start() + && spannable.getSpanEnd(span) <= matcher.end()) + spannable.removeSpan(span); + else { + set = false; + break; + } + if (set) { + spannable.setSpan(new ImageSpan(context, entry.getValue()), + matcher.start(), matcher.end(), + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + hasChanges = true; + } + } + } + return hasChanges; + } + + private final static class EmoticonPattern { + Pattern pattern; + String replacement; + + EmoticonPattern(String ascii, int unicode) { + this.pattern = Pattern.compile("(?<=(^|\\s))" + ascii + + "(?=(\\s|$))"); + this.replacement = new String(new int[] { unicode, }, 0, 1); + } + + String replaceAll(String body) { + return pattern.matcher(body).replaceAll(replacement); + } + } + + private static final EmoticonPattern[] patterns = new EmoticonPattern[] { + new EmoticonPattern(":-?D", 0x1f600), + new EmoticonPattern("\\^\\^", 0x1f601), + new EmoticonPattern(":'D", 0x1f602), + new EmoticonPattern("\\]-?D", 0x1f608), + new EmoticonPattern(";-?\\)", 0x1f609), + new EmoticonPattern(":-?\\)", 0x1f60a), + new EmoticonPattern("[B8]-?\\)", 0x1f60e), + new EmoticonPattern(":-?\\|", 0x1f610), + new EmoticonPattern(":-?[/\\\\]", 0x1f615), + new EmoticonPattern(":-?\\*", 0x1f617), + new EmoticonPattern(":-?[Ppb]", 0x1f61b), + new EmoticonPattern(":-?\\(", 0x1f61e), + new EmoticonPattern(":-?[0Oo]", 0x1f62e), + new EmoticonPattern("\\\\o/", 0x1F631), }; + + public static String transformAsciiEmoticonsToUtf8(String body) { + if (body != null) { + for (EmoticonPattern p : patterns) { + body = p.replaceAll(body); + } + body = body.trim(); + } + return body; + } + + public static Spannable transformAsciiEmoticons(Context context, String body) { + Spannable spannable; + if (Config.UTF8_EMOTICONS) { + spannable = spannableFactory.newSpannable(transformAsciiEmoticonsToUtf8(body)); + } + else { + spannable = spannableFactory.newSpannable(body); + getSmiledText(context, spannable); + } + return spannable; + } + public static int getColorForName(String name) { if (name.isEmpty()) { return 0xFF202020; |