Skip to content

Commit ee9a14b

Browse files
committed
Merge remote-tracking branch 'origin/master' into develop
2 parents 090e027 + 480fcb3 commit ee9a14b

20 files changed

+357
-94
lines changed

WordPressUtils/build.gradle

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
buildscript {
22
repositories {
3-
mavenCentral()
3+
jcenter()
44
}
55
dependencies {
6-
classpath 'com.android.tools.build:gradle:1.0.0'
6+
classpath 'com.android.tools.build:gradle:1.5.0'
77
}
88
}
99

@@ -12,25 +12,29 @@ apply plugin: 'maven'
1212
apply plugin: 'signing'
1313

1414
repositories {
15-
mavenCentral()
15+
jcenter()
1616
}
1717

1818
dependencies {
19-
compile 'commons-lang:commons-lang:2.6'
20-
compile 'com.mcxiaoke.volley:library:1.0.10'
21-
compile 'com.android.support:support-v13:21.0.3'
19+
compile('commons-lang:commons-lang:2.6') {
20+
exclude group: 'commons-logging'
21+
}
22+
compile 'com.mcxiaoke.volley:library:1.0.18'
23+
compile 'com.android.support:support-v13:23.1.1'
2224
}
2325

2426
android {
27+
useLibrary 'org.apache.http.legacy'
28+
2529
publishNonDefault true
2630

27-
compileSdkVersion 19
28-
buildToolsVersion "21.1.1"
31+
compileSdkVersion 23
32+
buildToolsVersion "23.0.2"
2933

3034
defaultConfig {
3135
versionName "1.6.0"
3236
minSdkVersion 14
33-
targetSdkVersion 19
37+
targetSdkVersion 23
3438
}
3539
}
3640

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
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="org.wordpress.android.util">
4-
4+
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
55
</manifest>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class AppLog {
1717
// T for Tag
1818
public enum T {READER, EDITOR, MEDIA, NUX, API, STATS, UTILS, NOTIFS, DB, POSTS, COMMENTS, THEMES, TESTS, PROFILING,
19-
SIMPERIUM, SUGGESTION}
19+
SIMPERIUM, SUGGESTION, MAIN}
2020
public static final String TAG = "WordPress";
2121
public static final int HEADER_LINE_COUNT = 2;
2222

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/DisplayUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public static int getDisplayPixelHeight(Context context) {
3838
return (size.y);
3939
}
4040

41+
public static float spToPx(Context context, float sp){
42+
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
43+
final float scale = displayMetrics.scaledDensity;
44+
return sp * scale;
45+
}
46+
4147
public static int dpToPx(Context context, int dp) {
4248
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
4349
context.getResources().getDisplayMetrics());

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.text.TextUtils;
55
import android.view.inputmethod.InputMethodManager;
66
import android.widget.EditText;
7+
import android.widget.TextView;
78

89
/**
910
* EditText utils
@@ -14,13 +15,10 @@ private EditTextUtils() {
1415
}
1516

1617
/**
17-
* returns text string from passed EditText
18+
* returns non-null text string from passed TextView
1819
*/
19-
public static String getText(EditText edit) {
20-
if (edit.getText() == null) {
21-
return "";
22-
}
23-
return edit.getText().toString();
20+
public static String getText(TextView textView) {
21+
return (textView != null) ? textView.getText().toString() : "";
2422
}
2523

2624
/**

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

Lines changed: 25 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) {
@@ -37,6 +44,8 @@ public static <U> U queryJSON(JSONObject source, String query, U defaultObject)
3744
if (result.getClass().isAssignableFrom(defaultObject.getClass())) {
3845
return (U) result;
3946
} else {
47+
AppLog.w(T.UTILS, String.format("The returned object type %s is not assignable to the type %s. Using default!",
48+
result.getClass(),defaultObject.getClass()));
4049
return defaultObject;
4150
}
4251
} catch (java.lang.ClassCastException e) {
@@ -56,9 +65,6 @@ public static <U> U queryJSON(JSONObject source, String query, U defaultObject)
5665
String nextQuery = query.substring(endQuery);
5766
String key = query.substring(0, endQuery);
5867
try {
59-
if (source == null) {
60-
return defaultObject;
61-
}
6268
if (nextQuery.indexOf(QUERY_SEPERATOR) == 0) {
6369
return queryJSON(source.getJSONObject(key), nextQuery.substring(1), defaultObject);
6470
} else if (nextQuery.indexOf(QUERY_ARRAY_INDEX_START) == 0) {
@@ -89,7 +95,15 @@ public static <U> U queryJSON(JSONObject source, String query, U defaultObject)
8995
* Acceptable indexes include negative numbers to reference items from the end of
9096
* the list as well as "last" and "first" as more explicit references to "0" and "-1"
9197
*/
92-
public static <U> U queryJSON(JSONArray source, String query, U defaultObject){
98+
public static <U> U queryJSON(JSONArray source, String query, U defaultObject) {
99+
if (source == null) {
100+
AppLog.e(T.UTILS, "Parameter source is null, can't query a null object");
101+
return defaultObject;
102+
}
103+
if (query == null) {
104+
AppLog.e(T.UTILS, "Parameter query is null");
105+
return defaultObject;
106+
}
93107
// query must start with [ have an index and then have ]
94108
int indexStart = query.indexOf(QUERY_ARRAY_INDEX_START);
95109
int indexEnd = query.indexOf(QUERY_ARRAY_INDEX_END);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static boolean isVideo(String url) {
6868
}
6969
return url.endsWith(".ogv") || url.endsWith(".mp4") || url.endsWith(".m4v") || url.endsWith(".mov") ||
7070
url.endsWith(".wmv") || url.endsWith(".avi") || url.endsWith(".mpg") || url.endsWith(".3gp") ||
71-
url.endsWith(".3g2");
71+
url.endsWith(".3g2") || url.contains("video");
7272
}
7373

7474
public static boolean isAudio(String url) {

0 commit comments

Comments
 (0)