2008-11-01 17:32:06 -04:00
|
|
|
|
2010-05-19 14:17:06 -04:00
|
|
|
package com.fsck.k9.helper;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2012-10-01 13:37:51 -04:00
|
|
|
import android.app.Application;
|
|
|
|
import android.content.Context;
|
2011-10-20 02:25:27 -04:00
|
|
|
import android.database.Cursor;
|
2012-10-01 13:37:51 -04:00
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.NetworkInfo;
|
2013-03-23 22:41:40 -04:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
2009-12-09 22:16:42 -05:00
|
|
|
import android.text.Editable;
|
2014-02-15 16:19:58 -05:00
|
|
|
import android.text.TextUtils;
|
2010-12-18 17:56:40 -05:00
|
|
|
import android.util.Log;
|
2009-12-09 22:16:42 -05:00
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.TextView;
|
2010-12-18 17:56:40 -05:00
|
|
|
|
|
|
|
import com.fsck.k9.K9;
|
2009-12-09 22:16:42 -05:00
|
|
|
|
2014-09-28 06:39:32 -04:00
|
|
|
import java.nio.charset.Charset;
|
2012-10-08 16:51:29 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2010-09-21 18:12:45 -04:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public class Utility {
|
2010-09-21 18:12:45 -04:00
|
|
|
|
|
|
|
// \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(
|
2010-10-05 02:04:28 -04:00
|
|
|
"((Re|Fw|Fwd|Aw|R\\u00E9f\\.)(\\[\\d+\\])?[\\u00A0 ]?: *)+", Pattern.CASE_INSENSITIVE);
|
2010-09-21 18:12:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mailing-list tag pattern to match strings like "[foobar] "
|
|
|
|
*/
|
|
|
|
private static final Pattern TAG_PATTERN = Pattern.compile("\\[[-_a-z0-9]+\\] ",
|
|
|
|
Pattern.CASE_INSENSITIVE);
|
|
|
|
|
2013-03-23 22:41:40 -04:00
|
|
|
private static Handler sMainThreadHandler;
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public static boolean arrayContains(Object[] a, Object o) {
|
|
|
|
for (Object element : a) {
|
|
|
|
if (element.equals(o)) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-07 06:03:04 -05:00
|
|
|
public static boolean arrayContainsAny(Object[] a, Object... o) {
|
|
|
|
for (Object element : a) {
|
|
|
|
if (arrayContains(o, element)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
/**
|
2011-05-31 09:11:36 -04:00
|
|
|
* Combines the given array of Objects into a single String using
|
|
|
|
* each Object's toString() method and the separator character
|
|
|
|
* between each part.
|
2008-11-01 17:32:06 -04:00
|
|
|
*
|
|
|
|
* @param parts
|
2011-05-31 09:11:36 -04:00
|
|
|
* @param separator
|
|
|
|
* @return new String
|
2008-11-01 17:32:06 -04:00
|
|
|
*/
|
2011-05-31 09:11:36 -04:00
|
|
|
public static String combine(Object[] parts, char separator) {
|
2011-02-06 17:09:48 -05:00
|
|
|
if (parts == null) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return null;
|
|
|
|
}
|
2014-02-15 16:19:58 -05:00
|
|
|
return TextUtils.join(String.valueOf(separator), parts);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2014-10-04 05:37:30 -04:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public static boolean requiredFieldValid(TextView view) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return view.getText() != null && view.getText().length() > 0;
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public static boolean requiredFieldValid(Editable s) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return s != null && s.length() > 0;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public static boolean domainFieldValid(EditText view) {
|
|
|
|
if (view.getText() != null) {
|
2009-01-18 19:48:20 -05:00
|
|
|
String s = view.getText().toString();
|
2011-08-25 18:07:30 -04:00
|
|
|
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) {
|
2009-01-18 19:48:20 -05:00
|
|
|
return true;
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
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]?)$")) {
|
2009-01-18 19:48:20 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-18 19:20:56 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
/**
|
|
|
|
* A fast version of URLDecoder.decode() that works only with UTF-8 and does only two
|
|
|
|
* allocations. This version is around 3x as fast as the standard one and I'm using it
|
|
|
|
* hundreds of times in places that slow down the UI, so it helps.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public static String fastUrlDecode(String s) {
|
2014-09-28 06:39:32 -04:00
|
|
|
|
|
|
|
byte[] bytes = s.getBytes(Charset.forName("UTF-8"));
|
2008-11-01 17:32:06 -04:00
|
|
|
byte ch;
|
|
|
|
int length = 0;
|
2011-02-06 17:09:48 -05:00
|
|
|
for (int i = 0, count = bytes.length; i < count; i++) {
|
2008-11-01 17:32:06 -04:00
|
|
|
ch = bytes[i];
|
2011-02-06 17:09:48 -05:00
|
|
|
if (ch == '%') {
|
2008-11-01 17:32:06 -04:00
|
|
|
int h = (bytes[i + 1] - '0');
|
|
|
|
int l = (bytes[i + 2] - '0');
|
2011-02-06 17:09:48 -05:00
|
|
|
if (h > 9) {
|
2008-11-01 17:32:06 -04:00
|
|
|
h -= 7;
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (l > 9) {
|
2008-11-01 17:32:06 -04:00
|
|
|
l -= 7;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
bytes[length] = (byte)((h << 4) | l);
|
2008-11-01 17:32:06 -04:00
|
|
|
i += 2;
|
2011-02-06 17:09:48 -05:00
|
|
|
} else if (ch == '+') {
|
2008-11-01 17:32:06 -04:00
|
|
|
bytes[length] = ' ';
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2008-11-01 17:32:06 -04:00
|
|
|
bytes[length] = bytes[i];
|
|
|
|
}
|
|
|
|
length++;
|
|
|
|
}
|
2014-09-28 06:39:32 -04:00
|
|
|
return new String(bytes, 0, length, Charset.forName("UTF-8"));
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public static void setCompoundDrawablesAlpha(TextView view, int alpha) {
|
2008-11-01 17:32:06 -04:00
|
|
|
// Drawable[] drawables = view.getCompoundDrawables();
|
|
|
|
// for (Drawable drawable : drawables) {
|
|
|
|
// if (drawable != null) {
|
|
|
|
// drawable.setAlpha(alpha);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
}
|
2008-12-18 19:20:56 -05:00
|
|
|
|
2010-08-18 22:49:13 -04:00
|
|
|
/**
|
|
|
|
* <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)";
|
2011-02-06 17:09:48 -05:00
|
|
|
public static String wrap(String str, int wrapLength) {
|
2010-08-18 22:49:13 -04:00
|
|
|
StringBuilder result = new StringBuilder();
|
2011-02-06 17:09:48 -05:00
|
|
|
for (String piece : str.split(NEWLINE_REGEX)) {
|
2010-08-18 22:49:13 -04:00
|
|
|
result.append(wrap(piece, wrapLength, null, false));
|
2013-10-08 17:07:21 -04:00
|
|
|
result.append("\r\n");
|
2010-08-18 22:49:13 -04:00
|
|
|
}
|
|
|
|
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
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public static String wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords) {
|
|
|
|
if (str == null) {
|
2010-08-18 22:49:13 -04:00
|
|
|
return null;
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (newLineStr == null) {
|
2013-10-08 17:07:21 -04:00
|
|
|
newLineStr = "\r\n";
|
2010-08-18 22:49:13 -04:00
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (wrapLength < 1) {
|
2010-08-18 22:49:13 -04:00
|
|
|
wrapLength = 1;
|
|
|
|
}
|
|
|
|
int inputLineLength = str.length();
|
|
|
|
int offset = 0;
|
|
|
|
StringBuilder wrappedLine = new StringBuilder(inputLineLength + 32);
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
while ((inputLineLength - offset) > wrapLength) {
|
|
|
|
if (str.charAt(offset) == ' ') {
|
2010-08-18 22:49:13 -04:00
|
|
|
offset++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int spaceToWrapAt = str.lastIndexOf(' ', wrapLength + offset);
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (spaceToWrapAt >= offset) {
|
2010-08-18 22:49:13 -04:00
|
|
|
// normal case
|
|
|
|
wrappedLine.append(str.substring(offset, spaceToWrapAt));
|
|
|
|
wrappedLine.append(newLineStr);
|
|
|
|
offset = spaceToWrapAt + 1;
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2010-08-18 22:49:13 -04:00
|
|
|
// really long word or URL
|
2011-02-06 17:09:48 -05:00
|
|
|
if (wrapLongWords) {
|
2010-08-18 22:49:13 -04:00
|
|
|
// wrap really long word one line at a time
|
|
|
|
wrappedLine.append(str.substring(offset, wrapLength + offset));
|
|
|
|
wrappedLine.append(newLineStr);
|
|
|
|
offset += wrapLength;
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2010-08-18 22:49:13 -04:00
|
|
|
// do not wrap really long word, just extend beyond limit
|
|
|
|
spaceToWrapAt = str.indexOf(' ', wrapLength + offset);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (spaceToWrapAt >= 0) {
|
2010-08-18 22:49:13 -04:00
|
|
|
wrappedLine.append(str.substring(offset, spaceToWrapAt));
|
|
|
|
wrappedLine.append(newLineStr);
|
|
|
|
offset = spaceToWrapAt + 1;
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2010-08-18 22:49:13 -04:00
|
|
|
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();
|
|
|
|
}
|
2010-09-21 18:12:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract the 'original' subject value, by ignoring leading
|
|
|
|
* response/forward marker and '[XX]' formatted tags (as many mailing-list
|
|
|
|
* softwares do).
|
2010-10-05 02:04:28 -04:00
|
|
|
*
|
2010-09-21 18:12:45 -04:00
|
|
|
* <p>
|
|
|
|
* Result is also trimmed.
|
|
|
|
* </p>
|
2010-10-05 02:04:28 -04:00
|
|
|
*
|
2010-09-21 18:12:45 -04:00
|
|
|
* @param subject
|
|
|
|
* Never <code>null</code>.
|
|
|
|
* @return Never <code>null</code>.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public static String stripSubject(final String subject) {
|
2010-09-21 18:12:45 -04:00
|
|
|
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;
|
2011-02-06 17:09:48 -05:00
|
|
|
if (tagMatcher.find(0)) {
|
2010-09-21 18:12:45 -04:00
|
|
|
tagPresent = true;
|
2011-02-06 17:09:48 -05:00
|
|
|
if (tagMatcher.start() == 0) {
|
2010-09-21 18:12:45 -04:00
|
|
|
// 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,
|
2011-02-06 17:09:48 -05:00
|
|
|
tag.length()))) {
|
2010-09-21 18:12:45 -04:00
|
|
|
lastPrefix = matcher.end();
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (tagPresent) {
|
2010-09-21 18:12:45 -04:00
|
|
|
tagStripped = false;
|
2011-02-06 17:09:48 -05:00
|
|
|
if (tag == null) {
|
2010-09-21 18:12:45 -04:00
|
|
|
// attempt to find tag
|
2011-02-06 17:09:48 -05:00
|
|
|
if (tagMatcher.start() == lastPrefix) {
|
2010-09-21 18:12:45 -04:00
|
|
|
tag = tagMatcher.group();
|
|
|
|
lastPrefix += tag.length();
|
|
|
|
tagStripped = true;
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
} else if (lastPrefix < subject.length() - 1 && subject.startsWith(tag, lastPrefix)) {
|
2010-09-21 18:12:45 -04:00
|
|
|
// Re: [foo] Re: [foo] blah blah blah
|
|
|
|
// ^ ^
|
|
|
|
// ^ ^
|
|
|
|
// ^ new position
|
|
|
|
// ^
|
|
|
|
// initial position
|
|
|
|
lastPrefix += tag.length();
|
|
|
|
tagStripped = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-18 19:00:58 -05:00
|
|
|
// Null pointer check is to make the static analysis component of Eclipse happy.
|
2011-02-06 17:09:48 -05:00
|
|
|
if (tagStripped && (tag != null)) {
|
2010-09-21 18:12:45 -04:00
|
|
|
// restore the last tag
|
|
|
|
lastPrefix -= tag.length();
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (lastPrefix > -1 && lastPrefix < subject.length() - 1) {
|
2010-09-21 18:12:45 -04:00
|
|
|
return subject.substring(lastPrefix).trim();
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2010-09-21 18:12:45 -04:00
|
|
|
return subject.trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-28 04:07:59 -05:00
|
|
|
|
|
|
|
private static final String IMG_SRC_REGEX = "(?is:<img[^>]+src\\s*=\\s*['\"]?([a-z]+)\\:)";
|
2010-12-29 02:17:43 -05:00
|
|
|
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.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public static boolean hasExternalImages(final String message) {
|
2010-12-29 02:17:43 -05:00
|
|
|
Matcher imgMatches = IMG_PATTERN.matcher(message);
|
2011-02-06 17:09:48 -05:00
|
|
|
while (imgMatches.find()) {
|
|
|
|
if (!imgMatches.group(1).equals("content")) {
|
|
|
|
if (K9.DEBUG) {
|
2010-12-28 04:07:59 -05:00
|
|
|
Log.d(K9.LOG_TAG, "External images found");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (K9.DEBUG) {
|
2010-12-28 04:07:59 -05:00
|
|
|
Log.d(K9.LOG_TAG, "No external images.");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-20 02:25:27 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
}
|
2012-04-01 15:14:43 -04:00
|
|
|
|
2012-10-01 13:37:51 -04:00
|
|
|
/**
|
|
|
|
* Check to see if we have network connectivity.
|
|
|
|
* @param app Current application (Hint: see if your base class has a getApplication() method.)
|
|
|
|
* @return true if we have connectivity, false otherwise.
|
|
|
|
*/
|
|
|
|
public static boolean hasConnectivity(final Application app) {
|
|
|
|
final ConnectivityManager connectivityManager =
|
|
|
|
(ConnectivityManager) app.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;
|
|
|
|
}
|
|
|
|
}
|
2012-10-08 16:51:29 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2012-10-26 23:09:58 -04:00
|
|
|
|
2013-03-23 22:41:40 -04:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
2014-10-04 06:45:45 -04:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|