UIHelper.java

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