Skip to content

Commit 1786579

Browse files
committed
Merge pull request #256 from wordpress-mobile/issue-253
Fix for #253
2 parents 904d0d7 + 25d3789 commit 1786579

File tree

4 files changed

+97
-49
lines changed

4 files changed

+97
-49
lines changed

src/org/wordpress/android/models/MediaFile.java

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.webkit.MimeTypeMap;
77

88
import org.wordpress.android.WordPress;
9+
import org.wordpress.android.util.MapUtils;
910

1011
public class MediaFile {
1112

@@ -31,54 +32,19 @@ public class MediaFile {
3132
private String uploadState = null;
3233
private String mediaId;
3334

34-
/*
35-
* wrappers for extracting values from the resultMap passed to the MediaFile constructor
36-
* getMapStr() is guaranteed to return "" if value doesn't exist (never returns null)
37-
* getMapInt() & getMapLong() are guaranteed to return 0 if value doesn't exist or isn't a number
38-
* getMapDate() DOES return null if value doesn't exist or isn't a date
39-
*/
40-
private static String getMapStr(final Map<?, ?> map, final String key) {
41-
if (map==null || key==null || !map.containsKey(key))
42-
return "";
43-
return map.get(key).toString();
44-
}
45-
private static int getMapInt(final Map<?, ?> map, final String key) {
46-
try {
47-
return Integer.parseInt(getMapStr(map, key));
48-
} catch (NumberFormatException e) {
49-
return 0;
50-
}
51-
}
52-
private static long getMapLong(final Map<?, ?> map, final String key) {
53-
try {
54-
return Long.parseLong(getMapStr(map, key));
55-
} catch (NumberFormatException e) {
56-
return 0;
57-
}
58-
}
59-
private static Date getMapDate(final Map<?, ?> map, final String key) {
60-
if (map==null || key==null || !map.containsKey(key))
61-
return null;
62-
try {
63-
return (Date) map.get(key);
64-
} catch (ClassCastException e) {
65-
return null;
66-
}
67-
}
68-
6935
public MediaFile(String blogId, Map<?, ?> resultMap) {
7036

7137
boolean isDotCom = (WordPress.getCurrentBlog() != null && WordPress.getCurrentBlog().isDotcomFlag());
7238

7339
setBlogId(blogId);
74-
setMediaId(getMapStr(resultMap, "attachment_id"));
75-
setPostID(getMapLong(resultMap, "parent"));
76-
setTitle(getMapStr(resultMap, "title"));
77-
setCaption(getMapStr(resultMap, "caption"));
78-
setDescription(getMapStr(resultMap, "description"));
40+
setMediaId(MapUtils.getMapStr(resultMap, "attachment_id"));
41+
setPostID(MapUtils.getMapLong(resultMap, "parent"));
42+
setTitle(MapUtils.getMapStr(resultMap, "title"));
43+
setCaption(MapUtils.getMapStr(resultMap, "caption"));
44+
setDescription(MapUtils.getMapStr(resultMap, "description"));
7945

8046
// get the file name from the link
81-
String link = getMapStr(resultMap, "link");
47+
String link = MapUtils.getMapStr(resultMap, "link");
8248
setFileName(new String(link).replaceAll("^.*/([A-Za-z0-9_-]+)\\.\\w+$", "$1"));
8349

8450
String fileType = new String(link).replaceAll(".*\\.(\\w+)$", "$1").toLowerCase();
@@ -87,27 +53,27 @@ public MediaFile(String blogId, Map<?, ?> resultMap) {
8753

8854
// make the file urls be https://... so that we can get these images with oauth when the blogs are private
8955
// assume no https for images in self-hosted blogs
90-
String fileUrl = getMapStr(resultMap, "link");
56+
String fileUrl = MapUtils.getMapStr(resultMap, "link");
9157
if (isDotCom)
9258
fileUrl = fileUrl.replace("http:", "https:");
9359
setFileURL(fileUrl);
9460

95-
String thumbnailURL = getMapStr(resultMap, "thumbnail");
61+
String thumbnailURL = MapUtils.getMapStr(resultMap, "thumbnail");
9662
if (thumbnailURL.startsWith("http")) {
9763
if (isDotCom)
9864
thumbnailURL = thumbnailURL.replace("http:", "https:");
9965
setThumbnailURL(thumbnailURL);
10066
}
10167

102-
Date date = getMapDate(resultMap, "date_created_gmt");
68+
Date date = MapUtils.getMapDate(resultMap, "date_created_gmt");
10369
if (date != null)
10470
setDateCreatedGMT(date.getTime());
10571

10672
Object meta = resultMap.get("metadata");
10773
if(meta != null && meta instanceof Map) {
10874
Map<?, ?> metadata = (Map<?, ?>) meta;
109-
setWidth(getMapInt(metadata, "width"));
110-
setHeight(getMapInt(metadata, "height"));
75+
setWidth(MapUtils.getMapInt(metadata, "width"));
76+
setHeight(MapUtils.getMapInt(metadata, "height"));
11177
}
11278
}
11379

src/org/wordpress/android/ui/prefs/PreferencesActivity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.google.gson.JsonSyntaxException;
3939
import com.google.gson.internal.StringMap;
4040

41+
import org.wordpress.android.util.MapUtils;
4142
import org.wordpress.android.util.StringUtils;
4243
import org.xmlrpc.android.WPComXMLRPCApi;
4344
import org.xmlrpc.android.XMLRPCCallback;
@@ -474,7 +475,7 @@ private void loadNotifications() {
474475
.get(mTypeList[i].toString());
475476
CheckBoxPreference typePreference = new CheckBoxPreference(this);
476477
typePreference.setKey(mTypeList[i].toString());
477-
typePreference.setChecked(typeMap.get("value").toString().equals("1"));
478+
typePreference.setChecked(MapUtils.getMapBool(typeMap, "value"));
478479
typePreference.setTitle(typeMap.get("desc").toString());
479480
typePreference.setOnPreferenceChangeListener(mTypeChangeListener);
480481
notificationTypesCategory.addPreference(typePreference);
@@ -488,7 +489,7 @@ private void loadNotifications() {
488489
if (blogName == null || blogName.trim().equals(""))
489490
blogName = (String) blogMap.get("url");
490491
CheckBoxPreference blogPreference = new CheckBoxPreference(this);
491-
blogPreference.setChecked(!blogMap.get("value").toString().equals("1"));
492+
blogPreference.setChecked(!MapUtils.getMapBool(blogMap, "value"));
492493
blogPreference.setTitle(StringUtils.unescapeHTML(blogName));
493494
blogPreference.setOnPreferenceChangeListener(mMuteBlogChangeListener);
494495
selectBlogsCategory.addPreference(blogPreference);
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.wordpress.android.util;
2+
3+
import java.util.Date;
4+
import java.util.Map;
5+
6+
/**
7+
* Created by nbradbury on 11/4/13.
8+
* wrappers for extracting values from a Map object
9+
*/
10+
public class MapUtils {
11+
12+
/*
13+
* returns a String value for the passed key in the passed map
14+
* always returns "" instead of null
15+
*/
16+
public static String getMapStr(final Map<?, ?> map, final String key) {
17+
if (map==null || key==null || !map.containsKey(key))
18+
return "";
19+
return map.get(key).toString();
20+
}
21+
22+
/*
23+
* returns an int value for the passed key in the passed map
24+
* defaultValue is returned if key doesn't exist or isn't a number
25+
*/
26+
public static int getMapInt(final Map<?, ?> map, final String key) {
27+
return getMapInt(map, key, 0);
28+
}
29+
public static int getMapInt(final Map<?, ?> map, final String key, int defaultValue) {
30+
try {
31+
return Integer.parseInt(getMapStr(map, key));
32+
} catch (NumberFormatException e) {
33+
return defaultValue;
34+
}
35+
}
36+
37+
/*
38+
* long version of above
39+
*/
40+
public static long getMapLong(final Map<?, ?> map, final String key) {
41+
return getMapLong(map, key, 0);
42+
}
43+
public static long getMapLong(final Map<?, ?> map, final String key, long defaultValue) {
44+
try {
45+
return Long.parseLong(getMapStr(map, key));
46+
} catch (NumberFormatException e) {
47+
return defaultValue;
48+
}
49+
}
50+
51+
/*
52+
* returns a date object from the passed key in the passed map
53+
* returns null if key doesn't exist or isn't a date
54+
*/
55+
public static Date getMapDate(final Map<?, ?> map, final String key) {
56+
if (map==null || key==null || !map.containsKey(key))
57+
return null;
58+
try {
59+
return (Date) map.get(key);
60+
} catch (ClassCastException e) {
61+
return null;
62+
}
63+
}
64+
65+
/*
66+
* returns a boolean value from the passed key in the passed map
67+
* returns true unless key doesn't exist, or the value is "0" or "false"
68+
*/
69+
public static boolean getMapBool(final Map<?, ?> map, final String key) {
70+
String value = getMapStr(map, key);
71+
if (value.isEmpty())
72+
return false;
73+
if (value.startsWith("0")) // handles "0" and "0.0"
74+
return false;
75+
if (value.equalsIgnoreCase("false"))
76+
return false;
77+
// all other values are assume to be true
78+
return true;
79+
}
80+
}

src/org/xmlrpc/android/WPComXMLRPCApi.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.wordpress.android.WordPress;
2222
import org.wordpress.android.WordPressDB;
2323
import org.wordpress.android.util.DeviceUtils;
24+
import org.wordpress.android.util.MapUtils;
2425

2526
/**
2627
* WordPress.com specific XML-RPC API calls
@@ -173,7 +174,7 @@ public void setNotificationSettings(Context context) {
173174
ArrayList<StringMap<Double>> mutedBlogsList = new ArrayList<StringMap<Double>>();
174175
for (int i = 0; i < blogsList.size(); i++) {
175176
StringMap<Double> userBlog = blogsList.get(i);
176-
if (Double.valueOf(userBlog.get("value")) == 1.0) {
177+
if (MapUtils.getMapBool(userBlog, "value")) {
177178
mutedBlogsList.add(userBlog);
178179
}
179180
}

0 commit comments

Comments
 (0)