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
;
27 import android
.util
.Log
;
30 * A helper class for some string operations.
32 * @author Bartek Przybylski
33 * @author David A. Velasco
35 public class DisplayUtils
{
37 private static String TAG
= DisplayUtils
.class.getSimpleName();
39 private static final String
[] sizeSuffixes
= { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
41 private static HashMap
<String
, String
> mimeType2HUmanReadable
;
43 mimeType2HUmanReadable
= new HashMap
<String
, String
>();
45 mimeType2HUmanReadable
.put("image/jpeg", "JPEG image");
46 mimeType2HUmanReadable
.put("image/jpg", "JPEG image");
47 mimeType2HUmanReadable
.put("image/png", "PNG image");
48 mimeType2HUmanReadable
.put("image/bmp", "Bitmap image");
49 mimeType2HUmanReadable
.put("image/gif", "GIF image");
50 mimeType2HUmanReadable
.put("image/svg+xml", "JPEG image");
51 mimeType2HUmanReadable
.put("image/tiff", "TIFF image");
53 mimeType2HUmanReadable
.put("audio/mpeg", "MP3 music file");
54 mimeType2HUmanReadable
.put("application/ogg", "OGG music file");
58 private static final String TYPE_APPLICATION
= "application";
59 private static final String TYPE_AUDIO
= "audio";
60 private static final String TYPE_IMAGE
= "image";
61 private static final String TYPE_TXT
= "text";
62 private static final String TYPE_VIDEO
= "video";
64 private static final String SUBTYPE_PDF
= "pdf";
65 private static final String
[] SUBTYPES_DOCUMENT
= { "msword", "mspowerpoint", "msexcel",
66 "vnd.oasis.opendocument.presentation",
67 "vnd.oasis.opendocument.spreadsheet",
68 "vnd.oasis.opendocument.text"
70 private static Set
<String
> SUBTYPES_DOCUMENT_SET
= new HashSet
<String
>(Arrays
.asList(SUBTYPES_DOCUMENT
));
71 private static final String
[] SUBTYPES_COMPRESSED
= {"x-tar", "x-gzip", "zip"};
72 private static final Set
<String
> SUBTYPES_COMPRESSED_SET
= new HashSet
<String
>(Arrays
.asList(SUBTYPES_COMPRESSED
));
75 * Converts the file size in bytes to human readable output.
77 * @param bytes Input file size
78 * @return Like something readable like "12 MB"
80 public static String
bytesToHumanReadable(long bytes
) {
81 double result
= bytes
;
83 while (result
> 1024 && attachedsuff
< sizeSuffixes
.length
) {
87 result
= ((int) (result
* 100)) / 100.;
88 return result
+ " " + sizeSuffixes
[attachedsuff
];
92 * Removes special HTML entities from a string
94 * @param s Input string
95 * @return A cleaned version of the string
97 public static String
HtmlDecode(String s
) {
99 * TODO: Perhaps we should use something more proven like:
100 * http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml%28java.lang.String%29
104 for (int i
= 0; i
< s
.length(); ++i
) {
105 if (s
.charAt(i
) == '%') {
106 ret
+= (char) Integer
.parseInt(s
.substring(i
+ 1, i
+ 3), 16);
116 * Converts MIME types like "image/jpg" to more end user friendly output
119 * @param mimetype MIME type to convert
120 * @return A human friendly version of the MIME type
122 public static String
convertMIMEtoPrettyPrint(String mimetype
) {
123 if (mimeType2HUmanReadable
.containsKey(mimetype
)) {
124 return mimeType2HUmanReadable
.get(mimetype
);
126 if (mimetype
.split("/").length
>= 2)
127 return mimetype
.split("/")[1].toUpperCase() + " file";
128 return "Unknown type";
133 * Returns the resource identifier of an image resource to use as icon associated to a
136 * @param mimetype MIME type string.
137 * @return Resource identifier of an image resource.
139 public static int getResourceId(String mimetype
) {
141 if (mimetype
== null
|| "DIR".equals(mimetype
)) {
142 return R
.drawable
.ic_menu_archive
;
145 String
[] parts
= mimetype
.split("/");
146 String type
= parts
[0];
147 String subtype
= (parts
.length
> 1) ? parts
[1] : "";
149 if(TYPE_TXT
.equals(type
)) {
150 return R
.drawable
.file_doc
;
152 } else if(TYPE_IMAGE
.equals(type
)) {
153 return R
.drawable
.file_image
;
155 } else if(TYPE_VIDEO
.equals(type
)) {
156 return R
.drawable
.file_movie
;
158 } else if(TYPE_AUDIO
.equals(type
)) {
159 return R
.drawable
.file_sound
;
161 } else if(TYPE_APPLICATION
.equals(type
)) {
163 if (SUBTYPE_PDF
.equals(subtype
)) {
164 return R
.drawable
.file_pdf
;
166 } else if (SUBTYPES_DOCUMENT_SET
.contains(subtype
)) {
167 return R
.drawable
.file_doc
;
169 } else if (SUBTYPES_COMPRESSED_SET
.contains(subtype
)) {
170 return R
.drawable
.file_zip
;
174 // problems: RAR, RTF, 3GP are send as application/octet-stream from the server ; extension in the filename should be explicitly reviewed
178 return R
.drawable
.file
;
184 * Converts Unix time to human readable format
185 * @param miliseconds that have passed since 01/01/1970
186 * @return The human readable time for the users locale
188 public static String
unixTimeToHumanReadable(long milliseconds
) {
189 Date date
= new Date(milliseconds
);
190 return date
.toLocaleString();