Răsfoiți Sursa

Merge pull request #673 from owncloud/idn_hosts

Suuport for URLs to ownCloud servers running on IDN
David A. Velasco 10 ani în urmă
părinte
comite
fe299fc8a1

+ 5 - 1
src/com/owncloud/android/authentication/AuthenticatorActivity.java

@@ -85,6 +85,7 @@ import com.owncloud.android.ui.dialog.IndeterminateProgressDialog;
 import com.owncloud.android.ui.dialog.SamlWebViewDialog;
 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener;
+import com.owncloud.android.utils.DisplayUtils;
 
 /**
  * This Activity is used to add an ownCloud account to the App
@@ -356,7 +357,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         
         /// step 2 - set properties of UI elements (text, visibility, enabled...)
         mHostUrlInput = (EditText) findViewById(R.id.hostUrlInput);
-        mHostUrlInput.setText(mServerInfo.mBaseUrl);
+        // Convert IDN to Unicode
+        mHostUrlInput.setText(DisplayUtils.convertIdn(mServerInfo.mBaseUrl, false));
         if (mAction != ACTION_CREATE) {
             /// lock things that should not change
             mHostUrlInput.setEnabled(false);
@@ -737,6 +739,8 @@ SsoWebViewClientListener, OnSslUntrustedCertListener {
         showRefreshButton(false);
         
         if (uri.length() != 0) {
+            // Handle internationalized domain names
+            uri = DisplayUtils.convertIdn(uri, true);
             mServerStatusText = R.string.auth_testing_connection;
             mServerStatusIcon = R.drawable.progress_small;
             showServerStatus();

+ 3 - 2
src/com/owncloud/android/ui/activity/Preferences.java

@@ -104,7 +104,7 @@ public class Preferences extends SherlockPreferenceActivity implements AccountMa
 
                 if (obj != null && obj instanceof LongClickableCheckBoxPreference) {
                     mShowContextMenu = true;
-                    mAccountName = obj.toString();
+                    mAccountName = ((LongClickableCheckBoxPreference) obj).getKey();
 
                     Preferences.this.openContextMenu(listView);
 
@@ -406,7 +406,8 @@ public class Preferences extends SherlockPreferenceActivity implements AccountMa
             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

+ 35 - 0
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;
+        }
+    }
 }