d2939c87aeb19a75eaae1c8298d9216789386821
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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
.Arrays
;
22 import java
.util
.Date
;
23 import java
.util
.HashMap
;
24 import java
.util
.HashSet
;
28 * A helper class for some string operations.
30 * @author Bartek Przybylski
31 * @author David A. Velasco
33 public class DisplayUtils
{
35 private static String TAG
= DisplayUtils
.class.getSimpleName();
37 private static final String
[] sizeSuffixes
= { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
39 private static HashMap
<String
, String
> mimeType2HUmanReadable
;
41 mimeType2HUmanReadable
= new HashMap
<String
, String
>();
43 mimeType2HUmanReadable
.put("image/jpeg", "JPEG image");
44 mimeType2HUmanReadable
.put("image/jpg", "JPEG image");
45 mimeType2HUmanReadable
.put("image/png", "PNG image");
46 mimeType2HUmanReadable
.put("image/bmp", "Bitmap image");
47 mimeType2HUmanReadable
.put("image/gif", "GIF image");
48 mimeType2HUmanReadable
.put("image/svg+xml", "JPEG image");
49 mimeType2HUmanReadable
.put("image/tiff", "TIFF image");
51 mimeType2HUmanReadable
.put("audio/mpeg", "MP3 music file");
52 mimeType2HUmanReadable
.put("application/ogg", "OGG music file");
56 private static final String TYPE_APPLICATION
= "application";
57 private static final String TYPE_AUDIO
= "audio";
58 private static final String TYPE_IMAGE
= "image";
59 private static final String TYPE_TXT
= "text";
60 private static final String TYPE_VIDEO
= "video";
62 private static final String SUBTYPE_PDF
= "pdf";
63 private static final String
[] SUBTYPES_DOCUMENT
= { "msword", "mspowerpoint", "msexcel",
64 "vnd.oasis.opendocument.presentation",
65 "vnd.oasis.opendocument.spreadsheet",
66 "vnd.oasis.opendocument.text"
68 private static Set
<String
> SUBTYPES_DOCUMENT_SET
= new HashSet
<String
>(Arrays
.asList(SUBTYPES_DOCUMENT
));
69 private static final String
[] SUBTYPES_COMPRESSED
= {"x-tar", "x-gzip", "zip"};
70 private static final Set
<String
> SUBTYPES_COMPRESSED_SET
= new HashSet
<String
>(Arrays
.asList(SUBTYPES_COMPRESSED
));
73 * Converts the file size in bytes to human readable output.
75 * @param bytes Input file size
76 * @return Like something readable like "12 MB"
78 public static String
bytesToHumanReadable(long bytes
) {
79 double result
= bytes
;
81 while (result
> 1024 && attachedsuff
< sizeSuffixes
.length
) {
85 result
= ((int) (result
* 100)) / 100.;
86 return result
+ " " + sizeSuffixes
[attachedsuff
];
90 * Removes special HTML entities from a string
92 * @param s Input string
93 * @return A cleaned version of the string
95 public static String
HtmlDecode(String s
) {
97 * TODO: Perhaps we should use something more proven like:
98 * http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml%28java.lang.String%29
102 for (int i
= 0; i
< s
.length(); ++i
) {
103 if (s
.charAt(i
) == '%') {
104 ret
+= (char) Integer
.parseInt(s
.substring(i
+ 1, i
+ 3), 16);
114 * Converts MIME types like "image/jpg" to more end user friendly output
117 * @param mimetype MIME type to convert
118 * @return A human friendly version of the MIME type
120 public static String
convertMIMEtoPrettyPrint(String mimetype
) {
121 if (mimeType2HUmanReadable
.containsKey(mimetype
)) {
122 return mimeType2HUmanReadable
.get(mimetype
);
124 if (mimetype
.split("/").length
>= 2)
125 return mimetype
.split("/")[1].toUpperCase() + " file";
126 return "Unknown type";
131 * Returns the resource identifier of an image resource to use as icon associated to a
134 * @param mimetype MIME type string.
135 * @return Resource identifier of an image resource.
137 public static int getResourceId(String mimetype
) {
139 if (mimetype
== null
|| "DIR".equals(mimetype
)) {
140 return R
.drawable
.ic_menu_archive
;
143 String
[] parts
= mimetype
.split("/");
144 String type
= parts
[0];
145 String subtype
= (parts
.length
> 1) ? parts
[1] : "";
147 if(TYPE_TXT
.equals(type
)) {
148 return R
.drawable
.file_doc
;
150 } else if(TYPE_IMAGE
.equals(type
)) {
151 return R
.drawable
.file_image
;
153 } else if(TYPE_VIDEO
.equals(type
)) {
154 return R
.drawable
.file_movie
;
156 } else if(TYPE_AUDIO
.equals(type
)) {
157 return R
.drawable
.file_sound
;
159 } else if(TYPE_APPLICATION
.equals(type
)) {
161 if (SUBTYPE_PDF
.equals(subtype
)) {
162 return R
.drawable
.file_pdf
;
164 } else if (SUBTYPES_DOCUMENT_SET
.contains(subtype
)) {
165 return R
.drawable
.file_doc
;
167 } else if (SUBTYPES_COMPRESSED_SET
.contains(subtype
)) {
168 return R
.drawable
.file_zip
;
172 // problems: RAR, RTF, 3GP are send as application/octet-stream from the server ; extension in the filename should be explicitly reviewed
176 return R
.drawable
.file
;
182 * Converts Unix time to human readable format
183 * @param miliseconds that have passed since 01/01/1970
184 * @return The human readable time for the users locale
186 public static String
unixTimeToHumanReadable(long milliseconds
) {
187 Date date
= new Date(milliseconds
);
188 return date
.toLocaleString();