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