Browse Source

Merge pull request #957 from owncloud/fix_crash_invalid_server_url

Fix crash when typed invalid URL
David A. Velasco 10 years ago
parent
commit
2fcffbd3d5

+ 3 - 2
src/com/owncloud/android/authentication/AuthenticatorActivity.java

@@ -380,7 +380,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             findViewById(R.id.hostUrlFrame).setVisibility(View.GONE);
             mRefreshButton = findViewById(R.id.centeredRefreshButton);
         }
-        showRefreshButton(mServerIsChecked && !mServerIsValid && 
+        showRefreshButton(mServerIsChecked && !mServerIsValid &&
                 mWaitingForOpId > Integer.MAX_VALUE);
         mServerStatusView = (TextView) findViewById(R.id.server_status_text);
         showServerStatus();
@@ -773,10 +773,11 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         mOkButton.setEnabled(false);
         mServerInfo = new GetServerInfoOperation.ServerInfo();
         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();

+ 16 - 9
src/com/owncloud/android/utils/DisplayUtils.java

@@ -257,26 +257,33 @@ public class DisplayUtils {
      */
     @TargetApi(Build.VERSION_CODES.GINGERBREAD)
     public static String convertIdn(String url, boolean toASCII) {
-        
+
+        String urlNoDots = url;
+        String dots="";
+        while (urlNoDots.startsWith(".")) {
+            urlNoDots = url.substring(1);
+            dots = dots + ".";
+        }
+
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
             // Find host name after '//' or '@'
             int hostStart = 0;
-            if  (url.indexOf("//") != -1) {
+            if  (urlNoDots.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);
+            hostEnd = (hostEnd == -1 ? urlNoDots.length() : hostStart + hostEnd);
+
+            String host = urlNoDots.substring(hostStart, hostEnd);
             host = (toASCII ? IDN.toASCII(host) : IDN.toUnicode(host));
-            
-            return url.substring(0, hostStart) + host + url.substring(hostEnd);
+
+            return dots + urlNoDots.substring(0, hostStart) + host + urlNoDots.substring(hostEnd);
         } else {
-            return url;
+            return dots + url;
         }
     }