Update file images, add new mimetypes to show: spreadsheets, impress, rar, rtf and 3gp
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / 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.utils;
20
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;
26 import java.util.Set;
27
28 import android.util.Log;
29
30 import com.owncloud.android.R;
31
32 /**
33 * A helper class for some string operations.
34 *
35 * @author Bartek Przybylski
36 * @author David A. Velasco
37 */
38 public class DisplayUtils {
39
40 //private static String TAG = DisplayUtils.class.getSimpleName();
41
42 private static final String[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
43
44 private static HashMap<String, String> mimeType2HUmanReadable;
45 static {
46 mimeType2HUmanReadable = new HashMap<String, String>();
47 // images
48 mimeType2HUmanReadable.put("image/jpeg", "JPEG image");
49 mimeType2HUmanReadable.put("image/jpg", "JPEG image");
50 mimeType2HUmanReadable.put("image/png", "PNG image");
51 mimeType2HUmanReadable.put("image/bmp", "Bitmap image");
52 mimeType2HUmanReadable.put("image/gif", "GIF image");
53 mimeType2HUmanReadable.put("image/svg+xml", "JPEG image");
54 mimeType2HUmanReadable.put("image/tiff", "TIFF image");
55 // music
56 mimeType2HUmanReadable.put("audio/mpeg", "MP3 music file");
57 mimeType2HUmanReadable.put("application/ogg", "OGG music file");
58
59 }
60
61 private static final String TYPE_APPLICATION = "application";
62 private static final String TYPE_AUDIO = "audio";
63 private static final String TYPE_IMAGE = "image";
64 private static final String TYPE_TXT = "text";
65 private static final String TYPE_VIDEO = "video";
66
67 private static final String SUBTYPE_PDF = "pdf";
68 private static final String[] SUBTYPES_DOCUMENT = { "msword",
69 "vnd.openxmlformats-officedocument.wordprocessingml.document",
70 "vnd.oasis.opendocument.text",
71 "rtf"
72 };
73 private static Set<String> SUBTYPES_DOCUMENT_SET = new HashSet<String>(Arrays.asList(SUBTYPES_DOCUMENT));
74 private static final String[] SUBTYPES_SPREADSHEET = { "msexcel",
75 "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
76 "vnd.oasis.opendocument.spreadsheet"
77 };
78 private static Set<String> SUBTYPES_SPREADSHEET_SET = new HashSet<String>(Arrays.asList(SUBTYPES_SPREADSHEET));
79 private static final String[] SUBTYPES_PRESENTATION = { "mspowerpoint",
80 "vnd.openxmlformats-officedocument.presentationml.presentation",
81 "vnd.oasis.opendocument.presentation"
82 };
83 private static Set<String> SUBTYPES_PRESENTATION_SET = new HashSet<String>(Arrays.asList(SUBTYPES_PRESENTATION));
84 private static final String[] SUBTYPES_COMPRESSED = {"x-tar", "x-gzip", "zip"};
85 private static final Set<String> SUBTYPES_COMPRESSED_SET = new HashSet<String>(Arrays.asList(SUBTYPES_COMPRESSED));
86 private static final String SUBTYPE_OCTET_STREAM = "octet-stream";
87 private static final String EXTENSION_RAR = "rar";
88 private static final String EXTENSION_RTF = "rtf";
89 private static final String EXTENSION_3GP = "3gp";
90
91 /**
92 * Converts the file size in bytes to human readable output.
93 *
94 * @param bytes Input file size
95 * @return Like something readable like "12 MB"
96 */
97 public static String bytesToHumanReadable(long bytes) {
98 double result = bytes;
99 int attachedsuff = 0;
100 while (result > 1024 && attachedsuff < sizeSuffixes.length) {
101 result /= 1024.;
102 attachedsuff++;
103 }
104 result = ((int) (result * 100)) / 100.;
105 return result + " " + sizeSuffixes[attachedsuff];
106 }
107
108 /**
109 * Removes special HTML entities from a string
110 *
111 * @param s Input string
112 * @return A cleaned version of the string
113 */
114 public static String HtmlDecode(String s) {
115 /*
116 * TODO: Perhaps we should use something more proven like:
117 * http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml%28java.lang.String%29
118 */
119
120 String ret = "";
121 for (int i = 0; i < s.length(); ++i) {
122 if (s.charAt(i) == '%') {
123 ret += (char) Integer.parseInt(s.substring(i + 1, i + 3), 16);
124 i += 2;
125 } else {
126 ret += s.charAt(i);
127 }
128 }
129 return ret;
130 }
131
132 /**
133 * Converts MIME types like "image/jpg" to more end user friendly output
134 * like "JPG image".
135 *
136 * @param mimetype MIME type to convert
137 * @return A human friendly version of the MIME type
138 */
139 public static String convertMIMEtoPrettyPrint(String mimetype) {
140 if (mimeType2HUmanReadable.containsKey(mimetype)) {
141 return mimeType2HUmanReadable.get(mimetype);
142 }
143 if (mimetype.split("/").length >= 2)
144 return mimetype.split("/")[1].toUpperCase() + " file";
145 return "Unknown type";
146 }
147
148
149 /**
150 * Returns the resource identifier of an image resource to use as icon associated to a
151 * known MIME type.
152 *
153 * @param mimetype MIME type string.
154 * @return Resource identifier of an image resource.
155 */
156 public static int getResourceId(String mimetype, String filename) {
157
158 if (mimetype == null || "DIR".equals(mimetype)) {
159 return R.drawable.ic_menu_archive;
160
161 } else {
162 String [] parts = mimetype.split("/");
163 String type = parts[0];
164 String subtype = (parts.length > 1) ? parts[1] : "";
165
166 if(TYPE_TXT.equals(type)) {
167 return R.drawable.file_doc;
168
169 } else if(TYPE_IMAGE.equals(type)) {
170 return R.drawable.file_image;
171
172 } else if(TYPE_VIDEO.equals(type)) {
173 return R.drawable.file_movie;
174
175 } else if(TYPE_AUDIO.equals(type)) {
176 return R.drawable.file_sound;
177
178 } else if(TYPE_APPLICATION.equals(type)) {
179
180 if (SUBTYPE_PDF.equals(subtype)) {
181 return R.drawable.file_pdf;
182
183 } else if (SUBTYPES_DOCUMENT_SET.contains(subtype)) {
184 return R.drawable.file_doc;
185
186 } else if (SUBTYPES_SPREADSHEET_SET.contains(subtype)) {
187 return R.drawable.file_xls;
188
189 } else if (SUBTYPES_PRESENTATION_SET.contains(subtype)) {
190 return R.drawable.file_ppt;
191
192 } else if (SUBTYPES_COMPRESSED_SET.contains(subtype)) {
193 return R.drawable.file_zip;
194
195 } else if (SUBTYPE_OCTET_STREAM.equals(subtype) ) {
196 if (getExtension(filename).equalsIgnoreCase(EXTENSION_RAR)) {
197 return R.drawable.file_zip;
198
199 } else if (getExtension(filename).equalsIgnoreCase(EXTENSION_RTF)) {
200 return R.drawable.file_doc;
201
202 } else if (getExtension(filename).equalsIgnoreCase(EXTENSION_3GP)) {
203 return R.drawable.file_movie;
204
205 }
206 }
207 }
208 }
209
210 // default icon
211 return R.drawable.file;
212 }
213
214
215 private static String getExtension(String filename) {
216 String extension = filename.substring(filename.lastIndexOf(".") + 1);
217
218 return extension;
219 }
220
221 /**
222 * Converts Unix time to human readable format
223 * @param miliseconds that have passed since 01/01/1970
224 * @return The human readable time for the users locale
225 */
226 public static String unixTimeToHumanReadable(long milliseconds) {
227 Date date = new Date(milliseconds);
228 return date.toLocaleString();
229 }
230
231
232 public static int getSeasonalIconId() {
233 if (Calendar.getInstance().get(Calendar.DAY_OF_YEAR) >= 354) {
234 return R.drawable.winter_holidays_icon;
235 } else {
236 return R.drawable.icon;
237 }
238 }
239 }