Search in sources :

Example 6 with MediaFile

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);
    }
}
Also used : MediaFile(org.wordpress.android.util.helpers.MediaFile) MediaModel(org.wordpress.android.fluxc.model.MediaModel)

Example 7 with MediaFile

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);
    }
}
Also used : MediaFile(org.wordpress.android.util.helpers.MediaFile) WPImageSpan(org.wordpress.android.util.helpers.WPImageSpan) SuggestionSpan(android.text.style.SuggestionSpan) MediaGalleryImageSpan(org.wordpress.android.util.helpers.MediaGalleryImageSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) CharacterStyle(android.text.style.CharacterStyle)

Example 8 with MediaFile

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);
}
Also used : MediaFile(org.wordpress.android.util.helpers.MediaFile)

Example 9 with MediaFile

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();
}
Also used : AlertDialog(android.app.AlertDialog) MediaFile(org.wordpress.android.util.helpers.MediaFile) DialogInterface(android.content.DialogInterface) WPImageSpan(org.wordpress.android.util.helpers.WPImageSpan) Spannable(android.text.Spannable)

Example 10 with MediaFile

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();
}
Also used : EditorMediaUploadListener(org.wordpress.android.editor.EditorMediaUploadListener) MediaFile(org.wordpress.android.util.helpers.MediaFile)

Aggregations

MediaFile (org.wordpress.android.util.helpers.MediaFile)15 WPImageSpan (org.wordpress.android.util.helpers.WPImageSpan)5 Uri (android.net.Uri)4 MediaModel (org.wordpress.android.fluxc.model.MediaModel)4 EditorMediaUploadListener (org.wordpress.android.editor.EditorMediaUploadListener)3 Bitmap (android.graphics.Bitmap)2 Spannable (android.text.Spannable)2 EditText (android.widget.EditText)2 MediaGalleryImageSpan (org.wordpress.android.util.helpers.MediaGalleryImageSpan)2 WPEditText (org.wordpress.android.util.widgets.WPEditText)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 Drawable (android.graphics.drawable.Drawable)1 ActionBar (android.support.v7.app.ActionBar)1 Editable (android.text.Editable)1 Layout (android.text.Layout)1 SpannableString (android.text.SpannableString)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 AlignmentSpan (android.text.style.AlignmentSpan)1