UIHelper.java

  1package eu.siacs.conversations.utils;
  2
  3import android.content.Context;
  4import android.text.format.DateFormat;
  5import android.text.format.DateUtils;
  6import android.util.Pair;
  7
  8import java.util.ArrayList;
  9import java.util.Arrays;
 10import java.util.Calendar;
 11import java.util.Date;
 12import java.util.Locale;
 13
 14import eu.siacs.conversations.Config;
 15import eu.siacs.conversations.R;
 16import eu.siacs.conversations.crypto.axolotl.AxolotlService;
 17import eu.siacs.conversations.entities.Contact;
 18import eu.siacs.conversations.entities.Conversation;
 19import eu.siacs.conversations.entities.ListItem;
 20import eu.siacs.conversations.entities.Message;
 21import eu.siacs.conversations.entities.Presence;
 22import eu.siacs.conversations.entities.Transferable;
 23import eu.siacs.conversations.ui.XmppActivity;
 24import eu.siacs.conversations.xmpp.jid.Jid;
 25
 26public class UIHelper {
 27
 28	private static String BLACK_HEART_SUIT = "\u2665";
 29	private static String HEAVY_BLACK_HEART_SUIT = "\u2764";
 30	private static String WHITE_HEART_SUIT = "\u2661";
 31
 32	public static final ArrayList<String> HEARTS = new ArrayList<>(Arrays.asList(BLACK_HEART_SUIT,HEAVY_BLACK_HEART_SUIT,WHITE_HEART_SUIT));
 33
 34	private static final ArrayList<String> LOCATION_QUESTIONS = new ArrayList<>(Arrays.asList(
 35			"where are you", //en
 36			"where are you now", //en
 37			"where are you right now", //en
 38			"whats your 20", //en
 39			"what is your 20", //en
 40			"what's your 20", //en
 41			"whats your twenty", //en
 42			"what is your twenty", //en
 43			"what's your twenty", //en
 44			"wo bist du", //de
 45			"wo bist du jetzt", //de
 46			"wo bist du gerade", //de
 47			"wo seid ihr", //de
 48			"wo seid ihr jetzt", //de
 49			"wo seid ihr gerade", //de
 50			"dónde estás", //es
 51			"donde estas" //es
 52		));
 53
 54	private static final int SHORT_DATE_FLAGS = DateUtils.FORMAT_SHOW_DATE
 55		| DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
 56	private static final int FULL_DATE_FLAGS = DateUtils.FORMAT_SHOW_TIME
 57		| DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE;
 58
 59	public static String readableTimeDifference(Context context, long time) {
 60		return readableTimeDifference(context, time, false);
 61	}
 62
 63	public static String readableTimeDifferenceFull(Context context, long time) {
 64		return readableTimeDifference(context, time, true);
 65	}
 66
 67	private static String readableTimeDifference(Context context, long time,
 68			boolean fullDate) {
 69		if (time == 0) {
 70			return context.getString(R.string.just_now);
 71		}
 72		Date date = new Date(time);
 73		long difference = (System.currentTimeMillis() - time) / 1000;
 74		if (difference < 60) {
 75			return context.getString(R.string.just_now);
 76		} else if (difference < 60 * 2) {
 77			return context.getString(R.string.minute_ago);
 78		} else if (difference < 60 * 15) {
 79			return context.getString(R.string.minutes_ago,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	private static boolean sameDay(Date a, Date b) {
 99		Calendar cal1 = Calendar.getInstance();
100		Calendar cal2 = Calendar.getInstance();
101		cal1.setTime(a);
102		cal2.setTime(b);
103		return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
104			&& cal1.get(Calendar.DAY_OF_YEAR) == cal2
105			.get(Calendar.DAY_OF_YEAR);
106	}
107
108	public static String lastseen(Context context, boolean active, long time) {
109		long difference = (System.currentTimeMillis() - time) / 1000;
110		active = active && difference <= 300;
111		if (active || difference < 60) {
112			return context.getString(R.string.last_seen_now);
113		} else if (difference < 60 * 2) {
114			return context.getString(R.string.last_seen_min);
115		} else if (difference < 60 * 60) {
116			return context.getString(R.string.last_seen_mins,
117					Math.round(difference / 60.0));
118		} else if (difference < 60 * 60 * 2) {
119			return context.getString(R.string.last_seen_hour);
120		} else if (difference < 60 * 60 * 24) {
121			return context.getString(R.string.last_seen_hours,
122					Math.round(difference / (60.0 * 60.0)));
123		} else if (difference < 60 * 60 * 48) {
124			return context.getString(R.string.last_seen_day);
125		} else {
126			return context.getString(R.string.last_seen_days,
127					Math.round(difference / (60.0 * 60.0 * 24.0)));
128		}
129	}
130
131	public static int getColorForName(String name) {
132		if (name == null || name.isEmpty()) {
133			return 0xFF202020;
134		}
135		int colors[] = {0xFFe91e63, 0xFF9c27b0, 0xFF673ab7, 0xFF3f51b5,
136			0xFF5677fc, 0xFF03a9f4, 0xFF00bcd4, 0xFF009688, 0xFFff5722,
137			0xFF795548, 0xFF607d8b};
138		return colors[(int) ((name.hashCode() & 0xffffffffl) % colors.length)];
139	}
140
141	public static Pair<String,Boolean> getMessagePreview(final Context context, final Message message) {
142		final Transferable d = message.getTransferable();
143		if (d != null ) {
144			switch (d.getStatus()) {
145				case Transferable.STATUS_CHECKING:
146					return new Pair<>(context.getString(R.string.checking_x,
147									getFileDescriptionString(context,message)),true);
148				case Transferable.STATUS_DOWNLOADING:
149					return new Pair<>(context.getString(R.string.receiving_x_file,
150									getFileDescriptionString(context,message),
151									d.getProgress()),true);
152				case Transferable.STATUS_OFFER:
153				case Transferable.STATUS_OFFER_CHECK_FILESIZE:
154					return new Pair<>(context.getString(R.string.x_file_offered_for_download,
155									getFileDescriptionString(context,message)),true);
156				case Transferable.STATUS_DELETED:
157					return new Pair<>(context.getString(R.string.file_deleted),true);
158				case Transferable.STATUS_FAILED:
159					return new Pair<>(context.getString(R.string.file_transmission_failed),true);
160				case Transferable.STATUS_UPLOADING:
161					if (message.getStatus() == Message.STATUS_OFFERED) {
162						return new Pair<>(context.getString(R.string.offering_x_file,
163								getFileDescriptionString(context, message)), true);
164					} else {
165						return new Pair<>(context.getString(R.string.sending_x_file,
166								getFileDescriptionString(context, message)), true);
167					}
168				default:
169					return new Pair<>("",false);
170			}
171		} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
172			return new Pair<>(context.getString(R.string.pgp_message),true);
173		} else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
174			return new Pair<>(context.getString(R.string.decryption_failed), true);
175		} else if (message.getType() == Message.TYPE_FILE || message.getType() == Message.TYPE_IMAGE) {
176			if (message.getStatus() == Message.STATUS_RECEIVED) {
177				return new Pair<>(context.getString(R.string.received_x_file,
178							getFileDescriptionString(context, message)), true);
179			} else {
180				return new Pair<>(getFileDescriptionString(context,message),true);
181			}
182		} else {
183			final String body = message.getBody();
184			if (body.startsWith(Message.ME_COMMAND)) {
185				return new Pair<>(body.replaceAll("^" + Message.ME_COMMAND,
186						UIHelper.getMessageDisplayName(message) + " "), false);
187			} else if (GeoHelper.isGeoUri(message.getBody())) {
188				if (message.getStatus() == Message.STATUS_RECEIVED) {
189					return new Pair<>(context.getString(R.string.received_location), true);
190				} else {
191					return new Pair<>(context.getString(R.string.location), true);
192				}
193			} else if (message.treatAsDownloadable() == Message.Decision.MUST) {
194				return new Pair<>(context.getString(R.string.x_file_offered_for_download,
195						getFileDescriptionString(context,message)),true);
196			} else {
197				String[] lines = body.split("\n");
198				StringBuilder builder = new StringBuilder();
199				for(String l : lines) {
200					if (l.length() > 0) {
201						char first = l.charAt(0);
202						if (first != '>' && first != '\u00bb') {
203							String line = l.trim();
204							if (line.isEmpty()) {
205								continue;
206							}
207							char last = line.charAt(line.length()-1);
208							if (builder.length() != 0) {
209								builder.append(' ');
210							}
211							builder.append(line);
212							if (last != '.' && last != '!' && last != '?' && last != ',') {
213								break;
214							}
215						}
216					}
217				}
218				if (builder.length() == 0) {
219					builder.append(body.trim());
220				}
221				return new Pair<>(builder.length() > 256 ? builder.substring(0,256) : builder.toString(), false);
222			}
223		}
224	}
225
226	public static String getFileDescriptionString(final Context context, final Message message) {
227		if (message.getType() == Message.TYPE_IMAGE) {
228			return context.getString(R.string.image);
229		}
230		final String mime = message.getMimeType();
231		if (mime == null) {
232			return context.getString(R.string.file);
233		} else if (mime.startsWith("audio/")) {
234			return context.getString(R.string.audio);
235		} else if(mime.startsWith("video/")) {
236			return context.getString(R.string.video);
237		} else if (mime.startsWith("image/")) {
238			return context.getString(R.string.image);
239		} else if (mime.contains("pdf")) {
240			return context.getString(R.string.pdf_document)	;
241		} else if (mime.contains("application/vnd.android.package-archive")) {
242			return context.getString(R.string.apk)	;
243		} else if (mime.contains("vcard")) {
244			return context.getString(R.string.vcard)	;
245		} else {
246			return mime;
247		}
248	}
249
250	public static String getMessageDisplayName(final Message message) {
251		final Conversation conversation = message.getConversation();
252		if (message.getStatus() == Message.STATUS_RECEIVED) {
253			final Contact contact = message.getContact();
254			if (conversation.getMode() == Conversation.MODE_MULTI) {
255				if (contact != null) {
256					return contact.getDisplayName();
257				} else {
258					return getDisplayedMucCounterpart(message.getCounterpart());
259				}
260			} else {
261				return contact != null ? contact.getDisplayName() : "";
262			}
263		} else {
264			if (conversation.getMode() == Conversation.MODE_MULTI) {
265				return conversation.getMucOptions().getSelf().getName();
266			} else {
267				final Jid jid = conversation.getAccount().getJid();
268				return jid.hasLocalpart() ? jid.getLocalpart() : jid.toDomainJid().toString();
269			}
270		}
271	}
272
273	public static String getMessageHint(Context context, Conversation conversation) {
274		switch (conversation.getNextEncryption()) {
275			case Message.ENCRYPTION_NONE:
276				if (Config.multipleEncryptionChoices()) {
277					return context.getString(R.string.send_unencrypted_message);
278				} else {
279					return context.getString(R.string.send_message_to_x,conversation.getName());
280				}
281			case Message.ENCRYPTION_OTR:
282				return context.getString(R.string.send_otr_message);
283			case Message.ENCRYPTION_AXOLOTL:
284				AxolotlService axolotlService = conversation.getAccount().getAxolotlService();
285				if (axolotlService != null && axolotlService.trustedSessionVerified(conversation)) {
286					return context.getString(R.string.send_omemo_x509_message);
287				} else {
288					return context.getString(R.string.send_omemo_message);
289				}
290			case Message.ENCRYPTION_PGP:
291				return context.getString(R.string.send_pgp_message);
292			default:
293				return "";
294		}
295	}
296
297	public static String getDisplayedMucCounterpart(final Jid counterpart) {
298		if (counterpart==null) {
299			return "";
300		} else if (!counterpart.isBareJid()) {
301			return counterpart.getResourcepart().trim();
302		} else {
303			return counterpart.toString().trim();
304		}
305	}
306
307	public static boolean receivedLocationQuestion(Message message) {
308		if (message == null
309				|| message.getStatus() != Message.STATUS_RECEIVED
310				|| message.getType() != Message.TYPE_TEXT) {
311			return false;
312		}
313		String body = message.getBody() == null ? null : message.getBody().trim().toLowerCase(Locale.getDefault());
314		body = body.replace("?","").replace("¿","");
315		return LOCATION_QUESTIONS.contains(body);
316	}
317
318	public static ListItem.Tag getTagForStatus(Context context, Presence.Status status) {
319		switch (status) {
320			case CHAT:
321				return new ListItem.Tag(context.getString(R.string.presence_chat), 0xff259b24);
322			case AWAY:
323				return new ListItem.Tag(context.getString(R.string.presence_away), 0xffff9800);
324			case XA:
325				return new ListItem.Tag(context.getString(R.string.presence_xa), 0xfff44336);
326			case DND:
327				return new ListItem.Tag(context.getString(R.string.presence_dnd), 0xfff44336);
328			default:
329				return new ListItem.Tag(context.getString(R.string.presence_online), 0xff259b24);
330		}
331	}
332
333	public static String tranlasteType(Context context, String type) {
334		switch (type.toLowerCase()) {
335			case "pc":
336				return context.getString(R.string.type_pc);
337			case "phone":
338				return context.getString(R.string.type_phone);
339			case "tablet":
340				return context.getString(R.string.type_tablet);
341			case "web":
342				return context.getString(R.string.type_web);
343			case "console":
344				return context.getString(R.string.type_console);
345			default:
346				return type;
347		}
348	}
349}