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 r u?", //en
33 "whats your 20?", //en
34 "what is your 20?", //en
35 "what's your 20?", //en
36 "whats your twenty?", //en
37 "what is your twenty?", //en
38 "what's your twenty?", //en
39 "wo bist du?", //de
40 "wo sind sie?" //de
41 ));
42
43 private static final int SHORT_DATE_FLAGS = DateUtils.FORMAT_SHOW_DATE
44 | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
45 private static final int FULL_DATE_FLAGS = DateUtils.FORMAT_SHOW_TIME
46 | DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE;
47
48 public static String readableTimeDifference(Context context, long time) {
49 return readableTimeDifference(context, time, false);
50 }
51
52 public static String readableTimeDifferenceFull(Context context, long time) {
53 return readableTimeDifference(context, time, true);
54 }
55
56 private static String readableTimeDifference(Context context, long time,
57 boolean fullDate) {
58 if (time == 0) {
59 return context.getString(R.string.just_now);
60 }
61 Date date = new Date(time);
62 long difference = (System.currentTimeMillis() - time) / 1000;
63 if (difference < 60) {
64 return context.getString(R.string.just_now);
65 } else if (difference < 60 * 2) {
66 return context.getString(R.string.minute_ago);
67 } else if (difference < 60 * 15) {
68 return context.getString(R.string.minutes_ago,
69 Math.round(difference / 60.0));
70 } else if (today(date)) {
71 java.text.DateFormat df = DateFormat.getTimeFormat(context);
72 return df.format(date);
73 } else {
74 if (fullDate) {
75 return DateUtils.formatDateTime(context, date.getTime(),
76 FULL_DATE_FLAGS);
77 } else {
78 return DateUtils.formatDateTime(context, date.getTime(),
79 SHORT_DATE_FLAGS);
80 }
81 }
82 }
83
84 private static boolean today(Date date) {
85 return sameDay(date,new Date(System.currentTimeMillis()));
86 }
87
88 public static boolean sameDay(long timestamp1, long timestamp2) {
89 return sameDay(new Date(timestamp1),new Date(timestamp2));
90 }
91
92 private static boolean sameDay(Date a, Date b) {
93 Calendar cal1 = Calendar.getInstance();
94 Calendar cal2 = Calendar.getInstance();
95 cal1.setTime(a);
96 cal2.setTime(b);
97 return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
98 && cal1.get(Calendar.DAY_OF_YEAR) == cal2
99 .get(Calendar.DAY_OF_YEAR);
100 }
101
102 public static String lastseen(Context context, long time) {
103 if (time == 0) {
104 return context.getString(R.string.never_seen);
105 }
106 long difference = (System.currentTimeMillis() - time) / 1000;
107 if (difference < 60) {
108 return context.getString(R.string.last_seen_now);
109 } else if (difference < 60 * 2) {
110 return context.getString(R.string.last_seen_min);
111 } else if (difference < 60 * 60) {
112 return context.getString(R.string.last_seen_mins,
113 Math.round(difference / 60.0));
114 } else if (difference < 60 * 60 * 2) {
115 return context.getString(R.string.last_seen_hour);
116 } else if (difference < 60 * 60 * 24) {
117 return context.getString(R.string.last_seen_hours,
118 Math.round(difference / (60.0 * 60.0)));
119 } else if (difference < 60 * 60 * 48) {
120 return context.getString(R.string.last_seen_day);
121 } else {
122 return context.getString(R.string.last_seen_days,
123 Math.round(difference / (60.0 * 60.0 * 24.0)));
124 }
125 }
126
127 public static int getColorForName(String name) {
128 if (name.isEmpty()) {
129 return 0xFF202020;
130 }
131 int colors[] = {0xFFe91e63, 0xFF9c27b0, 0xFF673ab7, 0xFF3f51b5,
132 0xFF5677fc, 0xFF03a9f4, 0xFF00bcd4, 0xFF009688, 0xFFff5722,
133 0xFF795548, 0xFF607d8b};
134 return colors[(int) ((name.hashCode() & 0xffffffffl) % colors.length)];
135 }
136
137 public static Pair<String,Boolean> getMessagePreview(final Context context, final Message message) {
138 final Downloadable d = message.getDownloadable();
139 if (d != null ) {
140 switch (d.getStatus()) {
141 case Downloadable.STATUS_CHECKING:
142 return new Pair<>(context.getString(R.string.checking_image),true);
143 case Downloadable.STATUS_DOWNLOADING:
144 return new Pair<>(context.getString(R.string.receiving_x_file,
145 getFileDescriptionString(context,message),
146 d.getProgress()),true);
147 case Downloadable.STATUS_OFFER:
148 case Downloadable.STATUS_OFFER_CHECK_FILESIZE:
149 return new Pair<>(context.getString(R.string.x_file_offered_for_download,
150 getFileDescriptionString(context,message)),true);
151 case Downloadable.STATUS_DELETED:
152 return new Pair<>(context.getString(R.string.file_deleted),true);
153 case Downloadable.STATUS_FAILED:
154 return new Pair<>(context.getString(R.string.file_transmission_failed),true);
155 case Downloadable.STATUS_UPLOADING:
156 if (message.getStatus() == Message.STATUS_OFFERED) {
157 return new Pair<>(context.getString(R.string.offering_x_file,
158 getFileDescriptionString(context, message)), true);
159 } else {
160 return new Pair<>(context.getString(R.string.sending_x_file,
161 getFileDescriptionString(context, message)), true);
162 }
163 default:
164 return new Pair<>("",false);
165 }
166 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
167 return new Pair<>(context.getString(R.string.encrypted_message_received),true);
168 } else if (message.getType() == Message.TYPE_FILE || message.getType() == Message.TYPE_IMAGE) {
169 if (message.getStatus() == Message.STATUS_RECEIVED) {
170 return new Pair<>(context.getString(R.string.received_x_file,
171 getFileDescriptionString(context, message)), true);
172 } else {
173 return new Pair<>(getFileDescriptionString(context,message),true);
174 }
175 } else {
176 if (message.getBody().startsWith(Message.ME_COMMAND)) {
177 return new Pair<>(message.getBody().replaceAll("^" + Message.ME_COMMAND,
178 UIHelper.getMessageDisplayName(message) + " "), false);
179 } else if (GeoHelper.isGeoUri(message.getBody())) {
180 if (message.getStatus() == Message.STATUS_RECEIVED) {
181 return new Pair<>(context.getString(R.string.received_location),true);
182 } else {
183 return new Pair<>(context.getString(R.string.location), true);
184 }
185 } else{
186 return new Pair<>(message.getBody().trim(), false);
187 }
188 }
189 }
190
191 public static String getFileDescriptionString(final Context context, final Message message) {
192 if (message.getType() == Message.TYPE_IMAGE) {
193 return context.getString(R.string.image);
194 }
195 final String path = message.getRelativeFilePath();
196 if (path == null) {
197 return "";
198 }
199 final String mime;
200 try {
201 mime = URLConnection.guessContentTypeFromName(path.replace("#",""));
202 } catch (final StringIndexOutOfBoundsException ignored) {
203 return context.getString(R.string.file);
204 }
205 if (mime == null) {
206 return context.getString(R.string.file);
207 } else if (mime.startsWith("audio/")) {
208 return context.getString(R.string.audio);
209 } else if(mime.startsWith("video/")) {
210 return context.getString(R.string.video);
211 } else if (mime.startsWith("image/")) {
212 return context.getString(R.string.image);
213 } else if (mime.contains("pdf")) {
214 return context.getString(R.string.pdf_document) ;
215 } else if (mime.contains("application/vnd.android.package-archive")) {
216 return context.getString(R.string.apk) ;
217 } else if (mime.contains("vcard")) {
218 return context.getString(R.string.vcard) ;
219 } else {
220 return mime;
221 }
222 }
223
224 public static String getMessageDisplayName(final Message message) {
225 if (message.getStatus() == Message.STATUS_RECEIVED) {
226 if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
227 return getDisplayedMucCounterpart(message.getCounterpart());
228 } else {
229 final Contact contact = message.getContact();
230 return contact != null ? contact.getDisplayName() : "";
231 }
232 } else {
233 if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
234 return getDisplayedMucCounterpart(message.getConversation().getJid());
235 } else {
236 final Jid jid = message.getConversation().getAccount().getJid();
237 return jid.hasLocalpart() ? jid.getLocalpart() : jid.toDomainJid().toString();
238 }
239 }
240 }
241
242 private static String getDisplayedMucCounterpart(final Jid counterpart) {
243 if (counterpart==null) {
244 return "";
245 } else if (!counterpart.isBareJid()) {
246 return counterpart.getResourcepart().trim();
247 } else {
248 return counterpart.toString().trim();
249 }
250 }
251
252 public static boolean receivedLocationQuestion(Message message) {
253 if (message == null
254 || message.getStatus() != Message.STATUS_RECEIVED
255 || message.getType() != Message.TYPE_TEXT) {
256 return false;
257 }
258 String body = message.getBody() == null ? null : message.getBody().trim().toLowerCase(Locale.getDefault());
259 return LOCATION_QUESTIONS.contains(body);
260 }
261}