DisplayUtils.java 7.1 KB

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