X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/e1245bbda7894e1a3028d2b54a6918e4bdd46ae2..b20ace9185ea9b174675b77bcc71ee04d2578cc4:/src/com/owncloud/android/utils/DisplayUtils.java diff --git a/src/com/owncloud/android/utils/DisplayUtils.java b/src/com/owncloud/android/utils/DisplayUtils.java index e4bdd999..8c4c492b 100644 --- a/src/com/owncloud/android/utils/DisplayUtils.java +++ b/src/com/owncloud/android/utils/DisplayUtils.java @@ -18,14 +18,18 @@ package com.owncloud.android.utils; +import java.net.IDN; import java.util.Arrays; +import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Set; +import android.annotation.TargetApi; +import android.os.Build; + import com.owncloud.android.R; -import com.owncloud.android.R.drawable; /** * A helper class for some string operations. @@ -63,14 +67,28 @@ public class DisplayUtils { 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 final String[] SUBTYPES_DOCUMENT = { "msword", + "vnd.openxmlformats-officedocument.wordprocessingml.document", + "vnd.oasis.opendocument.text", + "rtf" }; private static Set SUBTYPES_DOCUMENT_SET = new HashSet(Arrays.asList(SUBTYPES_DOCUMENT)); + private static final String[] SUBTYPES_SPREADSHEET = { "msexcel", + "vnd.openxmlformats-officedocument.spreadsheetml.sheet", + "vnd.oasis.opendocument.spreadsheet" + }; + private static Set SUBTYPES_SPREADSHEET_SET = new HashSet(Arrays.asList(SUBTYPES_SPREADSHEET)); + private static final String[] SUBTYPES_PRESENTATION = { "mspowerpoint", + "vnd.openxmlformats-officedocument.presentationml.presentation", + "vnd.oasis.opendocument.presentation" + }; + private static Set SUBTYPES_PRESENTATION_SET = new HashSet(Arrays.asList(SUBTYPES_PRESENTATION)); private static final String[] SUBTYPES_COMPRESSED = {"x-tar", "x-gzip", "zip"}; private static final Set SUBTYPES_COMPRESSED_SET = new HashSet(Arrays.asList(SUBTYPES_COMPRESSED)); + private static final String SUBTYPE_OCTET_STREAM = "octet-stream"; + private static final String EXTENSION_RAR = "rar"; + private static final String EXTENSION_RTF = "rtf"; + private static final String EXTENSION_3GP = "3gp"; /** * Converts the file size in bytes to human readable output. @@ -135,9 +153,10 @@ public class DisplayUtils { * known MIME type. * * @param mimetype MIME type string. + * @param filename name, with extension * @return Resource identifier of an image resource. */ - public static int getResourceId(String mimetype) { + public static int getResourceId(String mimetype, String filename) { if (mimetype == null || "DIR".equals(mimetype)) { return R.drawable.ic_menu_archive; @@ -167,12 +186,28 @@ public class DisplayUtils { } else if (SUBTYPES_DOCUMENT_SET.contains(subtype)) { return R.drawable.file_doc; + } else if (SUBTYPES_SPREADSHEET_SET.contains(subtype)) { + return R.drawable.file_xls; + + } else if (SUBTYPES_PRESENTATION_SET.contains(subtype)) { + return R.drawable.file_ppt; + } else if (SUBTYPES_COMPRESSED_SET.contains(subtype)) { return R.drawable.file_zip; - } - + + } else if (SUBTYPE_OCTET_STREAM.equals(subtype) ) { + if (getExtension(filename).equalsIgnoreCase(EXTENSION_RAR)) { + return R.drawable.file_zip; + + } else if (getExtension(filename).equalsIgnoreCase(EXTENSION_RTF)) { + return R.drawable.file_doc; + + } else if (getExtension(filename).equalsIgnoreCase(EXTENSION_3GP)) { + return R.drawable.file_movie; + + } + } } - // problems: RAR, RTF, 3GP are send as application/octet-stream from the server ; extension in the filename should be explicitly reviewed } // default icon @@ -180,7 +215,12 @@ public class DisplayUtils { } - + private static String getExtension(String filename) { + String extension = filename.substring(filename.lastIndexOf(".") + 1); + + return extension; + } + /** * Converts Unix time to human readable format * @param miliseconds that have passed since 01/01/1970 @@ -190,4 +230,44 @@ public class DisplayUtils { Date date = new Date(milliseconds); return date.toLocaleString(); } + + + public static int getSeasonalIconId() { + if (Calendar.getInstance().get(Calendar.DAY_OF_YEAR) >= 354) { + return R.drawable.winter_holidays_icon; + } else { + return R.drawable.icon; + } + } + + /** + * Converts an internationalized domain name (IDN) in an URL to and from ASCII/Unicode. + * @param url the URL where the domain name should be converted + * @param toASCII if true converts from Unicode to ASCII, if false converts from ASCII to Unicode + * @return the URL containing the converted domain name + */ + @TargetApi(Build.VERSION_CODES.GINGERBREAD) + public static String convertIdn(String url, boolean toASCII) { + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { + // Find host name after '//' or '@' + int hostStart = 0; + if (url.indexOf("//") != -1) { + hostStart = url.indexOf("//") + "//".length(); + } else if (url.indexOf("@") != -1) { + hostStart = url.indexOf("@") + "@".length(); + } + + int hostEnd = url.substring(hostStart).indexOf("/"); + // Handle URL which doesn't have a path (path is implicitly '/') + hostEnd = (hostEnd == -1 ? url.length() : hostStart + hostEnd); + + String host = url.substring(hostStart, hostEnd); + host = (toASCII ? IDN.toASCII(host) : IDN.toUnicode(host)); + + return url.substring(0, hostStart) + host + url.substring(hostEnd); + } else { + return url; + } + } }