From: Lennart Rosam Date: Tue, 22 May 2012 16:17:16 +0000 (+0200) Subject: Display modified / created timestamp, JavaDocs in DisplayUtils X-Git-Tag: oc-android-1.4.3~398 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/5d7a57ec185c220a98b8779fb8099eb989ed6b6c Display modified / created timestamp, JavaDocs in DisplayUtils --- diff --git a/src/eu/alefzero/owncloud/DisplayUtils.java b/src/eu/alefzero/owncloud/DisplayUtils.java index 08afd65d..bf86c6f9 100644 --- a/src/eu/alefzero/owncloud/DisplayUtils.java +++ b/src/eu/alefzero/owncloud/DisplayUtils.java @@ -18,6 +18,7 @@ package eu.alefzero.owncloud; +import java.util.Date; import java.util.HashMap; /** @@ -27,8 +28,34 @@ import java.util.HashMap; * */ public class DisplayUtils { - public static String bitsToHumanReadable(long bitsLen) { - double result = bitsLen; + + private static final String[] suffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; + + private static HashMap mimeType2HUmanReadable; + static { + mimeType2HUmanReadable = new HashMap(); + // images + mimeType2HUmanReadable.put("image/jpeg", "JPEG image"); + mimeType2HUmanReadable.put("image/jpg", "JPEG image"); + mimeType2HUmanReadable.put("image/png", "PNG image"); + mimeType2HUmanReadable.put("image/bmp", "Bitmap image"); + mimeType2HUmanReadable.put("image/gif", "GIF image"); + mimeType2HUmanReadable.put("image/svg+xml", "JPEG image"); + mimeType2HUmanReadable.put("image/tiff", "TIFF image"); + // music + mimeType2HUmanReadable.put("audio/mpeg", "MP3 music file"); + mimeType2HUmanReadable.put("application/ogg", "OGG music file"); + + } + + /** + * Converts the file size in bytes to human readable output. + * + * @param bytes Input file size + * @return Like something readable like "12 MB" + */ + public static String bytesToHumanReadable(long bytes) { + double result = bytes; int attachedsuff = 0; while (result > 1024 && attachedsuff < suffixes.length) { result /= 1024.; @@ -38,7 +65,18 @@ public class DisplayUtils { return result + " " + suffixes[attachedsuff]; } + /** + * Removes special HTML entities from a string + * + * @param s Input string + * @return A cleaned version of the string + */ public static String HtmlDecode(String s) { + /* + * TODO: Perhaps we should use something more proven like: + * http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml%28java.lang.String%29 + */ + String ret = ""; for (int i = 0; i < s.length(); ++i) { if (s.charAt(i) == '%') { @@ -51,6 +89,13 @@ public class DisplayUtils { return ret; } + /** + * Converts MIME types like "image/jpg" to more end user friendly output + * like "JPG image". + * + * @param mimetype MIME type to convert + * @return A human friendly version of the MIME type + */ public static String convertMIMEtoPrettyPrint(String mimetype) { if (mimeType2HUmanReadable.containsKey(mimetype)) { return mimeType2HUmanReadable.get(mimetype); @@ -58,23 +103,13 @@ public class DisplayUtils { return mimetype.split("/")[1].toUpperCase() + " file"; } - private static final String[] suffixes = { "B", "KB", "MB", "GB", "TB", - "PB", "EB", "ZB", "YB" }; - - private static HashMap mimeType2HUmanReadable; - static { - mimeType2HUmanReadable = new HashMap(); - // images - mimeType2HUmanReadable.put("image/jpeg", "JPEG image"); - mimeType2HUmanReadable.put("image/jpg", "JPEG image"); - mimeType2HUmanReadable.put("image/png", "PNG image"); - mimeType2HUmanReadable.put("image/bmp", "Bitmap image"); - mimeType2HUmanReadable.put("image/gif", "GIF image"); - mimeType2HUmanReadable.put("image/svg+xml", "JPEG image"); - mimeType2HUmanReadable.put("image/tiff", "TIFF image"); - // music - mimeType2HUmanReadable.put("audio/mpeg", "MP3 music file"); - mimeType2HUmanReadable.put("application/ogg", "OGG music file"); - + /** + * Converts Unix time to human readable format + * @param miliseconds that have passed since 01/01/1970 + * @return The human readable time for the users locale + */ + public static String unixTimeToHumanReadable(long milliseconds) { + Date date = new Date(milliseconds); + return date.toLocaleString(); } } diff --git a/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java b/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java index 7d10ebac..f76c2b2a 100644 --- a/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java +++ b/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java @@ -135,6 +135,9 @@ public class FileDetailFragment extends SherlockFragment implements setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile .getMimetype())); setFilesize(mFile.getFileLength()); + setTimeCreated(mFile.getCreationTimestamp()); + setTimeModified(mFile.getModificationTimestamp()); + // Update preview if (mFile.getStoragePath() != null) { try { @@ -178,7 +181,21 @@ public class FileDetailFragment extends SherlockFragment implements private void setFilesize(long filesize) { TextView tv = (TextView) getView().findViewById(R.id.fdSize); if (tv != null) - tv.setText(DisplayUtils.bitsToHumanReadable(filesize)); + tv.setText(DisplayUtils.bytesToHumanReadable(filesize)); + } + + private void setTimeCreated(long milliseconds){ + TextView tv = (TextView) getView().findViewById(R.id.fdCreated); + if(tv != null){ + tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds)); + } + } + + private void setTimeModified(long milliseconds){ + TextView tv = (TextView) getView().findViewById(R.id.fdModified); + if(tv != null){ + tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds)); + } } @Override