UIHelper.java

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