forked from thunderbird/thunderbird-android
/
Utility.java
492 lines (443 loc) · 17.6 KB
/
Utility.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package com.fsck.k9.helper;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.ContactsContract;
import android.text.Editable;
import android.text.TextUtils;
import timber.log.Timber;
import android.widget.EditText;
import android.widget.TextView;
import com.fsck.k9.ui.ContactBadge;
import com.fsck.k9.mail.Address;
import org.apache.james.mime4j.util.MimeUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Utility {
// \u00A0 (non-breaking space) happens to be used by French MUA
// Note: no longer using the ^ beginning character combined with (...)+
// repetition matching as we might want to strip ML tags. Ex:
// Re: [foo] Re: RE : [foo] blah blah blah
private static final Pattern RESPONSE_PATTERN = Pattern.compile(
"((Re|Fw|Fwd|Aw|R\\u00E9f\\.)(\\[\\d+\\])?[\\u00A0 ]?: *)+", Pattern.CASE_INSENSITIVE);
/**
* Mailing-list tag pattern to match strings like "[foobar] "
*/
private static final Pattern TAG_PATTERN = Pattern.compile("\\[[-_a-z0-9]+\\] ",
Pattern.CASE_INSENSITIVE);
private static Handler sMainThreadHandler;
public static boolean arrayContains(Object[] a, Object o) {
for (Object element : a) {
if (element.equals(o)) {
return true;
}
}
return false;
}
public static boolean isAnyMimeType(String o, String... a) {
for (String element : a) {
if (MimeUtil.isSameMimeType(element, o)) {
return true;
}
}
return false;
}
public static boolean arrayContainsAny(Object[] a, Object... o) {
for (Object element : a) {
if (arrayContains(o, element)) {
return true;
}
}
return false;
}
/**
* Combines the given array of Objects into a single String using
* each Object's toString() method and the separator character
* between each part.
*
* @param parts
* @param separator
* @return new String
*/
public static String combine(Object[] parts, char separator) {
if (parts == null) {
return null;
}
return TextUtils.join(String.valueOf(separator), parts);
}
/**
* Combines the given Objects into a single String using
* each Object's toString() method and the separator character
* between each part.
*
* @param parts
* @param separator
* @return new String
*/
public static String combine(Iterable<?> parts, char separator) {
if (parts == null) {
return null;
}
return TextUtils.join(String.valueOf(separator), parts);
}
public static boolean requiredFieldValid(TextView view) {
return view.getText() != null && view.getText().length() > 0;
}
public static boolean requiredFieldValid(Editable s) {
return s != null && s.length() > 0;
}
public static boolean domainFieldValid(EditText view) {
if (view.getText() != null) {
String s = view.getText().toString();
if (s.matches("^([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)*[a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?$") &&
s.length() <= 253) {
return true;
}
if (s.matches("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")) {
return true;
}
}
return false;
}
/*
* TODO disabled this method globally. It is used in all the settings screens but I just
* noticed that an unrelated icon was dimmed. Android must share drawables internally.
*/
public static void setCompoundDrawablesAlpha(TextView view, int alpha) {
// Drawable[] drawables = view.getCompoundDrawables();
// for (Drawable drawable : drawables) {
// if (drawable != null) {
// drawable.setAlpha(alpha);
// }
// }
}
/**
* <p>Wraps a multiline string of text, identifying words by <code>' '</code>.</p>
*
* <p>New lines will be separated by the system property line separator.
* Very long words, such as URLs will <i>not</i> be wrapped.</p>
*
* <p>Leading spaces on a new line are stripped.
* Trailing spaces are not stripped.</p>
*
* <pre>
* WordUtils.wrap(null, *) = null
* WordUtils.wrap("", *) = ""
* </pre>
*
* Adapted from the Apache Commons Lang library.
* http://svn.apache.org/viewvc/commons/proper/lang
* /trunk/src/main/java/org/apache/commons/lang3/text/WordUtils.java
* SVN Revision 925967, Mon Mar 22 06:16:49 2010 UTC
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @param str the String to be word wrapped, may be null
* @param wrapLength the column to wrap the words at, less than 1 is treated as 1
* @return a line with newlines inserted, <code>null</code> if null input
*/
private static final String NEWLINE_REGEX = "(?:\\r?\\n)";
public static String wrap(String str, int wrapLength) {
StringBuilder result = new StringBuilder();
for (String piece : str.split(NEWLINE_REGEX)) {
result.append(wrap(piece, wrapLength, null, false));
result.append("\r\n");
}
return result.toString();
}
/**
* <p>Wraps a single line of text, identifying words by <code>' '</code>.</p>
*
* <p>Leading spaces on a new line are stripped.
* Trailing spaces are not stripped.</p>
*
* <pre>
* WordUtils.wrap(null, *, *, *) = null
* WordUtils.wrap("", *, *, *) = ""
* </pre>
*
* This is from the Apache Commons Lang library.
* http://svn.apache.org/viewvc/commons/proper/lang
* /trunk/src/main/java/org/apache/commons/lang3/text/WordUtils.java
* SVN Revision 925967, Mon Mar 22 06:16:49 2010 UTC
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @param str the String to be word wrapped, may be null
* @param wrapLength the column to wrap the words at, less than 1 is treated as 1
* @param newLineStr the string to insert for a new line,
* <code>null</code> uses the system property line separator
* @param wrapLongWords true if long words (such as URLs) should be wrapped
* @return a line with newlines inserted, <code>null</code> if null input
*/
public static String wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords) {
if (str == null) {
return null;
}
if (newLineStr == null) {
newLineStr = "\r\n";
}
if (wrapLength < 1) {
wrapLength = 1;
}
int inputLineLength = str.length();
int offset = 0;
StringBuilder wrappedLine = new StringBuilder(inputLineLength + 32);
while ((inputLineLength - offset) > wrapLength) {
if (str.charAt(offset) == ' ') {
offset++;
continue;
}
int spaceToWrapAt = str.lastIndexOf(' ', wrapLength + offset);
if (spaceToWrapAt >= offset) {
// normal case
wrappedLine.append(str.substring(offset, spaceToWrapAt));
wrappedLine.append(newLineStr);
offset = spaceToWrapAt + 1;
} else {
// really long word or URL
if (wrapLongWords) {
// wrap really long word one line at a time
wrappedLine.append(str.substring(offset, wrapLength + offset));
wrappedLine.append(newLineStr);
offset += wrapLength;
} else {
// do not wrap really long word, just extend beyond limit
spaceToWrapAt = str.indexOf(' ', wrapLength + offset);
if (spaceToWrapAt >= 0) {
wrappedLine.append(str.substring(offset, spaceToWrapAt));
wrappedLine.append(newLineStr);
offset = spaceToWrapAt + 1;
} else {
wrappedLine.append(str.substring(offset));
offset = inputLineLength;
}
}
}
}
// Whatever is left in line is short enough to just pass through
wrappedLine.append(str.substring(offset));
return wrappedLine.toString();
}
/**
* Extract the 'original' subject value, by ignoring leading
* response/forward marker and '[XX]' formatted tags (as many mailing-list
* softwares do).
*
* <p>
* Result is also trimmed.
* </p>
*
* @param subject
* Never <code>null</code>.
* @return Never <code>null</code>.
*/
public static String stripSubject(final String subject) {
int lastPrefix = 0;
final Matcher tagMatcher = TAG_PATTERN.matcher(subject);
String tag = null;
// whether tag stripping logic should be active
boolean tagPresent = false;
// whether the last action stripped a tag
boolean tagStripped = false;
if (tagMatcher.find(0)) {
tagPresent = true;
if (tagMatcher.start() == 0) {
// found at beginning of subject, considering it an actual tag
tag = tagMatcher.group();
// now need to find response marker after that tag
lastPrefix = tagMatcher.end();
tagStripped = true;
}
}
final Matcher matcher = RESPONSE_PATTERN.matcher(subject);
// while:
// - lastPrefix is within the bounds
// - response marker found at lastPrefix position
// (to make sure we don't catch response markers that are part of
// the actual subject)
while (lastPrefix < subject.length() - 1
&& matcher.find(lastPrefix)
&& matcher.start() == lastPrefix
&& (!tagPresent || tag == null || subject.regionMatches(matcher.end(), tag, 0,
tag.length()))) {
lastPrefix = matcher.end();
if (tagPresent) {
tagStripped = false;
if (tag == null) {
// attempt to find tag
if (tagMatcher.start() == lastPrefix) {
tag = tagMatcher.group();
lastPrefix += tag.length();
tagStripped = true;
}
} else if (lastPrefix < subject.length() - 1 && subject.startsWith(tag, lastPrefix)) {
// Re: [foo] Re: [foo] blah blah blah
// ^ ^
// ^ ^
// ^ new position
// ^
// initial position
lastPrefix += tag.length();
tagStripped = true;
}
}
}
// Null pointer check is to make the static analysis component of Eclipse happy.
if (tagStripped && (tag != null)) {
// restore the last tag
lastPrefix -= tag.length();
}
if (lastPrefix > -1 && lastPrefix < subject.length() - 1) {
return subject.substring(lastPrefix).trim();
} else {
return subject.trim();
}
}
public static String stripNewLines(String multiLineString) {
return multiLineString.replaceAll("[\\r\\n]", "");
}
private static final String IMG_SRC_REGEX = "(?is:<img[^>]+src\\s*=\\s*['\"]?([a-z]+)\\:)";
private static final Pattern IMG_PATTERN = Pattern.compile(IMG_SRC_REGEX);
/**
* Figure out if this part has images.
* TODO: should only return true if we're an html part
* @param message Content to evaluate
* @return True if it has external images; false otherwise.
*/
public static boolean hasExternalImages(final String message) {
Matcher imgMatches = IMG_PATTERN.matcher(message);
while (imgMatches.find()) {
String uriScheme = imgMatches.group(1);
if (uriScheme.equals("http") || uriScheme.equals("https")) {
Timber.d("External images found");
return true;
}
}
Timber.d("No external images.");
return false;
}
/**
* Unconditionally close a Cursor. Equivalent to {@link Cursor#close()},
* if cursor is non-null. This is typically used in finally blocks.
*
* @param cursor cursor to close
*/
public static void closeQuietly(final Cursor cursor) {
if (cursor != null) {
cursor.close();
}
}
/**
* Check to see if we have network connectivity.
*/
public static boolean hasConnectivity(final Context context) {
final ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
return false;
}
final NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
return true;
} else {
return false;
}
}
private static final Pattern MESSAGE_ID = Pattern.compile("<" +
"(?:" +
"[a-zA-Z0-9!#$%&'*+\\-/=?^_`{|}~]+" +
"(?:\\.[a-zA-Z0-9!#$%&'*+\\-/=?^_`{|}~]+)*" +
"|" +
"\"(?:[^\\\\\"]|\\\\.)*\"" +
")" +
"@" +
"(?:" +
"[a-zA-Z0-9!#$%&'*+\\-/=?^_`{|}~]+" +
"(?:\\.[a-zA-Z0-9!#$%&'*+\\-/=?^_`{|}~]+)*" +
"|" +
"\\[(?:[^\\\\\\]]|\\\\.)*\\]" +
")" +
">");
public static List<String> extractMessageIds(final String text) {
List<String> messageIds = new ArrayList<String>();
Matcher matcher = MESSAGE_ID.matcher(text);
int start = 0;
while (matcher.find(start)) {
String messageId = text.substring(matcher.start(), matcher.end());
messageIds.add(messageId);
start = matcher.end();
}
return messageIds;
}
public static String extractMessageId(final String text) {
Matcher matcher = MESSAGE_ID.matcher(text);
if (matcher.find()) {
return text.substring(matcher.start(), matcher.end());
}
return null;
}
/**
* @return a {@link Handler} tied to the main thread.
*/
public static Handler getMainThreadHandler() {
if (sMainThreadHandler == null) {
// No need to synchronize -- it's okay to create an extra Handler, which will be used
// only once and then thrown away.
sMainThreadHandler = new Handler(Looper.getMainLooper());
}
return sMainThreadHandler;
}
/**
* Assign the contact to the badge.
*
* On 4.3, we pass the address name as extra info so that if the contact doesn't exist
* the name is auto-populated.
*
* @param contactBadge the badge to the set the contact for
* @param address the address to look for a contact for.
*/
public static void setContactForBadge(ContactBadge contactBadge,
Address address) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
Bundle extraContactInfo = new Bundle();
extraContactInfo.putString(ContactsContract.Intents.Insert.NAME, address.getPersonal());
contactBadge.assignContactFromEmail(address.getAddress(), true, extraContactInfo);
} else {
contactBadge.assignContactFromEmail(address.getAddress(), true);
}
}
}