Skip to content

Commit

Permalink
Merge pull request #673 from owncloud/idn_hosts
Browse files Browse the repository at this point in the history
Suuport for URLs to ownCloud servers running on IDN
  • Loading branch information
davivel committed Oct 28, 2014
2 parents 5acc314 + d9604bf commit fe299fc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
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
Expand Down Expand Up @@ -356,7 +357,8 @@ private void initServerPreFragment(Bundle savedInstanceState) {

/// 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);
Expand Down Expand Up @@ -737,6 +739,8 @@ private void checkOcServer() {
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();
Expand Down
5 changes: 3 additions & 2 deletions src/com/owncloud/android/ui/activity/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public boolean onItemLongClick(AdapterView<?> parent, View view, int position, l

if (obj != null && obj instanceof LongClickableCheckBoxPreference) {
mShowContextMenu = true;
mAccountName = obj.toString();
mAccountName = ((LongClickableCheckBoxPreference) obj).getKey();

Preferences.this.openContextMenu(listView);

Expand Down Expand Up @@ -406,7 +406,8 @@ private void addAccountsCheckboxPreferences() {
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
Expand Down
35 changes: 35 additions & 0 deletions src/com/owncloud/android/utils/DisplayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@

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;

/**
Expand Down Expand Up @@ -235,4 +239,35 @@ public static int getSeasonalIconId() {
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;
}
}
}

0 comments on commit fe299fc

Please sign in to comment.