DisplayUtils.java 12 KB

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