DisplayUtils.java 11 KB

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