1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
package de.thedevstack.conversationsplus.utils;
import android.graphics.BitmapFactory;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import de.thedevstack.conversationsplus.ConversationsPlusApplication;
import de.thedevstack.conversationsplus.entities.Conversation;
import de.thedevstack.conversationsplus.entities.DownloadableFile;
import de.thedevstack.conversationsplus.entities.Message;
import de.thedevstack.conversationsplus.persistance.DatabaseBackend;
import de.thedevstack.conversationsplus.persistance.FileBackend;
/**
* Utility class to work with messages.
*/
public final class MessageUtil {
public static boolean markMessage(Conversation conversation, String uuid, int status) {
if (uuid == null) {
return false;
} else {
Message message = conversation.findSentMessageWithUuid(uuid);
if (message != null) {
markMessage(message, status);
return true;
} else {
return false;
}
}
}
public static void markMessage(Message message, int status) {
if (status == Message.STATUS_SEND_FAILED
&& (message.getStatus() == Message.STATUS_SEND_RECEIVED || message
.getStatus() == Message.STATUS_SEND_DISPLAYED)) {
return;
}
message.setStatus(status);
DatabaseBackend.getInstance(ConversationsPlusApplication.getAppContext()).updateMessage(message);
UiUpdateHelper.updateConversationUi();
}
public static boolean wasHighlightedOrPrivate(final Message message) {
final String nick = message.getConversation().getMucOptions().getActualNick();
final Pattern highlight = generateNickHighlightPattern(nick);
if (message.getBody() == null || nick == null) {
return false;
}
final Matcher m = highlight.matcher(message.getBody());
return (m.find() || message.getType() == Message.TYPE_PRIVATE);
}
private static Pattern generateNickHighlightPattern(final String nick) {
// We expect a word boundary, i.e. space or start of string, followed by
// the
// nick (matched in case-insensitive manner), followed by optional
// punctuation (for example "bob: i disagree" or "how are you alice?"),
// followed by another word boundary.
return Pattern.compile("\\b" + Pattern.quote(nick) + "\\p{Punct}?\\b",
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
}
public static void updateMessageWithImageDetails(Message message, String filePath, long size, int imageWidth, int imageHeight) {
message.setRelativeFilePath(filePath);
MessageUtil.updateMessageBodyWithImageParams(message, size, imageWidth, imageHeight);
}
public static void updateFileParams(Message message) {
updateFileParams(message, null);
}
public static void updateFileParams(Message message, URL url) {
DownloadableFile file = FileBackend.getFile(message);
int imageWidth = -1;
int imageHeight = -1;
if (message.getType() == Message.TYPE_IMAGE || file.getMimeType().startsWith("image/")) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
imageHeight = options.outHeight;
imageWidth = options.outWidth;
}
MessageUtil.updateMessageBodyWithFileParams(message, url, file.getSize(), imageWidth, imageHeight);
}
private static void updateMessageBodyWithFileParams(Message message, URL url, long fileSize, int imageWidth, int imageHeight) {
message.setBody(MessageUtil.getMessageBodyWithImageParams(url, fileSize, imageWidth, imageHeight));
}
private static void updateMessageBodyWithImageParams(Message message, long size, int imageWidth, int imageHeight) {
MessageUtil.updateMessageBodyWithImageParams(message, null, size, imageWidth, imageHeight);
}
private static void updateMessageBodyWithImageParams(Message message, URL url, long size, int imageWidth, int imageHeight) {
message.setBody(MessageUtil.getMessageBodyWithImageParams(url, size, imageWidth, imageHeight));
}
private static String getMessageBodyWithImageParams(URL url, long size, int imageWidth, int imageHeight) {
StringBuilder sb = new StringBuilder();
if (null != url) {
sb.append(url.toString());
sb.append('|');
}
sb.append(size);
if (-1 < imageWidth) {
sb.append('|');
sb.append(imageWidth);
}
if (-1 < imageHeight) {
sb.append('|');
sb.append(imageHeight);
}
return sb.toString();
}
private MessageUtil() {
// Static helper class
}
}
|