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() }, size, bgColor,
219 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 showNofifications = preferences.getBoolean("show_notification",
336 true);
337 boolean vibrate = preferences.getBoolean("vibrate_on_notification",
338 true);
339 boolean alwaysNotify = preferences.getBoolean(
340 "notify_in_conversation_when_highlighted", false);
341
342 if (!showNofifications) {
343 mNotificationManager.cancel(2342);
344 return;
345 }
346
347 String targetUuid = "";
348
349 if ((currentCon != null)
350 && (currentCon.getMode() == Conversation.MODE_MULTI)
351 && (!alwaysNotify) && notify) {
352 String nick = currentCon.getMucOptions().getActualNick();
353 Pattern highlight = generateNickHighlightPattern(nick);
354 Matcher m = highlight.matcher(currentCon.getLatestMessage()
355 .getBody());
356 notify = m.find()
357 || (currentCon.getLatestMessage().getType() == Message.TYPE_PRIVATE);
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 && ((wasHighlightedOrPrivate(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());
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() + ", ");
417 } else {
418 names.append(unread.get(i).getName());
419 }
420 style.addLine(Html.fromHtml("<b>"
421 + unread.get(i).getName()
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 wasHighlightedOrPrivate(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 || messages.get(i).getType() == Message.TYPE_PRIVATE) {
481 return true;
482 }
483 }
484 }
485 return false;
486 }
487
488 public static void prepareContactBadge(final Activity activity,
489 QuickContactBadge badge, final Contact contact, Context context) {
490 if (contact.getSystemAccount() != null) {
491 String[] systemAccount = contact.getSystemAccount().split("#");
492 long id = Long.parseLong(systemAccount[0]);
493 badge.assignContactUri(Contacts.getLookupUri(id, systemAccount[1]));
494 }
495 badge.setImageBitmap(contact.getImage(72, context));
496 }
497
498 public static AlertDialog getVerifyFingerprintDialog(
499 final ConversationActivity activity,
500 final Conversation conversation, final View msg) {
501 final Contact contact = conversation.getContact();
502 final Account account = conversation.getAccount();
503
504 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
505 builder.setTitle("Verify fingerprint");
506 LayoutInflater inflater = activity.getLayoutInflater();
507 View view = inflater.inflate(R.layout.dialog_verify_otr, null);
508 TextView jid = (TextView) view.findViewById(R.id.verify_otr_jid);
509 TextView fingerprint = (TextView) view
510 .findViewById(R.id.verify_otr_fingerprint);
511 TextView yourprint = (TextView) view
512 .findViewById(R.id.verify_otr_yourprint);
513
514 jid.setText(contact.getJid());
515 fingerprint.setText(conversation.getOtrFingerprint());
516 yourprint.setText(account.getOtrFingerprint());
517 builder.setNegativeButton("Cancel", null);
518 builder.setPositiveButton("Verify", new OnClickListener() {
519
520 @Override
521 public void onClick(DialogInterface dialog, int which) {
522 contact.addOtrFingerprint(conversation.getOtrFingerprint());
523 msg.setVisibility(View.GONE);
524 activity.xmppConnectionService.syncRosterToDisk(account);
525 }
526 });
527 builder.setView(view);
528 return builder.create();
529 }
530
531 public static Bitmap getSelfContactPicture(Account account, int size,
532 boolean showPhoneSelfContactPicture, Context context) {
533 if (showPhoneSelfContactPicture) {
534 Uri selfiUri = PhoneHelper.getSefliUri(context);
535 if (selfiUri != null) {
536 try {
537 return BitmapFactory.decodeStream(context
538 .getContentResolver().openInputStream(selfiUri));
539 } catch (FileNotFoundException e) {
540 return getContactPicture(account.getJid(), size, context,
541 false);
542 }
543 }
544 return getContactPicture(account.getJid(), size, context, false);
545 } else {
546 return getContactPicture(account.getJid(), size, context, false);
547 }
548 }
549
550 private final static class EmoticonPattern {
551 Pattern pattern;
552 String replacement;
553
554 EmoticonPattern(String ascii, int unicode) {
555 this.pattern = Pattern.compile("(?<=(^|\\s))" + ascii
556 + "(?=(\\s|$))");
557 this.replacement = new String(new int[] { unicode, }, 0, 1);
558 }
559
560 String replaceAll(String body) {
561 return pattern.matcher(body).replaceAll(replacement);
562 }
563 }
564
565 private static final EmoticonPattern[] patterns = new EmoticonPattern[] {
566 new EmoticonPattern(":-?D", 0x1f600),
567 new EmoticonPattern("\\^\\^", 0x1f601),
568 new EmoticonPattern(":'D", 0x1f602),
569 new EmoticonPattern("\\]-?D", 0x1f608),
570 new EmoticonPattern(";-?\\)", 0x1f609),
571 new EmoticonPattern(":-?\\)", 0x1f60a),
572 new EmoticonPattern("[B8]-?\\)", 0x1f60e),
573 new EmoticonPattern(":-?\\|", 0x1f610),
574 new EmoticonPattern(":-?[/\\\\]", 0x1f615),
575 new EmoticonPattern(":-?\\*", 0x1f617),
576 new EmoticonPattern(":-?[Ppb]", 0x1f61b),
577 new EmoticonPattern(":-?\\(", 0x1f61e),
578 new EmoticonPattern(":-?[0Oo]", 0x1f62e),
579 new EmoticonPattern("\\\\o/", 0x1F631), };
580
581 public static String transformAsciiEmoticons(String body) {
582 if (body != null) {
583 for (EmoticonPattern p : patterns) {
584 body = p.replaceAll(body);
585 }
586 body = body.trim();
587 }
588 return body;
589 }
590}