DisplayUtils.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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;
  19. import java.util.Arrays;
  20. import java.util.Date;
  21. import java.util.HashMap;
  22. import java.util.HashSet;
  23. import java.util.Set;
  24. /**
  25. * A helper class for some string operations.
  26. *
  27. * @author Bartek Przybylski
  28. * @author David A. Velasco
  29. */
  30. public class DisplayUtils {
  31. //private static String TAG = DisplayUtils.class.getSimpleName();
  32. private static final String[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
  33. private static HashMap<String, String> mimeType2HUmanReadable;
  34. static {
  35. mimeType2HUmanReadable = new HashMap<String, String>();
  36. // images
  37. mimeType2HUmanReadable.put("image/jpeg", "JPEG image");
  38. mimeType2HUmanReadable.put("image/jpg", "JPEG image");
  39. mimeType2HUmanReadable.put("image/png", "PNG image");
  40. mimeType2HUmanReadable.put("image/bmp", "Bitmap image");
  41. mimeType2HUmanReadable.put("image/gif", "GIF image");
  42. mimeType2HUmanReadable.put("image/svg+xml", "JPEG image");
  43. mimeType2HUmanReadable.put("image/tiff", "TIFF image");
  44. // music
  45. mimeType2HUmanReadable.put("audio/mpeg", "MP3 music file");
  46. mimeType2HUmanReadable.put("application/ogg", "OGG music file");
  47. }
  48. private static final String TYPE_APPLICATION = "application";
  49. private static final String TYPE_AUDIO = "audio";
  50. private static final String TYPE_IMAGE = "image";
  51. private static final String TYPE_TXT = "text";
  52. private static final String TYPE_VIDEO = "video";
  53. private static final String SUBTYPE_PDF = "pdf";
  54. private static final String[] SUBTYPES_DOCUMENT = { "msword", "mspowerpoint", "msexcel",
  55. "vnd.oasis.opendocument.presentation",
  56. "vnd.oasis.opendocument.spreadsheet",
  57. "vnd.oasis.opendocument.text"
  58. };
  59. private static Set<String> SUBTYPES_DOCUMENT_SET = new HashSet<String>(Arrays.asList(SUBTYPES_DOCUMENT));
  60. private static final String[] SUBTYPES_COMPRESSED = {"x-tar", "x-gzip", "zip"};
  61. private static final Set<String> SUBTYPES_COMPRESSED_SET = new HashSet<String>(Arrays.asList(SUBTYPES_COMPRESSED));
  62. /**
  63. * Converts the file size in bytes to human readable output.
  64. *
  65. * @param bytes Input file size
  66. * @return Like something readable like "12 MB"
  67. */
  68. public static String bytesToHumanReadable(long bytes) {
  69. double result = bytes;
  70. int attachedsuff = 0;
  71. while (result > 1024 && attachedsuff < sizeSuffixes.length) {
  72. result /= 1024.;
  73. attachedsuff++;
  74. }
  75. result = ((int) (result * 100)) / 100.;
  76. return result + " " + sizeSuffixes[attachedsuff];
  77. }
  78. /**
  79. * Removes special HTML entities from a string
  80. *
  81. * @param s Input string
  82. * @return A cleaned version of the string
  83. */
  84. public static String HtmlDecode(String s) {
  85. /*
  86. * TODO: Perhaps we should use something more proven like:
  87. * http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml%28java.lang.String%29
  88. */
  89. String ret = "";
  90. for (int i = 0; i < s.length(); ++i) {
  91. if (s.charAt(i) == '%') {
  92. ret += (char) Integer.parseInt(s.substring(i + 1, i + 3), 16);
  93. i += 2;
  94. } else {
  95. ret += s.charAt(i);
  96. }
  97. }
  98. return ret;
  99. }
  100. /**
  101. * Converts MIME types like "image/jpg" to more end user friendly output
  102. * like "JPG image".
  103. *
  104. * @param mimetype MIME type to convert
  105. * @return A human friendly version of the MIME type
  106. */
  107. public static String convertMIMEtoPrettyPrint(String mimetype) {
  108. if (mimeType2HUmanReadable.containsKey(mimetype)) {
  109. return mimeType2HUmanReadable.get(mimetype);
  110. }
  111. if (mimetype.split("/").length >= 2)
  112. return mimetype.split("/")[1].toUpperCase() + " file";
  113. return "Unknown type";
  114. }
  115. /**
  116. * Returns the resource identifier of an image resource to use as icon associated to a
  117. * known MIME type.
  118. *
  119. * @param mimetype MIME type string.
  120. * @return Resource identifier of an image resource.
  121. */
  122. public static int getResourceId(String mimetype) {
  123. if (mimetype == null || "DIR".equals(mimetype)) {
  124. return R.drawable.ic_menu_archive;
  125. } else {
  126. String [] parts = mimetype.split("/");
  127. String type = parts[0];
  128. String subtype = (parts.length > 1) ? parts[1] : "";
  129. if(TYPE_TXT.equals(type)) {
  130. return R.drawable.file_doc;
  131. } else if(TYPE_IMAGE.equals(type)) {
  132. return R.drawable.file_image;
  133. } else if(TYPE_VIDEO.equals(type)) {
  134. return R.drawable.file_movie;
  135. } else if(TYPE_AUDIO.equals(type)) {
  136. return R.drawable.file_sound;
  137. } else if(TYPE_APPLICATION.equals(type)) {
  138. if (SUBTYPE_PDF.equals(subtype)) {
  139. return R.drawable.file_pdf;
  140. } else if (SUBTYPES_DOCUMENT_SET.contains(subtype)) {
  141. return R.drawable.file_doc;
  142. } else if (SUBTYPES_COMPRESSED_SET.contains(subtype)) {
  143. return R.drawable.file_zip;
  144. }
  145. }
  146. // problems: RAR, RTF, 3GP are send as application/octet-stream from the server ; extension in the filename should be explicitly reviewed
  147. }
  148. // default icon
  149. return R.drawable.file;
  150. }
  151. /**
  152. * Converts Unix time to human readable format
  153. * @param miliseconds that have passed since 01/01/1970
  154. * @return The human readable time for the users locale
  155. */
  156. public static String unixTimeToHumanReadable(long milliseconds) {
  157. Date date = new Date(milliseconds);
  158. return date.toLocaleString();
  159. }
  160. }