79b56b936791608acc88f12c039a289e1e29101c
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 as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
;
22 import java
.util
.Arrays
;
23 import java
.util
.Date
;
24 import java
.util
.HashMap
;
25 import java
.util
.HashSet
;
28 import android
.util
.Log
;
31 * A helper class for some string operations.
33 * @author Bartek Przybylski
34 * @author David A. Velasco
36 public class DisplayUtils
{
38 private static String TAG
= DisplayUtils
.class.getSimpleName();
40 private static final String
[] sizeSuffixes
= { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
42 private static HashMap
<String
, String
> mimeType2HUmanReadable
;
44 mimeType2HUmanReadable
= new HashMap
<String
, String
>();
46 mimeType2HUmanReadable
.put("image/jpeg", "JPEG image");
47 mimeType2HUmanReadable
.put("image/jpg", "JPEG image");
48 mimeType2HUmanReadable
.put("image/png", "PNG image");
49 mimeType2HUmanReadable
.put("image/bmp", "Bitmap image");
50 mimeType2HUmanReadable
.put("image/gif", "GIF image");
51 mimeType2HUmanReadable
.put("image/svg+xml", "JPEG image");
52 mimeType2HUmanReadable
.put("image/tiff", "TIFF image");
54 mimeType2HUmanReadable
.put("audio/mpeg", "MP3 music file");
55 mimeType2HUmanReadable
.put("application/ogg", "OGG music file");
59 private static final String TYPE_APPLICATION
= "application";
60 private static final String TYPE_AUDIO
= "audio";
61 private static final String TYPE_IMAGE
= "image";
62 private static final String TYPE_TXT
= "text";
63 private static final String TYPE_VIDEO
= "video";
65 private static final String SUBTYPE_PDF
= "pdf";
66 private static final String
[] SUBTYPES_DOCUMENT
= { "msword", "mspowerpoint", "msexcel",
67 "vnd.oasis.opendocument.presentation",
68 "vnd.oasis.opendocument.spreadsheet",
69 "vnd.oasis.opendocument.text"
71 private static Set
<String
> SUBTYPES_DOCUMENT_SET
= new HashSet
<String
>(Arrays
.asList(SUBTYPES_DOCUMENT
));
72 private static final String
[] SUBTYPES_COMPRESSED
= {"x-tar", "x-gzip", "zip"};
73 private static final Set
<String
> SUBTYPES_COMPRESSED_SET
= new HashSet
<String
>(Arrays
.asList(SUBTYPES_COMPRESSED
));
76 * Converts the file size in bytes to human readable output.
78 * @param bytes Input file size
79 * @return Like something readable like "12 MB"
81 public static String
bytesToHumanReadable(long bytes
) {
82 double result
= bytes
;
84 while (result
> 1024 && attachedsuff
< sizeSuffixes
.length
) {
88 result
= ((int) (result
* 100)) / 100.;
89 return result
+ " " + sizeSuffixes
[attachedsuff
];
93 * Removes special HTML entities from a string
95 * @param s Input string
96 * @return A cleaned version of the string
98 public static String
HtmlDecode(String s
) {
100 * TODO: Perhaps we should use something more proven like:
101 * http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml%28java.lang.String%29
105 for (int i
= 0; i
< s
.length(); ++i
) {
106 if (s
.charAt(i
) == '%') {
107 ret
+= (char) Integer
.parseInt(s
.substring(i
+ 1, i
+ 3), 16);
117 * Converts MIME types like "image/jpg" to more end user friendly output
120 * @param mimetype MIME type to convert
121 * @return A human friendly version of the MIME type
123 public static String
convertMIMEtoPrettyPrint(String mimetype
) {
124 if (mimeType2HUmanReadable
.containsKey(mimetype
)) {
125 return mimeType2HUmanReadable
.get(mimetype
);
127 if (mimetype
.split("/").length
>= 2)
128 return mimetype
.split("/")[1].toUpperCase() + " file";
129 return "Unknown type";
134 * Returns the resource identifier of an image resource to use as icon associated to a
137 * @param mimetype MIME type string.
138 * @return Resource identifier of an image resource.
140 public static int getResourceId(String mimetype
) {
142 if (mimetype
== null
|| "DIR".equals(mimetype
)) {
143 return R
.drawable
.ic_menu_archive
;
146 String
[] parts
= mimetype
.split("/");
147 String type
= parts
[0];
148 String subtype
= parts
[1];
150 if(TYPE_TXT
.equals(type
)) {
151 return R
.drawable
.file_doc
;
153 } else if(TYPE_IMAGE
.equals(type
)) {
154 return R
.drawable
.file_image
;
156 } else if(TYPE_VIDEO
.equals(type
)) {
157 return R
.drawable
.file_movie
;
159 } else if(TYPE_AUDIO
.equals(type
)) {
160 return R
.drawable
.file_sound
;
162 } else if(TYPE_APPLICATION
.equals(type
)) {
164 if (SUBTYPE_PDF
.equals(subtype
)) {
165 return R
.drawable
.file_pdf
;
167 } else if (SUBTYPES_DOCUMENT_SET
.contains(subtype
)) {
168 return R
.drawable
.file_doc
;
170 } else if (SUBTYPES_COMPRESSED_SET
.contains(subtype
)) {
171 return R
.drawable
.file_zip
;
175 // problems: RAR, RTF, 3GP are send as application/octet-stream from the server ; extension in the filename should be explicitly reviewed
179 return R
.drawable
.file
;
185 * Converts Unix time to human readable format
186 * @param miliseconds that have passed since 01/01/1970
187 * @return The human readable time for the users locale
189 public static String
unixTimeToHumanReadable(long milliseconds
) {
190 Date date
= new Date(milliseconds
);
191 return date
.toLocaleString();