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.R;
 15import eu.siacs.conversations.entities.Contact;
 16import eu.siacs.conversations.entities.Conversation;
 17import eu.siacs.conversations.entities.Message;
 18import eu.siacs.conversations.entities.Transferable;
 19import eu.siacs.conversations.xmpp.jid.Jid;
 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 ArrayList<String> LOCATION_QUESTIONS = new ArrayList<>(Arrays.asList(
 30			"where are you", //en
 31			"where are you now", //en
 32			"where are you right now", //en
 33			"whats your 20", //en
 34			"what is your 20", //en
 35			"what's your 20", //en
 36			"whats your twenty", //en
 37			"what is your twenty", //en
 38			"what's your twenty", //en
 39			"wo bist du", //de
 40			"wo bist du jetzt", //de
 41			"wo bist du gerade", //de
 42			"wo seid ihr", //de
 43			"wo seid ihr jetzt", //de
 44			"wo seid ihr gerade", //de
 45			"dónde estás", //es
 46			"donde estas" //es
 47		));
 48
 49	private static final int SHORT_DATE_FLAGS = DateUtils.FORMAT_SHOW_DATE
 50		| DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
 51	private static final int FULL_DATE_FLAGS = DateUtils.FORMAT_SHOW_TIME
 52		| DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE;
 53
 54	public static String readableTimeDifference(Context context, long time) {
 55		return readableTimeDifference(context, time, false);
 56	}
 57
 58	public static String readableTimeDifferenceFull(Context context, long time) {
 59		return readableTimeDifference(context, time, true);
 60	}
 61
 62	private static String readableTimeDifference(Context context, long time,
 63			boolean fullDate) {
 64		if (time == 0) {
 65			return context.getString(R.string.just_now);
 66		}
 67		Date date = new Date(time);
 68		long difference = (System.currentTimeMillis() - time) / 1000;
 69		if (difference < 60) {
 70			return context.getString(R.string.just_now);
 71		} else if (difference < 60 * 2) {
 72			return context.getString(R.string.minute_ago);
 73		} else if (difference < 60 * 15) {
 74			return context.getString(R.string.minutes_ago,
 75					Math.round(difference / 60.0));
 76		} else if (today(date)) {
 77			java.text.DateFormat df = DateFormat.getTimeFormat(context);
 78			return df.format(date);
 79		} else {
 80			if (fullDate) {
 81				return DateUtils.formatDateTime(context, date.getTime(),
 82						FULL_DATE_FLAGS);
 83			} else {
 84				return DateUtils.formatDateTime(context, date.getTime(),
 85						SHORT_DATE_FLAGS);
 86			}
 87		}
 88	}
 89
 90	private static boolean today(Date date) {
 91		return sameDay(date,new Date(System.currentTimeMillis()));
 92	}
 93
 94	public static boolean sameDay(long timestamp1, long timestamp2) {
 95		return sameDay(new Date(timestamp1),new Date(timestamp2));
 96	}
 97
 98	private static boolean sameDay(Date a, Date b) {
 99		Calendar cal1 = Calendar.getInstance();
100		Calendar cal2 = Calendar.getInstance();
101		cal1.setTime(a);
102		cal2.setTime(b);
103		return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
104			&& cal1.get(Calendar.DAY_OF_YEAR) == cal2
105			.get(Calendar.DAY_OF_YEAR);
106	}
107
108	public static String lastseen(Context context, long time) {
109		if (time == 0) {
110			return context.getString(R.string.never_seen);
111		}
112		long difference = (System.currentTimeMillis() - time) / 1000;
113		if (difference < 60) {
114			return context.getString(R.string.last_seen_now);
115		} else if (difference < 60 * 2) {
116			return context.getString(R.string.last_seen_min);
117		} else if (difference < 60 * 60) {
118			return context.getString(R.string.last_seen_mins,
119					Math.round(difference / 60.0));
120		} else if (difference < 60 * 60 * 2) {
121			return context.getString(R.string.last_seen_hour);
122		} else if (difference < 60 * 60 * 24) {
123			return context.getString(R.string.last_seen_hours,
124					Math.round(difference / (60.0 * 60.0)));
125		} else if (difference < 60 * 60 * 48) {
126			return context.getString(R.string.last_seen_day);
127		} else {
128			return context.getString(R.string.last_seen_days,
129					Math.round(difference / (60.0 * 60.0 * 24.0)));
130		}
131	}
132
133	public static int getColorForName(String name) {
134		if (name == null || name.isEmpty()) {
135			return 0xFF202020;
136		}
137		int colors[] = {0xFFe91e63, 0xFF9c27b0, 0xFF673ab7, 0xFF3f51b5,
138			0xFF5677fc, 0xFF03a9f4, 0xFF00bcd4, 0xFF009688, 0xFFff5722,
139			0xFF795548, 0xFF607d8b};
140		return colors[(int) ((name.hashCode() & 0xffffffffl) % colors.length)];
141	}
142
143	public static Pair<String,Boolean> getMessagePreview(final Context context, final Message message) {
144		final Transferable d = message.getTransferable();
145		if (d != null ) {
146			switch (d.getStatus()) {
147				case Transferable.STATUS_CHECKING:
148					return new Pair<>(context.getString(R.string.checking_x,
149									getFileDescriptionString(context,message)),true);
150				case Transferable.STATUS_DOWNLOADING:
151					return new Pair<>(context.getString(R.string.receiving_x_file,
152									getFileDescriptionString(context,message),
153									d.getProgress()),true);
154				case Transferable.STATUS_OFFER:
155				case Transferable.STATUS_OFFER_CHECK_FILESIZE:
156					return new Pair<>(context.getString(R.string.x_file_offered_for_download,
157									getFileDescriptionString(context,message)),true);
158				case Transferable.STATUS_DELETED:
159					return new Pair<>(context.getString(R.string.file_deleted),true);
160				case Transferable.STATUS_FAILED:
161					return new Pair<>(context.getString(R.string.file_transmission_failed),true);
162				case Transferable.STATUS_UPLOADING:
163					if (message.getStatus() == Message.STATUS_OFFERED) {
164						return new Pair<>(context.getString(R.string.offering_x_file,
165								getFileDescriptionString(context, message)), true);
166					} else {
167						return new Pair<>(context.getString(R.string.sending_x_file,
168								getFileDescriptionString(context, message)), true);
169					}
170				default:
171					return new Pair<>("",false);
172			}
173		} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
174			return new Pair<>(context.getString(R.string.encrypted_message_received),true);
175		} else if (message.getType() == Message.TYPE_FILE || message.getType() == Message.TYPE_IMAGE) {
176			if (message.getStatus() == Message.STATUS_RECEIVED) {
177				return new Pair<>(context.getString(R.string.received_x_file,
178							getFileDescriptionString(context, message)), true);
179			} else {
180				return new Pair<>(getFileDescriptionString(context,message),true);
181			}
182		} else {
183			if (message.getBody().startsWith(Message.ME_COMMAND)) {
184				return new Pair<>(message.getBody().replaceAll("^" + Message.ME_COMMAND,
185						UIHelper.getMessageDisplayName(message) + " "), false);
186			} else if (GeoHelper.isGeoUri(message.getBody())) {
187				if (message.getStatus() == Message.STATUS_RECEIVED) {
188					return new Pair<>(context.getString(R.string.received_location),true);
189				} else {
190					return new Pair<>(context.getString(R.string.location), true);
191				}
192			} else{
193				return new Pair<>(message.getBody().trim(), false);
194			}
195		}
196	}
197
198	public static String getFileDescriptionString(final Context context, final Message message) {
199		if (message.getType() == Message.TYPE_IMAGE) {
200			return context.getString(R.string.image);
201		}
202		final String mime = message.getMimeType();
203		if (mime == null) {
204			return context.getString(R.string.file);
205		} else if (mime.startsWith("audio/")) {
206			return context.getString(R.string.audio);
207		} else if(mime.startsWith("video/")) {
208			return context.getString(R.string.video);
209		} else if (mime.startsWith("image/")) {
210			return context.getString(R.string.image);
211		} else if (mime.contains("pdf")) {
212			return context.getString(R.string.pdf_document)	;
213		} else if (mime.contains("application/vnd.android.package-archive")) {
214			return context.getString(R.string.apk)	;
215		} else if (mime.contains("vcard")) {
216			return context.getString(R.string.vcard)	;
217		} else {
218			return mime;
219		}
220	}
221
222	public static String getMessageDisplayName(final Message message) {
223		if (message.getStatus() == Message.STATUS_RECEIVED) {
224			final Contact contact = message.getContact();
225			if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
226				if (contact != null) {
227					return contact.getDisplayName();
228				} else {
229					return getDisplayedMucCounterpart(message.getCounterpart());
230				}
231			} else {
232				return contact != null ? contact.getDisplayName() : "";
233			}
234		} else {
235			if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
236				return getDisplayedMucCounterpart(message.getConversation().getJid());
237			} else {
238				final Jid jid = message.getConversation().getAccount().getJid();
239				return jid.hasLocalpart() ? jid.getLocalpart() : jid.toDomainJid().toString();
240			}
241		}
242	}
243
244	private static String getDisplayedMucCounterpart(final Jid counterpart) {
245		if (counterpart==null) {
246			return "";
247		} else if (!counterpart.isBareJid()) {
248			return counterpart.getResourcepart().trim();
249		} else {
250			return counterpart.toString().trim();
251		}
252	}
253
254	public static boolean receivedLocationQuestion(Message message) {
255		if (message == null
256				|| message.getStatus() != Message.STATUS_RECEIVED
257				|| message.getType() != Message.TYPE_TEXT) {
258			return false;
259		}
260		String body = message.getBody() == null ? null : message.getBody().trim().toLowerCase(Locale.getDefault());
261		body = body.replace("?","").replace("¿","");
262		return LOCATION_QUESTIONS.contains(body);
263	}
264}