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