Merge branch 'develop' into release-1.6.0
[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 com.owncloud.android.R;
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",
67 "vnd.openxmlformats-officedocument.wordprocessingml.document",
68 "vnd.oasis.opendocument.text",
69 "rtf"
70 };
71 private static Set<String> SUBTYPES_DOCUMENT_SET = new HashSet<String>(Arrays.asList(SUBTYPES_DOCUMENT));
72 private static final String[] SUBTYPES_SPREADSHEET = { "msexcel",
73 "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
74 "vnd.oasis.opendocument.spreadsheet"
75 };
76 private static Set<String> SUBTYPES_SPREADSHEET_SET = new HashSet<String>(Arrays.asList(SUBTYPES_SPREADSHEET));
77 private static final String[] SUBTYPES_PRESENTATION = { "mspowerpoint",
78 "vnd.openxmlformats-officedocument.presentationml.presentation",
79 "vnd.oasis.opendocument.presentation"
80 };
81 private static Set<String> SUBTYPES_PRESENTATION_SET = new HashSet<String>(Arrays.asList(SUBTYPES_PRESENTATION));
82 private static final String[] SUBTYPES_COMPRESSED = {"x-tar", "x-gzip", "zip"};
83 private static final Set<String> SUBTYPES_COMPRESSED_SET = new HashSet<String>(Arrays.asList(SUBTYPES_COMPRESSED));
84 private static final String SUBTYPE_OCTET_STREAM = "octet-stream";
85 private static final String EXTENSION_RAR = "rar";
86 private static final String EXTENSION_RTF = "rtf";
87 private static final String EXTENSION_3GP = "3gp";
88
89 /**
90 * Converts the file size in bytes to human readable output.
91 *
92 * @param bytes Input file size
93 * @return Like something readable like "12 MB"
94 */
95 public static String bytesToHumanReadable(long bytes) {
96 double result = bytes;
97 int attachedsuff = 0;
98 while (result > 1024 && attachedsuff < sizeSuffixes.length) {
99 result /= 1024.;
100 attachedsuff++;
101 }
102 result = ((int) (result * 100)) / 100.;
103 return result + " " + sizeSuffixes[attachedsuff];
104 }
105
106 /**
107 * Removes special HTML entities from a string
108 *
109 * @param s Input string
110 * @return A cleaned version of the string
111 */
112 public static String HtmlDecode(String s) {
113 /*
114 * TODO: Perhaps we should use something more proven like:
115 * http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml%28java.lang.String%29
116 */
117
118 String ret = "";
119 for (int i = 0; i < s.length(); ++i) {
120 if (s.charAt(i) == '%') {
121 ret += (char) Integer.parseInt(s.substring(i + 1, i + 3), 16);
122 i += 2;
123 } else {
124 ret += s.charAt(i);
125 }
126 }
127 return ret;
128 }
129
130 /**
131 * Converts MIME types like "image/jpg" to more end user friendly output
132 * like "JPG image".
133 *
134 * @param mimetype MIME type to convert
135 * @return A human friendly version of the MIME type
136 */
137 public static String convertMIMEtoPrettyPrint(String mimetype) {
138 if (mimeType2HUmanReadable.containsKey(mimetype)) {
139 return mimeType2HUmanReadable.get(mimetype);
140 }
141 if (mimetype.split("/").length >= 2)
142 return mimetype.split("/")[1].toUpperCase() + " file";
143 return "Unknown type";
144 }
145
146
147 /**
148 * Returns the resource identifier of an image resource to use as icon associated to a
149 * known MIME type.
150 *
151 * @param mimetype MIME type string.
152 * @param filename name, with extension
153 * @return Resource identifier of an image resource.
154 */
155 public static int getResourceId(String mimetype, String filename) {
156
157 if (mimetype == null || "DIR".equals(mimetype)) {
158 return R.drawable.ic_menu_archive;
159
160 } else {
161 String [] parts = mimetype.split("/");
162 String type = parts[0];
163 String subtype = (parts.length > 1) ? parts[1] : "";
164
165 if(TYPE_TXT.equals(type)) {
166 return R.drawable.file_doc;
167
168 } else if(TYPE_IMAGE.equals(type)) {
169 return R.drawable.file_image;
170
171 } else if(TYPE_VIDEO.equals(type)) {
172 return R.drawable.file_movie;
173
174 } else if(TYPE_AUDIO.equals(type)) {
175 return R.drawable.file_sound;
176
177 } else if(TYPE_APPLICATION.equals(type)) {
178
179 if (SUBTYPE_PDF.equals(subtype)) {
180 return R.drawable.file_pdf;
181
182 } else if (SUBTYPES_DOCUMENT_SET.contains(subtype)) {
183 return R.drawable.file_doc;
184
185 } else if (SUBTYPES_SPREADSHEET_SET.contains(subtype)) {
186 return R.drawable.file_xls;
187
188 } else if (SUBTYPES_PRESENTATION_SET.contains(subtype)) {
189 return R.drawable.file_ppt;
190
191 } else if (SUBTYPES_COMPRESSED_SET.contains(subtype)) {
192 return R.drawable.file_zip;
193
194 } else if (SUBTYPE_OCTET_STREAM.equals(subtype) ) {
195 if (getExtension(filename).equalsIgnoreCase(EXTENSION_RAR)) {
196 return R.drawable.file_zip;
197
198 } else if (getExtension(filename).equalsIgnoreCase(EXTENSION_RTF)) {
199 return R.drawable.file_doc;
200
201 } else if (getExtension(filename).equalsIgnoreCase(EXTENSION_3GP)) {
202 return R.drawable.file_movie;
203
204 }
205 }
206 }
207 }
208
209 // default icon
210 return R.drawable.file;
211 }
212
213
214 private static String getExtension(String filename) {
215 String extension = filename.substring(filename.lastIndexOf(".") + 1);
216
217 return extension;
218 }
219
220 /**
221 * Converts Unix time to human readable format
222 * @param miliseconds that have passed since 01/01/1970
223 * @return The human readable time for the users locale
224 */
225 public static String unixTimeToHumanReadable(long milliseconds) {
226 Date date = new Date(milliseconds);
227 return date.toLocaleString();
228 }
229
230
231 public static int getSeasonalIconId() {
232 if (Calendar.getInstance().get(Calendar.DAY_OF_YEAR) >= 354) {
233 return R.drawable.winter_holidays_icon;
234 } else {
235 return R.drawable.icon;
236 }
237 }
238 }