From afb508d3ee6d9105ea55353ab3f5e71602d3988f Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Wed, 13 Apr 2016 10:50:57 +0200 Subject: [PATCH 1/2] Drop the AsyncTask - HandleMediaSelectionTask, it could cause a lot of troubles in many places --- .../wordpress/android/ui/posts/EditPostActivity.java | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) 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 e197d5c24b29..7018e85a282d 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 @@ -1061,14 +1061,6 @@ protected void onPostExecute(Spanned spanned) { } } - private class HandleMediaSelectionTask extends AsyncTask { - @Override - protected Void doInBackground(Intent... params) { - handleMediaSelectionResult(params[0]); - return null; - } - } - private void fillContentEditorFields() { // Needed blog settings needed by the editor if (WordPress.getCurrentBlog() != null) { @@ -1498,7 +1490,6 @@ private boolean addMediaLegacyEditor(Uri mediaUri, boolean isVideo) { } WordPress.wpDB.saveMediaFile(mediaFile); mEditorFragment.appendMediaFile(mediaFile, mediaFile.getFilePath(), WordPress.imageLoader); - return true; } @@ -1511,8 +1502,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case MediaPickerActivity.ACTIVITY_REQUEST_CODE_MEDIA_SELECTION: if (resultCode == MediaPickerActivity.ACTIVITY_RESULT_CODE_MEDIA_SELECTED) { - new HandleMediaSelectionTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, - data); + handleMediaSelectionResult(data); } else if (resultCode == MediaPickerActivity.ACTIVITY_RESULT_CODE_GALLERY_CREATED) { handleGalleryResult(data); } From 4fb8fc118fc0d61b2597d75b7a31c2f48c94ec76 Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Wed, 13 Apr 2016 10:52:21 +0200 Subject: [PATCH 2/2] Move the addMediaFile logic in an AsyncTask, only WPEditImageSpan construction now lives in a background thread --- .../android/editor/LegacyEditorFragment.java | 124 ++++++++++-------- 1 file changed, 70 insertions(+), 54 deletions(-) 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 e3d142e96b59..246082504cc7 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 @@ -9,9 +9,8 @@ import android.graphics.BitmapFactory; import android.graphics.Typeface; import android.net.Uri; +import android.os.AsyncTask; import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; import android.os.Parcelable; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; @@ -1023,66 +1022,83 @@ public void onSaveInstanceState(Bundle outState) { outState.putString(KEY_CONTENT, mContentEditText.getText().toString()); } - public void addMediaFile(final MediaFile mediaFile, final String imageUrl, final ImageLoader imageLoader, final int start, final int end) { - mediaFile.setFileURL(imageUrl); - mediaFile.setFilePath(imageUrl); - final WPEditImageSpan imageSpan = createWPEditImageSpan(getActivity(), mediaFile); - mEditorFragmentListener.saveMediaFile(mediaFile); - imageSpan.setMediaFile(mediaFile); + private class AddMediaFileTask extends AsyncTask { + private MediaFile mMediaFile; + private String mImageUrl; + private ImageLoader mImageLoader; + private int mStart; + private int mEnd; + + public AddMediaFileTask(MediaFile mediaFile, String imageUrl, ImageLoader imageLoader, int start, int end) { + mMediaFile = mediaFile; + mImageUrl = imageUrl; + mImageLoader = imageLoader; + mStart = start; + mEnd = end; + } - Handler handler = new Handler(Looper.getMainLooper()); - final Runnable r = new Runnable() { - @Override - public void run() { - // Insert the WPImageSpan in the content field - int selectionStart = start; - int selectionEnd = end; - - if (selectionStart > selectionEnd) { - int temp = selectionEnd; - selectionEnd = selectionStart; - selectionStart = temp; - } + protected WPEditImageSpan doInBackground(Void... voids) { + mMediaFile.setFileURL(mImageUrl); + mMediaFile.setFilePath(mImageUrl); + WPEditImageSpan imageSpan = createWPEditImageSpan(getActivity(), mMediaFile); + mEditorFragmentListener.saveMediaFile(mMediaFile); + return imageSpan; + } - imageSpan.setPosition(selectionStart, selectionEnd); + protected void onPostExecute(WPEditImageSpan imageSpan) { + // Insert the WPImageSpan in the content field + int selectionStart = mStart; + int selectionEnd = mEnd; - int line, column = 0; - if (mContentEditText.getLayout() != null) { - line = mContentEditText.getLayout().getLineForOffset(selectionStart); - column = selectionStart - mContentEditText.getLayout().getLineStart(line); - } + if (selectionStart > selectionEnd) { + int temp = selectionEnd; + selectionEnd = selectionStart; + selectionStart = temp; + } - Editable s = mContentEditText.getText(); - if (s == null) { - return; - } + imageSpan.setPosition(selectionStart, selectionEnd); - WPImageSpan[] imageSpans = s.getSpans(selectionStart, selectionEnd, WPImageSpan.class); - if (imageSpans.length != 0) { - // insert a few line breaks if the cursor is already on an image - s.insert(selectionEnd, "\n\n"); - selectionStart = selectionStart + 2; - selectionEnd = selectionEnd + 2; - } else if (column != 0) { - // insert one line break if the cursor is not at the first column - s.insert(selectionEnd, "\n"); - selectionStart = selectionStart + 1; - selectionEnd = selectionEnd + 1; - } + int line, column = 0; + if (mContentEditText.getLayout() != null) { + line = mContentEditText.getLayout().getLineForOffset(selectionStart); + column = selectionStart - mContentEditText.getLayout().getLineStart(line); + } - s.insert(selectionStart, " "); - s.setSpan(imageSpan, selectionStart, selectionEnd + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); - AlignmentSpan.Standard as = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER); - s.setSpan(as, selectionStart, selectionEnd + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); - s.insert(selectionEnd + 1, "\n\n"); + Editable s = mContentEditText.getText(); + if (s == null) { + return; + } - // Fetch and replace the WPImageSpan if it's a remote media - if (imageLoader != null && URLUtil.isNetworkUrl(imageUrl)) { - loadWPImageSpanThumbnail(mediaFile, imageUrl, imageLoader); - } + WPImageSpan[] imageSpans = s.getSpans(selectionStart, selectionEnd, WPImageSpan.class); + if (imageSpans.length != 0) { + // insert a few line breaks if the cursor is already on an image + s.insert(selectionEnd, "\n\n"); + selectionStart = selectionStart + 2; + selectionEnd = selectionEnd + 2; + } else if (column != 0) { + // insert one line break if the cursor is not at the first column + s.insert(selectionEnd, "\n"); + selectionStart = selectionStart + 1; + selectionEnd = selectionEnd + 1; } - }; - handler.postDelayed(r, 1); + + s.insert(selectionStart, " "); + s.setSpan(imageSpan, selectionStart, selectionEnd + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + AlignmentSpan.Standard as = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER); + s.setSpan(as, selectionStart, selectionEnd + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + s.insert(selectionEnd + 1, "\n\n"); + + // Fetch and replace the WPImageSpan if it's a remote media + if (mImageLoader != null && URLUtil.isNetworkUrl(mImageUrl)) { + loadWPImageSpanThumbnail(mMediaFile, mImageUrl, mImageLoader); + } + } + } + + public void addMediaFile(final MediaFile mediaFile, final String imageUrl, final ImageLoader imageLoader, + final int start, final int end) { + AddMediaFileTask addMediaFileTask = new AddMediaFileTask(mediaFile, imageUrl, imageLoader, start, end); + addMediaFileTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } @Override