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