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