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
.utils
;
21 import java
.util
.Arrays
;
22 import java
.util
.Calendar
;
23 import java
.util
.Date
;
24 import java
.util
.HashMap
;
25 import java
.util
.HashSet
;
28 import com
.owncloud
.android
.R
;
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",
67 "vnd.openxmlformats-officedocument.wordprocessingml.document",
68 "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_SPREADSHEET
= { "msexcel",
73 "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
74 "vnd.oasis.opendocument.spreadsheet"
76 private static Set
<String
> SUBTYPES_SPREADSHEET_SET
= new HashSet
<String
>(Arrays
.asList(SUBTYPES_SPREADSHEET
));
77 private static final String
[] SUBTYPES_PRESENTATION
= { "mspowerpoint",
78 "vnd.openxmlformats-officedocument.presentationml.presentation",
79 "vnd.oasis.opendocument.presentation"
81 private static Set
<String
> SUBTYPES_PRESENTATION_SET
= new HashSet
<String
>(Arrays
.asList(SUBTYPES_PRESENTATION
));
82 private static final String
[] SUBTYPES_COMPRESSED
= {"x-tar", "x-gzip", "zip"};
83 private static final Set
<String
> SUBTYPES_COMPRESSED_SET
= new HashSet
<String
>(Arrays
.asList(SUBTYPES_COMPRESSED
));
84 private static final String SUBTYPE_OCTET_STREAM
= "octet-stream";
85 private static final String EXTENSION_RAR
= "rar";
86 private static final String EXTENSION_RTF
= "rtf";
87 private static final String EXTENSION_3GP
= "3gp";
90 * Converts the file size in bytes to human readable output.
92 * @param bytes Input file size
93 * @return Like something readable like "12 MB"
95 public static String
bytesToHumanReadable(long bytes
) {
96 double result
= bytes
;
98 while (result
> 1024 && attachedsuff
< sizeSuffixes
.length
) {
102 result
= ((int) (result
* 100)) / 100.;
103 return result
+ " " + sizeSuffixes
[attachedsuff
];
107 * Removes special HTML entities from a string
109 * @param s Input string
110 * @return A cleaned version of the string
112 public static String
HtmlDecode(String s
) {
114 * TODO: Perhaps we should use something more proven like:
115 * http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml%28java.lang.String%29
119 for (int i
= 0; i
< s
.length(); ++i
) {
120 if (s
.charAt(i
) == '%') {
121 ret
+= (char) Integer
.parseInt(s
.substring(i
+ 1, i
+ 3), 16);
131 * Converts MIME types like "image/jpg" to more end user friendly output
134 * @param mimetype MIME type to convert
135 * @return A human friendly version of the MIME type
137 public static String
convertMIMEtoPrettyPrint(String mimetype
) {
138 if (mimeType2HUmanReadable
.containsKey(mimetype
)) {
139 return mimeType2HUmanReadable
.get(mimetype
);
141 if (mimetype
.split("/").length
>= 2)
142 return mimetype
.split("/")[1].toUpperCase() + " file";
143 return "Unknown type";
148 * Returns the resource identifier of an image resource to use as icon associated to a
151 * @param mimetype MIME type string.
152 * @param filename name, with extension
153 * @return Resource identifier of an image resource.
155 public static int getResourceId(String mimetype
, String filename
) {
157 if (mimetype
== null
|| "DIR".equals(mimetype
)) {
158 return R
.drawable
.ic_menu_archive
;
161 String
[] parts
= mimetype
.split("/");
162 String type
= parts
[0];
163 String subtype
= (parts
.length
> 1) ? parts
[1] : "";
165 if(TYPE_TXT
.equals(type
)) {
166 return R
.drawable
.file_doc
;
168 } else if(TYPE_IMAGE
.equals(type
)) {
169 return R
.drawable
.file_image
;
171 } else if(TYPE_VIDEO
.equals(type
)) {
172 return R
.drawable
.file_movie
;
174 } else if(TYPE_AUDIO
.equals(type
)) {
175 return R
.drawable
.file_sound
;
177 } else if(TYPE_APPLICATION
.equals(type
)) {
179 if (SUBTYPE_PDF
.equals(subtype
)) {
180 return R
.drawable
.file_pdf
;
182 } else if (SUBTYPES_DOCUMENT_SET
.contains(subtype
)) {
183 return R
.drawable
.file_doc
;
185 } else if (SUBTYPES_SPREADSHEET_SET
.contains(subtype
)) {
186 return R
.drawable
.file_xls
;
188 } else if (SUBTYPES_PRESENTATION_SET
.contains(subtype
)) {
189 return R
.drawable
.file_ppt
;
191 } else if (SUBTYPES_COMPRESSED_SET
.contains(subtype
)) {
192 return R
.drawable
.file_zip
;
194 } else if (SUBTYPE_OCTET_STREAM
.equals(subtype
) ) {
195 if (getExtension(filename
).equalsIgnoreCase(EXTENSION_RAR
)) {
196 return R
.drawable
.file_zip
;
198 } else if (getExtension(filename
).equalsIgnoreCase(EXTENSION_RTF
)) {
199 return R
.drawable
.file_doc
;
201 } else if (getExtension(filename
).equalsIgnoreCase(EXTENSION_3GP
)) {
202 return R
.drawable
.file_movie
;
210 return R
.drawable
.file
;
214 private static String
getExtension(String filename
) {
215 String extension
= filename
.substring(filename
.lastIndexOf(".") + 1);
221 * Converts Unix time to human readable format
222 * @param miliseconds that have passed since 01/01/1970
223 * @return The human readable time for the users locale
225 public static String
unixTimeToHumanReadable(long milliseconds
) {
226 Date date
= new Date(milliseconds
);
227 return date
.toLocaleString();
231 public static int getSeasonalIconId() {
232 if (Calendar
.getInstance().get(Calendar
.DAY_OF_YEAR
) >= 354) {
233 return R
.drawable
.winter_holidays_icon
;
235 return R
.drawable
.icon
;