Skip to content

Commit

Permalink
Merge develop into site-settings-review
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyr59h committed Jan 6, 2016
2 parents 5d281f1 + a731d83 commit 920ea46
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 59 deletions.
2 changes: 1 addition & 1 deletion WordPressUtils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ android {
buildToolsVersion "23.0.2"

defaultConfig {
versionName "1.5.0"
versionName "1.8.0"
minSdkVersion 14
targetSdkVersion 23
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

import android.test.InstrumentationTestCase;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class UrlUtilsTest extends InstrumentationTestCase {
public void testGetDomainFromUrlWithEmptyStringDoesNotReturnNull() {
assertNotNull(UrlUtils.getDomainFromUrl(""));
assertNotNull(UrlUtils.getHost(""));
}

public void testGetDomainFromUrlWithNoHostDoesNotReturnNull() {
assertNotNull(UrlUtils.getDomainFromUrl("wordpress"));
assertNotNull(UrlUtils.getHost("wordpress"));
}

public void testGetDomainFromUrlWithHostReturnsHost() {
String url = "http://www.wordpress.com";
String host = UrlUtils.getDomainFromUrl(url);
String host = UrlUtils.getHost(url);

assertTrue(host.equals("www.wordpress.com"));
}
Expand Down Expand Up @@ -75,4 +77,32 @@ public void testAppendUrlParameters2() {
assertTrue("failed test on url: " + url, false);
}
}

public void testHttps1() {
assertFalse(UrlUtils.isHttps(buildURL("http://wordpress.com/xmlrpc.php")));
}

public void testHttps2() {
assertFalse(UrlUtils.isHttps(buildURL("http://wordpress.com#.b.com/test")));
}

public void testHttps3() {
assertFalse(UrlUtils.isHttps(buildURL("http://wordpress.com/xmlrpc.php")));
}

public void testHttps4() {
assertTrue(UrlUtils.isHttps(buildURL("https://wordpress.com")));
}

public void testHttps5() {
assertTrue(UrlUtils.isHttps(buildURL("https://wordpress.com/test#test")));
}

private URL buildURL(String address) {
URL url = null;
try {
url = new URL(address);
} catch (MalformedURLException e) {}
return url;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static String getHomeURLOrHostNameFromAccountMap(Map<String, Object> acco
homeURL = StringUtils.removeTrailingSlash(homeURL);

if (homeURL.length() == 0) {
return StringUtils.getHost(MapUtils.getMapStr(account, "url"));
return UrlUtils.getHost(MapUtils.getMapStr(account, "url"));
}

return homeURL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static String blavatarFromUrl(final String url, int size) {
}
public static String blavatarFromUrl(final String url, int size, DefaultImage defaultImage) {
return "http://gravatar.com/blavatar/"
+ StringUtils.getMd5Hash(UrlUtils.getDomainFromUrl(url))
+ StringUtils.getMd5Hash(UrlUtils.getHost(url))
+ "?d=" + defaultImage.toString()
+ "&size=" + Integer.toString(size);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.wordpress.android.util;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class HTTPUtils {
public static final int REQUEST_TIMEOUT_MS = 30000;

/**
* Builds an HttpURLConnection from a URL and header map. Will force HTTPS usage if given an Authorization header.
* @throws IOException
*/
public static HttpURLConnection setupUrlConnection(String url, Map<String, String> headers) throws IOException {
// Force HTTPS usage if an authorization header was specified
if (headers.keySet().contains("Authorization")) {
url = UrlUtils.makeHttps(url);
}

HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setReadTimeout(REQUEST_TIMEOUT_MS);
conn.setConnectTimeout(REQUEST_TIMEOUT_MS);

for (Map.Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}

return conn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.MimeTypeMap;
import android.widget.ImageView;

import org.apache.http.HttpEntity;
Expand All @@ -31,6 +32,7 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -396,6 +398,60 @@ public static Bitmap getScaledBitmapAtLongestSide(Bitmap bitmap, int targetSize)
return Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, true);
}

/**
* Given the path to an image, resize the image down to within a maximum width
* @param path the path to the original image
* @param maxWidth the maximum allowed width
* @return the path to the resized image
*/
public static String createResizedImageWithMaxWidth(Context context, String path, int maxWidth) {
File file = new File(path);
if (!file.exists()) {
return path;
}

String mimeType = MediaUtils.getMediaFileMimeType(file);
if (mimeType.equals("image/gif")) {
// Don't rescale gifs to maintain their quality
return path;
}

String fileName = MediaUtils.getMediaFileName(file, mimeType);
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(fileName).toLowerCase();

int[] dimensions = getImageSize(Uri.fromFile(file), context);
int orientation = getImageOrientation(context, path);

if (dimensions[0] <= maxWidth) {
// Image width is within limits; don't resize
return path;
}

// Create resized image
byte[] bytes = ImageUtils.createThumbnailFromUri(context, Uri.parse(path), maxWidth, fileExtension, orientation);

if (bytes != null) {
try {
File resizedImageFile = File.createTempFile("wp-image-", fileExtension);
FileOutputStream out = new FileOutputStream(resizedImageFile);
out.write(bytes);
out.close();

String tempFilePath = resizedImageFile.getPath();

if (!TextUtils.isEmpty(tempFilePath)) {
return tempFilePath;
} else {
AppLog.e(AppLog.T.POSTS, "Failed to create resized image");
}
} catch (IOException e) {
AppLog.e(AppLog.T.POSTS, "Failed to create image temp file");
}
}

return path;
}

/**
* nbradbury - 21-Feb-2014 - similar to createThumbnail but more efficient since it doesn't
* require passing the full-size image as an array of bytes[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static boolean isMshotsUrl(final String imageUrl) {
* returns a photon url for the passed image with the resize query set to the passed
* dimensions - note that the passed quality parameter will only affect JPEGs
*/
public static enum Quality {
public enum Quality {
HIGH,
MEDIUM,
LOW
Expand All @@ -45,13 +45,6 @@ public static String getPhotonImageUrl(String imageUrl, int width, int height, Q
// remove existing query string since it may contain params that conflict with the passed ones
imageUrl = UrlUtils.removeQuery(imageUrl);

// don't use with GIFs - photon breaks animated GIFs, and sometimes returns a GIF that
// can't be read by BitmapFactory.decodeByteArray (used by Volley in ImageRequest.java
// to decode the downloaded image)
if (imageUrl.endsWith(".gif")) {
return imageUrl;
}

// if this is an "mshots" url, skip photon and return it with a query that sets the width/height
if (isMshotsUrl(imageUrl)) {
return imageUrl + "?w=" + width + "&h=" + height;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,24 +198,6 @@ public static String getPhotonUrl(String imageUrl, int size) {
return "http://i0.wp.com/" + imageUrl + "?w=" + size;
}

public static String getHost(String url) {
if (TextUtils.isEmpty(url)) {
return "";
}

int doubleslash = url.indexOf("//");
if (doubleslash == -1) {
doubleslash = 0;
} else {
doubleslash += 2;
}

int end = url.indexOf('/', doubleslash);
end = (end >= 0) ? end : url.length();

return url.substring(doubleslash, end);
}

public static String replaceUnicodeSurrogateBlocksWithHTMLEntities(final String inputString) {
final int length = inputString.length();
StringBuilder out = new StringBuilder(); // Used to hold the output.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.UnsupportedEncodingException;
import java.net.IDN;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
Expand All @@ -34,18 +35,16 @@ public static String urlDecode(final String text) {
}

/**
*
* @param urlString url to get domain from
* @return domain of uri if available. Empty string otherwise.
* @param urlString url to get host from
* @return host of uri if available. Empty string otherwise.
*/
public static String getDomainFromUrl(final String urlString) {
public static String getHost(final String urlString) {
if (urlString != null) {
Uri uri = Uri.parse(urlString);
if (uri.getHost() != null) {
return uri.getHost();
}
}

return "";
}

Expand Down Expand Up @@ -174,6 +173,17 @@ public static boolean isHttps(final String urlString) {
return (urlString != null && urlString.startsWith("https:"));
}

public static boolean isHttps(URL url) {
return url != null && "https".equals(url.getProtocol());
}

public static boolean isHttps(URI uri) {
if (uri == null) return false;

String protocol = uri.getScheme();
return protocol != null && protocol.equals("https");
}

/**
* returns https: version of passed http: url
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,18 @@
package org.wordpress.android.util.helpers;

import android.os.Parcel;
import android.text.ParcelableSpan;
import android.text.TextPaint;
import android.text.style.CharacterStyle;
import android.text.style.UpdateAppearance;
import android.text.style.UnderlineSpan;

public class WPUnderlineSpan extends CharacterStyle
implements UpdateAppearance, ParcelableSpan {
/**
* WPUnderlineSpan is used as an alternative class to UnderlineSpan. UnderlineSpan is used by EditText auto
* correct, so it can get mixed up with our formatting.
*/
public class WPUnderlineSpan extends UnderlineSpan {
public WPUnderlineSpan() {
super();
}

public WPUnderlineSpan(Parcel src) {
}

public int getSpanTypeId() {
return 6;
}

public int describeContents() {
return 0;
}

public void writeToParcel(Parcel dest, int flags) {
}

@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(true);
super(src);
}
}

0 comments on commit 920ea46

Please sign in to comment.