1package eu.siacs.conversations.utils;
2
3import android.content.Context;
4import android.text.format.DateFormat;
5import android.text.format.DateUtils;
6import android.util.DisplayMetrics;
7import android.util.Pair;
8import android.view.View;
9import android.widget.LinearLayout;
10import android.widget.RelativeLayout;
11
12import java.util.ArrayList;
13import java.util.Arrays;
14import java.util.Calendar;
15import java.util.Date;
16import java.util.Locale;
17
18import eu.siacs.conversations.R;
19import eu.siacs.conversations.entities.Contact;
20import eu.siacs.conversations.entities.Conversation;
21import eu.siacs.conversations.entities.Message;
22import eu.siacs.conversations.entities.Transferable;
23import eu.siacs.conversations.xmpp.jid.Jid;
24
25public class UIHelper {
26
27 private static String BLACK_HEART_SUIT = "\u2665";
28 private static String HEAVY_BLACK_HEART_SUIT = "\u2764";
29 private static String WHITE_HEART_SUIT = "\u2661";
30
31 public static final ArrayList<String> HEARTS = new ArrayList<>(Arrays.asList(BLACK_HEART_SUIT,HEAVY_BLACK_HEART_SUIT,WHITE_HEART_SUIT));
32
33 private static final ArrayList<String> LOCATION_QUESTIONS = new ArrayList<>(Arrays.asList(
34 "where are you", //en
35 "where are you now", //en
36 "where are you right now", //en
37 "whats your 20", //en
38 "what is your 20", //en
39 "what's your 20", //en
40 "whats your twenty", //en
41 "what is your twenty", //en
42 "what's your twenty", //en
43 "wo bist du", //de
44 "wo bist du jetzt", //de
45 "wo bist du gerade", //de
46 "wo seid ihr", //de
47 "wo seid ihr jetzt", //de
48 "wo seid ihr gerade", //de
49 "dónde estás", //es
50 "donde estas" //es
51 ));
52
53 private static final int SHORT_DATE_FLAGS = DateUtils.FORMAT_SHOW_DATE
54 | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
55 private static final int FULL_DATE_FLAGS = DateUtils.FORMAT_SHOW_TIME
56 | DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE;
57
58 public static String readableTimeDifference(Context context, long time) {
59 return readableTimeDifference(context, time, false);
60 }
61
62 public static String readableTimeDifferenceFull(Context context, long time) {
63 return readableTimeDifference(context, time, true);
64 }
65
66 private static String readableTimeDifference(Context context, long time,
67 boolean fullDate) {
68 if (time == 0) {
69 return context.getString(R.string.just_now);
70 }
71 Date date = new Date(time);
72 long difference = (System.currentTimeMillis() - time) / 1000;
73 if (difference < 60) {
74 return context.getString(R.string.just_now);
75 } else if (difference < 60 * 2) {
76 return context.getString(R.string.minute_ago);
77 } else if (difference < 60 * 15) {
78 return context.getString(R.string.minutes_ago,
79 Math.round(difference / 60.0));
80 } else if (today(date)) {
81 java.text.DateFormat df = DateFormat.getTimeFormat(context);
82 return df.format(date);
83 } else {
84 if (fullDate) {
85 return DateUtils.formatDateTime(context, date.getTime(),
86 FULL_DATE_FLAGS);
87 } else {
88 return DateUtils.formatDateTime(context, date.getTime(),
89 SHORT_DATE_FLAGS);
90 }
91 }
92 }
93
94 private static boolean today(Date date) {
95 return sameDay(date,new Date(System.currentTimeMillis()));
96 }
97
98 public static boolean sameDay(long timestamp1, long timestamp2) {
99 return sameDay(new Date(timestamp1),new Date(timestamp2));
100 }
101
102 private static boolean sameDay(Date a, Date b) {
103 Calendar cal1 = Calendar.getInstance();
104 Calendar cal2 = Calendar.getInstance();
105 cal1.setTime(a);
106 cal2.setTime(b);
107 return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
108 && cal1.get(Calendar.DAY_OF_YEAR) == cal2
109 .get(Calendar.DAY_OF_YEAR);
110 }
111
112 public static String lastseen(Context context, long time) {
113 if (time == 0) {
114 return context.getString(R.string.never_seen);
115 }
116 long difference = (System.currentTimeMillis() - time) / 1000;
117 if (difference < 60) {
118 return context.getString(R.string.last_seen_now);
119 } else if (difference < 60 * 2) {
120 return context.getString(R.string.last_seen_min);
121 } else if (difference < 60 * 60) {
122 return context.getString(R.string.last_seen_mins,
123 Math.round(difference / 60.0));
124 } else if (difference < 60 * 60 * 2) {
125 return context.getString(R.string.last_seen_hour);
126 } else if (difference < 60 * 60 * 24) {
127 return context.getString(R.string.last_seen_hours,
128 Math.round(difference / (60.0 * 60.0)));
129 } else if (difference < 60 * 60 * 48) {
130 return context.getString(R.string.last_seen_day);
131 } else {
132 return context.getString(R.string.last_seen_days,
133 Math.round(difference / (60.0 * 60.0 * 24.0)));
134 }
135 }
136
137 public static int getColorForName(String name) {
138 if (name.isEmpty()) {
139 return 0xFF202020;
140 }
141 int colors[] = {0xFFe91e63, 0xFF9c27b0, 0xFF673ab7, 0xFF3f51b5,
142 0xFF5677fc, 0xFF03a9f4, 0xFF00bcd4, 0xFF009688, 0xFFff5722,
143 0xFF795548, 0xFF607d8b};
144 return colors[(int) ((name.hashCode() & 0xffffffffl) % colors.length)];
145 }
146
147 public static Pair<String,Boolean> getMessagePreview(final Context context, final Message message) {
148 final Transferable d = message.getTransferable();
149 if (d != null ) {
150 switch (d.getStatus()) {
151 case Transferable.STATUS_CHECKING:
152 return new Pair<>(context.getString(R.string.checking_x,
153 getFileDescriptionString(context,message)),true);
154 case Transferable.STATUS_DOWNLOADING:
155 return new Pair<>(context.getString(R.string.receiving_x_file,
156 getFileDescriptionString(context,message),
157 d.getProgress()),true);
158 case Transferable.STATUS_OFFER:
159 case Transferable.STATUS_OFFER_CHECK_FILESIZE:
160 return new Pair<>(context.getString(R.string.x_file_offered_for_download,
161 getFileDescriptionString(context,message)),true);
162 case Transferable.STATUS_DELETED:
163 return new Pair<>(context.getString(R.string.file_deleted),true);
164 case Transferable.STATUS_FAILED:
165 return new Pair<>(context.getString(R.string.file_transmission_failed),true);
166 case Transferable.STATUS_UPLOADING:
167 if (message.getStatus() == Message.STATUS_OFFERED) {
168 return new Pair<>(context.getString(R.string.offering_x_file,
169 getFileDescriptionString(context, message)), true);
170 } else {
171 return new Pair<>(context.getString(R.string.sending_x_file,
172 getFileDescriptionString(context, message)), true);
173 }
174 default:
175 return new Pair<>("",false);
176 }
177 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
178 return new Pair<>(context.getString(R.string.encrypted_message_received),true);
179 } else if (message.getType() == Message.TYPE_FILE || message.getType() == Message.TYPE_IMAGE) {
180 if (message.getStatus() == Message.STATUS_RECEIVED) {
181 return new Pair<>(context.getString(R.string.received_x_file,
182 getFileDescriptionString(context, message)), true);
183 } else {
184 return new Pair<>(getFileDescriptionString(context,message),true);
185 }
186 } else {
187 if (message.getBody().startsWith(Message.ME_COMMAND)) {
188 return new Pair<>(message.getBody().replaceAll("^" + Message.ME_COMMAND,
189 UIHelper.getMessageDisplayName(message) + " "), false);
190 } else if (GeoHelper.isGeoUri(message.getBody())) {
191 if (message.getStatus() == Message.STATUS_RECEIVED) {
192 return new Pair<>(context.getString(R.string.received_location),true);
193 } else {
194 return new Pair<>(context.getString(R.string.location), true);
195 }
196 } else{
197 return new Pair<>(message.getBody().trim(), false);
198 }
199 }
200 }
201
202 public static String getFileDescriptionString(final Context context, final Message message) {
203 if (message.getType() == Message.TYPE_IMAGE) {
204 return context.getString(R.string.image);
205 }
206 final String mime = message.getMimeType();
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 final Contact contact = message.getContact();
229 if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
230 if (contact != null) {
231 return contact.getDisplayName();
232 } else {
233 return getDisplayedMucCounterpart(message.getCounterpart());
234 }
235 } else {
236 return contact != null ? contact.getDisplayName() : "";
237 }
238 } else {
239 if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
240 return getDisplayedMucCounterpart(message.getConversation().getJid());
241 } else {
242 final Jid jid = message.getConversation().getAccount().getJid();
243 return jid.hasLocalpart() ? jid.getLocalpart() : jid.toDomainJid().toString();
244 }
245 }
246 }
247
248 private static String getDisplayedMucCounterpart(final Jid counterpart) {
249 if (counterpart==null) {
250 return "";
251 } else if (!counterpart.isBareJid()) {
252 return counterpart.getResourcepart().trim();
253 } else {
254 return counterpart.toString().trim();
255 }
256 }
257
258 public static boolean receivedLocationQuestion(Message message) {
259 if (message == null
260 || message.getStatus() != Message.STATUS_RECEIVED
261 || message.getType() != Message.TYPE_TEXT) {
262 return false;
263 }
264 String body = message.getBody() == null ? null : message.getBody().trim().toLowerCase(Locale.getDefault());
265 body = body.replace("?","").replace("¿","");
266 return LOCATION_QUESTIONS.contains(body);
267 }
268
269 public static void resetChildMargins(LinearLayout view) {
270 int childCount = view.getChildCount();
271 for (int i = 0; i < childCount; i++) {
272 UIHelper.resetMargins(view.getChildAt(i));
273 }
274 }
275
276 private static void resetMargins(View view) {
277 LinearLayout.MarginLayoutParams marginLayoutParams = new LinearLayout.MarginLayoutParams(view.getLayoutParams());
278 marginLayoutParams.setMargins(view.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin),
279 view.getResources().getDimensionPixelSize(R.dimen.activity_vertical_margin),
280 view.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin),
281 view.getResources().getDimensionPixelSize(R.dimen.activity_vertical_margin));
282 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(marginLayoutParams);
283 view.setLayoutParams(layoutParams);
284 }
285}