Skip to content

Commit d5de3a0

Browse files
committed
split MediaUtils in MediaUtils and WordPressMediaUtils - remove references to WordPress.java in MediaUtils
1 parent 374a7c3 commit d5de3a0

File tree

1 file changed

+301
-0
lines changed

1 file changed

+301
-0
lines changed
Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
package org.wordpress.android.util;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import android.content.CursorLoader;
6+
import android.database.Cursor;
7+
import android.graphics.BitmapFactory;
8+
import android.net.Uri;
9+
import android.provider.MediaStore;
10+
import android.text.TextUtils;
11+
import android.webkit.MimeTypeMap;
12+
13+
import org.wordpress.android.util.AppLog.T;
14+
import org.wordpress.android.util.helpers.MediaFile;
15+
import org.wordpress.android.util.helpers.WPImageSpan;
16+
17+
import java.io.DataInputStream;
18+
import java.io.File;
19+
import java.io.FileInputStream;
20+
import java.io.FileNotFoundException;
21+
import java.io.FileOutputStream;
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.io.OutputStream;
25+
import java.net.MalformedURLException;
26+
import java.net.URL;
27+
import java.net.URLConnection;
28+
import java.text.SimpleDateFormat;
29+
import java.util.Date;
30+
import java.util.Locale;
31+
import java.util.TimeZone;
32+
33+
public class MediaUtils {
34+
public static boolean isValidImage(String url) {
35+
if (url == null) {
36+
return false;
37+
}
38+
39+
return url.endsWith(".png") || url.endsWith(".jpg") || url.endsWith(".jpeg") || url.endsWith(".gif");
40+
}
41+
42+
public static boolean isDocument(String url) {
43+
if (url == null) {
44+
return false;
45+
}
46+
47+
return url.endsWith(".doc") || url.endsWith(".docx") || url.endsWith(".odt") || url.endsWith(".pdf");
48+
}
49+
50+
public static boolean isPowerpoint(String url) {
51+
if (url == null) {
52+
return false;
53+
}
54+
55+
return url.endsWith(".ppt") || url.endsWith(".pptx") || url.endsWith(".pps") || url.endsWith(".ppsx") ||
56+
url.endsWith(".key");
57+
}
58+
59+
public static boolean isSpreadsheet(String url) {
60+
if (url == null) {
61+
return false;
62+
}
63+
64+
return url.endsWith(".xls") || url.endsWith(".xlsx");
65+
}
66+
67+
public static boolean isVideo(String url) {
68+
if (url == null) {
69+
return false;
70+
}
71+
return url.endsWith(".ogv") || url.endsWith(".mp4") || url.endsWith(".m4v") || url.endsWith(".mov") ||
72+
url.endsWith(".wmv") || url.endsWith(".avi") || url.endsWith(".mpg") || url.endsWith(".3gp") ||
73+
url.endsWith(".3g2");
74+
}
75+
76+
/**
77+
* E.g. Jul 2, 2013 @ 21:57
78+
*/
79+
public static String getDate(long ms) {
80+
Date date = new Date(ms);
81+
SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy '@' HH:mm", Locale.ENGLISH);
82+
83+
// The timezone on the website is at GMT
84+
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
85+
86+
return sdf.format(date);
87+
}
88+
89+
public static boolean isLocalFile(String state) {
90+
if (state == null) {
91+
return false;
92+
}
93+
94+
return (state.equals("queued") || state.equals("uploading") || state.equals("retry")
95+
|| state.equals("failed"));
96+
}
97+
98+
public static Uri getLastRecordedVideoUri(Activity activity) {
99+
String[] proj = { MediaStore.Video.Media._ID };
100+
Uri contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
101+
String sortOrder = MediaStore.Video.VideoColumns.DATE_TAKEN + " DESC";
102+
CursorLoader loader = new CursorLoader(activity, contentUri, proj, null, null, sortOrder);
103+
Cursor cursor = loader.loadInBackground();
104+
cursor.moveToFirst();
105+
106+
return Uri.parse(contentUri.toString() + "/" + cursor.getLong(0));
107+
}
108+
109+
// Calculate the minimun width between the blog setting and picture real width
110+
public static int getMinimumImageWidth(Context context, Uri curStream, String imageWidthBlogSettingString) {
111+
int imageWidthBlogSetting = Integer.MAX_VALUE;
112+
113+
if (!imageWidthBlogSettingString.equals("Original Size")) {
114+
try {
115+
imageWidthBlogSetting = Integer.valueOf(imageWidthBlogSettingString);
116+
} catch (NumberFormatException e) {
117+
AppLog.e(T.POSTS, e);
118+
}
119+
}
120+
121+
int[] dimensions = ImageUtils.getImageSize(curStream, context);
122+
int imageWidthPictureSetting = dimensions[0] == 0 ? Integer.MAX_VALUE : dimensions[0];
123+
124+
if (Math.min(imageWidthPictureSetting, imageWidthBlogSetting) == Integer.MAX_VALUE) {
125+
// Default value in case of errors reading the picture size and the blog settings is set to Original size
126+
return 1024;
127+
} else {
128+
return Math.min(imageWidthPictureSetting, imageWidthBlogSetting);
129+
}
130+
}
131+
132+
public static boolean isInMediaStore(Uri mediaUri) {
133+
// Check if the image is externally hosted (Picasa/Google Photos for example)
134+
if (mediaUri != null && mediaUri.toString().startsWith("content://media/")) {
135+
return true;
136+
} else {
137+
return false;
138+
}
139+
}
140+
141+
public static Uri downloadExternalMedia(Context context, Uri imageUri) {
142+
if (context == null || imageUri == null) {
143+
return null;
144+
}
145+
File cacheDir = null;
146+
147+
String mimeType = context.getContentResolver().getType(imageUri);
148+
boolean isVideo = (mimeType != null && mimeType.contains("video"));
149+
150+
// If the device has an SD card
151+
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
152+
String mediaFolder = isVideo ? "video" : "images";
153+
cacheDir = new File(android.os.Environment.getExternalStorageDirectory() + "/WordPress/" + mediaFolder);
154+
} else {
155+
if (context.getApplicationContext() != null) {
156+
cacheDir = context.getApplicationContext().getCacheDir();
157+
}
158+
}
159+
160+
if (cacheDir != null && !cacheDir.exists()) {
161+
cacheDir.mkdirs();
162+
}
163+
try {
164+
InputStream input;
165+
// Download the file
166+
if (imageUri.toString().startsWith("content://")) {
167+
input = context.getContentResolver().openInputStream(imageUri);
168+
if (input == null) {
169+
AppLog.e(T.UTILS, "openInputStream returned null");
170+
return null;
171+
}
172+
} else {
173+
input = new URL(imageUri.toString()).openStream();
174+
}
175+
176+
String fileName = "wp-" + System.currentTimeMillis();
177+
if (isVideo) {
178+
fileName += "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
179+
}
180+
181+
File f = new File(cacheDir, fileName);
182+
183+
OutputStream output = new FileOutputStream(f);
184+
185+
byte data[] = new byte[1024];
186+
int count;
187+
while ((count = input.read(data)) != -1) {
188+
output.write(data, 0, count);
189+
}
190+
191+
output.flush();
192+
output.close();
193+
input.close();
194+
195+
return Uri.fromFile(f);
196+
} catch (FileNotFoundException e) {
197+
AppLog.e(T.UTILS, e);
198+
} catch (MalformedURLException e) {
199+
AppLog.e(T.UTILS, e);
200+
} catch (IOException e) {
201+
AppLog.e(T.UTILS, e);
202+
}
203+
204+
return null;
205+
}
206+
207+
public static String getMimeTypeOfInputStream(InputStream stream) {
208+
BitmapFactory.Options options = new BitmapFactory.Options();
209+
options.inJustDecodeBounds = true;
210+
BitmapFactory.decodeStream(stream, null, options);
211+
return options.outMimeType;
212+
}
213+
214+
public static String getMediaFileMimeType(File mediaFile) {
215+
String originalFileName = mediaFile.getName().toLowerCase();
216+
String mimeType = UrlUtils.getUrlMimeType(originalFileName);
217+
218+
if (TextUtils.isEmpty(mimeType)) {
219+
try {
220+
String filePathForGuessingMime;
221+
if (mediaFile.getPath().contains("://")) {
222+
filePathForGuessingMime = Uri.encode(mediaFile.getPath(), ":/");
223+
} else {
224+
filePathForGuessingMime = "file://"+ Uri.encode(mediaFile.getPath(), "/");
225+
}
226+
URL urlForGuessingMime = new URL(filePathForGuessingMime);
227+
URLConnection uc = urlForGuessingMime.openConnection();
228+
String guessedContentType = uc.getContentType(); //internally calls guessContentTypeFromName(url.getFile()); and guessContentTypeFromStream(is);
229+
// check if returned "content/unknown"
230+
if (!TextUtils.isEmpty(guessedContentType) && !guessedContentType.equals("content/unknown")) {
231+
mimeType = guessedContentType;
232+
}
233+
} catch (MalformedURLException e) {
234+
AppLog.e(AppLog.T.API, "MalformedURLException while trying to guess the content type for the file here " + mediaFile.getPath() + " with URLConnection", e);
235+
}
236+
catch (IOException e) {
237+
AppLog.e(AppLog.T.API, "Error while trying to guess the content type for the file here " + mediaFile.getPath() +" with URLConnection", e);
238+
}
239+
}
240+
241+
// No mimeType yet? Try to decode the image and get the mimeType from there
242+
if (TextUtils.isEmpty(mimeType)) {
243+
try {
244+
DataInputStream inputStream = new DataInputStream(new FileInputStream(mediaFile));
245+
String mimeTypeFromStream = getMimeTypeOfInputStream(inputStream);
246+
if (!TextUtils.isEmpty(mimeTypeFromStream)) {
247+
mimeType = mimeTypeFromStream;
248+
}
249+
inputStream.close();
250+
} catch (FileNotFoundException e) {
251+
AppLog.e(AppLog.T.API, "FileNotFoundException while trying to guess the content type for the file " + mediaFile.getPath(), e);
252+
} catch (IOException e) {
253+
AppLog.e(AppLog.T.API, "IOException while trying to guess the content type for the file " + mediaFile.getPath(), e);
254+
}
255+
}
256+
257+
if (TextUtils.isEmpty(mimeType)) {
258+
mimeType = "";
259+
} else {
260+
if (mimeType.equalsIgnoreCase("video/mp4v-es")) { //Fixes #533. See: http://tools.ietf.org/html/rfc3016
261+
mimeType = "video/mp4";
262+
}
263+
}
264+
265+
return mimeType;
266+
}
267+
268+
public static String getMediaFileName(File mediaFile, String mimeType) {
269+
String originalFileName = mediaFile.getName().toLowerCase();
270+
String extension = MimeTypeMap.getFileExtensionFromUrl(originalFileName);
271+
if (!TextUtils.isEmpty(extension)) //File name already has the extension in it
272+
return originalFileName;
273+
274+
if (!TextUtils.isEmpty(mimeType)) { //try to get the extension from mimeType
275+
String fileExtension = getExtensionForMimeType(mimeType);
276+
if (!TextUtils.isEmpty(fileExtension)) {
277+
originalFileName += "." + fileExtension;
278+
}
279+
} else {
280+
//No mimetype and no extension!!
281+
AppLog.e(AppLog.T.API, "No mimetype and no extension for " + mediaFile.getPath());
282+
}
283+
284+
return originalFileName;
285+
}
286+
287+
public static String getExtensionForMimeType(String mimeType) {
288+
if (TextUtils.isEmpty(mimeType))
289+
return "";
290+
291+
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
292+
String fileExtensionFromMimeType = mimeTypeMap.getExtensionFromMimeType(mimeType);
293+
if (TextUtils.isEmpty(fileExtensionFromMimeType)) {
294+
// We're still without an extension - split the mime type and retrieve it
295+
String[] split = mimeType.split("/");
296+
fileExtensionFromMimeType = split.length > 1 ? split[1] : split[0];
297+
}
298+
299+
return fileExtensionFromMimeType.toLowerCase();
300+
}
301+
}

0 commit comments

Comments
 (0)