X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/d24ba158d24c161a0db484ea551a40851a9d60e8..b20ace9185ea9b174675b77bcc71ee04d2578cc4:/src/com/owncloud/android/utils/DisplayUtils.java?ds=inline diff --git a/src/com/owncloud/android/utils/DisplayUtils.java b/src/com/owncloud/android/utils/DisplayUtils.java index 682d2be0..8c4c492b 100644 --- a/src/com/owncloud/android/utils/DisplayUtils.java +++ b/src/com/owncloud/android/utils/DisplayUtils.java @@ -18,6 +18,7 @@ package com.owncloud.android.utils; +import java.net.IDN; import java.util.Arrays; import java.util.Calendar; import java.util.Date; @@ -25,6 +26,9 @@ 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; /** @@ -235,4 +239,35 @@ public class DisplayUtils { 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; + } + } }