DisplayUtils.java 13 KB

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