d2939c87aeb19a75eaae1c8298d9216789386821
[pub/Android/ownCloud.git] / src / com / owncloud / android / DisplayUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android;
20
21 import java.util.Arrays;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Set;
26
27 /**
28 * A helper class for some string operations.
29 *
30 * @author Bartek Przybylski
31 * @author David A. Velasco
32 */
33 public class DisplayUtils {
34
35 private static String TAG = DisplayUtils.class.getSimpleName();
36
37 private static final String[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
38
39 private static HashMap<String, String> mimeType2HUmanReadable;
40 static {
41 mimeType2HUmanReadable = new HashMap<String, String>();
42 // images
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");
50 // music
51 mimeType2HUmanReadable.put("audio/mpeg", "MP3 music file");
52 mimeType2HUmanReadable.put("application/ogg", "OGG music file");
53
54 }
55
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";
61
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"
67 };
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));
71
72 /**
73 * Converts the file size in bytes to human readable output.
74 *
75 * @param bytes Input file size
76 * @return Like something readable like "12 MB"
77 */
78 public static String bytesToHumanReadable(long bytes) {
79 double result = bytes;
80 int attachedsuff = 0;
81 while (result > 1024 && attachedsuff < sizeSuffixes.length) {
82 result /= 1024.;
83 attachedsuff++;
84 }
85 result = ((int) (result * 100)) / 100.;
86 return result + " " + sizeSuffixes[attachedsuff];
87 }
88
89 /**
90 * Removes special HTML entities from a string
91 *
92 * @param s Input string
93 * @return A cleaned version of the string
94 */
95 public static String HtmlDecode(String s) {
96 /*
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
99 */
100
101 String ret = "";
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);
105 i += 2;
106 } else {
107 ret += s.charAt(i);
108 }
109 }
110 return ret;
111 }
112
113 /**
114 * Converts MIME types like "image/jpg" to more end user friendly output
115 * like "JPG image".
116 *
117 * @param mimetype MIME type to convert
118 * @return A human friendly version of the MIME type
119 */
120 public static String convertMIMEtoPrettyPrint(String mimetype) {
121 if (mimeType2HUmanReadable.containsKey(mimetype)) {
122 return mimeType2HUmanReadable.get(mimetype);
123 }
124 if (mimetype.split("/").length >= 2)
125 return mimetype.split("/")[1].toUpperCase() + " file";
126 return "Unknown type";
127 }
128
129
130 /**
131 * Returns the resource identifier of an image resource to use as icon associated to a
132 * known MIME type.
133 *
134 * @param mimetype MIME type string.
135 * @return Resource identifier of an image resource.
136 */
137 public static int getResourceId(String mimetype) {
138
139 if (mimetype == null || "DIR".equals(mimetype)) {
140 return R.drawable.ic_menu_archive;
141
142 } else {
143 String [] parts = mimetype.split("/");
144 String type = parts[0];
145 String subtype = (parts.length > 1) ? parts[1] : "";
146
147 if(TYPE_TXT.equals(type)) {
148 return R.drawable.file_doc;
149
150 } else if(TYPE_IMAGE.equals(type)) {
151 return R.drawable.file_image;
152
153 } else if(TYPE_VIDEO.equals(type)) {
154 return R.drawable.file_movie;
155
156 } else if(TYPE_AUDIO.equals(type)) {
157 return R.drawable.file_sound;
158
159 } else if(TYPE_APPLICATION.equals(type)) {
160
161 if (SUBTYPE_PDF.equals(subtype)) {
162 return R.drawable.file_pdf;
163
164 } else if (SUBTYPES_DOCUMENT_SET.contains(subtype)) {
165 return R.drawable.file_doc;
166
167 } else if (SUBTYPES_COMPRESSED_SET.contains(subtype)) {
168 return R.drawable.file_zip;
169 }
170
171 }
172 // problems: RAR, RTF, 3GP are send as application/octet-stream from the server ; extension in the filename should be explicitly reviewed
173 }
174
175 // default icon
176 return R.drawable.file;
177 }
178
179
180
181 /**
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
185 */
186 public static String unixTimeToHumanReadable(long milliseconds) {
187 Date date = new Date(milliseconds);
188 return date.toLocaleString();
189 }
190 }