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