1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
19 package com
.owncloud
.android
.utils
;
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
;
29 import android
.annotation
.TargetApi
;
30 import android
.content
.Context
;
31 import android
.os
.Build
;
32 import android
.text
.format
.DateFormat
;
33 import android
.text
.format
.DateUtils
;
35 import com
.owncloud
.android
.MainApp
;
36 import com
.owncloud
.android
.R
;
39 * A helper class for some string operations.
41 * @author Bartek Przybylski
42 * @author David A. Velasco
44 public class DisplayUtils
{
46 private static final String OWNCLOUD_APP_NAME
= "ownCloud";
48 //private static String TAG = DisplayUtils.class.getSimpleName();
50 private static final String
[] sizeSuffixes
= { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
52 private static HashMap
<String
, String
> mimeType2HUmanReadable
;
54 mimeType2HUmanReadable
= new HashMap
<String
, String
>();
56 mimeType2HUmanReadable
.put("image/jpeg", "JPEG image");
57 mimeType2HUmanReadable
.put("image/jpg", "JPEG image");
58 mimeType2HUmanReadable
.put("image/png", "PNG image");
59 mimeType2HUmanReadable
.put("image/bmp", "Bitmap image");
60 mimeType2HUmanReadable
.put("image/gif", "GIF image");
61 mimeType2HUmanReadable
.put("image/svg+xml", "JPEG image");
62 mimeType2HUmanReadable
.put("image/tiff", "TIFF image");
64 mimeType2HUmanReadable
.put("audio/mpeg", "MP3 music file");
65 mimeType2HUmanReadable
.put("application/ogg", "OGG music file");
69 private static final String TYPE_APPLICATION
= "application";
70 private static final String TYPE_AUDIO
= "audio";
71 private static final String TYPE_IMAGE
= "image";
72 private static final String TYPE_TXT
= "text";
73 private static final String TYPE_VIDEO
= "video";
75 private static final String SUBTYPE_PDF
= "pdf";
76 private static final String
[] SUBTYPES_DOCUMENT
= { "msword",
77 "vnd.openxmlformats-officedocument.wordprocessingml.document",
78 "vnd.oasis.opendocument.text",
81 private static Set
<String
> SUBTYPES_DOCUMENT_SET
= new HashSet
<String
>(Arrays
.asList(SUBTYPES_DOCUMENT
));
82 private static final String
[] SUBTYPES_SPREADSHEET
= { "msexcel",
83 "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
84 "vnd.oasis.opendocument.spreadsheet"
86 private static Set
<String
> SUBTYPES_SPREADSHEET_SET
= new HashSet
<String
>(Arrays
.asList(SUBTYPES_SPREADSHEET
));
87 private static final String
[] SUBTYPES_PRESENTATION
= { "mspowerpoint",
88 "vnd.openxmlformats-officedocument.presentationml.presentation",
89 "vnd.oasis.opendocument.presentation"
91 private static Set
<String
> SUBTYPES_PRESENTATION_SET
= new HashSet
<String
>(Arrays
.asList(SUBTYPES_PRESENTATION
));
92 private static final String
[] SUBTYPES_COMPRESSED
= {"x-tar", "x-gzip", "zip"};
93 private static final Set
<String
> SUBTYPES_COMPRESSED_SET
= new HashSet
<String
>(Arrays
.asList(SUBTYPES_COMPRESSED
));
94 private static final String SUBTYPE_OCTET_STREAM
= "octet-stream";
95 private static final String EXTENSION_RAR
= "rar";
96 private static final String EXTENSION_RTF
= "rtf";
97 private static final String EXTENSION_3GP
= "3gp";
100 * Converts the file size in bytes to human readable output.
102 * @param bytes Input file size
103 * @return Like something readable like "12 MB"
105 public static String
bytesToHumanReadable(long bytes
) {
106 double result
= bytes
;
107 int attachedsuff
= 0;
108 while (result
> 1024 && attachedsuff
< sizeSuffixes
.length
) {
112 result
= ((int) (result
* 100)) / 100.;
113 return result
+ " " + sizeSuffixes
[attachedsuff
];
117 * Removes special HTML entities from a string
119 * @param s Input string
120 * @return A cleaned version of the string
122 public static String
HtmlDecode(String s
) {
124 * TODO: Perhaps we should use something more proven like:
125 * http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml%28java.lang.String%29
129 for (int i
= 0; i
< s
.length(); ++i
) {
130 if (s
.charAt(i
) == '%') {
131 ret
+= (char) Integer
.parseInt(s
.substring(i
+ 1, i
+ 3), 16);
141 * Converts MIME types like "image/jpg" to more end user friendly output
144 * @param mimetype MIME type to convert
145 * @return A human friendly version of the MIME type
147 public static String
convertMIMEtoPrettyPrint(String mimetype
) {
148 if (mimeType2HUmanReadable
.containsKey(mimetype
)) {
149 return mimeType2HUmanReadable
.get(mimetype
);
151 if (mimetype
.split("/").length
>= 2)
152 return mimetype
.split("/")[1].toUpperCase() + " file";
153 return "Unknown type";
158 * Returns the resource identifier of an image resource to use as icon associated to a
161 * @param mimetype MIME type string.
162 * @param filename name, with extension
163 * @return Resource identifier of an image resource.
165 public static int getResourceId(String mimetype
, String filename
) {
167 if (mimetype
== null
|| "DIR".equals(mimetype
)) {
168 return R
.drawable
.ic_menu_archive
;
171 String
[] parts
= mimetype
.split("/");
172 String type
= parts
[0];
173 String subtype
= (parts
.length
> 1) ? parts
[1] : "";
175 if(TYPE_TXT
.equals(type
)) {
176 return R
.drawable
.file_doc
;
178 } else if(TYPE_IMAGE
.equals(type
)) {
179 return R
.drawable
.file_image
;
181 } else if(TYPE_VIDEO
.equals(type
)) {
182 return R
.drawable
.file_movie
;
184 } else if(TYPE_AUDIO
.equals(type
)) {
185 return R
.drawable
.file_sound
;
187 } else if(TYPE_APPLICATION
.equals(type
)) {
189 if (SUBTYPE_PDF
.equals(subtype
)) {
190 return R
.drawable
.file_pdf
;
192 } else if (SUBTYPES_DOCUMENT_SET
.contains(subtype
)) {
193 return R
.drawable
.file_doc
;
195 } else if (SUBTYPES_SPREADSHEET_SET
.contains(subtype
)) {
196 return R
.drawable
.file_xls
;
198 } else if (SUBTYPES_PRESENTATION_SET
.contains(subtype
)) {
199 return R
.drawable
.file_ppt
;
201 } else if (SUBTYPES_COMPRESSED_SET
.contains(subtype
)) {
202 return R
.drawable
.file_zip
;
204 } else if (SUBTYPE_OCTET_STREAM
.equals(subtype
) ) {
205 if (getExtension(filename
).equalsIgnoreCase(EXTENSION_RAR
)) {
206 return R
.drawable
.file_zip
;
208 } else if (getExtension(filename
).equalsIgnoreCase(EXTENSION_RTF
)) {
209 return R
.drawable
.file_doc
;
211 } else if (getExtension(filename
).equalsIgnoreCase(EXTENSION_3GP
)) {
212 return R
.drawable
.file_movie
;
220 return R
.drawable
.file
;
224 private static String
getExtension(String filename
) {
225 String extension
= filename
.substring(filename
.lastIndexOf(".") + 1);
231 * Converts Unix time to human readable format
232 * @param miliseconds that have passed since 01/01/1970
233 * @return The human readable time for the users locale
235 public static String
unixTimeToHumanReadable(long milliseconds
) {
236 Date date
= new Date(milliseconds
);
237 return date
.toLocaleString();
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
;
246 return R
.drawable
.icon
;
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
256 @TargetApi(Build
.VERSION_CODES
.GINGERBREAD
)
257 public static String
convertIdn(String url
, boolean toASCII
) {
259 if (Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.GINGERBREAD
) {
260 // Find host name after '//' or '@'
262 if (url
.indexOf("//") != -1) {
263 hostStart
= url
.indexOf("//") + "//".length();
264 } else if (url
.indexOf("@") != -1) {
265 hostStart
= url
.indexOf("@") + "@".length();
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
);
272 String host
= url
.substring(hostStart
, hostEnd
);
273 host
= (toASCII ? IDN
.toASCII(host
) : IDN
.toUnicode(host
));
275 return url
.substring(0, hostStart
) + host
+ url
.substring(hostEnd
);
281 public static CharSequence
getRelativeDateTimeString(Context c
, long time
, long minResolution
, long transitionResolution
, int flags
){
282 CharSequence dateString
= "";
285 if (time
> System
.currentTimeMillis()){
286 return DisplayUtils
.unixTimeToHumanReadable(time
);
288 // < 60 seconds -> seconds ago
289 else if ((System
.currentTimeMillis() - time
) < 60 * 1000) {
290 return c
.getString(R
.string
.file_list_seconds_ago
);
292 // Workaround 2.x bug
293 if (Build
.VERSION
.SDK_INT
<= Build
.VERSION_CODES
.HONEYCOMB
&& (System
.currentTimeMillis() - time
) > 24 * 60 * 60 * 1000){
294 Date date
= new Date(time
);
298 dateString
= DateUtils
.getRelativeDateTimeString(c
, date
.getTime(), minResolution
, transitionResolution
, flags
);
300 dateString
= DateUtils
.getRelativeDateTimeString(c
, time
, minResolution
, transitionResolution
, flags
);
304 return dateString
.toString().split(",")[0];