Skip to content

Commit e0721c8

Browse files
committed
Merge branch 'release/4.6' into issue/3115-runtime-permissions
2 parents c3ad1f2 + bb4cb66 commit e0721c8

File tree

8 files changed

+184
-28
lines changed

8 files changed

+184
-28
lines changed

WordPressUtils/build.gradle

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ dependencies {
2020
exclude group: 'commons-logging'
2121
}
2222
compile 'com.mcxiaoke.volley:library:1.0.18'
23-
compile 'com.android.support:support-v13:23.0.0'
24-
compile 'com.android.support:design:23.0.0'
23+
compile 'com.android.support:support-v13:23.0.1'
2524
}
2625

2726
android {
@@ -30,7 +29,7 @@ android {
3029
publishNonDefault true
3130

3231
compileSdkVersion 23
33-
buildToolsVersion '23.0.0'
32+
buildToolsVersion '23.0.1'
3433

3534
defaultConfig {
3635
versionName "1.5.0"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.wordpress.android.util;
2+
3+
import android.test.InstrumentationTestCase;
4+
5+
import org.json.JSONArray;
6+
import org.json.JSONObject;
7+
8+
public class JSONUtilsTest extends InstrumentationTestCase {
9+
public void testQueryJSONNullSource1() {
10+
JSONUtils.queryJSON((JSONObject) null, "", "");
11+
}
12+
13+
public void testQueryJSONNullSource2() {
14+
JSONUtils.queryJSON((JSONArray) null, "", "");
15+
}
16+
17+
public void testQueryJSONNullQuery1() {
18+
JSONUtils.queryJSON(new JSONObject(), null, "");
19+
}
20+
21+
public void testQueryJSONNullQuery2() {
22+
JSONUtils.queryJSON(new JSONArray(), null, "");
23+
}
24+
25+
public void testQueryJSONNullReturnValue1() {
26+
JSONUtils.queryJSON(new JSONObject(), "", null);
27+
}
28+
29+
public void testQueryJSONNullReturnValue2() {
30+
JSONUtils.queryJSON(new JSONArray(), "", null);
31+
}
32+
}

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import android.test.InstrumentationTestCase;
44

5+
import java.util.HashMap;
6+
import java.util.Map;
7+
58
public class UrlUtilsTest extends InstrumentationTestCase {
69
public void testGetDomainFromUrlWithEmptyStringDoesNotReturnNull() {
710
assertNotNull(UrlUtils.getDomainFromUrl(""));
@@ -17,4 +20,59 @@ public void testGetDomainFromUrlWithHostReturnsHost() {
1720

1821
assertTrue(host.equals("www.wordpress.com"));
1922
}
23+
24+
public void testAppendUrlParameter1() {
25+
String url = UrlUtils.appendUrlParameter("http://wp.com/test", "preview", "true");
26+
assertEquals("http://wp.com/test?preview=true", url);
27+
}
28+
29+
public void testAppendUrlParameter2() {
30+
String url = UrlUtils.appendUrlParameter("http://wp.com/test?q=pony", "preview", "true");
31+
assertEquals("http://wp.com/test?q=pony&preview=true", url);
32+
}
33+
34+
public void testAppendUrlParameter3() {
35+
String url = UrlUtils.appendUrlParameter("http://wp.com/test?q=pony#unicorn", "preview", "true");
36+
assertEquals("http://wp.com/test?q=pony&preview=true#unicorn", url);
37+
}
38+
39+
public void testAppendUrlParameter4() {
40+
String url = UrlUtils.appendUrlParameter("/relative/test", "preview", "true");
41+
assertEquals("/relative/test?preview=true", url);
42+
}
43+
44+
public void testAppendUrlParameter5() {
45+
String url = UrlUtils.appendUrlParameter("/relative/", "preview", "true");
46+
assertEquals("/relative/?preview=true", url);
47+
}
48+
49+
public void testAppendUrlParameter6() {
50+
String url = UrlUtils.appendUrlParameter("http://wp.com/test/", "preview", "true");
51+
assertEquals("http://wp.com/test/?preview=true", url);
52+
}
53+
54+
public void testAppendUrlParameter7() {
55+
String url = UrlUtils.appendUrlParameter("http://wp.com/test/?q=pony", "preview", "true");
56+
assertEquals("http://wp.com/test/?q=pony&preview=true", url);
57+
}
58+
59+
public void testAppendUrlParameters1() {
60+
Map<String, String> params = new HashMap<>();
61+
params.put("w", "200");
62+
params.put("h", "300");
63+
String url = UrlUtils.appendUrlParameters("http://wp.com/test", params);
64+
if (!url.equals("http://wp.com/test?h=300&w=200") && !url.equals("http://wp.com/test?w=200&h=300")) {
65+
assertTrue("failed test on url: " + url, false);
66+
}
67+
}
68+
69+
public void testAppendUrlParameters2() {
70+
Map<String, String> params = new HashMap<>();
71+
params.put("h", "300");
72+
params.put("w", "200");
73+
String url = UrlUtils.appendUrlParameters("/relative/test", params);
74+
if (!url.equals("/relative/test?h=300&w=200") && !url.equals("/relative/test?w=200&h=300")) {
75+
assertTrue("failed test on url: " + url, false);
76+
}
77+
}
2078
}

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ public class BlogUtils {
88
public int compare(Object blog1, Object blog2) {
99
Map<String, Object> blogMap1 = (Map<String, Object>) blog1;
1010
Map<String, Object> blogMap2 = (Map<String, Object>) blog2;
11-
String blogName1 = getBlogNameOrHostNameFromAccountMap(blogMap1);
12-
String blogName2 = getBlogNameOrHostNameFromAccountMap(blogMap2);
11+
String blogName1 = getBlogNameOrHomeURLFromAccountMap(blogMap1);
12+
String blogName2 = getBlogNameOrHomeURLFromAccountMap(blogMap2);
1313
return blogName1.compareToIgnoreCase(blogName2);
1414
}
1515
};
1616

1717
/**
18-
* Return a blog name or blog url (host part only) if trimmed name is an empty string
18+
* Return a blog name or blog home URL if trimmed name is an empty string
1919
*/
20-
public static String getBlogNameOrHostNameFromAccountMap(Map<String, Object> account) {
20+
public static String getBlogNameOrHomeURLFromAccountMap(Map<String, Object> account) {
2121
String blogName = getBlogNameFromAccountMap(account);
2222
if (blogName.trim().length() == 0) {
23-
blogName = StringUtils.getHost(MapUtils.getMapStr(account, "url"));
23+
blogName = BlogUtils.getHomeURLOrHostNameFromAccountMap(account);
2424
}
2525
return blogName;
2626
}
@@ -33,9 +33,16 @@ public static String getBlogNameFromAccountMap(Map<String, Object> account) {
3333
}
3434

3535
/**
36-
* Return blog url (host part only) if trimmed name is an empty string
36+
* Return the blog home URL setting or the host name if home URL is an empty string.
3737
*/
38-
public static String getHostNameFromAccountMap(Map<String, Object> account) {
39-
return StringUtils.getHost(MapUtils.getMapStr(account, "url"));
38+
public static String getHomeURLOrHostNameFromAccountMap(Map<String, Object> account) {
39+
String homeURL = UrlUtils.removeScheme(MapUtils.getMapStr(account, "homeURL"));
40+
homeURL = StringUtils.removeTrailingSlash(homeURL);
41+
42+
if (homeURL.length() == 0) {
43+
return StringUtils.getHost(MapUtils.getMapStr(account, "url"));
44+
}
45+
46+
return homeURL;
4047
}
4148
}

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,30 @@
88
import org.wordpress.android.util.AppLog.T;
99

1010
import java.util.ArrayList;
11-
import java.util.Iterator;
1211

1312
public class JSONUtils {
14-
private static String QUERY_SEPERATOR=".";
15-
private static String QUERY_ARRAY_INDEX_START="[";
16-
private static String QUERY_ARRAY_INDEX_END="]";
17-
private static String QUERY_ARRAY_FIRST="first";
18-
private static String QUERY_ARRAY_LAST="last";
13+
private static String QUERY_SEPERATOR = ".";
14+
private static String QUERY_ARRAY_INDEX_START = "[";
15+
private static String QUERY_ARRAY_INDEX_END = "]";
16+
private static String QUERY_ARRAY_FIRST = "first";
17+
private static String QUERY_ARRAY_LAST = "last";
1918

2019
private static final String JSON_NULL_STR = "null";
20+
private static final String TAG = "JSONUtils";
2121

22-
private static final String TAG="JSONUtils";
2322
/**
2423
* Given a JSONObject and a key path (e.g property.child) and a default it will
2524
* traverse the object graph and pull out the desired property
2625
*/
2726
public static <U> U queryJSON(JSONObject source, String query, U defaultObject) {
27+
if (source == null) {
28+
AppLog.e(T.UTILS, "Parameter source is null, can't query a null object");
29+
return defaultObject;
30+
}
31+
if (query == null) {
32+
AppLog.e(T.UTILS, "Parameter query is null");
33+
return defaultObject;
34+
}
2835
int nextSeperator = query.indexOf(QUERY_SEPERATOR);
2936
int nextIndexStart = query.indexOf(QUERY_ARRAY_INDEX_START);
3037
if (nextSeperator == -1 && nextIndexStart == -1) {
@@ -56,9 +63,6 @@ public static <U> U queryJSON(JSONObject source, String query, U defaultObject)
5663
String nextQuery = query.substring(endQuery);
5764
String key = query.substring(0, endQuery);
5865
try {
59-
if (source == null) {
60-
return defaultObject;
61-
}
6266
if (nextQuery.indexOf(QUERY_SEPERATOR) == 0) {
6367
return queryJSON(source.getJSONObject(key), nextQuery.substring(1), defaultObject);
6468
} else if (nextQuery.indexOf(QUERY_ARRAY_INDEX_START) == 0) {
@@ -89,7 +93,15 @@ public static <U> U queryJSON(JSONObject source, String query, U defaultObject)
8993
* Acceptable indexes include negative numbers to reference items from the end of
9094
* the list as well as "last" and "first" as more explicit references to "0" and "-1"
9195
*/
92-
public static <U> U queryJSON(JSONArray source, String query, U defaultObject){
96+
public static <U> U queryJSON(JSONArray source, String query, U defaultObject) {
97+
if (source == null) {
98+
AppLog.e(T.UTILS, "Parameter source is null, can't query a null object");
99+
return defaultObject;
100+
}
101+
if (query == null) {
102+
AppLog.e(T.UTILS, "Parameter query is null");
103+
return defaultObject;
104+
}
93105
// query must start with [ have an index and then have ]
94106
int indexStart = query.indexOf(QUERY_ARRAY_INDEX_START);
95107
int indexEnd = query.indexOf(QUERY_ARRAY_INDEX_END);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public static void dump() {
2828
getInstance().dumpToLog();
2929
}
3030

31+
public static void stop() {
32+
getInstance().reset(null);
33+
}
34+
3135
private static ProfilingUtils getInstance() {
3236
if (sInstance == null) {
3337
sInstance = new ProfilingUtils();
@@ -56,12 +60,18 @@ public void reset() {
5660
}
5761

5862
public void addSplit(String splitLabel) {
63+
if (mLabel == null) {
64+
return;
65+
}
5966
long now = SystemClock.elapsedRealtime();
6067
mSplits.add(now);
6168
mSplitLabels.add(splitLabel);
6269
}
6370

6471
public void dumpToLog() {
72+
if (mLabel == null) {
73+
return;
74+
}
6575
AppLog.d(T.PROFILING, mLabel + ": begin");
6676
final long first = mSplits.get(0);
6777
long now = first;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ public static String capitalize(final String str) {
180180
return new StringBuilder(strLen).append(Character.toTitleCase(firstChar)).append(str.substring(1)).toString();
181181
}
182182

183+
public static String removeTrailingSlash(final String str) {
184+
if (TextUtils.isEmpty(str) || !str.endsWith("/")) {
185+
return str;
186+
}
187+
188+
return str.substring(0, str.length() -1);
189+
}
190+
183191
/*
184192
* Wrap an image URL in a photon URL
185193
* Check out http://developer.wordpress.com/docs/photon/

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

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
import java.io.UnsupportedEncodingException;
1111
import java.net.IDN;
1212
import java.net.URI;
13-
import java.net.URL;
1413
import java.net.URLDecoder;
1514
import java.net.URLEncoder;
1615
import java.nio.charset.Charset;
16+
import java.util.HashMap;
17+
import java.util.Map;
1718

1819
public class UrlUtils {
1920
public static String urlEncode(final String text) {
@@ -137,18 +138,33 @@ public static String normalizeUrl(final String urlString) {
137138
}
138139
}
139140

141+
142+
/**
143+
* returns the passed url without the scheme
144+
*/
145+
public static String removeScheme(final String urlString) {
146+
if (urlString == null) {
147+
return null;
148+
}
149+
150+
int doubleslash = urlString.indexOf("//");
151+
if (doubleslash == -1) {
152+
doubleslash = 0;
153+
} else {
154+
doubleslash += 2;
155+
}
156+
157+
return urlString.substring(doubleslash, urlString.length());
158+
}
159+
140160
/**
141161
* returns the passed url without the query parameters
142162
*/
143163
public static String removeQuery(final String urlString) {
144164
if (urlString == null) {
145165
return null;
146166
}
147-
int pos = urlString.indexOf("?");
148-
if (pos == -1) {
149-
return urlString;
150-
}
151-
return urlString.substring(0, pos);
167+
return Uri.parse(urlString).buildUpon().clearQuery().toString();
152168
}
153169

154170
/**
@@ -214,4 +230,18 @@ public static boolean isImageUrl(String url) {
214230
return cleanedUrl.endsWith("jpg") || cleanedUrl.endsWith("jpeg") ||
215231
cleanedUrl.endsWith("gif") || cleanedUrl.endsWith("png");
216232
}
233+
234+
public static String appendUrlParameter(String url, String paramName, String paramValue) {
235+
Map<String, String> parameters = new HashMap<>();
236+
parameters.put(paramName, paramValue);
237+
return appendUrlParameters(url, parameters);
238+
}
239+
240+
public static String appendUrlParameters(String url, Map<String, String> parameters) {
241+
Uri.Builder uriBuilder = Uri.parse(url).buildUpon();
242+
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
243+
uriBuilder.appendQueryParameter(parameter.getKey(), parameter.getValue());
244+
}
245+
return uriBuilder.build().toString();
246+
}
217247
}

0 commit comments

Comments
 (0)