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;
10
11import eu.siacs.conversations.R;
12import eu.siacs.conversations.entities.Account;
13import eu.siacs.conversations.entities.Contact;
14import eu.siacs.conversations.entities.Conversation;
15import eu.siacs.conversations.entities.MucOptions.User;
16import eu.siacs.conversations.ui.ConversationActivity;
17import eu.siacs.conversations.ui.ManageAccountActivity;
18import android.annotation.SuppressLint;
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Notification;
22import android.app.NotificationManager;
23import android.app.PendingIntent;
24import android.content.Context;
25import android.content.DialogInterface;
26import android.content.DialogInterface.OnClickListener;
27import android.content.Intent;
28import android.graphics.Bitmap;
29import android.graphics.BitmapFactory;
30import android.graphics.Canvas;
31import android.graphics.Paint;
32import android.graphics.Rect;
33import android.graphics.Typeface;
34import android.net.Uri;
35import android.provider.ContactsContract.Contacts;
36import android.support.v4.app.NotificationCompat;
37import android.support.v4.app.TaskStackBuilder;
38import android.text.format.DateFormat;
39import android.text.format.DateUtils;
40import android.util.DisplayMetrics;
41import android.view.LayoutInflater;
42import android.view.View;
43import android.widget.QuickContactBadge;
44import android.widget.TextView;
45
46public class UIHelper {
47 private static final int BG_COLOR = 0xFF181818;
48 private static final int FG_COLOR = 0xFFFAFAFA;
49 private static final int TRANSPARENT = 0x00000000;
50 private static final int SHORT_DATE_FLAGS = DateUtils.FORMAT_SHOW_DATE
51 | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
52 private static final int FULL_DATE_FLAGS = DateUtils.FORMAT_SHOW_TIME
53 | DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE;
54
55 public static String readableTimeDifference(Context context, long time) {
56 return readableTimeDifference(context, time, false);
57 }
58
59 public static String readableTimeDifferenceFull(Context context, long time) {
60 return readableTimeDifference(context, time, true);
61 }
62
63 private static String readableTimeDifference(Context context, long time,
64 boolean fullDate) {
65 if (time == 0) {
66 return context.getString(R.string.just_now);
67 }
68 Date date = new Date(time);
69 long difference = (System.currentTimeMillis() - time) / 1000;
70 if (difference < 60) {
71 return context.getString(R.string.just_now);
72 } else if (difference < 60 * 2) {
73 return context.getString(R.string.minute_ago);
74 } else if (difference < 60 * 15) {
75 return context.getString(R.string.minutes_ago,
76 Math.round(difference / 60.0));
77 } else if (today(date)) {
78 java.text.DateFormat df = DateFormat.getTimeFormat(context);
79 return df.format(date);
80 } else {
81 if (fullDate) {
82 return DateUtils.formatDateTime(context, date.getTime(),
83 FULL_DATE_FLAGS);
84 } else {
85 return DateUtils.formatDateTime(context, date.getTime(),
86 SHORT_DATE_FLAGS);
87 }
88 }
89 }
90
91 private static boolean today(Date date) {
92 Calendar cal1 = Calendar.getInstance();
93 Calendar cal2 = Calendar.getInstance();
94 cal1.setTime(date);
95 cal2.setTimeInMillis(System.currentTimeMillis());
96 return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
97 && cal1.get(Calendar.DAY_OF_YEAR) == cal2
98 .get(Calendar.DAY_OF_YEAR);
99 }
100
101 public static String lastseen(Context context, long time) {
102 if (time == 0) {
103 return context.getString(R.string.never_seen);
104 }
105 long difference = (System.currentTimeMillis() - time) / 1000;
106 if (difference < 60) {
107 return context.getString(R.string.last_seen_now);
108 } else if (difference < 60 * 2) {
109 return context.getString(R.string.last_seen_min);
110 } else if (difference < 60 * 60) {
111 return context.getString(R.string.last_seen_mins,
112 Math.round(difference / 60.0));
113 } else if (difference < 60 * 60 * 2) {
114 return context.getString(R.string.last_seen_hour);
115 } else if (difference < 60 * 60 * 24) {
116 return context.getString(R.string.last_seen_hours,
117 Math.round(difference / (60.0 * 60.0)));
118 } else if (difference < 60 * 60 * 48) {
119 return context.getString(R.string.last_seen_day);
120 } else {
121 return context.getString(R.string.last_seen_days,
122 Math.round(difference / (60.0 * 60.0 * 24.0)));
123 }
124 }
125
126 public static int getRealPx(int dp, Context context) {
127 final DisplayMetrics metrics = context.getResources()
128 .getDisplayMetrics();
129 return ((int) (dp * metrics.density));
130 }
131
132 private static int getNameColor(String name) {
133 /*
134 * int holoColors[] = { 0xFF1da9da, 0xFFb368d9, 0xFF83b600, 0xFFffa713,
135 * 0xFFe92727 };
136 */
137 int holoColors[] = { 0xFFe91e63, 0xFF9c27b0, 0xFF673ab7, 0xFF3f51b5,
138 0xFF5677fc, 0xFF03a9f4, 0xFF00bcd4, 0xFF009688, 0xFFff5722,
139 0xFF795548, 0xFF607d8b };
140 return holoColors[(int) ((name.hashCode() & 0xffffffffl) % holoColors.length)];
141 }
142
143 private static void drawTile(Canvas canvas, String letter, int tileColor,
144 int textColor, int left, int top, int right, int bottom) {
145 Paint tilePaint = new Paint(), textPaint = new Paint();
146 tilePaint.setColor(tileColor);
147 textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
148 textPaint.setColor(textColor);
149 textPaint.setTypeface(Typeface.create("sans-serif-light",
150 Typeface.NORMAL));
151 textPaint.setTextSize((float) ((right - left) * 0.8));
152 Rect rect = new Rect();
153
154 canvas.drawRect(new Rect(left, top, right, bottom), tilePaint);
155 textPaint.getTextBounds(letter, 0, 1, rect);
156 float width = textPaint.measureText(letter);
157 canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom)
158 / 2 + rect.height() / 2, textPaint);
159 }
160
161 private static Bitmap getUnknownContactPicture(String[] names, int size,
162 int bgColor, int fgColor) {
163 int tiles = (names.length > 4) ? 4 : (names.length < 1) ? 1
164 : names.length;
165 Bitmap bitmap = Bitmap
166 .createBitmap(size, size, Bitmap.Config.ARGB_8888);
167 Canvas canvas = new Canvas(bitmap);
168
169 String[] letters = new String[tiles];
170 int[] colors = new int[tiles];
171 if (names.length < 1) {
172 letters[0] = "?";
173 colors[0] = 0xFFe92727;
174 } else {
175 for (int i = 0; i < tiles; ++i) {
176 letters[i] = (names[i].length() > 0) ? names[i].substring(0, 1)
177 .toUpperCase(Locale.US) : " ";
178 colors[i] = getNameColor(names[i]);
179 }
180
181 if (names.length > 4) {
182 letters[3] = "\u2026"; // Unicode ellipsis
183 colors[3] = 0xFF202020;
184 }
185 }
186
187 bitmap.eraseColor(bgColor);
188
189 switch (tiles) {
190 case 1:
191 drawTile(canvas, letters[0], colors[0], fgColor, 0, 0, size, size);
192 break;
193
194 case 2:
195 drawTile(canvas, letters[0], colors[0], fgColor, 0, 0,
196 size / 2 - 1, size);
197 drawTile(canvas, letters[1], colors[1], fgColor, size / 2 + 1, 0,
198 size, size);
199 break;
200
201 case 3:
202 drawTile(canvas, letters[0], colors[0], fgColor, 0, 0,
203 size / 2 - 1, size);
204 drawTile(canvas, letters[1], colors[1], fgColor, size / 2 + 1, 0,
205 size, size / 2 - 1);
206 drawTile(canvas, letters[2], colors[2], fgColor, size / 2 + 1,
207 size / 2 + 1, size, size);
208 break;
209
210 case 4:
211 drawTile(canvas, letters[0], colors[0], fgColor, 0, 0,
212 size / 2 - 1, size / 2 - 1);
213 drawTile(canvas, letters[1], colors[1], fgColor, 0, size / 2 + 1,
214 size / 2 - 1, size);
215 drawTile(canvas, letters[2], colors[2], fgColor, size / 2 + 1, 0,
216 size, size / 2 - 1);
217 drawTile(canvas, letters[3], colors[3], fgColor, size / 2 + 1,
218 size / 2 + 1, size, size);
219 break;
220 }
221
222 return bitmap;
223 }
224
225 private static Bitmap getMucContactPicture(Conversation conversation,
226 int size, int bgColor, int fgColor) {
227 List<User> members = conversation.getMucOptions().getUsers();
228 if (members.size() == 0) {
229 return getUnknownContactPicture(
230 new String[] { conversation.getName() }, size, bgColor,
231 fgColor);
232 }
233 ArrayList<String> names = new ArrayList<String>();
234 names.add(conversation.getMucOptions().getActualNick());
235 for (User user : members) {
236 names.add(user.getName());
237 if (names.size() > 4) {
238 break;
239 }
240 }
241 String[] mArrayNames = new String[names.size()];
242 names.toArray(mArrayNames);
243 return getUnknownContactPicture(mArrayNames, size, bgColor, fgColor);
244 }
245
246 public static Bitmap getContactPicture(Conversation conversation,
247 int dpSize, Context context, boolean notification) {
248 if (conversation.getMode() == Conversation.MODE_SINGLE) {
249 return getContactPicture(conversation.getContact(), dpSize,
250 context, notification);
251 } else {
252 int fgColor = UIHelper.FG_COLOR, bgColor = (notification) ? UIHelper.BG_COLOR
253 : UIHelper.TRANSPARENT;
254
255 return getMucContactPicture(conversation,
256 getRealPx(dpSize, context), bgColor, fgColor);
257 }
258 }
259
260 public static Bitmap getContactPicture(Contact contact, int dpSize,
261 Context context, boolean notification) {
262 String uri = contact.getProfilePhoto();
263 if (uri == null) {
264 return getContactPicture(contact.getDisplayName(), dpSize, context,
265 notification);
266 }
267 try {
268 Bitmap bm = BitmapFactory.decodeStream(context.getContentResolver()
269 .openInputStream(Uri.parse(uri)));
270 return Bitmap.createScaledBitmap(bm, getRealPx(dpSize, context),
271 getRealPx(dpSize, context), false);
272 } catch (FileNotFoundException e) {
273 return getContactPicture(contact.getDisplayName(), dpSize, context,
274 notification);
275 }
276 }
277
278 public static Bitmap getContactPicture(String name, int dpSize,
279 Context context, boolean notification) {
280 int fgColor = UIHelper.FG_COLOR, bgColor = (notification) ? UIHelper.BG_COLOR
281 : UIHelper.TRANSPARENT;
282
283 return getUnknownContactPicture(new String[] { name },
284 getRealPx(dpSize, context), bgColor, fgColor);
285 }
286
287 public static void showErrorNotification(Context context,
288 List<Account> accounts) {
289 NotificationManager mNotificationManager = (NotificationManager) context
290 .getSystemService(Context.NOTIFICATION_SERVICE);
291 List<Account> accountsWproblems = new ArrayList<Account>();
292 for (Account account : accounts) {
293 if (account.hasErrorStatus()) {
294 accountsWproblems.add(account);
295 }
296 }
297 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
298 context);
299 if (accountsWproblems.size() == 0) {
300 mNotificationManager.cancel(1111);
301 return;
302 } else if (accountsWproblems.size() == 1) {
303 mBuilder.setContentTitle(context
304 .getString(R.string.problem_connecting_to_account));
305 mBuilder.setContentText(accountsWproblems.get(0).getJid());
306 } else {
307 mBuilder.setContentTitle(context
308 .getString(R.string.problem_connecting_to_accounts));
309 mBuilder.setContentText(context.getString(R.string.touch_to_fix));
310 }
311 mBuilder.setOngoing(true);
312 mBuilder.setLights(0xffffffff, 2000, 4000);
313 mBuilder.setSmallIcon(R.drawable.ic_notification);
314 TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
315 stackBuilder.addParentStack(ConversationActivity.class);
316
317 Intent manageAccountsIntent = new Intent(context,
318 ManageAccountActivity.class);
319 stackBuilder.addNextIntent(manageAccountsIntent);
320
321 PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
322 PendingIntent.FLAG_UPDATE_CURRENT);
323
324 mBuilder.setContentIntent(resultPendingIntent);
325 Notification notification = mBuilder.build();
326 mNotificationManager.notify(1111, notification);
327 }
328
329 public static void prepareContactBadge(final Activity activity,
330 QuickContactBadge badge, final Contact contact, Context context) {
331 if (contact.getSystemAccount() != null) {
332 String[] systemAccount = contact.getSystemAccount().split("#");
333 long id = Long.parseLong(systemAccount[0]);
334 badge.assignContactUri(Contacts.getLookupUri(id, systemAccount[1]));
335 }
336 badge.setImageBitmap(contact.getImage(72, context));
337 }
338
339 @SuppressLint("InflateParams")
340 public static AlertDialog getVerifyFingerprintDialog(
341 final ConversationActivity activity,
342 final Conversation conversation, final View msg) {
343 final Contact contact = conversation.getContact();
344 final Account account = conversation.getAccount();
345
346 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
347 builder.setTitle("Verify fingerprint");
348 LayoutInflater inflater = activity.getLayoutInflater();
349 View view = inflater.inflate(R.layout.dialog_verify_otr, null);
350 TextView jid = (TextView) view.findViewById(R.id.verify_otr_jid);
351 TextView fingerprint = (TextView) view
352 .findViewById(R.id.verify_otr_fingerprint);
353 TextView yourprint = (TextView) view
354 .findViewById(R.id.verify_otr_yourprint);
355
356 jid.setText(contact.getJid());
357 fingerprint.setText(conversation.getOtrFingerprint());
358 yourprint.setText(account.getOtrFingerprint());
359 builder.setNegativeButton("Cancel", null);
360 builder.setPositiveButton("Verify", new OnClickListener() {
361
362 @Override
363 public void onClick(DialogInterface dialog, int which) {
364 contact.addOtrFingerprint(conversation.getOtrFingerprint());
365 msg.setVisibility(View.GONE);
366 activity.xmppConnectionService.syncRosterToDisk(account);
367 }
368 });
369 builder.setView(view);
370 return builder.create();
371 }
372
373 public static Bitmap getSelfContactPicture(Account account, int size,
374 boolean showPhoneSelfContactPicture, Context context) {
375 if (showPhoneSelfContactPicture) {
376 Uri selfiUri = PhoneHelper.getSefliUri(context);
377 if (selfiUri != null) {
378 try {
379 return BitmapFactory.decodeStream(context
380 .getContentResolver().openInputStream(selfiUri));
381 } catch (FileNotFoundException e) {
382 return getContactPicture(account.getJid(), size, context,
383 false);
384 }
385 }
386 return getContactPicture(account.getJid(), size, context, false);
387 } else {
388 return getContactPicture(account.getJid(), size, context, false);
389 }
390 }
391
392 private final static class EmoticonPattern {
393 Pattern pattern;
394 String replacement;
395
396 EmoticonPattern(String ascii, int unicode) {
397 this.pattern = Pattern.compile("(?<=(^|\\s))" + ascii
398 + "(?=(\\s|$))");
399 this.replacement = new String(new int[] { unicode, }, 0, 1);
400 }
401
402 String replaceAll(String body) {
403 return pattern.matcher(body).replaceAll(replacement);
404 }
405 }
406
407 private static final EmoticonPattern[] patterns = new EmoticonPattern[] {
408 new EmoticonPattern(":-?D", 0x1f600),
409 new EmoticonPattern("\\^\\^", 0x1f601),
410 new EmoticonPattern(":'D", 0x1f602),
411 new EmoticonPattern("\\]-?D", 0x1f608),
412 new EmoticonPattern(";-?\\)", 0x1f609),
413 new EmoticonPattern(":-?\\)", 0x1f60a),
414 new EmoticonPattern("[B8]-?\\)", 0x1f60e),
415 new EmoticonPattern(":-?\\|", 0x1f610),
416 new EmoticonPattern(":-?[/\\\\]", 0x1f615),
417 new EmoticonPattern(":-?\\*", 0x1f617),
418 new EmoticonPattern(":-?[Ppb]", 0x1f61b),
419 new EmoticonPattern(":-?\\(", 0x1f61e),
420 new EmoticonPattern(":-?[0Oo]", 0x1f62e),
421 new EmoticonPattern("\\\\o/", 0x1F631), };
422
423 public static String transformAsciiEmoticons(String body) {
424 if (body != null) {
425 for (EmoticonPattern p : patterns) {
426 body = p.replaceAll(body);
427 }
428 body = body.trim();
429 }
430 return body;
431 }
432}