Skip to content

Commit

Permalink
Merge pull request #2047 from vishalduggal/timob-2653
Browse files Browse the repository at this point in the history
[TIMOB-2653] Android: Complex/autogenerated remote image URLs fail, whereas simple URLs succeed
  • Loading branch information
ayeung committed Apr 24, 2012
2 parents f2349a3 + f560d01 commit 160966b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import org.appcelerator.titanium.io.TiResourceFile;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiMimeTypeHelper;
import org.appcelerator.titanium.util.TiUrl;

import ti.modules.titanium.xml.DocumentProxy;
import ti.modules.titanium.xml.XMLModule;
Expand Down Expand Up @@ -720,30 +721,6 @@ public String getResponseHeader(String headerName)
return result;
}

private static Uri getCleanUri(String uri)
{
Uri base = Uri.parse(uri);

Uri.Builder builder = base.buildUpon();
builder.encodedQuery(Uri.encode(Uri.decode(base.getQuery()), "&="));
String encodedAuthority = Uri.encode(Uri.decode(base.getAuthority()),"/:@");
int firstAt = encodedAuthority.indexOf('@');
if (firstAt >= 0) {
int lastAt = encodedAuthority.lastIndexOf('@');
if (lastAt > firstAt) {
// We have a situation that might be like this:
// http://user@domain.com:password@api.mickey.com
// i.e., the user name is user@domain.com, and the host
// is api.mickey.com. We need all at-signs prior to the final one (which
// indicates the host) to be encoded.
encodedAuthority = Uri.encode(encodedAuthority.substring(0, lastAt), "/:") + encodedAuthority.substring(lastAt);
}
}
builder.encodedAuthority(encodedAuthority);
builder.encodedPath(Uri.encode(Uri.decode(base.getPath()), "/"));
return builder.build();
}

public void open(String method, String url)
{
if (DBG) {
Expand All @@ -764,7 +741,7 @@ public void open(String method, String url)
}

if (autoEncodeUrl) {
this.uri = getCleanUri(url);
this.uri = TiUrl.getCleanUri(url);

} else {
this.uri = Uri.parse(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.appcelerator.titanium.util.TiDownloadListener;
import org.appcelerator.titanium.util.TiResponseCache;
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.util.TiUrl;
import org.appcelerator.titanium.view.TiDrawableReference;
import org.appcelerator.titanium.view.TiUIView;

Expand Down Expand Up @@ -724,11 +725,16 @@ private void setImage(boolean recycle)
}
boolean getAsync = true;
try {
URI uri = new URI(imageref.getUrl());
String imageUrl = TiUrl.getCleanUri(imageref.getUrl()).toString();

URI uri = new URI(imageUrl);
getAsync = !TiResponseCache.peek(uri);
} catch (URISyntaxException e) {
Log.e(LCAT, "URISyntaxException for url " + imageref.getUrl(), e);
getAsync = false;
} catch (NullPointerException e) {
Log.e(LCAT, "NullPointerException for url " + imageref.getUrl(), e);
getAsync = false;
}
if (getAsync) {
imageref.getBitmapAsync(downloadListener);
Expand Down Expand Up @@ -830,7 +836,7 @@ public void processProperties(KrollDict d)
Object image = d.get(TiC.PROPERTY_IMAGE);

if (image instanceof String) {
String imageUrl = (String) image;
String imageUrl = TiUrl.getCleanUri((String)image).toString();
URI imageUri = new URI(imageUrl);
if (URLUtil.isNetworkUrl(imageUrl) && !TiResponseCache.peek(imageUri)) {
setDefaultImageSource(defaultImage);
Expand All @@ -842,6 +848,8 @@ public void processProperties(KrollDict d)

} catch (URISyntaxException e) {
setDefaultImageSource(defaultImage);
} catch (NullPointerException e) {
setDefaultImageSource(defaultImage);
}
}
if (d.containsKey(TiC.PROPERTY_IMAGE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class TiUrl
public static final String CURRENT_PATH = ".";
public static final String PARENT_PATH_WITH_SEPARATOR = "../";
public static final String CURRENT_PATH_WITH_SEPARATOR = "./";


public String baseUrl;
public String url;
Expand Down Expand Up @@ -289,4 +290,37 @@ public static String absoluteUrl(String defaultScheme, String url, String baseUr
return url;
}
}

public static Uri getCleanUri(String argString)
{
try {
if (argString == null) {
return null;
}

Uri base = Uri.parse(argString);

Uri.Builder builder = base.buildUpon();
builder.encodedQuery(Uri.encode(Uri.decode(base.getQuery()), "&="));
String encodedAuthority = Uri.encode(Uri.decode(base.getAuthority()),"/:@");
int firstAt = encodedAuthority.indexOf('@');
if (firstAt >= 0) {
int lastAt = encodedAuthority.lastIndexOf('@');
if (lastAt > firstAt) {
// We have a situation that might be like this:
// http://user@domain.com:password@api.mickey.com
// i.e., the user name is user@domain.com, and the host
// is api.mickey.com. We need all at-signs prior to the final one (which
// indicates the host) to be encoded.
encodedAuthority = Uri.encode(encodedAuthority.substring(0, lastAt), "/:") + encodedAuthority.substring(lastAt);
}
}
builder.encodedAuthority(encodedAuthority);
builder.encodedPath(Uri.encode(Uri.decode(base.getPath()), "/"));
return builder.build();
} catch (Exception e) {
Log.e(TAG, "Exception in getCleanUri argString= " + argString, e);
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.appcelerator.titanium.util.TiDownloadManager;
import org.appcelerator.titanium.util.TiFileHelper;
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.util.TiUrl;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
Expand Down Expand Up @@ -625,9 +626,11 @@ public void getBitmapAsync(TiDownloadListener listener)
}

try {
TiDownloadManager.getInstance().download(new URI(url), listener);
TiDownloadManager.getInstance().download(new URI(TiUrl.getCleanUri(url).toString()), listener);
} catch (URISyntaxException e) {
Log.e(LCAT, "URI Invalid: " + url, e);
} catch (NullPointerException e) {
Log.e(LCAT, "NullPointerException: " + url, e);
}
}

Expand Down

0 comments on commit 160966b

Please sign in to comment.