DisplayUtils.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 Bartek Przybylski
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  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.Date;
  20. import java.util.HashMap;
  21. /**
  22. * A helper class for some string operations.
  23. *
  24. * @author Bartek Przybylski
  25. *
  26. */
  27. public class DisplayUtils {
  28. private static final String[] suffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
  29. private static HashMap<String, String> mimeType2HUmanReadable;
  30. static {
  31. mimeType2HUmanReadable = new HashMap<String, String>();
  32. // images
  33. mimeType2HUmanReadable.put("image/jpeg", "JPEG image");
  34. mimeType2HUmanReadable.put("image/jpg", "JPEG image");
  35. mimeType2HUmanReadable.put("image/png", "PNG image");
  36. mimeType2HUmanReadable.put("image/bmp", "Bitmap image");
  37. mimeType2HUmanReadable.put("image/gif", "GIF image");
  38. mimeType2HUmanReadable.put("image/svg+xml", "JPEG image");
  39. mimeType2HUmanReadable.put("image/tiff", "TIFF image");
  40. // music
  41. mimeType2HUmanReadable.put("audio/mpeg", "MP3 music file");
  42. mimeType2HUmanReadable.put("application/ogg", "OGG music file");
  43. }
  44. /**
  45. * Converts the file size in bytes to human readable output.
  46. *
  47. * @param bytes Input file size
  48. * @return Like something readable like "12 MB"
  49. */
  50. public static String bytesToHumanReadable(long bytes) {
  51. double result = bytes;
  52. int attachedsuff = 0;
  53. while (result > 1024 && attachedsuff < suffixes.length) {
  54. result /= 1024.;
  55. attachedsuff++;
  56. }
  57. result = ((int) (result * 100)) / 100.;
  58. return result + " " + suffixes[attachedsuff];
  59. }
  60. /**
  61. * Removes special HTML entities from a string
  62. *
  63. * @param s Input string
  64. * @return A cleaned version of the string
  65. */
  66. public static String HtmlDecode(String s) {
  67. /*
  68. * TODO: Perhaps we should use something more proven like:
  69. * http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml%28java.lang.String%29
  70. */
  71. String ret = "";
  72. for (int i = 0; i < s.length(); ++i) {
  73. if (s.charAt(i) == '%') {
  74. ret += (char) Integer.parseInt(s.substring(i + 1, i + 3), 16);
  75. i += 2;
  76. } else {
  77. ret += s.charAt(i);
  78. }
  79. }
  80. return ret;
  81. }
  82. /**
  83. * Converts MIME types like "image/jpg" to more end user friendly output
  84. * like "JPG image".
  85. *
  86. * @param mimetype MIME type to convert
  87. * @return A human friendly version of the MIME type
  88. */
  89. public static String convertMIMEtoPrettyPrint(String mimetype) {
  90. if (mimeType2HUmanReadable.containsKey(mimetype)) {
  91. return mimeType2HUmanReadable.get(mimetype);
  92. }
  93. if (mimetype.split("/").length >= 2)
  94. return mimetype.split("/")[1].toUpperCase() + " file";
  95. return "Unknown type";
  96. }
  97. /**
  98. * Converts Unix time to human readable format
  99. * @param miliseconds that have passed since 01/01/1970
  100. * @return The human readable time for the users locale
  101. */
  102. public static String unixTimeToHumanReadable(long milliseconds) {
  103. Date date = new Date(milliseconds);
  104. return date.toLocaleString();
  105. }
  106. }