1package eu.siacs.conversations.utils;
2
3import java.net.URLConnection;
4import java.util.ArrayList;
5import java.util.Arrays;
6import java.util.Calendar;
7import java.util.Date;
8import java.util.Locale;
9
10import eu.siacs.conversations.R;
11import eu.siacs.conversations.entities.Contact;
12import eu.siacs.conversations.entities.Conversation;
13import eu.siacs.conversations.entities.Downloadable;
14import eu.siacs.conversations.entities.Message;
15import eu.siacs.conversations.xmpp.jid.Jid;
16
17import android.content.Context;
18import android.text.format.DateFormat;
19import android.text.format.DateUtils;
20import android.util.Pair;
21
22public class UIHelper {
23
24 private static String BLACK_HEART_SUIT = "\u2665";
25 private static String HEAVY_BLACK_HEART_SUIT = "\u2764";
26 private static String WHITE_HEART_SUIT = "\u2661";
27
28 public static final ArrayList<String> HEARTS = new ArrayList<>(Arrays.asList(BLACK_HEART_SUIT,HEAVY_BLACK_HEART_SUIT,WHITE_HEART_SUIT));
29
30 private static final ArrayList<String> LOCATION_QUESTIONS = new ArrayList<>(Arrays.asList(
31 "where are you", //en
32 "where are you now", //en
33 "where are you right now", //en
34 "whats your 20", //en
35 "what is your 20", //en
36 "what's your 20", //en
37 "whats your twenty", //en
38 "what is your twenty", //en
39 "what's your twenty", //en
40 "wo bist du", //de
41 "wo bist du jetzt", //de
42 "wo bist du gerade", //de
43 "wo seid ihr", //de
44 "wo seid ihr jetzt", //de
45 "wo seid ihr gerade", //de
46 "dónde estás", //es
47 "donde estas" //es
48 ));
49
50 private static final int SHORT_DATE_FLAGS = DateUtils.FORMAT_SHOW_DATE
51 | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
52 private static final int FULL_DATE_FLAGS = DateUtils.FORMAT_SHOW_TIME
53 | DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE;
54
55 public static String readableTimeDifference(Context context, long time) {
56 return readableTimeDifference(context, time, false);
57 }
58
59 public static String readableTimeDifferenceFull(Context context, long time) {
60 return readableTimeDifference(context, time, true);
61 }
62
63 private static String readableTimeDifference(Context context, long time,
64 boolean fullDate) {
65 if (time == 0) {
66 return context.getString(R.string.just_now);
67 }
68 Date date = new Date(time);
69 long difference = (System.currentTimeMillis() - time) / 1000;
70 if (difference < 60) {
71 return context.getString(R.string.just_now);
72 } else if (difference < 60 * 2) {
73 return context.getString(R.string.minute_ago);
74 } else if (difference < 60 * 15) {
75 return context.getString(R.string.minutes_ago,
76 Math.round(difference / 60.0));
77 } else if (today(date)) {
78 java.text.DateFormat df = DateFormat.getTimeFormat(context);
79 return df.format(date);
80 } else {
81 if (fullDate) {
82 return DateUtils.formatDateTime(context, date.getTime(),
83 FULL_DATE_FLAGS);
84 } else {
85 return DateUtils.formatDateTime(context, date.getTime(),
86 SHORT_DATE_FLAGS);
87 }
88 }
89 }
90
91 private static boolean today(Date date) {
92 return sameDay(date,new Date(System.currentTimeMillis()));
93 }
94
95 public static boolean sameDay(long timestamp1, long timestamp2) {
96 return sameDay(new Date(timestamp1),new Date(timestamp2));
97 }
98
99 private static boolean sameDay(Date a, Date b) {
100 Calendar cal1 = Calendar.getInstance();
101 Calendar cal2 = Calendar.getInstance();
102 cal1.setTime(a);
103 cal2.setTime(b);
104 return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
105 && cal1.get(Calendar.DAY_OF_YEAR) == cal2
106 .get(Calendar.DAY_OF_YEAR);
107 }
108
109 public static String lastseen(Context context, long time) {
110 if (time == 0) {
111 return context.getString(R.string.never_seen);
112 }
113 long difference = (System.currentTimeMillis() - time) / 1000;
114 if (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.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 Downloadable d = message.getDownloadable();
146 if (d != null ) {
147 switch (d.getStatus()) {
148 case Downloadable.STATUS_CHECKING:
149 return new Pair<>(context.getString(R.string.checking_image),true);
150 case Downloadable.STATUS_DOWNLOADING:
151 return new Pair<>(context.getString(R.string.receiving_x_file,
152 getFileDescriptionString(context,message),
153 d.getProgress()),true);
154 case Downloadable.STATUS_OFFER:
155 case Downloadable.STATUS_OFFER_CHECK_FILESIZE:
156 return new Pair<>(context.getString(R.string.x_file_offered_for_download,
157 getFileDescriptionString(context,message)),true);
158 case Downloadable.STATUS_DELETED:
159 return new Pair<>(context.getString(R.string.file_deleted),true);
160 case Downloadable.STATUS_FAILED:
161 return new Pair<>(context.getString(R.string.file_transmission_failed),true);
162 case Downloadable.STATUS_UPLOADING:
163 if (message.getStatus() == Message.STATUS_OFFERED) {
164 return new Pair<>(context.getString(R.string.offering_x_file,
165 getFileDescriptionString(context, message)), true);
166 } else {
167 return new Pair<>(context.getString(R.string.sending_x_file,
168 getFileDescriptionString(context, message)), true);
169 }
170 default:
171 return new Pair<>("",false);
172 }
173 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
174 return new Pair<>(context.getString(R.string.encrypted_message_received),true);
175 } else if (message.getType() == Message.TYPE_FILE || message.getType() == Message.TYPE_IMAGE) {
176 if (message.getStatus() == Message.STATUS_RECEIVED) {
177 return new Pair<>(context.getString(R.string.received_x_file,
178 getFileDescriptionString(context, message)), true);
179 } else {
180 return new Pair<>(getFileDescriptionString(context,message),true);
181 }
182 } else {
183 if (message.getBody().startsWith(Message.ME_COMMAND)) {
184 return new Pair<>(message.getBody().replaceAll("^" + Message.ME_COMMAND,
185 UIHelper.getMessageDisplayName(message) + " "), false);
186 } else if (GeoHelper.isGeoUri(message.getBody())) {
187 if (message.getStatus() == Message.STATUS_RECEIVED) {
188 return new Pair<>(context.getString(R.string.received_location),true);
189 } else {
190 return new Pair<>(context.getString(R.string.location), true);
191 }
192 } else{
193 return new Pair<>(message.getBody().trim(), false);
194 }
195 }
196 }
197
198 public static String getFileDescriptionString(final Context context, final Message message) {
199 if (message.getType() == Message.TYPE_IMAGE) {
200 return context.getString(R.string.image);
201 }
202 final String path = message.getRelativeFilePath();
203 if (path == null) {
204 return "";
205 }
206 final String mime;
207 try {
208 mime = URLConnection.guessContentTypeFromName(path.replace("#",""));
209 } catch (final StringIndexOutOfBoundsException ignored) {
210 return context.getString(R.string.file);
211 }
212 if (mime == null) {
213 return context.getString(R.string.file);
214 } else if (mime.startsWith("audio/")) {
215 return context.getString(R.string.audio);
216 } else if(mime.startsWith("video/")) {
217 return context.getString(R.string.video);
218 } else if (mime.startsWith("image/")) {
219 return context.getString(R.string.image);
220 } else if (mime.contains("pdf")) {
221 return context.getString(R.string.pdf_document) ;
222 } else if (mime.contains("application/vnd.android.package-archive")) {
223 return context.getString(R.string.apk) ;
224 } else if (mime.contains("vcard")) {
225 return context.getString(R.string.vcard) ;
226 } else {
227 return mime;
228 }
229 }
230
231 public static String getMessageDisplayName(final Message message) {
232 if (message.getStatus() == Message.STATUS_RECEIVED) {
233 if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
234 return getDisplayedMucCounterpart(message.getCounterpart());
235 } else {
236 final Contact contact = message.getContact();
237 return contact != null ? contact.getDisplayName() : "";
238 }
239 } else {
240 if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
241 return getDisplayedMucCounterpart(message.getConversation().getJid());
242 } else {
243 final Jid jid = message.getConversation().getAccount().getJid();
244 return jid.hasLocalpart() ? jid.getLocalpart() : jid.toDomainJid().toString();
245 }
246 }
247 }
248
249 private static String getDisplayedMucCounterpart(final Jid counterpart) {
250 if (counterpart==null) {
251 return "";
252 } else if (!counterpart.isBareJid()) {
253 return counterpart.getResourcepart().trim();
254 } else {
255 return counterpart.toString().trim();
256 }
257 }
258
259 public static boolean receivedLocationQuestion(Message message) {
260 if (message == null
261 || message.getStatus() != Message.STATUS_RECEIVED
262 || message.getType() != Message.TYPE_TEXT) {
263 return false;
264 }
265 String body = message.getBody() == null ? null : message.getBody().trim().toLowerCase(Locale.getDefault());
266 body = body.replace("?","").replace("¿","");
267 return LOCATION_QUESTIONS.contains(body);
268 }
269}