Search in sources :

Example 1 with MediaFile

use of org.wordpress.android.util.helpers.MediaFile in project WordPress-Android by wordpress-mobile.

the class EditorExampleActivity method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (data == null) {
        return;
    }
    Uri mediaUri = data.getData();
    MediaFile mediaFile = new MediaFile();
    String mediaId = String.valueOf(System.currentTimeMillis());
    mediaFile.setMediaId(mediaId);
    mediaFile.setVideo(mediaUri.toString().contains("video"));
    switch(requestCode) {
        case ADD_MEDIA_ACTIVITY_REQUEST_CODE:
            mEditorFragment.appendMediaFile(mediaFile, mediaUri.toString(), null);
            if (mEditorFragment instanceof EditorMediaUploadListener) {
                simulateFileUpload(mediaId, mediaUri.toString());
            }
            break;
        case ADD_MEDIA_FAIL_ACTIVITY_REQUEST_CODE:
            mEditorFragment.appendMediaFile(mediaFile, mediaUri.toString(), null);
            if (mEditorFragment instanceof EditorMediaUploadListener) {
                simulateFileUploadFail(mediaId, mediaUri.toString());
            }
            break;
        case ADD_MEDIA_SLOW_NETWORK_REQUEST_CODE:
            mEditorFragment.appendMediaFile(mediaFile, mediaUri.toString(), null);
            if (mEditorFragment instanceof EditorMediaUploadListener) {
                simulateSlowFileUpload(mediaId, mediaUri.toString());
            }
            break;
    }
}
Also used : MediaFile(org.wordpress.android.util.helpers.MediaFile) EditorMediaUploadListener(org.wordpress.android.editor.EditorMediaUploadListener) Uri(android.net.Uri)

Example 2 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 3 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 4 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 5 with MediaFile

use of org.wordpress.android.util.helpers.MediaFile in project WordPress-Android by wordpress-mobile.

the class LegacyEditorFragment method loadWPImageSpanThumbnail.

private void loadWPImageSpanThumbnail(MediaFile mediaFile, String imageURL, ImageLoader imageLoader) {
    if (mediaFile == null || imageURL == null) {
        return;
    }
    final String mediaId = mediaFile.getMediaId();
    if (mediaId == null) {
        return;
    }
    final int maxThumbWidth = ImageUtils.getMaximumThumbnailWidthForEditor(getActivity());
    imageLoader.get(imageURL, new ImageLoader.ImageListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
        }

        @Override
        public void onResponse(ImageLoader.ImageContainer container, boolean arg1) {
            Bitmap downloadedBitmap = container.getBitmap();
            if (downloadedBitmap == null) {
                // no bitmap downloaded from the server.
                return;
            }
            if (downloadedBitmap.getWidth() < MIN_THUMBNAIL_WIDTH) {
                // Picture is too small. Show the placeholder in this case.
                return;
            }
            Bitmap resizedBitmap;
            // resize the downloaded bitmap
            resizedBitmap = ImageUtils.getScaledBitmapAtLongestSide(downloadedBitmap, maxThumbWidth);
            if (resizedBitmap == null) {
                return;
            }
            final EditText editText = mContentEditText;
            Editable s = editText.getText();
            if (s == null) {
                return;
            }
            WPImageSpan[] spans = s.getSpans(0, s.length(), WPImageSpan.class);
            if (spans.length != 0 && getActivity() != null) {
                for (WPImageSpan is : spans) {
                    MediaFile mediaFile = is.getMediaFile();
                    if (mediaFile == null) {
                        continue;
                    }
                    if (mediaId.equals(mediaFile.getMediaId()) && !is.isNetworkImageLoaded()) {
                        // replace the existing span with a new one with the correct image, re-add
                        // it to the same position.
                        int spanStart = is.getStartPosition();
                        int spanEnd = is.getEndPosition();
                        WPEditImageSpan imageSpan = new WPEditImageSpan(getActivity(), resizedBitmap, is.getImageSource());
                        imageSpan.setMediaFile(is.getMediaFile());
                        imageSpan.setNetworkImageLoaded(true);
                        imageSpan.setPosition(spanStart, spanEnd);
                        s.removeSpan(is);
                        s.setSpan(imageSpan, spanStart, spanEnd + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        break;
                    }
                }
            }
        }
    }, 0, 0);
}
Also used : VolleyError(com.android.volley.VolleyError) WPEditText(org.wordpress.android.util.widgets.WPEditText) EditText(android.widget.EditText) MediaFile(org.wordpress.android.util.helpers.MediaFile) WPEditImageSpan(org.wordpress.android.editor.legacy.WPEditImageSpan) WPImageSpan(org.wordpress.android.util.helpers.WPImageSpan) Bitmap(android.graphics.Bitmap) Editable(android.text.Editable) ImageLoader(com.android.volley.toolbox.ImageLoader)

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