-1L
if the file does not exist, or an
14 | * exception is thrown accessing the file.
15 | * Some operating systems may return 0L
for pathnames
16 | * denoting system-dependent entities such as devices or pipes.
17 | */
18 | public static long length(String path) {
19 | // File not found
20 | File file = new File(path);
21 | try {
22 | if (!file.exists()) {
23 | AppLog.w(AppLog.T.MEDIA, "Can't access the file. It doesn't exists anymore?");
24 | return -1L;
25 | }
26 |
27 | return file.length();
28 | } catch (SecurityException e) {
29 | AppLog.e(AppLog.T.MEDIA, "Can't access the file.", e);
30 | return -1L;
31 | }
32 | }
33 |
34 | /**
35 | * Given the full file path, or the filename with extension (i.e. my-picture.jpg), returns the filename part only
36 | * (my-picture).
37 | *
38 | * @param filePath The path to the file or the full filename
39 | * @return filename part only or null
40 | */
41 | public static String getFileNameFromPath(String filePath) {
42 | if (TextUtils.isEmpty(filePath)) {
43 | return null;
44 | }
45 | if (filePath.contains("/")) {
46 | if (filePath.lastIndexOf("/") + 1 >= filePath.length()) {
47 | filePath = filePath.substring(0, filePath.length() - 1);
48 | }
49 | filePath = filePath.substring(filePath.lastIndexOf("/") + 1);
50 | }
51 |
52 | String filename;
53 | int dotPos = filePath.indexOf('.');
54 | if (dotPos > 0) {
55 | filename = filePath.substring(0, dotPos);
56 | } else {
57 | filename = filePath;
58 | }
59 | return filename;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/WordPressUtils/src/main/java/org/wordpress/android/util/FormatUtils.java:
--------------------------------------------------------------------------------
1 | package org.wordpress.android.util;
2 |
3 | import java.text.DecimalFormat;
4 | import java.text.NumberFormat;
5 |
6 | public class FormatUtils {
7 | /*
8 | * NumberFormat isn't synchronized, so a separate instance must be created for each thread
9 | * http://developer.android.com/reference/java/text/NumberFormat.html
10 | */
11 | private static final ThreadLocal0) { 46 | str = str.replaceAll("
", "\n
");
47 | }
48 |
49 | // convert BR tags to line breaks
50 | if (str.contains("
", "\n");
52 | }
53 |
54 | // use regex to strip tags, then convert entities in the result
55 | return trimStart(StringEscapeUtils.unescapeHtml4(str.replaceAll("<(.|\n)*?>", "")));
56 | }
57 |
58 | /*
59 | * Same as apache.commons.lang.StringUtils.stripStart() but also removes non-breaking
60 | * space (160) chars
61 | */
62 | private static String trimStart(final String str) {
63 | int strLen;
64 | if (str == null || (strLen = str.length()) == 0) {
65 | return "";
66 | }
67 | int start = 0;
68 | while (start != strLen && (Character.isWhitespace(str.charAt(start)) || str.charAt(start) == 160)) {
69 | start++;
70 | }
71 | return str.substring(start);
72 | }
73 |
74 | /**
75 | * Converts an R.color.xxx resource to an HTML hex color
76 | * @param context Android Context
77 | * @param resId Android R.color.xxx
78 | * @return A String HTML hex color code
79 | */
80 | public static String colorResToHtmlColor(Context context, int resId) {
81 | try {
82 | return String.format("#%06X", 0xFFFFFF & context.getResources().getColor(resId));
83 | } catch (Resources.NotFoundException e) {
84 | return "#000000";
85 | }
86 | }
87 |
88 | /**
89 | * Remove {@code } blocks from the passed string - added to project after noticing
90 | * comments on posts that use the "Sociable" plugin ( http://wordpress.org/plugins/sociable/ )
91 | * may have a script block which contains {@code } followed by a CDATA section followed by {@code ,}
92 | * all of which will show up if we don't strip it here.
93 | * @see Wordpress Sociable Plugin
94 | * @return String without {@code }, {@code } blocks followed by a CDATA section
95 | * followed by {@code ,}
96 | * @param text String containing script tags
97 | */
98 | public static String stripScript(final String text) {
99 | if (text == null) {
100 | return null;
101 | }
102 |
103 | StringBuilder sb = new StringBuilder(text);
104 | int start = sb.indexOf("", start);
108 | if (end == -1) {
109 | return sb.toString();
110 | }
111 | sb.delete(start, end + 9);
112 | start = sb.indexOf("