Skip to content

Commit 55a65b2

Browse files
committed
move WPImageSpan and MediaFile to WPUtils - also create a WPEditImageSpan class
1 parent 0a14aa6 commit 55a65b2

File tree

2 files changed

+389
-0
lines changed

2 files changed

+389
-0
lines changed
Lines changed: 339 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,339 @@
1+
package org.wordpress.android.util.helpers;
2+
3+
import android.text.TextUtils;
4+
import android.webkit.MimeTypeMap;
5+
6+
import org.wordpress.android.util.MapUtils;
7+
import org.wordpress.android.util.StringUtils;
8+
9+
import java.util.Date;
10+
import java.util.Map;
11+
12+
public class MediaFile {
13+
protected int id;
14+
protected long postID;
15+
protected String filePath = null; //path of the file into disk
16+
protected String fileName = null; //name of the file into the server
17+
protected String title = null;
18+
protected String description = null;
19+
protected String caption = null;
20+
protected int horizontalAlignment; //0 = none, 1 = left, 2 = center, 3 = right
21+
protected boolean verticalAligment = false; //false = bottom, true = top
22+
protected int width = 500, height;
23+
protected String mimeType = "";
24+
protected String videoPressShortCode = null;
25+
protected boolean featured = false;
26+
protected boolean isVideo = false;
27+
protected boolean featuredInPost;
28+
protected String fileURL = null; // url of the file to download
29+
protected String thumbnailURL = null; // url of the thumbnail to download
30+
private String blogId;
31+
private long dateCreatedGmt;
32+
private String uploadState = null;
33+
private String mediaId;
34+
35+
public static String VIDEOPRESS_SHORTCODE_ID = "videopress_shortcode";
36+
37+
public MediaFile(String blogId, Map<?, ?> resultMap, boolean isDotCom) {
38+
setBlogId(blogId);
39+
setMediaId(MapUtils.getMapStr(resultMap, "attachment_id"));
40+
setPostID(MapUtils.getMapLong(resultMap, "parent"));
41+
setTitle(MapUtils.getMapStr(resultMap, "title"));
42+
setCaption(MapUtils.getMapStr(resultMap, "caption"));
43+
setDescription(MapUtils.getMapStr(resultMap, "description"));
44+
setVideoPressShortCode(MapUtils.getMapStr(resultMap, VIDEOPRESS_SHORTCODE_ID));
45+
46+
// get the file name from the link
47+
String link = MapUtils.getMapStr(resultMap, "link");
48+
setFileName(new String(link).replaceAll("^.*/([A-Za-z0-9_-]+)\\.\\w+$", "$1"));
49+
50+
String fileType = new String(link).replaceAll(".*\\.(\\w+)$", "$1").toLowerCase();
51+
String fileMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileType);
52+
setMimeType(fileMimeType);
53+
54+
// make the file urls be https://... so that we can get these images with oauth when the blogs are private
55+
// assume no https for images in self-hosted blogs
56+
String fileUrl = MapUtils.getMapStr(resultMap, "link");
57+
if (isDotCom) {
58+
fileUrl = fileUrl.replace("http:", "https:");
59+
}
60+
setFileURL(fileUrl);
61+
62+
String thumbnailURL = MapUtils.getMapStr(resultMap, "thumbnail");
63+
if (thumbnailURL.startsWith("http")) {
64+
if (isDotCom) {
65+
thumbnailURL = thumbnailURL.replace("http:", "https:");
66+
}
67+
setThumbnailURL(thumbnailURL);
68+
}
69+
70+
Date date = MapUtils.getMapDate(resultMap, "date_created_gmt");
71+
if (date != null) {
72+
setDateCreatedGMT(date.getTime());
73+
}
74+
75+
Object meta = resultMap.get("metadata");
76+
if (meta != null && meta instanceof Map) {
77+
Map<?, ?> metadata = (Map<?, ?>) meta;
78+
setWidth(MapUtils.getMapInt(metadata, "width"));
79+
setHeight(MapUtils.getMapInt(metadata, "height"));
80+
}
81+
}
82+
83+
public MediaFile() {
84+
// default constructor
85+
}
86+
87+
public MediaFile(MediaFile mediaFile) {
88+
this.id = mediaFile.id;
89+
this.postID = mediaFile.postID;
90+
this.filePath = mediaFile.filePath;
91+
this.fileName = mediaFile.fileName;
92+
this.title = mediaFile.title;
93+
this.description = mediaFile.description;
94+
this.caption = mediaFile.caption;
95+
this.horizontalAlignment = mediaFile.horizontalAlignment;
96+
this.verticalAligment = mediaFile.verticalAligment;
97+
this.width = mediaFile.width;
98+
this.height = mediaFile.height;
99+
this.mimeType = mediaFile.mimeType;
100+
this.videoPressShortCode = mediaFile.videoPressShortCode;
101+
this.featured = mediaFile.featured;
102+
this.isVideo = mediaFile.isVideo;
103+
this.featuredInPost = mediaFile.featuredInPost;
104+
this.fileURL = mediaFile.fileURL;
105+
this.thumbnailURL = mediaFile.thumbnailURL;
106+
this.blogId = mediaFile.blogId;
107+
this.dateCreatedGmt = mediaFile.dateCreatedGmt;
108+
this.uploadState = mediaFile.uploadState;
109+
this.mediaId = mediaFile.mediaId;
110+
}
111+
112+
public int getId() {
113+
return id;
114+
}
115+
116+
public void setId(int id) {
117+
this.id = id;
118+
}
119+
120+
public String getMediaId() {
121+
return mediaId;
122+
}
123+
124+
public void setMediaId(String id) {
125+
mediaId = id;
126+
}
127+
128+
public boolean isFeatured() {
129+
return featured;
130+
}
131+
132+
public void setFeatured(boolean featured) {
133+
this.featured = featured;
134+
}
135+
136+
public long getPostID() {
137+
return postID;
138+
}
139+
140+
public void setPostID(long postID) {
141+
this.postID = postID;
142+
}
143+
144+
public String getFilePath() {
145+
return filePath;
146+
}
147+
148+
public void setFilePath(String filePath) {
149+
this.filePath = filePath;
150+
}
151+
152+
public String getTitle() {
153+
return title;
154+
}
155+
156+
public void setTitle(String title) {
157+
this.title = title;
158+
}
159+
160+
public String getCaption() {
161+
return caption;
162+
}
163+
164+
public void setCaption(String caption) {
165+
this.caption = caption;
166+
}
167+
168+
public String getDescription() {
169+
return description;
170+
}
171+
172+
public void setDescription(String description) {
173+
this.description = description;
174+
}
175+
176+
public String getFileURL() {
177+
return fileURL;
178+
}
179+
180+
public void setFileURL(String fileURL) {
181+
this.fileURL = fileURL;
182+
}
183+
184+
public String getThumbnailURL() {
185+
return thumbnailURL;
186+
}
187+
188+
public void setThumbnailURL(String thumbnailURL) {
189+
this.thumbnailURL = thumbnailURL;
190+
}
191+
192+
public boolean isVerticalAlignmentOnTop() {
193+
return verticalAligment;
194+
}
195+
196+
public void setVerticalAlignmentOnTop(boolean verticalAligment) {
197+
this.verticalAligment = verticalAligment;
198+
}
199+
200+
public int getWidth() {
201+
return width;
202+
}
203+
204+
public void setWidth(int width) {
205+
this.width = width;
206+
}
207+
208+
public int getHeight() {
209+
return height;
210+
}
211+
212+
public void setHeight(int height) {
213+
this.height = height;
214+
}
215+
216+
public String getFileName() {
217+
return fileName;
218+
}
219+
220+
public void setFileName(String fileName) {
221+
this.fileName = fileName;
222+
}
223+
224+
public String getMimeType() {
225+
return StringUtils.notNullStr(mimeType);
226+
}
227+
228+
public void setMimeType(String type) {
229+
mimeType = StringUtils.notNullStr(type);
230+
}
231+
232+
public String getVideoPressShortCode() {
233+
return videoPressShortCode;
234+
}
235+
236+
public void setVideoPressShortCode(String videoPressShortCode) {
237+
this.videoPressShortCode = videoPressShortCode;
238+
}
239+
240+
public int getHorizontalAlignment() {
241+
return horizontalAlignment;
242+
}
243+
244+
public void setHorizontalAlignment(int horizontalAlignment) {
245+
this.horizontalAlignment = horizontalAlignment;
246+
}
247+
248+
public boolean isVideo() {
249+
return isVideo;
250+
}
251+
252+
public void setVideo(boolean isVideo) {
253+
this.isVideo = isVideo;
254+
}
255+
256+
public boolean isFeaturedInPost() {
257+
return featuredInPost;
258+
}
259+
260+
public void setFeaturedInPost(boolean featuredInPost) {
261+
this.featuredInPost = featuredInPost;
262+
}
263+
264+
public String getBlogId() {
265+
return blogId;
266+
}
267+
268+
public void setBlogId(String blogId) {
269+
this.blogId = blogId;
270+
}
271+
272+
public void setDateCreatedGMT(long date_created_gmt) {
273+
this.dateCreatedGmt = date_created_gmt;
274+
}
275+
276+
public long getDateCreatedGMT() {
277+
return dateCreatedGmt;
278+
}
279+
280+
public void setUploadState(String uploadState) {
281+
this.uploadState = uploadState;
282+
}
283+
284+
public String getUploadState() {
285+
return uploadState;
286+
}
287+
288+
/**
289+
* Outputs the Html for an image
290+
* If a fullSizeUrl exists, a link will be created to it from the resizedPictureUrl
291+
*/
292+
public String getImageHtmlForUrls(String fullSizeUrl, String resizedPictureURL, boolean shouldAddImageWidthCSS) {
293+
String alignment = "";
294+
switch (getHorizontalAlignment()) {
295+
case 0:
296+
alignment = "alignnone";
297+
break;
298+
case 1:
299+
alignment = "alignleft";
300+
break;
301+
case 2:
302+
alignment = "aligncenter";
303+
break;
304+
case 3:
305+
alignment = "alignright";
306+
break;
307+
}
308+
309+
String alignmentCSS = "class=\"" + alignment + " size-full\" ";
310+
311+
if (shouldAddImageWidthCSS) {
312+
alignmentCSS += "style=\"max-width: " + getWidth() + "px\" ";
313+
}
314+
315+
// Check if we uploaded a featured picture that is not added to the Post content (normal case)
316+
if ((fullSizeUrl != null && fullSizeUrl.equalsIgnoreCase("")) ||
317+
(resizedPictureURL != null && resizedPictureURL.equalsIgnoreCase(""))) {
318+
return ""; // Not featured in Post. Do not add to the content.
319+
}
320+
321+
if (fullSizeUrl == null && resizedPictureURL != null) {
322+
fullSizeUrl = resizedPictureURL;
323+
} else if (fullSizeUrl != null && resizedPictureURL == null) {
324+
resizedPictureURL = fullSizeUrl;
325+
}
326+
327+
String mediaTitle = StringUtils.notNullStr(getTitle());
328+
329+
String content = String.format("<a href=\"%s\"><img title=\"%s\" %s alt=\"image\" src=\"%s\" /></a>",
330+
fullSizeUrl, mediaTitle, alignmentCSS, resizedPictureURL);
331+
332+
if (!TextUtils.isEmpty(getCaption())) {
333+
content = String.format("[caption id=\"\" align=\"%s\" width=\"%d\" caption=\"%s\"]%s[/caption]",
334+
alignment, getWidth(), TextUtils.htmlEncode(getCaption()), content);
335+
}
336+
337+
return content;
338+
}
339+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//Add WordPress image fields to ImageSpan object
2+
3+
package org.wordpress.android.util.helpers;
4+
5+
import android.content.Context;
6+
import android.graphics.Bitmap;
7+
import android.net.Uri;
8+
import android.text.style.ImageSpan;
9+
10+
public class WPImageSpan extends ImageSpan {
11+
protected Uri mImageSource = null;
12+
protected boolean mNetworkImageLoaded = false;
13+
protected MediaFile mMediaFile;
14+
15+
public WPImageSpan(Context context, Bitmap b, Uri src) {
16+
super(context, b);
17+
this.mImageSource = src;
18+
mMediaFile = new MediaFile();
19+
}
20+
21+
public WPImageSpan(Context context, int resId, Uri src) {
22+
super(context, resId);
23+
this.mImageSource = src;
24+
mMediaFile = new MediaFile();
25+
}
26+
27+
public MediaFile getMediaFile() {
28+
return mMediaFile;
29+
}
30+
31+
public void setMediaFile(MediaFile mMediaFile) {
32+
this.mMediaFile = mMediaFile;
33+
}
34+
35+
public void setImageSource(Uri mImageSource) {
36+
this.mImageSource = mImageSource;
37+
}
38+
39+
public Uri getImageSource() {
40+
return mImageSource;
41+
}
42+
43+
public boolean isNetworkImageLoaded() {
44+
return mNetworkImageLoaded;
45+
}
46+
47+
public void setNetworkImageLoaded(boolean networkImageLoaded) {
48+
this.mNetworkImageLoaded = networkImageLoaded;
49+
}
50+
}

0 commit comments

Comments
 (0)