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