|
9 | 9 | import android.graphics.BitmapFactory; |
10 | 10 | import android.graphics.Typeface; |
11 | 11 | import android.net.Uri; |
| 12 | +import android.os.AsyncTask; |
12 | 13 | import android.os.Bundle; |
13 | | -import android.os.Handler; |
14 | | -import android.os.Looper; |
15 | 14 | import android.os.Parcelable; |
16 | 15 | import android.support.v7.app.ActionBar; |
17 | 16 | import android.support.v7.app.AppCompatActivity; |
@@ -1023,66 +1022,83 @@ public void onSaveInstanceState(Bundle outState) { |
1023 | 1022 | outState.putString(KEY_CONTENT, mContentEditText.getText().toString()); |
1024 | 1023 | } |
1025 | 1024 |
|
1026 | | - public void addMediaFile(final MediaFile mediaFile, final String imageUrl, final ImageLoader imageLoader, final int start, final int end) { |
1027 | | - mediaFile.setFileURL(imageUrl); |
1028 | | - mediaFile.setFilePath(imageUrl); |
1029 | | - final WPEditImageSpan imageSpan = createWPEditImageSpan(getActivity(), mediaFile); |
1030 | | - mEditorFragmentListener.saveMediaFile(mediaFile); |
1031 | | - imageSpan.setMediaFile(mediaFile); |
| 1025 | + private class AddMediaFileTask extends AsyncTask<Void, Void, WPEditImageSpan> { |
| 1026 | + private MediaFile mMediaFile; |
| 1027 | + private String mImageUrl; |
| 1028 | + private ImageLoader mImageLoader; |
| 1029 | + private int mStart; |
| 1030 | + private int mEnd; |
| 1031 | + |
| 1032 | + public AddMediaFileTask(MediaFile mediaFile, String imageUrl, ImageLoader imageLoader, int start, int end) { |
| 1033 | + mMediaFile = mediaFile; |
| 1034 | + mImageUrl = imageUrl; |
| 1035 | + mImageLoader = imageLoader; |
| 1036 | + mStart = start; |
| 1037 | + mEnd = end; |
| 1038 | + } |
1032 | 1039 |
|
1033 | | - Handler handler = new Handler(Looper.getMainLooper()); |
1034 | | - final Runnable r = new Runnable() { |
1035 | | - @Override |
1036 | | - public void run() { |
1037 | | - // Insert the WPImageSpan in the content field |
1038 | | - int selectionStart = start; |
1039 | | - int selectionEnd = end; |
1040 | | - |
1041 | | - if (selectionStart > selectionEnd) { |
1042 | | - int temp = selectionEnd; |
1043 | | - selectionEnd = selectionStart; |
1044 | | - selectionStart = temp; |
1045 | | - } |
| 1040 | + protected WPEditImageSpan doInBackground(Void... voids) { |
| 1041 | + mMediaFile.setFileURL(mImageUrl); |
| 1042 | + mMediaFile.setFilePath(mImageUrl); |
| 1043 | + WPEditImageSpan imageSpan = createWPEditImageSpan(getActivity(), mMediaFile); |
| 1044 | + mEditorFragmentListener.saveMediaFile(mMediaFile); |
| 1045 | + return imageSpan; |
| 1046 | + } |
1046 | 1047 |
|
1047 | | - imageSpan.setPosition(selectionStart, selectionEnd); |
| 1048 | + protected void onPostExecute(WPEditImageSpan imageSpan) { |
| 1049 | + // Insert the WPImageSpan in the content field |
| 1050 | + int selectionStart = mStart; |
| 1051 | + int selectionEnd = mEnd; |
1048 | 1052 |
|
1049 | | - int line, column = 0; |
1050 | | - if (mContentEditText.getLayout() != null) { |
1051 | | - line = mContentEditText.getLayout().getLineForOffset(selectionStart); |
1052 | | - column = selectionStart - mContentEditText.getLayout().getLineStart(line); |
1053 | | - } |
| 1053 | + if (selectionStart > selectionEnd) { |
| 1054 | + int temp = selectionEnd; |
| 1055 | + selectionEnd = selectionStart; |
| 1056 | + selectionStart = temp; |
| 1057 | + } |
1054 | 1058 |
|
1055 | | - Editable s = mContentEditText.getText(); |
1056 | | - if (s == null) { |
1057 | | - return; |
1058 | | - } |
| 1059 | + imageSpan.setPosition(selectionStart, selectionEnd); |
1059 | 1060 |
|
1060 | | - WPImageSpan[] imageSpans = s.getSpans(selectionStart, selectionEnd, WPImageSpan.class); |
1061 | | - if (imageSpans.length != 0) { |
1062 | | - // insert a few line breaks if the cursor is already on an image |
1063 | | - s.insert(selectionEnd, "\n\n"); |
1064 | | - selectionStart = selectionStart + 2; |
1065 | | - selectionEnd = selectionEnd + 2; |
1066 | | - } else if (column != 0) { |
1067 | | - // insert one line break if the cursor is not at the first column |
1068 | | - s.insert(selectionEnd, "\n"); |
1069 | | - selectionStart = selectionStart + 1; |
1070 | | - selectionEnd = selectionEnd + 1; |
1071 | | - } |
| 1061 | + int line, column = 0; |
| 1062 | + if (mContentEditText.getLayout() != null) { |
| 1063 | + line = mContentEditText.getLayout().getLineForOffset(selectionStart); |
| 1064 | + column = selectionStart - mContentEditText.getLayout().getLineStart(line); |
| 1065 | + } |
1072 | 1066 |
|
1073 | | - s.insert(selectionStart, " "); |
1074 | | - s.setSpan(imageSpan, selectionStart, selectionEnd + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
1075 | | - AlignmentSpan.Standard as = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER); |
1076 | | - s.setSpan(as, selectionStart, selectionEnd + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
1077 | | - s.insert(selectionEnd + 1, "\n\n"); |
| 1067 | + Editable s = mContentEditText.getText(); |
| 1068 | + if (s == null) { |
| 1069 | + return; |
| 1070 | + } |
1078 | 1071 |
|
1079 | | - // Fetch and replace the WPImageSpan if it's a remote media |
1080 | | - if (imageLoader != null && URLUtil.isNetworkUrl(imageUrl)) { |
1081 | | - loadWPImageSpanThumbnail(mediaFile, imageUrl, imageLoader); |
1082 | | - } |
| 1072 | + WPImageSpan[] imageSpans = s.getSpans(selectionStart, selectionEnd, WPImageSpan.class); |
| 1073 | + if (imageSpans.length != 0) { |
| 1074 | + // insert a few line breaks if the cursor is already on an image |
| 1075 | + s.insert(selectionEnd, "\n\n"); |
| 1076 | + selectionStart = selectionStart + 2; |
| 1077 | + selectionEnd = selectionEnd + 2; |
| 1078 | + } else if (column != 0) { |
| 1079 | + // insert one line break if the cursor is not at the first column |
| 1080 | + s.insert(selectionEnd, "\n"); |
| 1081 | + selectionStart = selectionStart + 1; |
| 1082 | + selectionEnd = selectionEnd + 1; |
1083 | 1083 | } |
1084 | | - }; |
1085 | | - handler.postDelayed(r, 1); |
| 1084 | + |
| 1085 | + s.insert(selectionStart, " "); |
| 1086 | + s.setSpan(imageSpan, selectionStart, selectionEnd + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 1087 | + AlignmentSpan.Standard as = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER); |
| 1088 | + s.setSpan(as, selectionStart, selectionEnd + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 1089 | + s.insert(selectionEnd + 1, "\n\n"); |
| 1090 | + |
| 1091 | + // Fetch and replace the WPImageSpan if it's a remote media |
| 1092 | + if (mImageLoader != null && URLUtil.isNetworkUrl(mImageUrl)) { |
| 1093 | + loadWPImageSpanThumbnail(mMediaFile, mImageUrl, mImageLoader); |
| 1094 | + } |
| 1095 | + } |
| 1096 | + } |
| 1097 | + |
| 1098 | + public void addMediaFile(final MediaFile mediaFile, final String imageUrl, final ImageLoader imageLoader, |
| 1099 | + final int start, final int end) { |
| 1100 | + AddMediaFileTask addMediaFileTask = new AddMediaFileTask(mediaFile, imageUrl, imageLoader, start, end); |
| 1101 | + addMediaFileTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
1086 | 1102 | } |
1087 | 1103 |
|
1088 | 1104 | @Override |
|
0 commit comments