Considered MIME types for .py, .js, and fixed .xls, .ppt, and some clean-up
[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.net.IDN;
22 import java.text.DateFormat;
23 import java.util.Arrays;
24 import java.util.Calendar;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.Set;
29
30 import android.annotation.TargetApi;
31 import android.content.Context;
32 import android.os.Build;
33 import android.text.format.DateUtils;
34 import android.webkit.MimeTypeMap;
35
36 import com.owncloud.android.MainApp;
37 import com.owncloud.android.R;
38 import com.owncloud.android.datamodel.OCFile;
39
40 /**
41 * A helper class for some string operations.
42 *
43 * @author Bartek Przybylski
44 * @author David A. Velasco
45 */
46 public class DisplayUtils {
47
48 private static final String OWNCLOUD_APP_NAME = "ownCloud";
49
50 //private static String TAG = DisplayUtils.class.getSimpleName();
51
52 private static final String[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
53
54 private static HashMap<String, String> mimeType2HUmanReadable;
55 static {
56 mimeType2HUmanReadable = new HashMap<String, String>();
57 // images
58 mimeType2HUmanReadable.put("image/jpeg", "JPEG image");
59 mimeType2HUmanReadable.put("image/jpg", "JPEG image");
60 mimeType2HUmanReadable.put("image/png", "PNG image");
61 mimeType2HUmanReadable.put("image/bmp", "Bitmap image");
62 mimeType2HUmanReadable.put("image/gif", "GIF image");
63 mimeType2HUmanReadable.put("image/svg+xml", "JPEG image");
64 mimeType2HUmanReadable.put("image/tiff", "TIFF image");
65 // music
66 mimeType2HUmanReadable.put("audio/mpeg", "MP3 music file");
67 mimeType2HUmanReadable.put("application/ogg", "OGG music file");
68
69 }
70
71 private static final String TYPE_APPLICATION = "application";
72 private static final String TYPE_AUDIO = "audio";
73 private static final String TYPE_IMAGE = "image";
74 private static final String TYPE_TXT = "text";
75 private static final String TYPE_VIDEO = "video";
76
77 private static final String SUBTYPE_PDF = "pdf";
78 private static final String SUBTYPE_XML = "xml";
79 private static final String[] SUBTYPES_DOCUMENT = {
80 "msword",
81 "vnd.openxmlformats-officedocument.wordprocessingml.document",
82 "vnd.oasis.opendocument.text",
83 "rtf"
84 };
85 private static Set<String> SUBTYPES_DOCUMENT_SET = new HashSet<String>(Arrays.asList(SUBTYPES_DOCUMENT));
86 private static final String[] SUBTYPES_SPREADSHEET = {
87 "msexcel",
88 "vnd.ms-excel",
89 "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
90 "vnd.oasis.opendocument.spreadsheet"
91 };
92 private static Set<String> SUBTYPES_SPREADSHEET_SET = new HashSet<String>(Arrays.asList(SUBTYPES_SPREADSHEET));
93 private static final String[] SUBTYPES_PRESENTATION = {
94 "mspowerpoint",
95 "vnd.ms-powerpoint",
96 "vnd.openxmlformats-officedocument.presentationml.presentation",
97 "vnd.oasis.opendocument.presentation"
98 };
99 private static Set<String> SUBTYPES_PRESENTATION_SET = new HashSet<String>(Arrays.asList(SUBTYPES_PRESENTATION));
100 private static final String[] SUBTYPES_COMPRESSED = {"x-tar", "x-gzip", "zip"};
101 private static final Set<String> SUBTYPES_COMPRESSED_SET = new HashSet<String>(Arrays.asList(SUBTYPES_COMPRESSED));
102 private static final String SUBTYPE_OCTET_STREAM = "octet-stream";
103 private static final String EXTENSION_RAR = "rar";
104 private static final String EXTENSION_RTF = "rtf";
105 private static final String EXTENSION_3GP = "3gp";
106 private static final String EXTENSION_PY = "py";
107 private static final String EXTENSION_JS = "js";
108
109 /**
110 * Converts the file size in bytes to human readable output.
111 *
112 * @param bytes Input file size
113 * @return Like something readable like "12 MB"
114 */
115 public static String bytesToHumanReadable(long bytes) {
116 double result = bytes;
117 int attachedsuff = 0;
118 while (result > 1024 && attachedsuff < sizeSuffixes.length) {
119 result /= 1024.;
120 attachedsuff++;
121 }
122 result = ((int) (result * 100)) / 100.;
123 return result + " " + sizeSuffixes[attachedsuff];
124 }
125
126 /**
127 * Converts MIME types like "image/jpg" to more end user friendly output
128 * like "JPG image".
129 *
130 * @param mimetype MIME type to convert
131 * @return A human friendly version of the MIME type
132 */
133 public static String convertMIMEtoPrettyPrint(String mimetype) {
134 if (mimeType2HUmanReadable.containsKey(mimetype)) {
135 return mimeType2HUmanReadable.get(mimetype);
136 }
137 if (mimetype.split("/").length >= 2)
138 return mimetype.split("/")[1].toUpperCase() + " file";
139 return "Unknown type";
140 }
141
142
143 /**
144 * Returns the resource identifier of an image to use as icon associated to a known MIME type.
145 *
146 * @param mimetype MIME type string; if NULL, the method tries to guess it from the extension in filename
147 * @param filename Name, with extension.
148 * @return Identifier of an image resource.
149 */
150 public static int getFileTypeIconId(String mimetype, String filename) {
151
152 if (mimetype == null) {
153 String fileExtension = getExtension(filename);
154 mimetype = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
155 if (mimetype == null) {
156 mimetype = TYPE_APPLICATION + "/" + SUBTYPE_OCTET_STREAM;
157 }
158 }
159
160 if ("DIR".equals(mimetype)) {
161 return R.drawable.ic_menu_archive;
162
163 } else {
164 String [] parts = mimetype.split("/");
165 String type = parts[0];
166 String subtype = (parts.length > 1) ? parts[1] : "";
167
168 if(TYPE_TXT.equals(type)) {
169 return R.drawable.file_doc;
170
171 } else if(TYPE_IMAGE.equals(type)) {
172 return R.drawable.file_image;
173
174 } else if(TYPE_VIDEO.equals(type)) {
175 return R.drawable.file_movie;
176
177 } else if(TYPE_AUDIO.equals(type)) {
178 return R.drawable.file_sound;
179
180 } else if(TYPE_APPLICATION.equals(type)) {
181
182 if (SUBTYPE_PDF.equals(subtype)) {
183 return R.drawable.file_pdf;
184
185 } else if (SUBTYPE_XML.equals(subtype)) {
186 return R.drawable.file_doc;
187
188 } else if (SUBTYPES_DOCUMENT_SET.contains(subtype)) {
189 return R.drawable.file_doc;
190
191 } else if (SUBTYPES_SPREADSHEET_SET.contains(subtype)) {
192 return R.drawable.file_xls;
193
194 } else if (SUBTYPES_PRESENTATION_SET.contains(subtype)) {
195 return R.drawable.file_ppt;
196
197 } else if (SUBTYPES_COMPRESSED_SET.contains(subtype)) {
198 return R.drawable.file_zip;
199
200 } else if (SUBTYPE_OCTET_STREAM.equals(subtype) ) {
201 if (getExtension(filename).equalsIgnoreCase(EXTENSION_RAR)) {
202 return R.drawable.file_zip;
203
204 } else if (getExtension(filename).equalsIgnoreCase(EXTENSION_RTF)) {
205 return R.drawable.file_doc;
206
207 } else if (getExtension(filename).equalsIgnoreCase(EXTENSION_3GP)) {
208 return R.drawable.file_movie;
209
210 } else if ( getExtension(filename).equalsIgnoreCase(EXTENSION_PY) ||
211 getExtension(filename).equalsIgnoreCase(EXTENSION_JS)) {
212 return R.drawable.file_doc;
213 }
214 }
215 }
216 }
217
218 // default icon
219 return R.drawable.file;
220 }
221
222
223 private static String getExtension(String filename) {
224 String extension = filename.substring(filename.lastIndexOf(".") + 1);
225 return extension;
226 }
227
228 /**
229 * Converts Unix time to human readable format
230 * @param miliseconds that have passed since 01/01/1970
231 * @return The human readable time for the users locale
232 */
233 public static String unixTimeToHumanReadable(long milliseconds) {
234 Date date = new Date(milliseconds);
235 DateFormat df = DateFormat.getDateTimeInstance();
236 //return date.toLocaleString();
237 return df.format(date);
238 }
239
240
241 public static int getSeasonalIconId() {
242 if (Calendar.getInstance().get(Calendar.DAY_OF_YEAR) >= 354 &&
243 MainApp.getAppContext().getString(R.string.app_name).equals(OWNCLOUD_APP_NAME)) {
244 return R.drawable.winter_holidays_icon;
245 } else {
246 return R.drawable.icon;
247 }
248 }
249
250 /**
251 * Converts an internationalized domain name (IDN) in an URL to and from ASCII/Unicode.
252 * @param url the URL where the domain name should be converted
253 * @param toASCII if true converts from Unicode to ASCII, if false converts from ASCII to Unicode
254 * @return the URL containing the converted domain name
255 */
256 @TargetApi(Build.VERSION_CODES.GINGERBREAD)
257 public static String convertIdn(String url, boolean toASCII) {
258
259 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
260 // Find host name after '//' or '@'
261 int hostStart = 0;
262 if (url.indexOf("//") != -1) {
263 hostStart = url.indexOf("//") + "//".length();
264 } else if (url.indexOf("@") != -1) {
265 hostStart = url.indexOf("@") + "@".length();
266 }
267
268 int hostEnd = url.substring(hostStart).indexOf("/");
269 // Handle URL which doesn't have a path (path is implicitly '/')
270 hostEnd = (hostEnd == -1 ? url.length() : hostStart + hostEnd);
271
272 String host = url.substring(hostStart, hostEnd);
273 host = (toASCII ? IDN.toASCII(host) : IDN.toUnicode(host));
274
275 return url.substring(0, hostStart) + host + url.substring(hostEnd);
276 } else {
277 return url;
278 }
279 }
280
281 /**
282 * Get the file extension if it is on path as type "content://.../DocInfo.doc"
283 * @param filepath: Content Uri converted to string format
284 * @return String: fileExtension (type '.pdf'). Empty if no extension
285 */
286 public static String getComposedFileExtension(String filepath) {
287 String fileExtension = "";
288 String fileNameInContentUri = filepath.substring(filepath.lastIndexOf("/"));
289
290 // Check if extension is included in uri
291 int pos = fileNameInContentUri.lastIndexOf('.');
292 if (pos >= 0) {
293 fileExtension = fileNameInContentUri.substring(pos);
294 }
295 return fileExtension;
296 }
297
298 @SuppressWarnings("deprecation")
299 public static CharSequence getRelativeDateTimeString (
300 Context c, long time, long minResolution, long transitionResolution, int flags
301 ){
302
303 CharSequence dateString = "";
304
305 // in Future
306 if (time > System.currentTimeMillis()){
307 return DisplayUtils.unixTimeToHumanReadable(time);
308 }
309 // < 60 seconds -> seconds ago
310 else if ((System.currentTimeMillis() - time) < 60 * 1000) {
311 return c.getString(R.string.file_list_seconds_ago);
312 } else {
313 // Workaround 2.x bug (see https://github.com/owncloud/android/issues/716)
314 if ( Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB &&
315 (System.currentTimeMillis() - time) > 24 * 60 * 60 * 1000 ) {
316 Date date = new Date(time);
317 date.setHours(0);
318 date.setMinutes(0);
319 date.setSeconds(0);
320 dateString = DateUtils.getRelativeDateTimeString(
321 c, date.getTime(), minResolution, transitionResolution, flags
322 );
323 } else {
324 dateString = DateUtils.getRelativeDateTimeString(c, time, minResolution, transitionResolution, flags);
325 }
326 }
327
328 return dateString.toString().split(",")[0];
329 }
330
331 /**
332 * Update the passed path removing the last "/" if it is not the root folder
333 * @param path
334 */
335 public static String getPathWithoutLastSlash(String path) {
336
337 // Remove last slash from path
338 if (path.length() > 1 && path.charAt(path.length()-1) == OCFile.PATH_SEPARATOR.charAt(0)) {
339 path = path.substring(0, path.length()-1);
340 }
341 return path;
342 }
343 }