Skip to content

Commit 26983b7

Browse files
committed
Merge develop into site-settings-review
2 parents 8b485b6 + faf8169 commit 26983b7

File tree

10 files changed

+147
-59
lines changed

10 files changed

+147
-59
lines changed

WordPressUtils/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ android {
3232
buildToolsVersion "23.0.2"
3333

3434
defaultConfig {
35-
versionName "1.5.0"
35+
versionName "1.8.0"
3636
minSdkVersion 14
3737
targetSdkVersion 23
3838
}

WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22

33
import android.test.InstrumentationTestCase;
44

5+
import java.net.MalformedURLException;
6+
import java.net.URL;
57
import java.util.HashMap;
68
import java.util.Map;
79

810
public class UrlUtilsTest extends InstrumentationTestCase {
911
public void testGetDomainFromUrlWithEmptyStringDoesNotReturnNull() {
10-
assertNotNull(UrlUtils.getDomainFromUrl(""));
12+
assertNotNull(UrlUtils.getHost(""));
1113
}
1214

1315
public void testGetDomainFromUrlWithNoHostDoesNotReturnNull() {
14-
assertNotNull(UrlUtils.getDomainFromUrl("wordpress"));
16+
assertNotNull(UrlUtils.getHost("wordpress"));
1517
}
1618

1719
public void testGetDomainFromUrlWithHostReturnsHost() {
1820
String url = "http://www.wordpress.com";
19-
String host = UrlUtils.getDomainFromUrl(url);
21+
String host = UrlUtils.getHost(url);
2022

2123
assertTrue(host.equals("www.wordpress.com"));
2224
}
@@ -75,4 +77,32 @@ public void testAppendUrlParameters2() {
7577
assertTrue("failed test on url: " + url, false);
7678
}
7779
}
80+
81+
public void testHttps1() {
82+
assertFalse(UrlUtils.isHttps(buildURL("http://wordpress.com/xmlrpc.php")));
83+
}
84+
85+
public void testHttps2() {
86+
assertFalse(UrlUtils.isHttps(buildURL("http://wordpress.com#.b.com/test")));
87+
}
88+
89+
public void testHttps3() {
90+
assertFalse(UrlUtils.isHttps(buildURL("http://wordpress.com/xmlrpc.php")));
91+
}
92+
93+
public void testHttps4() {
94+
assertTrue(UrlUtils.isHttps(buildURL("https://wordpress.com")));
95+
}
96+
97+
public void testHttps5() {
98+
assertTrue(UrlUtils.isHttps(buildURL("https://wordpress.com/test#test")));
99+
}
100+
101+
private URL buildURL(String address) {
102+
URL url = null;
103+
try {
104+
url = new URL(address);
105+
} catch (MalformedURLException e) {}
106+
return url;
107+
}
78108
}

WordPressUtils/src/main/java/org/wordpress/android/util/BlogUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static String getHomeURLOrHostNameFromAccountMap(Map<String, Object> acco
4040
homeURL = StringUtils.removeTrailingSlash(homeURL);
4141

4242
if (homeURL.length() == 0) {
43-
return StringUtils.getHost(MapUtils.getMapStr(account, "url"));
43+
return UrlUtils.getHost(MapUtils.getMapStr(account, "url"));
4444
}
4545

4646
return homeURL;

WordPressUtils/src/main/java/org/wordpress/android/util/GravatarUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static String blavatarFromUrl(final String url, int size) {
7777
}
7878
public static String blavatarFromUrl(final String url, int size, DefaultImage defaultImage) {
7979
return "http://gravatar.com/blavatar/"
80-
+ StringUtils.getMd5Hash(UrlUtils.getDomainFromUrl(url))
80+
+ StringUtils.getMd5Hash(UrlUtils.getHost(url))
8181
+ "?d=" + defaultImage.toString()
8282
+ "&size=" + Integer.toString(size);
8383
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.wordpress.android.util;
2+
3+
import java.io.IOException;
4+
import java.net.HttpURLConnection;
5+
import java.net.URL;
6+
import java.util.Map;
7+
8+
public class HTTPUtils {
9+
public static final int REQUEST_TIMEOUT_MS = 30000;
10+
11+
/**
12+
* Builds an HttpURLConnection from a URL and header map. Will force HTTPS usage if given an Authorization header.
13+
* @throws IOException
14+
*/
15+
public static HttpURLConnection setupUrlConnection(String url, Map<String, String> headers) throws IOException {
16+
// Force HTTPS usage if an authorization header was specified
17+
if (headers.keySet().contains("Authorization")) {
18+
url = UrlUtils.makeHttps(url);
19+
}
20+
21+
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
22+
conn.setReadTimeout(REQUEST_TIMEOUT_MS);
23+
conn.setConnectTimeout(REQUEST_TIMEOUT_MS);
24+
25+
for (Map.Entry<String, String> entry : headers.entrySet()) {
26+
conn.setRequestProperty(entry.getKey(), entry.getValue());
27+
}
28+
29+
return conn;
30+
}
31+
}

WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.provider.MediaStore;
2121
import android.text.TextUtils;
2222
import android.util.Log;
23+
import android.webkit.MimeTypeMap;
2324
import android.widget.ImageView;
2425

2526
import org.apache.http.HttpEntity;
@@ -31,6 +32,7 @@
3132
import java.io.ByteArrayOutputStream;
3233
import java.io.File;
3334
import java.io.FileInputStream;
35+
import java.io.FileOutputStream;
3436
import java.io.IOException;
3537
import java.io.InputStream;
3638
import java.lang.ref.WeakReference;
@@ -396,6 +398,60 @@ public static Bitmap getScaledBitmapAtLongestSide(Bitmap bitmap, int targetSize)
396398
return Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, true);
397399
}
398400

401+
/**
402+
* Given the path to an image, resize the image down to within a maximum width
403+
* @param path the path to the original image
404+
* @param maxWidth the maximum allowed width
405+
* @return the path to the resized image
406+
*/
407+
public static String createResizedImageWithMaxWidth(Context context, String path, int maxWidth) {
408+
File file = new File(path);
409+
if (!file.exists()) {
410+
return path;
411+
}
412+
413+
String mimeType = MediaUtils.getMediaFileMimeType(file);
414+
if (mimeType.equals("image/gif")) {
415+
// Don't rescale gifs to maintain their quality
416+
return path;
417+
}
418+
419+
String fileName = MediaUtils.getMediaFileName(file, mimeType);
420+
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(fileName).toLowerCase();
421+
422+
int[] dimensions = getImageSize(Uri.fromFile(file), context);
423+
int orientation = getImageOrientation(context, path);
424+
425+
if (dimensions[0] <= maxWidth) {
426+
// Image width is within limits; don't resize
427+
return path;
428+
}
429+
430+
// Create resized image
431+
byte[] bytes = ImageUtils.createThumbnailFromUri(context, Uri.parse(path), maxWidth, fileExtension, orientation);
432+
433+
if (bytes != null) {
434+
try {
435+
File resizedImageFile = File.createTempFile("wp-image-", fileExtension);
436+
FileOutputStream out = new FileOutputStream(resizedImageFile);
437+
out.write(bytes);
438+
out.close();
439+
440+
String tempFilePath = resizedImageFile.getPath();
441+
442+
if (!TextUtils.isEmpty(tempFilePath)) {
443+
return tempFilePath;
444+
} else {
445+
AppLog.e(AppLog.T.POSTS, "Failed to create resized image");
446+
}
447+
} catch (IOException e) {
448+
AppLog.e(AppLog.T.POSTS, "Failed to create image temp file");
449+
}
450+
}
451+
452+
return path;
453+
}
454+
399455
/**
400456
* nbradbury - 21-Feb-2014 - similar to createThumbnail but more efficient since it doesn't
401457
* require passing the full-size image as an array of bytes[]

WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static boolean isMshotsUrl(final String imageUrl) {
2323
* returns a photon url for the passed image with the resize query set to the passed
2424
* dimensions - note that the passed quality parameter will only affect JPEGs
2525
*/
26-
public static enum Quality {
26+
public enum Quality {
2727
HIGH,
2828
MEDIUM,
2929
LOW
@@ -45,13 +45,6 @@ public static String getPhotonImageUrl(String imageUrl, int width, int height, Q
4545
// remove existing query string since it may contain params that conflict with the passed ones
4646
imageUrl = UrlUtils.removeQuery(imageUrl);
4747

48-
// don't use with GIFs - photon breaks animated GIFs, and sometimes returns a GIF that
49-
// can't be read by BitmapFactory.decodeByteArray (used by Volley in ImageRequest.java
50-
// to decode the downloaded image)
51-
if (imageUrl.endsWith(".gif")) {
52-
return imageUrl;
53-
}
54-
5548
// if this is an "mshots" url, skip photon and return it with a query that sets the width/height
5649
if (isMshotsUrl(imageUrl)) {
5750
return imageUrl + "?w=" + width + "&h=" + height;

WordPressUtils/src/main/java/org/wordpress/android/util/StringUtils.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -198,24 +198,6 @@ public static String getPhotonUrl(String imageUrl, int size) {
198198
return "http://i0.wp.com/" + imageUrl + "?w=" + size;
199199
}
200200

201-
public static String getHost(String url) {
202-
if (TextUtils.isEmpty(url)) {
203-
return "";
204-
}
205-
206-
int doubleslash = url.indexOf("//");
207-
if (doubleslash == -1) {
208-
doubleslash = 0;
209-
} else {
210-
doubleslash += 2;
211-
}
212-
213-
int end = url.indexOf('/', doubleslash);
214-
end = (end >= 0) ? end : url.length();
215-
216-
return url.substring(doubleslash, end);
217-
}
218-
219201
public static String replaceUnicodeSurrogateBlocksWithHTMLEntities(final String inputString) {
220202
final int length = inputString.length();
221203
StringBuilder out = new StringBuilder(); // Used to hold the output.

WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.UnsupportedEncodingException;
1111
import java.net.IDN;
1212
import java.net.URI;
13+
import java.net.URL;
1314
import java.net.URLDecoder;
1415
import java.net.URLEncoder;
1516
import java.nio.charset.Charset;
@@ -34,18 +35,16 @@ public static String urlDecode(final String text) {
3435
}
3536

3637
/**
37-
*
38-
* @param urlString url to get domain from
39-
* @return domain of uri if available. Empty string otherwise.
38+
* @param urlString url to get host from
39+
* @return host of uri if available. Empty string otherwise.
4040
*/
41-
public static String getDomainFromUrl(final String urlString) {
41+
public static String getHost(final String urlString) {
4242
if (urlString != null) {
4343
Uri uri = Uri.parse(urlString);
4444
if (uri.getHost() != null) {
4545
return uri.getHost();
4646
}
4747
}
48-
4948
return "";
5049
}
5150

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

176+
public static boolean isHttps(URL url) {
177+
return url != null && "https".equals(url.getProtocol());
178+
}
179+
180+
public static boolean isHttps(URI uri) {
181+
if (uri == null) return false;
182+
183+
String protocol = uri.getScheme();
184+
return protocol != null && protocol.equals("https");
185+
}
186+
177187
/**
178188
* returns https: version of passed http: url
179189
*/

WordPressUtils/src/main/java/org/wordpress/android/util/helpers/WPUnderlineSpan.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,18 @@
1717
package org.wordpress.android.util.helpers;
1818

1919
import android.os.Parcel;
20-
import android.text.ParcelableSpan;
21-
import android.text.TextPaint;
22-
import android.text.style.CharacterStyle;
23-
import android.text.style.UpdateAppearance;
20+
import android.text.style.UnderlineSpan;
2421

25-
public class WPUnderlineSpan extends CharacterStyle
26-
implements UpdateAppearance, ParcelableSpan {
22+
/**
23+
* WPUnderlineSpan is used as an alternative class to UnderlineSpan. UnderlineSpan is used by EditText auto
24+
* correct, so it can get mixed up with our formatting.
25+
*/
26+
public class WPUnderlineSpan extends UnderlineSpan {
2727
public WPUnderlineSpan() {
28+
super();
2829
}
2930

3031
public WPUnderlineSpan(Parcel src) {
31-
}
32-
33-
public int getSpanTypeId() {
34-
return 6;
35-
}
36-
37-
public int describeContents() {
38-
return 0;
39-
}
40-
41-
public void writeToParcel(Parcel dest, int flags) {
42-
}
43-
44-
@Override
45-
public void updateDrawState(TextPaint ds) {
46-
ds.setUnderlineText(true);
32+
super(src);
4733
}
4834
}

0 commit comments

Comments
 (0)