Fixed local path NULL when making favourite a file not down ; fixed change of local...
[pub/Android/ownCloud.git] / src / com / owncloud / android / DisplayUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android;
20
21 import java.util.Date;
22 import java.util.HashMap;
23
24 /**
25 * A helper class for some string operations.
26 *
27 * @author Bartek Przybylski
28 *
29 */
30 public class DisplayUtils {
31
32 private static final String[] suffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
33
34 private static HashMap<String, String> mimeType2HUmanReadable;
35 static {
36 mimeType2HUmanReadable = new HashMap<String, String>();
37 // images
38 mimeType2HUmanReadable.put("image/jpeg", "JPEG image");
39 mimeType2HUmanReadable.put("image/jpg", "JPEG image");
40 mimeType2HUmanReadable.put("image/png", "PNG image");
41 mimeType2HUmanReadable.put("image/bmp", "Bitmap image");
42 mimeType2HUmanReadable.put("image/gif", "GIF image");
43 mimeType2HUmanReadable.put("image/svg+xml", "JPEG image");
44 mimeType2HUmanReadable.put("image/tiff", "TIFF image");
45 // music
46 mimeType2HUmanReadable.put("audio/mpeg", "MP3 music file");
47 mimeType2HUmanReadable.put("application/ogg", "OGG music file");
48
49 }
50
51 /**
52 * Converts the file size in bytes to human readable output.
53 *
54 * @param bytes Input file size
55 * @return Like something readable like "12 MB"
56 */
57 public static String bytesToHumanReadable(long bytes) {
58 double result = bytes;
59 int attachedsuff = 0;
60 while (result > 1024 && attachedsuff < suffixes.length) {
61 result /= 1024.;
62 attachedsuff++;
63 }
64 result = ((int) (result * 100)) / 100.;
65 return result + " " + suffixes[attachedsuff];
66 }
67
68 /**
69 * Removes special HTML entities from a string
70 *
71 * @param s Input string
72 * @return A cleaned version of the string
73 */
74 public static String HtmlDecode(String s) {
75 /*
76 * TODO: Perhaps we should use something more proven like:
77 * http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml%28java.lang.String%29
78 */
79
80 String ret = "";
81 for (int i = 0; i < s.length(); ++i) {
82 if (s.charAt(i) == '%') {
83 ret += (char) Integer.parseInt(s.substring(i + 1, i + 3), 16);
84 i += 2;
85 } else {
86 ret += s.charAt(i);
87 }
88 }
89 return ret;
90 }
91
92 /**
93 * Converts MIME types like "image/jpg" to more end user friendly output
94 * like "JPG image".
95 *
96 * @param mimetype MIME type to convert
97 * @return A human friendly version of the MIME type
98 */
99 public static String convertMIMEtoPrettyPrint(String mimetype) {
100 if (mimeType2HUmanReadable.containsKey(mimetype)) {
101 return mimeType2HUmanReadable.get(mimetype);
102 }
103 if (mimetype.split("/").length >= 2)
104 return mimetype.split("/")[1].toUpperCase() + " file";
105 return "Unknown type";
106 }
107
108 /**
109 * Converts Unix time to human readable format
110 * @param miliseconds that have passed since 01/01/1970
111 * @return The human readable time for the users locale
112 */
113 public static String unixTimeToHumanReadable(long milliseconds) {
114 Date date = new Date(milliseconds);
115 return date.toLocaleString();
116 }
117 }