use of org.wordpress.android.util.helpers.MediaFile in project WordPress-Android by wordpress-mobile.
the class EditPostActivity method addExistingMediaToEditor.
private void addExistingMediaToEditor(long mediaId) {
MediaModel media = mMediaStore.getSiteMediaWithId(mSite, mediaId);
if (media != null) {
MediaFile mediaFile = FluxCUtils.mediaFileFromMediaModel(media);
trackAddMediaFromWPLibraryEvents(mediaFile.isVideo(), media.getMediaId());
String urlToUse = TextUtils.isEmpty(media.getUrl()) ? media.getFilePath() : media.getUrl();
mEditorFragment.appendMediaFile(mediaFile, urlToUse, mImageLoader);
}
}
use of org.wordpress.android.util.helpers.MediaFile in project WordPress-Android by wordpress-mobile.
the class EditPostActivity method updatePostContent.
// TODO: Replace with contents of the updatePostContentNewEditor() method when legacy editor is dropped
/**
* Updates post object with content of this fragment
*/
public void updatePostContent(boolean isAutoSave) throws IllegalEditorStateException {
if (mPost == null) {
return;
}
String title = StringUtils.notNullStr((String) mEditorFragment.getTitle());
SpannableStringBuilder postContent;
if (mEditorFragment.getSpannedContent() != null) {
// needed by the legacy editor to save local drafts
try {
postContent = new SpannableStringBuilder(mEditorFragment.getSpannedContent());
} catch (IndexOutOfBoundsException e) {
// A core android bug might cause an out of bounds exception, if so we'll just use the current editable
// See https://code.google.com/p/android/issues/detail?id=5164
postContent = new SpannableStringBuilder(StringUtils.notNullStr((String) mEditorFragment.getContent()));
}
} else {
postContent = new SpannableStringBuilder(StringUtils.notNullStr((String) mEditorFragment.getContent()));
}
String content;
if (mPost.isLocalDraft()) {
// remove suggestion spans, they cause craziness in WPHtml.toHTML().
CharacterStyle[] characterStyles = postContent.getSpans(0, postContent.length(), CharacterStyle.class);
for (CharacterStyle characterStyle : characterStyles) {
if (characterStyle instanceof SuggestionSpan) {
postContent.removeSpan(characterStyle);
}
}
content = WPHtml.toHtml(postContent);
// replace duplicate <p> tags so there's not duplicates, trac #86
content = content.replace("<p><p>", "<p>");
content = content.replace("</p></p>", "</p>");
content = content.replace("<br><br>", "<br>");
// sometimes the editor creates extra tags
content = content.replace("</strong><strong>", "").replace("</em><em>", "").replace("</u><u>", "").replace("</strike><strike>", "").replace("</blockquote><blockquote>", "");
} else {
if (!isAutoSave) {
// Add gallery shortcode
MediaGalleryImageSpan[] gallerySpans = postContent.getSpans(0, postContent.length(), MediaGalleryImageSpan.class);
for (MediaGalleryImageSpan gallerySpan : gallerySpans) {
int start = postContent.getSpanStart(gallerySpan);
postContent.removeSpan(gallerySpan);
postContent.insert(start, WPHtml.getGalleryShortcode(gallerySpan));
}
}
WPImageSpan[] imageSpans = postContent.getSpans(0, postContent.length(), WPImageSpan.class);
if (imageSpans.length != 0) {
for (WPImageSpan wpIS : imageSpans) {
MediaFile mediaFile = wpIS.getMediaFile();
if (mediaFile == null) {
continue;
}
if (mediaFile.getMediaId() != null) {
updateMediaFileOnServer(mediaFile);
} else {
mediaFile.setFileName(wpIS.getImageSource().toString());
mediaFile.setFilePath(wpIS.getImageSource().toString());
}
int tagStart = postContent.getSpanStart(wpIS);
if (!isAutoSave) {
postContent.removeSpan(wpIS);
// network image has a mediaId
if (mediaFile.getMediaId() != null && mediaFile.getMediaId().length() > 0) {
postContent.insert(tagStart, WPHtml.getContent(wpIS));
} else {
// local image for upload
postContent.insert(tagStart, "<img android-uri=\"" + wpIS.getImageSource().toString() + "\" />");
}
}
}
}
content = postContent.toString();
}
mPost.setTitle(title);
mPost.setContent(content);
if (!mPost.isLocalDraft()) {
mPost.setIsLocallyChanged(true);
}
}
use of org.wordpress.android.util.helpers.MediaFile in project WordPress-Android by wordpress-mobile.
the class EditPostActivity method onUploadError.
private void onUploadError(MediaModel media, MediaStore.MediaError error) {
String localMediaId = String.valueOf(media.getId());
Map<String, Object> properties = null;
MediaFile mf = FluxCUtils.mediaFileFromMediaModel(media);
if (mf != null) {
properties = AnalyticsUtils.getMediaProperties(this, mf.isVideo(), null, mf.getFilePath());
properties.put("error_type", error.type.name());
}
AnalyticsTracker.track(Stat.EDITOR_UPLOAD_MEDIA_FAILED, properties);
// Display custom error depending on error type
String errorMessage;
switch(error.type) {
case AUTHORIZATION_REQUIRED:
errorMessage = getString(R.string.media_error_no_permission_upload);
break;
case GENERIC_ERROR:
default:
errorMessage = TextUtils.isEmpty(error.message) ? getString(R.string.tap_to_try_again) : error.message;
}
mEditorMediaUploadListener.onMediaUploadFailed(localMediaId, errorMessage);
removeMediaFromPendingList(media);
}
use of org.wordpress.android.util.helpers.MediaFile in project WordPress-Android by wordpress-mobile.
the class LegacyEditorFragment method showImageSettings.
/**
* Rich Text Editor
*/
public void showImageSettings(final View alertView, final EditText titleText, final EditText caption, final EditText imageWidthText, final CheckBox featuredCheckBox, final CheckBox featuredInPostCheckBox, final int maxWidth, final Spinner alignmentSpinner, final WPImageSpan imageSpan) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getString(R.string.image_settings));
builder.setView(alertView);
builder.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String title = (titleText.getText() != null) ? titleText.getText().toString() : "";
MediaFile mediaFile = imageSpan.getMediaFile();
if (mediaFile == null) {
return;
}
mediaFile.setTitle(title);
mediaFile.setHorizontalAlignment(alignmentSpinner.getSelectedItemPosition());
mediaFile.setWidth(getEditTextIntegerClamped(imageWidthText, 10, maxWidth));
String captionText = (caption.getText() != null) ? caption.getText().toString() : "";
mediaFile.setCaption(captionText);
mediaFile.setFeatured(featuredCheckBox.isChecked());
if (featuredCheckBox.isChecked()) {
// remove featured flag from all other images
Spannable contentSpannable = mContentEditText.getText();
WPImageSpan[] imageSpans = contentSpannable.getSpans(0, contentSpannable.length(), WPImageSpan.class);
if (imageSpans.length > 1) {
for (WPImageSpan postImageSpan : imageSpans) {
if (postImageSpan != imageSpan) {
MediaFile postMediaFile = postImageSpan.getMediaFile();
postMediaFile.setFeatured(false);
postMediaFile.setFeaturedInPost(false);
// TODO: remove this
mEditorFragmentListener.saveMediaFile(postMediaFile);
}
}
}
}
mediaFile.setFeaturedInPost(featuredInPostCheckBox.isChecked());
// TODO: remove this
mEditorFragmentListener.saveMediaFile(mediaFile);
}
});
builder.setNegativeButton(getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
alertDialog.show();
}
use of org.wordpress.android.util.helpers.MediaFile in project WordPress-Android by wordpress-mobile.
the class EditorExampleActivity method simulateSlowFileUpload.
private void simulateSlowFileUpload(final String mediaId, final String mediaUrl) {
Thread thread = new Thread() {
@Override
public void run() {
try {
sleep(5000);
float count = (float) 0.1;
while (count < 1.1) {
sleep(2000);
((EditorMediaUploadListener) mEditorFragment).onMediaUploadProgress(mediaId, count);
count += 0.1;
}
MediaFile mediaFile = new MediaFile();
mediaFile.setMediaId(MEDIA_REMOTE_ID_SAMPLE);
mediaFile.setFileURL(mediaUrl);
((EditorMediaUploadListener) mEditorFragment).onMediaUploadSucceeded(mediaId, mediaFile);
if (mFailedUploads.containsKey(mediaId)) {
mFailedUploads.remove(mediaId);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
Aggregations