1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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.
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.
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/>.
19 package com
.owncloud
.android
;
21 import java
.util
.Date
;
22 import java
.util
.HashMap
;
25 * A helper class for some string operations.
27 * @author Bartek Przybylski
30 public class DisplayUtils
{
32 private static final String
[] suffixes
= { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
34 private static HashMap
<String
, String
> mimeType2HUmanReadable
;
36 mimeType2HUmanReadable
= new HashMap
<String
, String
>();
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");
46 mimeType2HUmanReadable
.put("audio/mpeg", "MP3 music file");
47 mimeType2HUmanReadable
.put("application/ogg", "OGG music file");
52 * Converts the file size in bytes to human readable output.
54 * @param bytes Input file size
55 * @return Like something readable like "12 MB"
57 public static String
bytesToHumanReadable(long bytes
) {
58 double result
= bytes
;
60 while (result
> 1024 && attachedsuff
< suffixes
.length
) {
64 result
= ((int) (result
* 100)) / 100.;
65 return result
+ " " + suffixes
[attachedsuff
];
69 * Removes special HTML entities from a string
71 * @param s Input string
72 * @return A cleaned version of the string
74 public static String
HtmlDecode(String s
) {
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
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);
93 * Converts MIME types like "image/jpg" to more end user friendly output
96 * @param mimetype MIME type to convert
97 * @return A human friendly version of the MIME type
99 public static String
convertMIMEtoPrettyPrint(String mimetype
) {
100 if (mimeType2HUmanReadable
.containsKey(mimetype
)) {
101 return mimeType2HUmanReadable
.get(mimetype
);
103 if (mimetype
.split("/").length
>= 2)
104 return mimetype
.split("/")[1].toUpperCase() + " file";
105 return "Unknown type";
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
113 public static String
unixTimeToHumanReadable(long milliseconds
) {
114 Date date
= new Date(milliseconds
);
115 return date
.toLocaleString();