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