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	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, boolean active, long time) {
113		long difference = (System.currentTimeMillis() - time) / 1000;
114		active = active && difference <= 300;
115		if (active || difference < 60) {
116			return context.getString(R.string.last_seen_now);
117		} else if (difference < 60 * 2) {
118			return context.getString(R.string.last_seen_min);
119		} else if (difference < 60 * 60) {
120			return context.getString(R.string.last_seen_mins,
121					Math.round(difference / 60.0));
122		} else if (difference < 60 * 60 * 2) {
123			return context.getString(R.string.last_seen_hour);
124		} else if (difference < 60 * 60 * 24) {
125			return context.getString(R.string.last_seen_hours,
126					Math.round(difference / (60.0 * 60.0)));
127		} else if (difference < 60 * 60 * 48) {
128			return context.getString(R.string.last_seen_day);
129		} else {
130			return context.getString(R.string.last_seen_days,
131					Math.round(difference / (60.0 * 60.0 * 24.0)));
132		}
133	}
134
135	public static int getColorForName(String name) {
136		if (name == null || name.isEmpty()) {
137			return 0xFF202020;
138		}
139		int colors[] = {0xFFe91e63, 0xFF9c27b0, 0xFF673ab7, 0xFF3f51b5,
140			0xFF5677fc, 0xFF03a9f4, 0xFF00bcd4, 0xFF009688, 0xFFff5722,
141			0xFF795548, 0xFF607d8b};
142		return colors[(int) ((name.hashCode() & 0xffffffffl) % colors.length)];
143	}
144
145	public static Pair<String,Boolean> getMessagePreview(final Context context, final Message message) {
146		final Transferable d = message.getTransferable();
147		if (d != null ) {
148			switch (d.getStatus()) {
149				case Transferable.STATUS_CHECKING:
150					return new Pair<>(context.getString(R.string.checking_x,
151									getFileDescriptionString(context,message)),true);
152				case Transferable.STATUS_DOWNLOADING:
153					return new Pair<>(context.getString(R.string.receiving_x_file,
154									getFileDescriptionString(context,message),
155									d.getProgress()),true);
156				case Transferable.STATUS_OFFER:
157				case Transferable.STATUS_OFFER_CHECK_FILESIZE:
158					return new Pair<>(context.getString(R.string.x_file_offered_for_download,
159									getFileDescriptionString(context,message)),true);
160				case Transferable.STATUS_DELETED:
161					return new Pair<>(context.getString(R.string.file_deleted),true);
162				case Transferable.STATUS_FAILED:
163					return new Pair<>(context.getString(R.string.file_transmission_failed),true);
164				case Transferable.STATUS_UPLOADING:
165					if (message.getStatus() == Message.STATUS_OFFERED) {
166						return new Pair<>(context.getString(R.string.offering_x_file,
167								getFileDescriptionString(context, message)), true);
168					} else {
169						return new Pair<>(context.getString(R.string.sending_x_file,
170								getFileDescriptionString(context, message)), true);
171					}
172				default:
173					return new Pair<>("",false);
174			}
175		} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
176			return new Pair<>(context.getString(R.string.pgp_message),true);
177		} else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
178			return new Pair<>(context.getString(R.string.decryption_failed), 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			String body = message.getBody();
188			if (body.length() > 256) {
189				body = body.substring(0,256);
190			}
191			if (body.startsWith(Message.ME_COMMAND)) {
192				return new Pair<>(body.replaceAll("^" + Message.ME_COMMAND,
193						UIHelper.getMessageDisplayName(message) + " "), false);
194			} else if (GeoHelper.isGeoUri(message.getBody())) {
195				if (message.getStatus() == Message.STATUS_RECEIVED) {
196					return new Pair<>(context.getString(R.string.received_location), true);
197				} else {
198					return new Pair<>(context.getString(R.string.location), true);
199				}
200			} else if (message.treatAsDownloadable() == Message.Decision.MUST) {
201				return new Pair<>(context.getString(R.string.x_file_offered_for_download,
202						getFileDescriptionString(context,message)),true);
203			} else{
204				return new Pair<>(body.trim(), false);
205			}
206		}
207	}
208
209	public static String getFileDescriptionString(final Context context, final Message message) {
210		if (message.getType() == Message.TYPE_IMAGE) {
211			return context.getString(R.string.image);
212		}
213		final String mime = message.getMimeType();
214		if (mime == null) {
215			return context.getString(R.string.file);
216		} else if (mime.startsWith("audio/")) {
217			return context.getString(R.string.audio);
218		} else if(mime.startsWith("video/")) {
219			return context.getString(R.string.video);
220		} else if (mime.startsWith("image/")) {
221			return context.getString(R.string.image);
222		} else if (mime.contains("pdf")) {
223			return context.getString(R.string.pdf_document)	;
224		} else if (mime.contains("application/vnd.android.package-archive")) {
225			return context.getString(R.string.apk)	;
226		} else if (mime.contains("vcard")) {
227			return context.getString(R.string.vcard)	;
228		} else {
229			return mime;
230		}
231	}
232
233	public static String getMessageDisplayName(final Message message) {
234		final Conversation conversation = message.getConversation();
235		if (message.getStatus() == Message.STATUS_RECEIVED) {
236			final Contact contact = message.getContact();
237			if (conversation.getMode() == Conversation.MODE_MULTI) {
238				if (contact != null) {
239					return contact.getDisplayName();
240				} else {
241					return getDisplayedMucCounterpart(message.getCounterpart());
242				}
243			} else {
244				return contact != null ? contact.getDisplayName() : "";
245			}
246		} else {
247			if (conversation.getMode() == Conversation.MODE_MULTI) {
248				return conversation.getMucOptions().getSelf().getName();
249			} else {
250				final Jid jid = conversation.getAccount().getJid();
251				return jid.hasLocalpart() ? jid.getLocalpart() : jid.toDomainJid().toString();
252			}
253		}
254	}
255
256	public static String getMessageHint(Context context, Conversation conversation) {
257		switch (conversation.getNextEncryption()) {
258			case Message.ENCRYPTION_NONE:
259				if (Config.multipleEncryptionChoices()) {
260					return context.getString(R.string.send_unencrypted_message);
261				} else {
262					return context.getString(R.string.send_message_to_x,conversation.getName());
263				}
264			case Message.ENCRYPTION_OTR:
265				return context.getString(R.string.send_otr_message);
266			case Message.ENCRYPTION_AXOLOTL:
267				AxolotlService axolotlService = conversation.getAccount().getAxolotlService();
268				if (axolotlService != null && axolotlService.trustedSessionVerified(conversation)) {
269					return context.getString(R.string.send_omemo_x509_message);
270				} else {
271					return context.getString(R.string.send_omemo_message);
272				}
273			case Message.ENCRYPTION_PGP:
274				return context.getString(R.string.send_pgp_message);
275			default:
276				return "";
277		}
278	}
279
280	public static String getDisplayedMucCounterpart(final Jid counterpart) {
281		if (counterpart==null) {
282			return "";
283		} else if (!counterpart.isBareJid()) {
284			return counterpart.getResourcepart().trim();
285		} else {
286			return counterpart.toString().trim();
287		}
288	}
289
290	public static boolean receivedLocationQuestion(Message message) {
291		if (message == null
292				|| message.getStatus() != Message.STATUS_RECEIVED
293				|| message.getType() != Message.TYPE_TEXT) {
294			return false;
295		}
296		String body = message.getBody() == null ? null : message.getBody().trim().toLowerCase(Locale.getDefault());
297		body = body.replace("?","").replace("¿","");
298		return LOCATION_QUESTIONS.contains(body);
299	}
300
301	public static ListItem.Tag getTagForStatus(Context context, Presence.Status status) {
302		switch (status) {
303			case CHAT:
304				return new ListItem.Tag(context.getString(R.string.presence_chat), 0xff259b24);
305			case AWAY:
306				return new ListItem.Tag(context.getString(R.string.presence_away), 0xffff9800);
307			case XA:
308				return new ListItem.Tag(context.getString(R.string.presence_xa), 0xfff44336);
309			case DND:
310				return new ListItem.Tag(context.getString(R.string.presence_dnd), 0xfff44336);
311			default:
312				return new ListItem.Tag(context.getString(R.string.presence_online), 0xff259b24);
313		}
314	}
315
316	public static String tranlasteType(Context context, String type) {
317		switch (type.toLowerCase()) {
318			case "pc":
319				return context.getString(R.string.type_pc);
320			case "phone":
321				return context.getString(R.string.type_phone);
322			case "tablet":
323				return context.getString(R.string.type_tablet);
324			case "web":
325				return context.getString(R.string.type_web);
326			case "console":
327				return context.getString(R.string.type_console);
328			default:
329				return type;
330		}
331	}
332}