1package eu.siacs.conversations.utils;
2
3import android.content.Context;
4import android.text.format.DateFormat;
5import android.text.format.DateUtils;
6import android.util.Pair;
7
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.Calendar;
11import java.util.Date;
12import java.util.Locale;
13
14import eu.siacs.conversations.R;
15import eu.siacs.conversations.entities.Contact;
16import eu.siacs.conversations.entities.Conversation;
17import eu.siacs.conversations.entities.ListItem;
18import eu.siacs.conversations.entities.Message;
19import eu.siacs.conversations.entities.Presence;
20import eu.siacs.conversations.entities.Transferable;
21import eu.siacs.conversations.xmpp.jid.Jid;
22
23public class UIHelper {
24
25 private static String BLACK_HEART_SUIT = "\u2665";
26 private static String HEAVY_BLACK_HEART_SUIT = "\u2764";
27 private static String WHITE_HEART_SUIT = "\u2661";
28
29 public static final ArrayList<String> HEARTS = new ArrayList<>(Arrays.asList(BLACK_HEART_SUIT,HEAVY_BLACK_HEART_SUIT,WHITE_HEART_SUIT));
30
31 private static final ArrayList<String> LOCATION_QUESTIONS = new ArrayList<>(Arrays.asList(
32 "where are you", //en
33 "where are you now", //en
34 "where are you right now", //en
35 "whats your 20", //en
36 "what is your 20", //en
37 "what's your 20", //en
38 "whats your twenty", //en
39 "what is your twenty", //en
40 "what's your twenty", //en
41 "wo bist du", //de
42 "wo bist du jetzt", //de
43 "wo bist du gerade", //de
44 "wo seid ihr", //de
45 "wo seid ihr jetzt", //de
46 "wo seid ihr gerade", //de
47 "dónde estás", //es
48 "donde estas" //es
49 ));
50
51 private static final int SHORT_DATE_FLAGS = DateUtils.FORMAT_SHOW_DATE
52 | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
53 private static final int FULL_DATE_FLAGS = DateUtils.FORMAT_SHOW_TIME
54 | DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE;
55
56 public static String readableTimeDifference(Context context, long time) {
57 return readableTimeDifference(context, time, false);
58 }
59
60 public static String readableTimeDifferenceFull(Context context, long time) {
61 return readableTimeDifference(context, time, true);
62 }
63
64 private static String readableTimeDifference(Context context, long time,
65 boolean fullDate) {
66 if (time == 0) {
67 return context.getString(R.string.just_now);
68 }
69 Date date = new Date(time);
70 long difference = (System.currentTimeMillis() - time) / 1000;
71 if (difference < 60) {
72 return context.getString(R.string.just_now);
73 } else if (difference < 60 * 2) {
74 return context.getString(R.string.minute_ago);
75 } else if (difference < 60 * 15) {
76 return context.getString(R.string.minutes_ago,
77 Math.round(difference / 60.0));
78 } else if (today(date)) {
79 java.text.DateFormat df = DateFormat.getTimeFormat(context);
80 return df.format(date);
81 } else {
82 if (fullDate) {
83 return DateUtils.formatDateTime(context, date.getTime(),
84 FULL_DATE_FLAGS);
85 } else {
86 return DateUtils.formatDateTime(context, date.getTime(),
87 SHORT_DATE_FLAGS);
88 }
89 }
90 }
91
92 private static boolean today(Date date) {
93 return sameDay(date,new Date(System.currentTimeMillis()));
94 }
95
96 public static boolean sameDay(long timestamp1, long timestamp2) {
97 return sameDay(new Date(timestamp1),new Date(timestamp2));
98 }
99
100 private static boolean sameDay(Date a, Date b) {
101 Calendar cal1 = Calendar.getInstance();
102 Calendar cal2 = Calendar.getInstance();
103 cal1.setTime(a);
104 cal2.setTime(b);
105 return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
106 && cal1.get(Calendar.DAY_OF_YEAR) == cal2
107 .get(Calendar.DAY_OF_YEAR);
108 }
109
110 public static String lastseen(Context context, long time) {
111 if (time == 0) {
112 return context.getString(R.string.never_seen);
113 }
114 long difference = (System.currentTimeMillis() - time) / 1000;
115 if (difference < 60) {
116 return context.getString(R.string.last_seen_now);
117 } else if (difference < 60 * 2) {
118 return context.getString(R.string.last_seen_min);
119 } else if (difference < 60 * 60) {
120 return context.getString(R.string.last_seen_mins,
121 Math.round(difference / 60.0));
122 } else if (difference < 60 * 60 * 2) {
123 return context.getString(R.string.last_seen_hour);
124 } else if (difference < 60 * 60 * 24) {
125 return context.getString(R.string.last_seen_hours,
126 Math.round(difference / (60.0 * 60.0)));
127 } else if (difference < 60 * 60 * 48) {
128 return context.getString(R.string.last_seen_day);
129 } else {
130 return context.getString(R.string.last_seen_days,
131 Math.round(difference / (60.0 * 60.0 * 24.0)));
132 }
133 }
134
135 public static int getColorForName(String name) {
136 if (name == null || name.isEmpty()) {
137 return 0xFF202020;
138 }
139 int colors[] = {0xFFe91e63, 0xFF9c27b0, 0xFF673ab7, 0xFF3f51b5,
140 0xFF5677fc, 0xFF03a9f4, 0xFF00bcd4, 0xFF009688, 0xFFff5722,
141 0xFF795548, 0xFF607d8b};
142 return colors[(int) ((name.hashCode() & 0xffffffffl) % colors.length)];
143 }
144
145 public static Pair<String,Boolean> getMessagePreview(final Context context, final Message message) {
146 final Transferable d = message.getTransferable();
147 if (d != null ) {
148 switch (d.getStatus()) {
149 case Transferable.STATUS_CHECKING:
150 return new Pair<>(context.getString(R.string.checking_x,
151 getFileDescriptionString(context,message)),true);
152 case Transferable.STATUS_DOWNLOADING:
153 return new Pair<>(context.getString(R.string.receiving_x_file,
154 getFileDescriptionString(context,message),
155 d.getProgress()),true);
156 case Transferable.STATUS_OFFER:
157 case Transferable.STATUS_OFFER_CHECK_FILESIZE:
158 return new Pair<>(context.getString(R.string.x_file_offered_for_download,
159 getFileDescriptionString(context,message)),true);
160 case Transferable.STATUS_DELETED:
161 return new Pair<>(context.getString(R.string.file_deleted),true);
162 case Transferable.STATUS_FAILED:
163 return new Pair<>(context.getString(R.string.file_transmission_failed),true);
164 case Transferable.STATUS_UPLOADING:
165 if (message.getStatus() == Message.STATUS_OFFERED) {
166 return new Pair<>(context.getString(R.string.offering_x_file,
167 getFileDescriptionString(context, message)), true);
168 } else {
169 return new Pair<>(context.getString(R.string.sending_x_file,
170 getFileDescriptionString(context, message)), true);
171 }
172 default:
173 return new Pair<>("",false);
174 }
175 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
176 return new Pair<>(context.getString(R.string.pgp_message),true);
177 } else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
178 return new Pair<>(context.getString(R.string.decryption_failed), true);
179 } else if (message.getType() == Message.TYPE_FILE || message.getType() == Message.TYPE_IMAGE) {
180 if (message.getStatus() == Message.STATUS_RECEIVED) {
181 return new Pair<>(context.getString(R.string.received_x_file,
182 getFileDescriptionString(context, message)), true);
183 } else {
184 return new Pair<>(getFileDescriptionString(context,message),true);
185 }
186 } else {
187 if (message.getBody().startsWith(Message.ME_COMMAND)) {
188 return new Pair<>(message.getBody().replaceAll("^" + Message.ME_COMMAND,
189 UIHelper.getMessageDisplayName(message) + " "), false);
190 } else if (GeoHelper.isGeoUri(message.getBody())) {
191 if (message.getStatus() == Message.STATUS_RECEIVED) {
192 return new Pair<>(context.getString(R.string.received_location), true);
193 } else {
194 return new Pair<>(context.getString(R.string.location), true);
195 }
196 } else if (message.treatAsDownloadable() == Message.Decision.MUST) {
197 return new Pair<>(context.getString(R.string.x_file_offered_for_download,
198 getFileDescriptionString(context,message)),true);
199 } else{
200 return new Pair<>(message.getBody().trim(), false);
201 }
202 }
203 }
204
205 public static String getFileDescriptionString(final Context context, final Message message) {
206 if (message.getType() == Message.TYPE_IMAGE) {
207 return context.getString(R.string.image);
208 }
209 final String mime = message.getMimeType();
210 if (mime == null) {
211 return context.getString(R.string.file);
212 } else if (mime.startsWith("audio/")) {
213 return context.getString(R.string.audio);
214 } else if(mime.startsWith("video/")) {
215 return context.getString(R.string.video);
216 } else if (mime.startsWith("image/")) {
217 return context.getString(R.string.image);
218 } else if (mime.contains("pdf")) {
219 return context.getString(R.string.pdf_document) ;
220 } else if (mime.contains("application/vnd.android.package-archive")) {
221 return context.getString(R.string.apk) ;
222 } else if (mime.contains("vcard")) {
223 return context.getString(R.string.vcard) ;
224 } else {
225 return mime;
226 }
227 }
228
229 public static String getMessageDisplayName(final Message message) {
230 final Conversation conversation = message.getConversation();
231 if (message.getStatus() == Message.STATUS_RECEIVED) {
232 final Contact contact = message.getContact();
233 if (conversation.getMode() == Conversation.MODE_MULTI) {
234 if (contact != null) {
235 return contact.getDisplayName();
236 } else {
237 return getDisplayedMucCounterpart(message.getCounterpart());
238 }
239 } else {
240 return contact != null ? contact.getDisplayName() : "";
241 }
242 } else {
243 if (conversation.getMode() == Conversation.MODE_MULTI) {
244 return conversation.getMucOptions().getSelf().getName();
245 } else {
246 final Jid jid = conversation.getAccount().getJid();
247 return jid.hasLocalpart() ? jid.getLocalpart() : jid.toDomainJid().toString();
248 }
249 }
250 }
251
252 public static String getDisplayedMucCounterpart(final Jid counterpart) {
253 if (counterpart==null) {
254 return "";
255 } else if (!counterpart.isBareJid()) {
256 return counterpart.getResourcepart().trim();
257 } else {
258 return counterpart.toString().trim();
259 }
260 }
261
262 public static boolean receivedLocationQuestion(Message message) {
263 if (message == null
264 || message.getStatus() != Message.STATUS_RECEIVED
265 || message.getType() != Message.TYPE_TEXT) {
266 return false;
267 }
268 String body = message.getBody() == null ? null : message.getBody().trim().toLowerCase(Locale.getDefault());
269 body = body.replace("?","").replace("¿","");
270 return LOCATION_QUESTIONS.contains(body);
271 }
272
273 public static ListItem.Tag getTagForStatus(Context context, Presence.Status status) {
274 switch (status) {
275 case CHAT:
276 return new ListItem.Tag(context.getString(R.string.presence_chat), 0xff259b24);
277 case AWAY:
278 return new ListItem.Tag(context.getString(R.string.presence_away), 0xffff9800);
279 case XA:
280 return new ListItem.Tag(context.getString(R.string.presence_xa), 0xfff44336);
281 case DND:
282 return new ListItem.Tag(context.getString(R.string.presence_dnd), 0xfff44336);
283 default:
284 return new ListItem.Tag(context.getString(R.string.presence_online), 0xff259b24);
285 }
286 }
287}