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	public static void showErrorNotification(Context context,
110			List<Account> accounts) {
111		NotificationManager mNotificationManager = (NotificationManager) context
112				.getSystemService(Context.NOTIFICATION_SERVICE);
113		List<Account> accountsWproblems = new ArrayList<>();
114		for (Account account : accounts) {
115			if (account.hasErrorStatus()) {
116				accountsWproblems.add(account);
117			}
118		}
119		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
120				context);
121		if (accountsWproblems.size() == 0) {
122			mNotificationManager.cancel(1111);
123			return;
124		} else if (accountsWproblems.size() == 1) {
125			mBuilder.setContentTitle(context
126					.getString(R.string.problem_connecting_to_account));
127			mBuilder.setContentText(accountsWproblems.get(0).getJid().toBareJid().toString());
128		} else {
129			mBuilder.setContentTitle(context
130					.getString(R.string.problem_connecting_to_accounts));
131			mBuilder.setContentText(context.getString(R.string.touch_to_fix));
132		}
133		mBuilder.setOngoing(true);
134		mBuilder.setLights(0xffffffff, 2000, 4000);
135		mBuilder.setSmallIcon(R.drawable.ic_notification);
136		TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
137		stackBuilder.addParentStack(ConversationActivity.class);
138
139		Intent manageAccountsIntent = new Intent(context,
140				ManageAccountActivity.class);
141		stackBuilder.addNextIntent(manageAccountsIntent);
142
143		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
144				PendingIntent.FLAG_UPDATE_CURRENT);
145
146		mBuilder.setContentIntent(resultPendingIntent);
147		Notification notification = mBuilder.build();
148		mNotificationManager.notify(1111, notification);
149	}
150
151	private final static class EmoticonPattern {
152		Pattern pattern;
153		String replacement;
154
155		EmoticonPattern(String ascii, int unicode) {
156			this.pattern = Pattern.compile("(?<=(^|\\s))" + ascii
157					+ "(?=(\\s|$))");
158			this.replacement = new String(new int[] { unicode, }, 0, 1);
159		}
160
161		String replaceAll(String body) {
162			return pattern.matcher(body).replaceAll(replacement);
163		}
164	}
165
166	private static final EmoticonPattern[] patterns = new EmoticonPattern[] {
167			new EmoticonPattern(":-?D", 0x1f600),
168			new EmoticonPattern("\\^\\^", 0x1f601),
169			new EmoticonPattern(":'D", 0x1f602),
170			new EmoticonPattern("\\]-?D", 0x1f608),
171			new EmoticonPattern(";-?\\)", 0x1f609),
172			new EmoticonPattern(":-?\\)", 0x1f60a),
173			new EmoticonPattern("[B8]-?\\)", 0x1f60e),
174			new EmoticonPattern(":-?\\|", 0x1f610),
175			new EmoticonPattern(":-?[/\\\\]", 0x1f615),
176			new EmoticonPattern(":-?\\*", 0x1f617),
177			new EmoticonPattern(":-?[Ppb]", 0x1f61b),
178			new EmoticonPattern(":-?\\(", 0x1f61e),
179			new EmoticonPattern(":-?[0Oo]", 0x1f62e),
180			new EmoticonPattern("\\\\o/", 0x1F631), };
181
182	public static String transformAsciiEmoticons(String body) {
183		if (body != null) {
184			for (EmoticonPattern p : patterns) {
185				body = p.replaceAll(body);
186			}
187			body = body.trim();
188		}
189		return body;
190	}
191}