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
|
package com.mapswithme.util;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import android.text.style.CharacterStyle;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;
import java.util.Locale;
public class StringUtils
{
/**
* Set span for the FIRST occurrence of EACH token.
*
* @param input
* @param spanStyle
* @param tokens
* @return
*/
public static CharSequence setSpansForTokens(String input, CharacterStyle spanStyle, String... tokens)
{
final SpannableStringBuilder spanStrBuilder = new SpannableStringBuilder(input);
for (final String token : tokens)
{
final int indexStart = input.indexOf(token);
if (indexStart != -1)
{
spanStrBuilder.setSpan(
spanStyle,
indexStart,
indexStart + token.length(),
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
}
return spanStrBuilder;
}
public static String formatUsingUsLocale(String pattern, Object... args)
{
return String.format(Locale.US, pattern, args);
}
public static String joinSkipEmpty(String delim, Object[] arr)
{
String res = null;
for (Object item : arr)
{
String s = (item != null) ? item.toString() : null;
if (s != null && s.length() > 0)
res = (res == null) ? s : res + delim + s;
}
return res;
}
public static native boolean isHtml(String text);
/**
* Removes html tags, generated from edittext content after it's transformed to html.
* In version 4.3.1 we converted descriptions, entered by users, to html automatically. Later html conversion was cancelled, but those converted descriptions should be converted back to
* plain text, that's why that ugly util is introduced.
*
* @param text source text
* @return result text
*/
public static String removeEditTextHtmlTags(String text)
{
return text.replaceAll("</p>", "").replaceAll("<br>", "").replaceAll("<p dir=\"ltr\">", "");
}
/**
* Formats size in bytes to "x MB" or "x KB" format.
* @param size Size in bytes
* @return formatted string
*/
public static String getFileSizeString(long size)
{
if (size > Constants.MB)
return (size + Constants.MB / 2) / Constants.MB + " " + MwmApplication.get().getString(R.string.mb);
else
return (size + Constants.KB - 1) / Constants.KB + " " + MwmApplication.get().getString(R.string.kb);
}
public static class SimpleTextWatcher implements TextWatcher
{
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override
public void afterTextChanged(Editable s) { }
}
private StringUtils() {}
}
|