Add code to handle ownCloud servers running on IDN
authorOliver Gasser <oliver@flowriver.net>
Wed, 17 Sep 2014 01:26:37 +0000 (03:26 +0200)
committerOliver Gasser <oliver@flowriver.net>
Wed, 17 Sep 2014 01:31:14 +0000 (03:31 +0200)
ownCloud instances can run on servers having an internationalized domain
name. This commit adds a conversion function between ASCII and Unicode.
The conversion function is used in the Authenticator and Preferences
activities.

See issue #469.

src/com/owncloud/android/authentication/AuthenticatorActivity.java
src/com/owncloud/android/ui/activity/Preferences.java
src/com/owncloud/android/utils/DisplayUtils.java

index 21aab72..9569a53 100644 (file)
@@ -84,6 +84,7 @@ import com.owncloud.android.ui.dialog.IndeterminateProgressDialog;
 import com.owncloud.android.ui.dialog.SamlWebViewDialog;\r
 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;\r
 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener;\r
 import com.owncloud.android.ui.dialog.SamlWebViewDialog;\r
 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;\r
 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener;\r
+import com.owncloud.android.utils.DisplayUtils;\r
 import com.owncloud.android.utils.Log_OC;\r
 \r
 /**\r
 import com.owncloud.android.utils.Log_OC;\r
 \r
 /**\r
@@ -351,7 +352,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         \r
         /// step 2 - set properties of UI elements (text, visibility, enabled...)\r
         mHostUrlInput = (EditText) findViewById(R.id.hostUrlInput);\r
         \r
         /// step 2 - set properties of UI elements (text, visibility, enabled...)\r
         mHostUrlInput = (EditText) findViewById(R.id.hostUrlInput);\r
-        mHostUrlInput.setText(mServerInfo.mBaseUrl);\r
+        // Convert IDN to Unicode\r
+        mHostUrlInput.setText(DisplayUtils.convertIdn(mServerInfo.mBaseUrl, false));\r
         if (mAction != ACTION_CREATE) {\r
             /// lock things that should not change\r
             mHostUrlInput.setEnabled(false);\r
         if (mAction != ACTION_CREATE) {\r
             /// lock things that should not change\r
             mHostUrlInput.setEnabled(false);\r
@@ -727,6 +729,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         showRefreshButton(false);\r
         \r
         if (uri.length() != 0) {\r
         showRefreshButton(false);\r
         \r
         if (uri.length() != 0) {\r
+            // Handle internationalized domain names\r
+            uri = DisplayUtils.convertIdn(uri, true);\r
             mServerStatusText = R.string.auth_testing_connection;\r
             mServerStatusIcon = R.drawable.progress_small;\r
             showServerStatus();\r
             mServerStatusText = R.string.auth_testing_connection;\r
             mServerStatusIcon = R.drawable.progress_small;\r
             showServerStatus();\r
index e726efa..e436dbb 100644 (file)
@@ -103,7 +103,7 @@ public class Preferences extends SherlockPreferenceActivity implements AccountMa
 
                 if (obj != null && obj instanceof LongClickableCheckBoxPreference) {
                     mShowContextMenu = true;
 
                 if (obj != null && obj instanceof LongClickableCheckBoxPreference) {
                     mShowContextMenu = true;
-                    mAccountName = obj.toString();
+                    mAccountName = ((LongClickableCheckBoxPreference) obj).getKey();
 
                     Preferences.this.openContextMenu(listView);
 
 
                     Preferences.this.openContextMenu(listView);
 
@@ -427,7 +427,8 @@ public class Preferences extends SherlockPreferenceActivity implements AccountMa
             for (Account a : accounts) {
                 LongClickableCheckBoxPreference accountPreference = new LongClickableCheckBoxPreference(this);
                 accountPreference.setKey(a.name);
             for (Account a : accounts) {
                 LongClickableCheckBoxPreference accountPreference = new LongClickableCheckBoxPreference(this);
                 accountPreference.setKey(a.name);
-                accountPreference.setTitle(a.name);
+                // Handle internationalized domain names
+                accountPreference.setTitle(DisplayUtils.convertIdn(a.name, false));
                 mAccountsPrefCategory.addPreference(accountPreference);
 
                 // Check the current account that is being used
                 mAccountsPrefCategory.addPreference(accountPreference);
 
                 // Check the current account that is being used
index 682d2be..8c4c492 100644 (file)
@@ -18,6 +18,7 @@
 \r
 package com.owncloud.android.utils;\r
 \r
 \r
 package com.owncloud.android.utils;\r
 \r
+import java.net.IDN;\r
 import java.util.Arrays;\r
 import java.util.Calendar;\r
 import java.util.Date;\r
 import java.util.Arrays;\r
 import java.util.Calendar;\r
 import java.util.Date;\r
@@ -25,6 +26,9 @@ import java.util.HashMap;
 import java.util.HashSet;\r
 import java.util.Set;\r
 \r
 import java.util.HashSet;\r
 import java.util.Set;\r
 \r
+import android.annotation.TargetApi;\r
+import android.os.Build;\r
+\r
 import com.owncloud.android.R;\r
 \r
 /**\r
 import com.owncloud.android.R;\r
 \r
 /**\r
@@ -235,4 +239,35 @@ public class DisplayUtils {
             return R.drawable.icon;\r
         }\r
     }\r
             return R.drawable.icon;\r
         }\r
     }\r
+    \r
+    /**\r
+     * Converts an internationalized domain name (IDN) in an URL to and from ASCII/Unicode.\r
+     * @param url the URL where the domain name should be converted\r
+     * @param toASCII if true converts from Unicode to ASCII, if false converts from ASCII to Unicode\r
+     * @return the URL containing the converted domain name\r
+     */\r
+    @TargetApi(Build.VERSION_CODES.GINGERBREAD)\r
+    public static String convertIdn(String url, boolean toASCII) {\r
+        \r
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {\r
+            // Find host name after '//' or '@'\r
+            int hostStart = 0;\r
+            if  (url.indexOf("//") != -1) {\r
+                hostStart = url.indexOf("//") + "//".length();\r
+            } else if (url.indexOf("@") != -1) {\r
+                hostStart = url.indexOf("@") + "@".length();\r
+            }\r
+            \r
+            int hostEnd = url.substring(hostStart).indexOf("/");\r
+            // Handle URL which doesn't have a path (path is implicitly '/')\r
+            hostEnd = (hostEnd == -1 ? url.length() : hostStart + hostEnd);\r
+            \r
+            String host = url.substring(hostStart, hostEnd);\r
+            host = (toASCII ? IDN.toASCII(host) : IDN.toUnicode(host));\r
+            \r
+            return url.substring(0, hostStart) + host + url.substring(hostEnd);\r
+        } else {\r
+            return url;\r
+        }\r
+    }\r
 }\r
 }\r