79b56b936791608acc88f12c039a289e1e29101c
[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 as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 package com.owncloud.android;
21
22 import java.util.Arrays;
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 /**
31 * A helper class for some string operations.
32 *
33 * @author Bartek Przybylski
34 * @author David A. Velasco
35 */
36 public class DisplayUtils {
37
38 private static String TAG = DisplayUtils.class.getSimpleName();
39
40 private static final String[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
41
42 private static HashMap<String, String> mimeType2HUmanReadable;
43 static {
44 mimeType2HUmanReadable = new HashMap<String, String>();
45 // images
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");
53 // music
54 mimeType2HUmanReadable.put("audio/mpeg", "MP3 music file");
55 mimeType2HUmanReadable.put("application/ogg", "OGG music file");
56
57 }
58
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";
64
65 private static final String SUBTYPE_PDF = "pdf";
66 private static final String[] SUBTYPES_DOCUMENT = { "msword", "mspowerpoint", "msexcel",
67 "vnd.oasis.opendocument.presentation",
68 "vnd.oasis.opendocument.spreadsheet",
69 "vnd.oasis.opendocument.text"
70 };
71 private static Set<String> SUBTYPES_DOCUMENT_SET = new HashSet<String>(Arrays.asList(SUBTYPES_DOCUMENT));
72 private static final String[] SUBTYPES_COMPRESSED = {"x-tar", "x-gzip", "zip"};
73 private static final Set<String> SUBTYPES_COMPRESSED_SET = new HashSet<String>(Arrays.asList(SUBTYPES_COMPRESSED));
74
75 /**
76 * Converts the file size in bytes to human readable output.
77 *
78 * @param bytes Input file size
79 * @return Like something readable like "12 MB"
80 */
81 public static String bytesToHumanReadable(long bytes) {
82 double result = bytes;
83 int attachedsuff = 0;
84 while (result > 1024 && attachedsuff < sizeSuffixes.length) {
85 result /= 1024.;
86 attachedsuff++;
87 }
88 result = ((int) (result * 100)) / 100.;
89 return result + " " + sizeSuffixes[attachedsuff];
90 }
91
92 /**
93 * Removes special HTML entities from a string
94 *
95 * @param s Input string
96 * @return A cleaned version of the string
97 */
98 public static String HtmlDecode(String s) {
99 /*
100 * TODO: Perhaps we should use something more proven like:
101 * http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml%28java.lang.String%29
102 */
103
104 String ret = "";
105 for (int i = 0; i < s.length(); ++i) {
106 if (s.charAt(i) == '%') {
107 ret += (char) Integer.parseInt(s.substring(i + 1, i + 3), 16);
108 i += 2;
109 } else {
110 ret += s.charAt(i);
111 }
112 }
113 return ret;
114 }
115
116 /**
117 * Converts MIME types like "image/jpg" to more end user friendly output
118 * like "JPG image".
119 *
120 * @param mimetype MIME type to convert
121 * @return A human friendly version of the MIME type
122 */
123 public static String convertMIMEtoPrettyPrint(String mimetype) {
124 if (mimeType2HUmanReadable.containsKey(mimetype)) {
125 return mimeType2HUmanReadable.get(mimetype);
126 }
127 if (mimetype.split("/").length >= 2)
128 return mimetype.split("/")[1].toUpperCase() + " file";
129 return "Unknown type";
130 }
131
132
133 /**
134 * Returns the resource identifier of an image resource to use as icon associated to a
135 * known MIME type.
136 *
137 * @param mimetype MIME type string.
138 * @return Resource identifier of an image resource.
139 */
140 public static int getResourceId(String mimetype) {
141
142 if (mimetype == null || "DIR".equals(mimetype)) {
143 return R.drawable.ic_menu_archive;
144
145 } else {
146 String [] parts = mimetype.split("/");
147 String type = parts[0];
148 String subtype = parts[1];
149
150 if(TYPE_TXT.equals(type)) {
151 return R.drawable.file_doc;
152
153 } else if(TYPE_IMAGE.equals(type)) {
154 return R.drawable.file_image;
155
156 } else if(TYPE_VIDEO.equals(type)) {
157 return R.drawable.file_movie;
158
159 } else if(TYPE_AUDIO.equals(type)) {
160 return R.drawable.file_sound;
161
162 } else if(TYPE_APPLICATION.equals(type)) {
163
164 if (SUBTYPE_PDF.equals(subtype)) {
165 return R.drawable.file_pdf;
166
167 } else if (SUBTYPES_DOCUMENT_SET.contains(subtype)) {
168 return R.drawable.file_doc;
169
170 } else if (SUBTYPES_COMPRESSED_SET.contains(subtype)) {
171 return R.drawable.file_zip;
172 }
173
174 }
175 // problems: RAR, RTF, 3GP are send as application/octet-stream from the server ; extension in the filename should be explicitly reviewed
176 }
177
178 // default icon
179 return R.drawable.file;
180 }
181
182
183
184 /**
185 * Converts Unix time to human readable format
186 * @param miliseconds that have passed since 01/01/1970
187 * @return The human readable time for the users locale
188 */
189 public static String unixTimeToHumanReadable(long milliseconds) {
190 Date date = new Date(milliseconds);
191 return date.toLocaleString();
192 }
193 }