X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/a4ba6170ea7696e085b07adfef73eeb8b77cb8e2..52bd01bcedf1b8f13c49b67ee40a848bae1a9d40:/src/com/owncloud/android/DisplayUtils.java diff --git a/src/com/owncloud/android/DisplayUtils.java b/src/com/owncloud/android/DisplayUtils.java index a9cfe1dc..d2939c87 100644 --- a/src/com/owncloud/android/DisplayUtils.java +++ b/src/com/owncloud/android/DisplayUtils.java @@ -1,10 +1,10 @@ /* ownCloud Android client application * Copyright (C) 2011 Bartek Przybylski + * Copyright (C) 2012-2013 ownCloud Inc. * * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -18,18 +18,23 @@ package com.owncloud.android; +import java.util.Arrays; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; /** * A helper class for some string operations. * * @author Bartek Przybylski - * + * @author David A. Velasco */ public class DisplayUtils { - - private static final String[] suffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; + + private static String TAG = DisplayUtils.class.getSimpleName(); + + private static final String[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; private static HashMap mimeType2HUmanReadable; static { @@ -48,6 +53,22 @@ public class DisplayUtils { } + 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 SUBTYPES_DOCUMENT_SET = new HashSet(Arrays.asList(SUBTYPES_DOCUMENT)); + private static final String[] SUBTYPES_COMPRESSED = {"x-tar", "x-gzip", "zip"}; + private static final Set SUBTYPES_COMPRESSED_SET = new HashSet(Arrays.asList(SUBTYPES_COMPRESSED)); + /** * Converts the file size in bytes to human readable output. * @@ -57,12 +78,12 @@ public class DisplayUtils { public static String bytesToHumanReadable(long bytes) { double result = bytes; int attachedsuff = 0; - while (result > 1024 && attachedsuff < suffixes.length) { + while (result > 1024 && attachedsuff < sizeSuffixes.length) { result /= 1024.; attachedsuff++; } result = ((int) (result * 100)) / 100.; - return result + " " + suffixes[attachedsuff]; + return result + " " + sizeSuffixes[attachedsuff]; } /** @@ -104,6 +125,58 @@ public class DisplayUtils { return mimetype.split("/")[1].toUpperCase() + " file"; return "Unknown type"; } + + + /** + * Returns the resource identifier of an image resource to use as icon associated to a + * known MIME type. + * + * @param mimetype MIME type string. + * @return Resource identifier of an image resource. + */ + 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; + } + + } + // problems: RAR, RTF, 3GP are send as application/octet-stream from the server ; extension in the filename should be explicitly reviewed + } + + // default icon + return R.drawable.file; + } + + /** * Converts Unix time to human readable format