123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- package com.owncloud.android;
- import java.util.Arrays;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Set;
- public class DisplayUtils {
-
-
-
- private static final String[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
- private static HashMap<String, String> mimeType2HUmanReadable;
- static {
- mimeType2HUmanReadable = new HashMap<String, String>();
-
- 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");
-
- mimeType2HUmanReadable.put("audio/mpeg", "MP3 music file");
- mimeType2HUmanReadable.put("application/ogg", "OGG music file");
- }
- private static final String TYPE_APPLICATION = "application";
- private static final String TYPE_AUDIO = "audio";
- private static final String TYPE_IMAGE = "image";
- private static final String TYPE_TXT = "text";
- private static final String TYPE_VIDEO = "video";
-
- private static final String SUBTYPE_PDF = "pdf";
- private static final String[] SUBTYPES_DOCUMENT = { "msword", "mspowerpoint", "msexcel",
- "vnd.oasis.opendocument.presentation",
- "vnd.oasis.opendocument.spreadsheet",
- "vnd.oasis.opendocument.text"
- };
- private static Set<String> SUBTYPES_DOCUMENT_SET = new HashSet<String>(Arrays.asList(SUBTYPES_DOCUMENT));
- private static final String[] SUBTYPES_COMPRESSED = {"x-tar", "x-gzip", "zip"};
- private static final Set<String> SUBTYPES_COMPRESSED_SET = new HashSet<String>(Arrays.asList(SUBTYPES_COMPRESSED));
-
-
- public static String bytesToHumanReadable(long bytes) {
- double result = bytes;
- int attachedsuff = 0;
- while (result > 1024 && attachedsuff < sizeSuffixes.length) {
- result /= 1024.;
- attachedsuff++;
- }
- result = ((int) (result * 100)) / 100.;
- return result + " " + sizeSuffixes[attachedsuff];
- }
-
- public static String HtmlDecode(String s) {
-
- String ret = "";
- for (int i = 0; i < s.length(); ++i) {
- if (s.charAt(i) == '%') {
- ret += (char) Integer.parseInt(s.substring(i + 1, i + 3), 16);
- i += 2;
- } else {
- ret += s.charAt(i);
- }
- }
- return ret;
- }
-
- public static String convertMIMEtoPrettyPrint(String mimetype) {
- if (mimeType2HUmanReadable.containsKey(mimetype)) {
- return mimeType2HUmanReadable.get(mimetype);
- }
- if (mimetype.split("/").length >= 2)
- return mimetype.split("/")[1].toUpperCase() + " file";
- return "Unknown type";
- }
-
-
-
- public static int getResourceId(String mimetype) {
- if (mimetype == null || "DIR".equals(mimetype)) {
- return R.drawable.ic_menu_archive;
-
- } else {
- String [] parts = mimetype.split("/");
- String type = parts[0];
- String subtype = (parts.length > 1) ? parts[1] : "";
-
- if(TYPE_TXT.equals(type)) {
- return R.drawable.file_doc;
-
- } else if(TYPE_IMAGE.equals(type)) {
- return R.drawable.file_image;
-
- } else if(TYPE_VIDEO.equals(type)) {
- return R.drawable.file_movie;
-
- } else if(TYPE_AUDIO.equals(type)) {
- return R.drawable.file_sound;
-
- } else if(TYPE_APPLICATION.equals(type)) {
-
- if (SUBTYPE_PDF.equals(subtype)) {
- return R.drawable.file_pdf;
-
- } else if (SUBTYPES_DOCUMENT_SET.contains(subtype)) {
- return R.drawable.file_doc;
- } else if (SUBTYPES_COMPRESSED_SET.contains(subtype)) {
- return R.drawable.file_zip;
- }
-
- }
-
- }
-
- return R.drawable.file;
- }
-
-
- public static String unixTimeToHumanReadable(long milliseconds) {
- Date date = new Date(milliseconds);
- return date.toLocaleString();
- }
- }
|