diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java index 82c6f7cbc4c0..48e3eba365d9 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java @@ -102,7 +102,6 @@ import org.wordpress.android.util.helpers.MediaGallery; import org.wordpress.android.util.helpers.MediaGalleryImageSpan; import org.wordpress.android.util.helpers.WPImageSpan; -import org.wordpress.android.widgets.SuggestionAutoCompleteText; import org.wordpress.android.widgets.WPViewPager; import org.wordpress.mediapicker.MediaItem; import org.wordpress.mediapicker.source.MediaSource; @@ -1560,7 +1559,7 @@ private boolean addMediaVisualEditor(Uri imageUri) { } Blog blog = WordPress.getCurrentBlog(); - if (!blog.getMaxImageWidth().equals("Original Size")) { + if (MediaUtils.getImageWidthSettingFromString(blog.getMaxImageWidth()) != Integer.MAX_VALUE) { // If the user has selected a maximum image width for uploads, rescale the image accordingly path = ImageUtils.createResizedImageWithMaxWidth(this, path, Integer.parseInt(blog.getMaxImageWidth())); } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/services/PostUploadService.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/services/PostUploadService.java index 224db4b81540..9b62c84b2bc7 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/services/PostUploadService.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/services/PostUploadService.java @@ -565,8 +565,9 @@ private String uploadImage(MediaFile mediaFile) { // We won't resize gif images to keep them awesome. boolean shouldUploadResizedVersion = false; // If it's not a gif and blog don't keep original size, there is a chance we need to resize - if (!mimeType.equals("image/gif") && !mBlog.getMaxImageWidth().equals("Original Size")) { - //check the picture settings + if (!mimeType.equals("image/gif") && MediaUtils.getImageWidthSettingFromString(mBlog.getMaxImageWidth()) + != Integer.MAX_VALUE) { + // check the picture settings int pictureSettingWidth = mediaFile.getWidth(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; @@ -696,7 +697,7 @@ private String uploadVideo(MediaFile mediaFile) { } } else { // set the width of the video to the thumbnail width, else 640x480 - if (!mBlog.getMaxImageWidth().equals("Original Size")) { + if (MediaUtils.getImageWidthSettingFromString(mBlog.getMaxImageWidth()) != Integer.MAX_VALUE) { xRes = mBlog.getMaxImageWidth(); yRes = String.valueOf(Math.round(Integer.valueOf(mBlog.getMaxImageWidth()) * 0.75)); } else { diff --git a/WordPress/src/main/java/org/wordpress/android/util/WPHtml.java b/WordPress/src/main/java/org/wordpress/android/util/WPHtml.java index 9d31ece99d0c..337de4789e9e 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/WPHtml.java +++ b/WordPress/src/main/java/org/wordpress/android/util/WPHtml.java @@ -69,6 +69,7 @@ import java.io.IOException; import java.io.StringReader; import java.util.HashMap; +import java.util.Locale; /** * This class processes HTML strings into displayable styled text. Not all HTML @@ -449,22 +450,20 @@ public static String getContent(WPImageSpan imageSpan) { String localBlogID = imageSpan.getMediaFile().getBlogId(); Blog currentBlog = WordPress.wpDB.instantiateBlogByLocalId(Integer.parseInt(localBlogID)); // If it's not a gif and blog don't keep original size, there is a chance we need to resize - if (currentBlog != null && !mediaFile.getMimeType().equals("image/gif") && - !currentBlog.getMaxImageWidth().equals("Original Size")) { - int maxImageWidth = Integer.parseInt(currentBlog.getMaxImageWidth()); - // use the correct resize settings. - width = Math.min(width, maxImageWidth); + if (currentBlog != null && !mediaFile.getMimeType().equals("image/gif") + && MediaUtils.getImageWidthSettingFromString(currentBlog.getMaxImageWidth()) != Integer.MAX_VALUE) { + width = MediaUtils.getMaximumImageWidth(width, currentBlog.getMaxImageWidth()); // Use inline CSS on self-hosted blogs to enforce picture resize settings if (!currentBlog.isDotcomFlag()) { - inlineCSS = String.format(" style=\"width:%dpx;max-width:%dpx;\" ", width, width); + inlineCSS = String.format(Locale.US, " style=\"width:%dpx;max-width:%dpx;\" ", width, width); } } - content = content + ""; if (!caption.equals("")) { - content = String.format("[caption id=\"\" align=\"%s\" width=\"%d\" caption=\"%s\"]%s[/caption]", + content = String.format(Locale.US, + "[caption id=\"\" align=\"%s\" width=\"%d\" caption=\"%s\"]%s[/caption]", alignment, width, TextUtils.htmlEncode(caption), content); } } diff --git a/libs/analytics/WordPressAnalytics/build.gradle b/libs/analytics/WordPressAnalytics/build.gradle index 91f3dac4a8c0..356cf6f80cc1 100644 --- a/libs/analytics/WordPressAnalytics/build.gradle +++ b/libs/analytics/WordPressAnalytics/build.gradle @@ -18,7 +18,7 @@ repositories { dependencies { compile 'com.automattic:tracks:1.1.0' compile 'com.mixpanel.android:mixpanel-android:4.6.4' - compile 'org.wordpress:utils:1.9.0' + compile 'org.wordpress:utils:1.10.0' } android { diff --git a/libs/editor/WordPressEditor/build.gradle b/libs/editor/WordPressEditor/build.gradle index 9d362ce28be5..73fa065287cb 100644 --- a/libs/editor/WordPressEditor/build.gradle +++ b/libs/editor/WordPressEditor/build.gradle @@ -48,7 +48,7 @@ dependencies { compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.android.support:support-v4:23.4.0' compile 'com.android.support:design:23.4.0' - compile 'org.wordpress:utils:1.9.0' + compile 'org.wordpress:utils:1.10.0' // Test libraries testCompile 'junit:junit:4.11' diff --git a/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/ImageSettingsDialogFragment.java b/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/ImageSettingsDialogFragment.java index ff8cfa7cf60d..b46200a0e909 100644 --- a/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/ImageSettingsDialogFragment.java +++ b/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/ImageSettingsDialogFragment.java @@ -28,6 +28,7 @@ import org.json.JSONException; import org.json.JSONObject; import org.wordpress.android.util.AppLog; +import org.wordpress.android.util.MediaUtils; import org.wordpress.android.util.ToastUtils; import java.util.Arrays; @@ -41,12 +42,9 @@ * when the fragment is dismissed to restore it. */ public class ImageSettingsDialogFragment extends DialogFragment { - public static final int IMAGE_SETTINGS_DIALOG_REQUEST_CODE = 5; public static final String IMAGE_SETTINGS_DIALOG_TAG = "image-settings"; - private static final int DEFAULT_MAX_IMAGE_WIDTH = 1024; - private JSONObject mImageMeta; private int mMaxImageWidth; @@ -170,7 +168,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa mLinkTo.setText(mImageMeta.getString("linkUrl")); - mMaxImageWidth = getMaximumImageWidth(mImageMeta.getInt("naturalWidth"), bundle.getString("maxWidth")); + mMaxImageWidth = MediaUtils.getMaximumImageWidth(mImageMeta.getInt("naturalWidth"), + bundle.getString("maxWidth")); setupWidthSeekBar(widthSeekBar, mWidthText, mImageMeta.getInt("width")); @@ -396,34 +395,6 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { }); } - /** - * Calculate and return the maximum allowed image width by comparing the width of the image at its full size with - * the maximum upload width set in the blog settings - * @param naturalImageWidth the image's natural (full) width - * @param imageWidthBlogSettingString the maximum upload width set in the blog settings - * @return - */ - public static int getMaximumImageWidth(int naturalImageWidth, String imageWidthBlogSettingString) { - int imageWidthBlogSetting = Integer.MAX_VALUE; - - if (!imageWidthBlogSettingString.equals("Original Size")) { - try { - imageWidthBlogSetting = Integer.valueOf(imageWidthBlogSettingString); - } catch (NumberFormatException e) { - AppLog.e(AppLog.T.EDITOR, e); - } - } - - int imageWidthPictureSetting = naturalImageWidth == 0 ? Integer.MAX_VALUE : naturalImageWidth; - - if (Math.min(imageWidthPictureSetting, imageWidthBlogSetting) == Integer.MAX_VALUE) { - // Default value in case of errors reading the picture size and the blog settings is set to Original size - return DEFAULT_MAX_IMAGE_WIDTH; - } else { - return Math.min(imageWidthPictureSetting, imageWidthBlogSetting); - } - } - /** * Return the integer value of the width EditText, adjusted to be within the given min and max, and stripped of the * 'px' units diff --git a/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/LegacyEditorFragment.java b/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/LegacyEditorFragment.java index 576dc1e8d296..75febb3fa6c9 100644 --- a/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/LegacyEditorFragment.java +++ b/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/LegacyEditorFragment.java @@ -69,6 +69,8 @@ import org.wordpress.android.util.helpers.WPUnderlineSpan; import org.wordpress.android.util.widgets.WPEditText; +import java.util.Locale; + public class LegacyEditorFragment extends EditorFragmentAbstract implements TextWatcher, WPEditText.OnSelectionChangedListener, View.OnTouchListener { public static final int ACTIVITY_REQUEST_CODE_CREATE_LINK = 4; @@ -483,7 +485,7 @@ private WPEditImageSpan createWPEditImageSpanLocal(Context context, MediaFile me } } WPEditImageSpan imageSpan = new WPEditImageSpan(context, thumbnailBitmap, imageUri); - mediaFile.setWidth(MediaUtils.getMinimumImageWidth(context, imageUri, mBlogSettingMaxImageWidth)); + mediaFile.setWidth(MediaUtils.getMaximumImageWidth(context, imageUri, mBlogSettingMaxImageWidth)); imageSpan.setMediaFile(mediaFile); return imageSpan; } @@ -742,7 +744,6 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); alignmentSpinner.setAdapter(adapter); - imageWidthText.setText(String.valueOf(mediaFile.getWidth()) + "px"); seekBar.setProgress(mediaFile.getWidth()); titleText.setText(mediaFile.getTitle()); caption.setText(mediaFile.getCaption()); @@ -758,9 +759,10 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { alignmentSpinner.setSelection(mediaFile.getHorizontalAlignment(), true); - final int maxWidth = MediaUtils.getMinimumImageWidth(getActivity(), + final int maxWidth = MediaUtils.getMaximumImageWidth(getActivity(), imageSpan.getImageSource(), mBlogSettingMaxImageWidth); seekBar.setMax(maxWidth / 10); + imageWidthText.setText(String.format(Locale.US, "%dpx", maxWidth)); if (mediaFile.getWidth() != 0) { seekBar.setProgress(mediaFile.getWidth() / 10); } @@ -778,7 +780,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (progress == 0) { progress = 1; } - imageWidthText.setText(progress * 10 + "px"); + imageWidthText.setText(String.format(Locale.US, "%dpx", progress * 10)); } }); diff --git a/libs/networking/WordPressNetworking/build.gradle b/libs/networking/WordPressNetworking/build.gradle index 76a09ba62a60..15d8d372b96a 100644 --- a/libs/networking/WordPressNetworking/build.gradle +++ b/libs/networking/WordPressNetworking/build.gradle @@ -29,7 +29,7 @@ android { } dependencies { - compile 'org.wordpress:utils:1.9.0' + compile 'org.wordpress:utils:1.10.0' compile 'com.automattic:rest:1.0.3' } diff --git a/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/MediaUtils.java b/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/MediaUtils.java index 8e2c773339ce..a96dadc7493e 100644 --- a/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/MediaUtils.java +++ b/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/MediaUtils.java @@ -29,6 +29,8 @@ import java.util.TimeZone; public class MediaUtils { + private static final int DEFAULT_MAX_IMAGE_WIDTH = 1024; + public static boolean isValidImage(String url) { if (url == null) { return false; @@ -114,29 +116,52 @@ public static Uri getLastRecordedVideoUri(Activity activity) { return Uri.parse(contentUri.toString() + "/" + cursor.getLong(0)); } - // Calculate the minimun width between the blog setting and picture real width - public static int getMinimumImageWidth(Context context, Uri curStream, String imageWidthBlogSettingString) { - int imageWidthBlogSetting = Integer.MAX_VALUE; - - if (!imageWidthBlogSettingString.equals("Original Size")) { - try { - imageWidthBlogSetting = Integer.valueOf(imageWidthBlogSettingString); - } catch (NumberFormatException e) { - AppLog.e(T.POSTS, e); - } + /** + * Get image width setting from the image width site setting string. This string can be an int, in this case it's + * the maximum image width defined by the site. + * Examples: + * "1000" will return 1000 + * "Original Size" will return Integer.MAX_VALUE + * "Largeur originale" will return Integer.MAX_VALUE + * null will return Integer.MAX_VALUE + * @param imageWidthSiteSettingString Image width site setting string + * @return Integer.MAX_VALUE if image width is not defined or invalid, maximum image width in other cases. + */ + public static int getImageWidthSettingFromString(String imageWidthSiteSettingString) { + if (imageWidthSiteSettingString == null) { + return Integer.MAX_VALUE; + } + try { + return Integer.valueOf(imageWidthSiteSettingString); + } catch (NumberFormatException e) { + return Integer.MAX_VALUE; } + } - int[] dimensions = ImageUtils.getImageSize(curStream, context); - int imageWidthPictureSetting = dimensions[0] == 0 ? Integer.MAX_VALUE : dimensions[0]; + /** + * Calculate and return the maximum allowed image width by comparing the width of the image at its full size with + * the maximum upload width set in the blog settings + * @param imageWidth the image's natural (full) width + * @param imageWidthSiteSettingString the maximum upload width set in the site settings + * @return maximum allowed image width + */ + public static int getMaximumImageWidth(int imageWidth, String imageWidthSiteSettingString) { + int imageWidthBlogSetting = getImageWidthSettingFromString(imageWidthSiteSettingString); + int imageWidthPictureSetting = imageWidth == 0 ? Integer.MAX_VALUE : imageWidth; if (Math.min(imageWidthPictureSetting, imageWidthBlogSetting) == Integer.MAX_VALUE) { - // Default value in case of errors reading the picture size and the blog settings is set to Original size - return 1024; + // Default value in case of errors reading the picture size or the blog settings is set to Original size + return DEFAULT_MAX_IMAGE_WIDTH; } else { return Math.min(imageWidthPictureSetting, imageWidthBlogSetting); } } + public static int getMaximumImageWidth(Context context, Uri curStream, String imageWidthBlogSettingString) { + int[] dimensions = ImageUtils.getImageSize(curStream, context); + return getMaximumImageWidth(dimensions[0], imageWidthBlogSettingString); + } + public static boolean isInMediaStore(Uri mediaUri) { // Check if the image is externally hosted (Picasa/Google Photos for example) if (mediaUri != null && mediaUri.toString().startsWith("content://media/")) {