UIHelper.java

  1package eu.siacs.conversations.utils;
  2
  3import java.util.ArrayList;
  4import java.util.Calendar;
  5import java.util.Date;
  6import java.util.List;
  7import java.util.regex.Pattern;
  8
  9import eu.siacs.conversations.R;
 10import eu.siacs.conversations.entities.Account;
 11import eu.siacs.conversations.entities.Contact;
 12import eu.siacs.conversations.entities.Conversation;
 13import eu.siacs.conversations.ui.ConversationActivity;
 14import eu.siacs.conversations.ui.ManageAccountActivity;
 15import android.annotation.SuppressLint;
 16import android.app.AlertDialog;
 17import android.app.Notification;
 18import android.app.NotificationManager;
 19import android.app.PendingIntent;
 20import android.content.Context;
 21import android.content.DialogInterface;
 22import android.content.DialogInterface.OnClickListener;
 23import android.content.Intent;
 24import android.support.v4.app.NotificationCompat;
 25import android.support.v4.app.TaskStackBuilder;
 26import android.text.format.DateFormat;
 27import android.text.format.DateUtils;
 28import android.view.LayoutInflater;
 29import android.view.View;
 30import android.widget.TextView;
 31
 32public class UIHelper {
 33	private static final int SHORT_DATE_FLAGS = DateUtils.FORMAT_SHOW_DATE
 34			| DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
 35	private static final int FULL_DATE_FLAGS = DateUtils.FORMAT_SHOW_TIME
 36			| DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE;
 37
 38	public static String readableTimeDifference(Context context, long time) {
 39		return readableTimeDifference(context, time, false);
 40	}
 41
 42	public static String readableTimeDifferenceFull(Context context, long time) {
 43		return readableTimeDifference(context, time, true);
 44	}
 45
 46	private static String readableTimeDifference(Context context, long time,
 47			boolean fullDate) {
 48		if (time == 0) {
 49			return context.getString(R.string.just_now);
 50		}
 51		Date date = new Date(time);
 52		long difference = (System.currentTimeMillis() - time) / 1000;
 53		if (difference < 60) {
 54			return context.getString(R.string.just_now);
 55		} else if (difference < 60 * 2) {
 56			return context.getString(R.string.minute_ago);
 57		} else if (difference < 60 * 15) {
 58			return context.getString(R.string.minutes_ago,
 59					Math.round(difference / 60.0));
 60		} else if (today(date)) {
 61			java.text.DateFormat df = DateFormat.getTimeFormat(context);
 62			return df.format(date);
 63		} else {
 64			if (fullDate) {
 65				return DateUtils.formatDateTime(context, date.getTime(),
 66						FULL_DATE_FLAGS);
 67			} else {
 68				return DateUtils.formatDateTime(context, date.getTime(),
 69						SHORT_DATE_FLAGS);
 70			}
 71		}
 72	}
 73
 74	private static boolean today(Date date) {
 75		Calendar cal1 = Calendar.getInstance();
 76		Calendar cal2 = Calendar.getInstance();
 77		cal1.setTime(date);
 78		cal2.setTimeInMillis(System.currentTimeMillis());
 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	private final static class EmoticonPattern {
110		Pattern pattern;
111		String replacement;
112
113		EmoticonPattern(String ascii, int unicode) {
114			this.pattern = Pattern.compile("(?<=(^|\\s))" + ascii
115					+ "(?=(\\s|$))");
116			this.replacement = new String(new int[] { unicode, }, 0, 1);
117		}
118
119		String replaceAll(String body) {
120			return pattern.matcher(body).replaceAll(replacement);
121		}
122	}
123
124	private static final EmoticonPattern[] patterns = new EmoticonPattern[] {
125			new EmoticonPattern(":-?D", 0x1f600),
126			new EmoticonPattern("\\^\\^", 0x1f601),
127			new EmoticonPattern(":'D", 0x1f602),
128			new EmoticonPattern("\\]-?D", 0x1f608),
129			new EmoticonPattern(";-?\\)", 0x1f609),
130			new EmoticonPattern(":-?\\)", 0x1f60a),
131			new EmoticonPattern("[B8]-?\\)", 0x1f60e),
132			new EmoticonPattern(":-?\\|", 0x1f610),
133			new EmoticonPattern(":-?[/\\\\]", 0x1f615),
134			new EmoticonPattern(":-?\\*", 0x1f617),
135			new EmoticonPattern(":-?[Ppb]", 0x1f61b),
136			new EmoticonPattern(":-?\\(", 0x1f61e),
137			new EmoticonPattern(":-?[0Oo]", 0x1f62e),
138			new EmoticonPattern("\\\\o/", 0x1F631), };
139
140	public static String transformAsciiEmoticons(String body) {
141		if (body != null) {
142			for (EmoticonPattern p : patterns) {
143				body = p.replaceAll(body);
144			}
145			body = body.trim();
146		}
147		return body;
148	}
149
150	public static int getColorForName(String name) {
151		int colors[] = {0xFFe91e63, 0xFF9c27b0, 0xFF673ab7, 0xFF3f51b5,
152				0xFF5677fc, 0xFF03a9f4, 0xFF00bcd4, 0xFF009688, 0xFFff5722,
153				0xFF795548, 0xFF607d8b};
154		return colors[(int) ((name.hashCode() & 0xffffffffl) % colors.length)];
155	}
156}