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