UIHelper.java

  1package eu.siacs.conversations.utils;
  2
  3import java.io.FileNotFoundException;
  4import java.util.ArrayList;
  5import java.util.Calendar;
  6import java.util.Date;
  7import java.util.List;
  8import java.util.Locale;
  9import java.util.regex.Pattern;
 10import java.util.regex.Matcher;
 11
 12import eu.siacs.conversations.R;
 13import eu.siacs.conversations.entities.Account;
 14import eu.siacs.conversations.entities.Contact;
 15import eu.siacs.conversations.entities.Conversation;
 16import eu.siacs.conversations.entities.Message;
 17import eu.siacs.conversations.entities.MucOptions.User;
 18import eu.siacs.conversations.ui.ConversationActivity;
 19import eu.siacs.conversations.ui.ManageAccountActivity;
 20import android.app.Activity;
 21import android.app.AlertDialog;
 22import android.app.Notification;
 23import android.app.NotificationManager;
 24import android.app.PendingIntent;
 25import android.content.Context;
 26import android.content.DialogInterface;
 27import android.content.DialogInterface.OnClickListener;
 28import android.content.Intent;
 29import android.content.SharedPreferences;
 30import android.graphics.Bitmap;
 31import android.graphics.BitmapFactory;
 32import android.graphics.Canvas;
 33import android.graphics.Paint;
 34import android.graphics.Rect;
 35import android.graphics.Typeface;
 36import android.net.Uri;
 37import android.preference.PreferenceManager;
 38import android.provider.ContactsContract.Contacts;
 39import android.support.v4.app.NotificationCompat;
 40import android.support.v4.app.TaskStackBuilder;
 41import android.text.format.DateFormat;
 42import android.text.format.DateUtils;
 43import android.text.Html;
 44import android.util.DisplayMetrics;
 45import android.view.LayoutInflater;
 46import android.view.View;
 47import android.widget.QuickContactBadge;
 48import android.widget.TextView;
 49
 50public class UIHelper {
 51	private static final int BG_COLOR = 0xFF181818;
 52	private static final int FG_COLOR = 0xFFFAFAFA;
 53	private static final int TRANSPARENT = 0x00000000;
 54	private static final int DATE_NO_YEAR_FLAGS = DateUtils.FORMAT_SHOW_DATE
 55			| DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
 56
 57	public static String readableTimeDifference(Context context, long time) {
 58		if (time == 0) {
 59			return context.getString(R.string.just_now);
 60		}
 61		Date date = new Date(time);
 62		long difference = (System.currentTimeMillis() - time) / 1000;
 63		if (difference < 60) {
 64			return context.getString(R.string.just_now);
 65		} else if (difference < 60 * 2) {
 66			return context.getString(R.string.minute_ago);
 67		} else if (difference < 60 * 15) {
 68			return context.getString(R.string.minutes_ago,
 69					Math.round(difference / 60.0));
 70		} else if (today(date) || difference < 6 * 60 * 60) {
 71			java.text.DateFormat df = DateFormat.getTimeFormat(context);
 72			return df.format(date);
 73		} else {
 74			return DateUtils.formatDateTime(context, date.getTime(),
 75					DATE_NO_YEAR_FLAGS);
 76		}
 77	}
 78
 79	private static boolean today(Date date) {
 80		Calendar cal1 = Calendar.getInstance();
 81		Calendar cal2 = Calendar.getInstance();
 82		cal1.setTime(date);
 83		cal2.setTimeInMillis(System.currentTimeMillis());
 84		return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
 85				&& cal1.get(Calendar.DAY_OF_YEAR) == cal2
 86						.get(Calendar.DAY_OF_YEAR);
 87	}
 88
 89	public static String lastseen(Context context, long time) {
 90		if (time == 0) {
 91			return context.getString(R.string.never_seen);
 92		}
 93		long difference = (System.currentTimeMillis() - time) / 1000;
 94		if (difference < 60) {
 95			return context.getString(R.string.last_seen_now);
 96		} else if (difference < 60 * 2) {
 97			return context.getString(R.string.last_seen_min);
 98		} else if (difference < 60 * 60) {
 99			return context.getString(R.string.last_seen_mins,
100					Math.round(difference / 60.0));
101		} else if (difference < 60 * 60 * 2) {
102			return context.getString(R.string.last_seen_hour);
103		} else if (difference < 60 * 60 * 24) {
104			return context.getString(R.string.last_seen_hours,
105					Math.round(difference / (60.0 * 60.0)));
106		} else if (difference < 60 * 60 * 48) {
107			return context.getString(R.string.last_seen_day);
108		} else {
109			return context.getString(R.string.last_seen_days,
110					Math.round(difference / (60.0 * 60.0 * 24.0)));
111		}
112	}
113
114	public static int getRealPx(int dp, Context context) {
115		final DisplayMetrics metrics = context.getResources()
116				.getDisplayMetrics();
117		return ((int) (dp * metrics.density));
118	}
119
120	private static int getNameColor(String name) {
121		/*
122		 * int holoColors[] = { 0xFF1da9da, 0xFFb368d9, 0xFF83b600, 0xFFffa713,
123		 * 0xFFe92727 };
124		 */
125		int holoColors[] = { 0xFFe91e63, 0xFF9c27b0, 0xFF673ab7, 0xFF3f51b5,
126				0xFF5677fc, 0xFF03a9f4, 0xFF00bcd4, 0xFF009688, 0xFFff5722,
127				0xFF795548, 0xFF607d8b };
128		return holoColors[(int) ((name.hashCode() & 0xffffffffl) % holoColors.length)];
129	}
130
131	private static void drawTile(Canvas canvas, String letter, int tileColor,
132			int textColor, int left, int top, int right, int bottom) {
133		Paint tilePaint = new Paint(), textPaint = new Paint();
134		tilePaint.setColor(tileColor);
135		textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
136		textPaint.setColor(textColor);
137		textPaint.setTypeface(Typeface.create("sans-serif-light",
138				Typeface.NORMAL));
139		textPaint.setTextSize((float) ((right - left) * 0.8));
140		Rect rect = new Rect();
141
142		canvas.drawRect(new Rect(left, top, right, bottom), tilePaint);
143		textPaint.getTextBounds(letter, 0, 1, rect);
144		float width = textPaint.measureText(letter);
145		canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom)
146				/ 2 + rect.height() / 2, textPaint);
147	}
148
149	private static Bitmap getUnknownContactPicture(String[] names, int size,
150			int bgColor, int fgColor) {
151		int tiles = (names.length > 4) ? 4 : (names.length < 1) ? 1
152				: names.length;
153		Bitmap bitmap = Bitmap
154				.createBitmap(size, size, Bitmap.Config.ARGB_8888);
155		Canvas canvas = new Canvas(bitmap);
156
157		String[] letters = new String[tiles];
158		int[] colors = new int[tiles];
159		if (names.length < 1) {
160			letters[0] = "?";
161			colors[0] = 0xFFe92727;
162		} else {
163			for (int i = 0; i < tiles; ++i) {
164				letters[i] = (names[i].length() > 0) ? names[i].substring(0, 1)
165						.toUpperCase(Locale.US) : " ";
166				colors[i] = getNameColor(names[i]);
167			}
168
169			if (names.length > 4) {
170				letters[3] = "\u2026"; // Unicode ellipsis
171				colors[3] = 0xFF202020;
172			}
173		}
174
175		bitmap.eraseColor(bgColor);
176
177		switch (tiles) {
178		case 1:
179			drawTile(canvas, letters[0], colors[0], fgColor, 0, 0, size, size);
180			break;
181
182		case 2:
183			drawTile(canvas, letters[0], colors[0], fgColor, 0, 0,
184					size / 2 - 1, size);
185			drawTile(canvas, letters[1], colors[1], fgColor, size / 2 + 1, 0,
186					size, size);
187			break;
188
189		case 3:
190			drawTile(canvas, letters[0], colors[0], fgColor, 0, 0,
191					size / 2 - 1, size);
192			drawTile(canvas, letters[1], colors[1], fgColor, size / 2 + 1, 0,
193					size, size / 2 - 1);
194			drawTile(canvas, letters[2], colors[2], fgColor, size / 2 + 1,
195					size / 2 + 1, size, size);
196			break;
197
198		case 4:
199			drawTile(canvas, letters[0], colors[0], fgColor, 0, 0,
200					size / 2 - 1, size / 2 - 1);
201			drawTile(canvas, letters[1], colors[1], fgColor, 0, size / 2 + 1,
202					size / 2 - 1, size);
203			drawTile(canvas, letters[2], colors[2], fgColor, size / 2 + 1, 0,
204					size, size / 2 - 1);
205			drawTile(canvas, letters[3], colors[3], fgColor, size / 2 + 1,
206					size / 2 + 1, size, size);
207			break;
208		}
209
210		return bitmap;
211	}
212
213	private static Bitmap getMucContactPicture(Conversation conversation,
214			int size, int bgColor, int fgColor) {
215		List<User> members = conversation.getMucOptions().getUsers();
216		if (members.size() == 0) {
217			return getUnknownContactPicture(
218					new String[] { conversation.getName(false) }, size,
219					bgColor, fgColor);
220		}
221		ArrayList<String> names = new ArrayList<String>();
222		names.add(conversation.getMucOptions().getActualNick());
223		for (User user : members) {
224			names.add(user.getName());
225			if (names.size() > 4) {
226				break;
227			}
228		}
229		String[] mArrayNames = new String[names.size()];
230		names.toArray(mArrayNames);
231		return getUnknownContactPicture(mArrayNames, size, bgColor, fgColor);
232	}
233
234	public static Bitmap getContactPicture(Conversation conversation,
235			int dpSize, Context context, boolean notification) {
236		if (conversation.getMode() == Conversation.MODE_SINGLE) {
237			return getContactPicture(conversation.getContact(), dpSize,
238					context, notification);
239		} else {
240			int fgColor = UIHelper.FG_COLOR, bgColor = (notification) ? UIHelper.BG_COLOR
241					: UIHelper.TRANSPARENT;
242
243			return getMucContactPicture(conversation,
244					getRealPx(dpSize, context), bgColor, fgColor);
245		}
246	}
247
248	public static Bitmap getContactPicture(Contact contact, int dpSize,
249			Context context, boolean notification) {
250		String uri = contact.getProfilePhoto();
251		if (uri == null) {
252			return getContactPicture(contact.getDisplayName(), dpSize, context,
253					notification);
254		}
255		try {
256			Bitmap bm = BitmapFactory.decodeStream(context.getContentResolver()
257					.openInputStream(Uri.parse(uri)));
258			return Bitmap.createScaledBitmap(bm, getRealPx(dpSize, context),
259					getRealPx(dpSize, context), false);
260		} catch (FileNotFoundException e) {
261			return getContactPicture(contact.getDisplayName(), dpSize, context,
262					notification);
263		}
264	}
265
266	public static Bitmap getContactPicture(String name, int dpSize,
267			Context context, boolean notification) {
268		int fgColor = UIHelper.FG_COLOR, bgColor = (notification) ? UIHelper.BG_COLOR
269				: UIHelper.TRANSPARENT;
270
271		return getUnknownContactPicture(new String[] { name },
272				getRealPx(dpSize, context), bgColor, fgColor);
273	}
274
275	public static void showErrorNotification(Context context,
276			List<Account> accounts) {
277		NotificationManager mNotificationManager = (NotificationManager) context
278				.getSystemService(Context.NOTIFICATION_SERVICE);
279		List<Account> accountsWproblems = new ArrayList<Account>();
280		for (Account account : accounts) {
281			if (account.hasErrorStatus()) {
282				accountsWproblems.add(account);
283			}
284		}
285		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
286				context);
287		if (accountsWproblems.size() == 0) {
288			mNotificationManager.cancel(1111);
289			return;
290		} else if (accountsWproblems.size() == 1) {
291			mBuilder.setContentTitle(context
292					.getString(R.string.problem_connecting_to_account));
293			mBuilder.setContentText(accountsWproblems.get(0).getJid());
294		} else {
295			mBuilder.setContentTitle(context
296					.getString(R.string.problem_connecting_to_accounts));
297			mBuilder.setContentText(context.getString(R.string.touch_to_fix));
298		}
299		mBuilder.setOngoing(true);
300		mBuilder.setLights(0xffffffff, 2000, 4000);
301		mBuilder.setSmallIcon(R.drawable.ic_notification);
302		TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
303		stackBuilder.addParentStack(ConversationActivity.class);
304
305		Intent manageAccountsIntent = new Intent(context,
306				ManageAccountActivity.class);
307		stackBuilder.addNextIntent(manageAccountsIntent);
308
309		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
310				PendingIntent.FLAG_UPDATE_CURRENT);
311
312		mBuilder.setContentIntent(resultPendingIntent);
313		Notification notification = mBuilder.build();
314		mNotificationManager.notify(1111, notification);
315	}
316
317	private static Pattern generateNickHighlightPattern(String nick) {
318		// We expect a word boundary, i.e. space or start of string, followed by
319		// the
320		// nick (matched in case-insensitive manner), followed by optional
321		// punctuation (for example "bob: i disagree" or "how are you alice?"),
322		// followed by another word boundary.
323		return Pattern.compile("\\b" + nick + "\\p{Punct}?\\b",
324				Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
325	}
326
327	public static void updateNotification(Context context,
328			List<Conversation> conversations, Conversation currentCon,
329			boolean notify) {
330		NotificationManager mNotificationManager = (NotificationManager) context
331				.getSystemService(Context.NOTIFICATION_SERVICE);
332
333		SharedPreferences preferences = PreferenceManager
334				.getDefaultSharedPreferences(context);
335		boolean useSubject = preferences.getBoolean("use_subject_in_muc", true);
336		boolean showNofifications = preferences.getBoolean("show_notification",
337				true);
338		boolean vibrate = preferences.getBoolean("vibrate_on_notification",
339				true);
340		boolean alwaysNotify = preferences.getBoolean(
341				"notify_in_conversation_when_highlighted", false);
342
343		if (!showNofifications) {
344			mNotificationManager.cancel(2342);
345			return;
346		}
347
348		String targetUuid = "";
349
350		if ((currentCon != null)
351				&& (currentCon.getMode() == Conversation.MODE_MULTI)
352				&& (!alwaysNotify) && notify) {
353			String nick = currentCon.getMucOptions().getActualNick();
354			Pattern highlight = generateNickHighlightPattern(nick);
355			Matcher m = highlight.matcher(currentCon.getLatestMessage()
356					.getBody());
357			notify = m.find();
358		}
359
360		List<Conversation> unread = new ArrayList<Conversation>();
361		for (Conversation conversation : conversations) {
362			if (conversation.getMode() == Conversation.MODE_MULTI) {
363				if ((!conversation.isRead())
364						&& ((wasHighlighted(conversation) || (alwaysNotify)))) {
365					unread.add(conversation);
366				}
367			} else {
368				if (!conversation.isRead()) {
369					unread.add(conversation);
370				}
371			}
372		}
373		String ringtone = preferences.getString("notification_ringtone", null);
374
375		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
376				context);
377		if (unread.size() == 0) {
378			mNotificationManager.cancel(2342);
379			return;
380		} else if (unread.size() == 1) {
381			Conversation conversation = unread.get(0);
382			targetUuid = conversation.getUuid();
383			mBuilder.setLargeIcon(conversation.getImage(context, 64));
384			mBuilder.setContentTitle(conversation.getName(useSubject));
385			if (notify) {
386				mBuilder.setTicker(conversation.getLatestMessage()
387						.getReadableBody(context));
388			}
389			StringBuilder bigText = new StringBuilder();
390			List<Message> messages = conversation.getMessages();
391			String firstLine = "";
392			for (int i = messages.size() - 1; i >= 0; --i) {
393				if (!messages.get(i).isRead()) {
394					if (i == messages.size() - 1) {
395						firstLine = messages.get(i).getReadableBody(context);
396						bigText.append(firstLine);
397					} else {
398						firstLine = messages.get(i).getReadableBody(context);
399						bigText.insert(0, firstLine + "\n");
400					}
401				} else {
402					break;
403				}
404			}
405			mBuilder.setContentText(firstLine);
406			mBuilder.setStyle(new NotificationCompat.BigTextStyle()
407					.bigText(bigText.toString()));
408		} else {
409			NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
410			style.setBigContentTitle(unread.size() + " "
411					+ context.getString(R.string.unread_conversations));
412			StringBuilder names = new StringBuilder();
413			for (int i = 0; i < unread.size(); ++i) {
414				targetUuid = unread.get(i).getUuid();
415				if (i < unread.size() - 1) {
416					names.append(unread.get(i).getName(useSubject) + ", ");
417				} else {
418					names.append(unread.get(i).getName(useSubject));
419				}
420				style.addLine(Html.fromHtml("<b>"
421						+ unread.get(i).getName(useSubject)
422						+ "</b> "
423						+ unread.get(i).getLatestMessage()
424								.getReadableBody(context)));
425			}
426			mBuilder.setContentTitle(unread.size() + " "
427					+ context.getString(R.string.unread_conversations));
428			mBuilder.setContentText(names.toString());
429			mBuilder.setStyle(style);
430		}
431		if ((currentCon != null) && (notify)) {
432			targetUuid = currentCon.getUuid();
433		}
434		if (unread.size() != 0) {
435			mBuilder.setSmallIcon(R.drawable.ic_notification);
436			if (notify) {
437				if (vibrate) {
438					int dat = 70;
439					long[] pattern = { 0, 3 * dat, dat, dat };
440					mBuilder.setVibrate(pattern);
441				}
442				mBuilder.setLights(0xffffffff, 2000, 4000);
443				if (ringtone != null) {
444					mBuilder.setSound(Uri.parse(ringtone));
445				}
446			}
447
448			TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
449			stackBuilder.addParentStack(ConversationActivity.class);
450
451			Intent viewConversationIntent = new Intent(context,
452					ConversationActivity.class);
453			viewConversationIntent.setAction(Intent.ACTION_VIEW);
454			viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
455					targetUuid);
456			viewConversationIntent
457					.setType(ConversationActivity.VIEW_CONVERSATION);
458
459			stackBuilder.addNextIntent(viewConversationIntent);
460
461			PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
462					0, PendingIntent.FLAG_UPDATE_CURRENT);
463
464			mBuilder.setContentIntent(resultPendingIntent);
465			Notification notification = mBuilder.build();
466			mNotificationManager.notify(2342, notification);
467		}
468	}
469
470	private static boolean wasHighlighted(Conversation conversation) {
471		List<Message> messages = conversation.getMessages();
472		String nick = conversation.getMucOptions().getActualNick();
473		Pattern highlight = generateNickHighlightPattern(nick);
474		for (int i = messages.size() - 1; i >= 0; --i) {
475			if (messages.get(i).isRead()) {
476				break;
477			} else {
478				Matcher m = highlight.matcher(messages.get(i).getBody());
479				if (m.find()) {
480					return true;
481				}
482			}
483		}
484		return false;
485	}
486
487	public static void prepareContactBadge(final Activity activity,
488			QuickContactBadge badge, final Contact contact, Context context) {
489		if (contact.getSystemAccount() != null) {
490			String[] systemAccount = contact.getSystemAccount().split("#");
491			long id = Long.parseLong(systemAccount[0]);
492			badge.assignContactUri(Contacts.getLookupUri(id, systemAccount[1]));
493		}
494		badge.setImageBitmap(contact.getImage(72, context));
495	}
496
497	public static AlertDialog getVerifyFingerprintDialog(
498			final ConversationActivity activity,
499			final Conversation conversation, final View msg) {
500		final Contact contact = conversation.getContact();
501		final Account account = conversation.getAccount();
502
503		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
504		builder.setTitle("Verify fingerprint");
505		LayoutInflater inflater = activity.getLayoutInflater();
506		View view = inflater.inflate(R.layout.dialog_verify_otr, null);
507		TextView jid = (TextView) view.findViewById(R.id.verify_otr_jid);
508		TextView fingerprint = (TextView) view
509				.findViewById(R.id.verify_otr_fingerprint);
510		TextView yourprint = (TextView) view
511				.findViewById(R.id.verify_otr_yourprint);
512
513		jid.setText(contact.getJid());
514		fingerprint.setText(conversation.getOtrFingerprint());
515		yourprint.setText(account.getOtrFingerprint());
516		builder.setNegativeButton("Cancel", null);
517		builder.setPositiveButton("Verify", new OnClickListener() {
518
519			@Override
520			public void onClick(DialogInterface dialog, int which) {
521				contact.addOtrFingerprint(conversation.getOtrFingerprint());
522				msg.setVisibility(View.GONE);
523				activity.xmppConnectionService.syncRosterToDisk(account);
524			}
525		});
526		builder.setView(view);
527		return builder.create();
528	}
529
530	public static Bitmap getSelfContactPicture(Account account, int size,
531			boolean showPhoneSelfContactPicture, Context context) {
532		if (showPhoneSelfContactPicture) {
533			Uri selfiUri = PhoneHelper.getSefliUri(context);
534			if (selfiUri != null) {
535				try {
536					return BitmapFactory.decodeStream(context
537							.getContentResolver().openInputStream(selfiUri));
538				} catch (FileNotFoundException e) {
539					return getContactPicture(account.getJid(), size, context,
540							false);
541				}
542			}
543			return getContactPicture(account.getJid(), size, context, false);
544		} else {
545			return getContactPicture(account.getJid(), size, context, false);
546		}
547	}
548
549	private static final Pattern armorRegex(String regex) { return Pattern.compile("(^|\\s+)" + regex + "(\\s+|$)"); }
550
551	private static final String armorReplacement(String replacement) { return "$1" + replacement + "$2"; }
552
553	public static String transformAsciiEmoticons(String body) {
554		if (body != null) {
555		// see https://developer.android.com/reference/java/util/regex/Pattern.html
556		// see http://userguide.icu-project.org/strings/regexp
557		// see https://de.wikipedia.org/wiki/Unicodeblock_Smileys
558			for (Object[] r: new Object[][]{
559				{armorRegex(":-?\\)"), armorReplacement("😃"), },
560				{armorRegex(";-?\\)"), armorReplacement("😉"), },
561				{armorRegex(":-?D"), armorReplacement("😀"), },
562				{armorRegex(":-?[Ppb]"), armorReplacement("😋"), },
563				{armorRegex("8-?\\)"), armorReplacement("😎"), },
564				{armorRegex(":-?\\|"), armorReplacement("😐"), },
565				{armorRegex(":-?[/\\\\]"), armorReplacement("😕"), },
566				{armorRegex(":-?\\*"), armorReplacement("😗"), },
567				{armorRegex(":-?[0Oo]"), armorReplacement("😮"), },
568				{armorRegex(":-?\\("), armorReplacement("😞"), },
569				{armorRegex("\\^\\^"), armorReplacement("😁"), },
570			}) {
571				Pattern pattern = (Pattern)r[0];
572				String replacement = (String)r[1];
573				body = pattern.matcher(body).replaceAll(replacement);
574			}
575			body = body.trim();
576		}
577		return body;
578	}
579}