From 62266e2e74b076607fbdbc9ccc200e1f747b6875 Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Mon, 5 Oct 2015 11:24:20 +0200 Subject: [PATCH 1/9] Obey maximum image width (from blog settings) when uploading images from visual editor --- .../wordpress/android/util/ImageUtils.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java index 75a09bab21c9..e0335c0af57a 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java @@ -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; @@ -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; @@ -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[] From ae44dd44b2b7b014a1a039a332e2f6e3d5d555ef Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Sun, 13 Dec 2015 15:01:29 -0500 Subject: [PATCH 2/9] Allow photon to resize GIFs --- .../java/org/wordpress/android/util/PhotonUtils.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java index b69ad501ef4f..a9975cc341ab 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java @@ -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 @@ -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; From 5f7cdf877a243f77ccdb070d038253207a9b2eb6 Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Wed, 16 Dec 2015 08:18:07 +0100 Subject: [PATCH 3/9] fix #3521: use the simplest UnderlineSpan subclass --- .../android/util/helpers/WPUnderlineSpan.java | 30 +++++-------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/helpers/WPUnderlineSpan.java b/WordPressUtils/src/main/java/org/wordpress/android/util/helpers/WPUnderlineSpan.java index 12eadb379253..4b6805ccfd2e 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/helpers/WPUnderlineSpan.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/helpers/WPUnderlineSpan.java @@ -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); } } From 261e48bb66b6c8ee2b9993611c8fc7797555f365 Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Thu, 17 Dec 2015 12:45:44 -0500 Subject: [PATCH 4/9] Moved setupUrlConnection() to the utils library --- .../org/wordpress/android/util/HTTPUtils.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 WordPressUtils/src/main/java/org/wordpress/android/util/HTTPUtils.java diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/HTTPUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/HTTPUtils.java new file mode 100644 index 000000000000..9773d45d7629 --- /dev/null +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/HTTPUtils.java @@ -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 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 entry : headers.entrySet()) { + conn.setRequestProperty(entry.getKey(), entry.getValue()); + } + + return conn; + } +} From 355e34b84ad8c79589065dafb7cf19b619eb957c Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Thu, 17 Dec 2015 13:02:22 -0500 Subject: [PATCH 5/9] 1.7.0 version bump --- WordPressUtils/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPressUtils/build.gradle b/WordPressUtils/build.gradle index e0ef2731bb57..340a9b050b9d 100644 --- a/WordPressUtils/build.gradle +++ b/WordPressUtils/build.gradle @@ -32,7 +32,7 @@ android { buildToolsVersion "23.0.2" defaultConfig { - versionName "1.5.0" + versionName "1.7.0" minSdkVersion 14 targetSdkVersion 23 } From bec2b0ba9dfe04ca7ee6f264d7c39c17d2ed2121 Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Thu, 17 Dec 2015 18:20:05 +0100 Subject: [PATCH 6/9] s/getDomainFromUri/getHost/ --- .../java/org/wordpress/android/util/UrlUtilsTest.java | 6 +++--- .../main/java/org/wordpress/android/util/BlogUtils.java | 2 +- .../java/org/wordpress/android/util/GravatarUtils.java | 2 +- .../main/java/org/wordpress/android/util/UrlUtils.java | 8 +++----- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java index b72e94821203..0b4ff2b62932 100644 --- a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java +++ b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java @@ -7,16 +7,16 @@ 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")); } diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/BlogUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/BlogUtils.java index 685aabf9ec53..0d21c0c2f1f2 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/BlogUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/BlogUtils.java @@ -40,7 +40,7 @@ public static String getHomeURLOrHostNameFromAccountMap(Map 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; diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java index 7ce327072659..1fbfb3e56025 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java @@ -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); } diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java index f4b5756d25ee..3a0d7fd59a28 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java @@ -34,18 +34,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 ""; } From 821c32c9495b20d39e5eeccb6fb67f5fac2d30f1 Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Thu, 17 Dec 2015 18:20:35 +0100 Subject: [PATCH 7/9] kill StringUtils.getHost function --- .../wordpress/android/util/StringUtils.java | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/StringUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/StringUtils.java index 136cc4e541a4..25ddd2a41226 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/StringUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/StringUtils.java @@ -197,24 +197,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. From c21f9694529baffff66999b9e78536e23df94939 Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Mon, 21 Dec 2015 12:29:46 +0100 Subject: [PATCH 8/9] Add utility methods to check if a URL is wpcom. Add utility methods that check if it's safe to add the Authentication token to the request. --- .../wordpress/android/util/UrlUtilsTest.java | 30 +++++++++++++++++++ .../org/wordpress/android/util/UrlUtils.java | 12 ++++++++ 2 files changed, 42 insertions(+) diff --git a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java index 0b4ff2b62932..abf7a8fae526 100644 --- a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java +++ b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java @@ -2,6 +2,8 @@ import android.test.InstrumentationTestCase; +import java.net.MalformedURLException; +import java.net.URL; import java.util.HashMap; import java.util.Map; @@ -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; + } } diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java index 3a0d7fd59a28..6843a87fb676 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java @@ -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; @@ -172,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 */ From a731d835c7f2b60790032ef1ec2f2af403040560 Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Tue, 29 Dec 2015 17:52:14 +0100 Subject: [PATCH 9/9] 1.8.0 version bump --- WordPressUtils/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPressUtils/build.gradle b/WordPressUtils/build.gradle index 340a9b050b9d..1b188062bde8 100644 --- a/WordPressUtils/build.gradle +++ b/WordPressUtils/build.gradle @@ -32,7 +32,7 @@ android { buildToolsVersion "23.0.2" defaultConfig { - versionName "1.7.0" + versionName "1.8.0" minSdkVersion 14 targetSdkVersion 23 }