Search in sources :

Example 11 with MediaFile

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

the class EditorExampleActivity method simulateFileUpload.

private void simulateFileUpload(final String mediaId, final String mediaUrl) {
    Thread thread = new Thread() {

        @Override
        public void run() {
            try {
                float count = (float) 0.1;
                while (count < 1.1) {
                    sleep(500);
                    ((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)

Example 12 with MediaFile

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

the class WPHtml method getContent.

/** Retrieve an image span content for a media file that exists on the server **/
public static String getContent(WPImageSpan imageSpan) {
    // based on PostUploadService
    String content = "";
    MediaFile mediaFile = imageSpan.getMediaFile();
    if (mediaFile == null)
        return content;
    String mediaId = mediaFile.getMediaId();
    if (mediaId == null || mediaId.length() == 0)
        return content;
    boolean isVideo = mediaFile.isVideo();
    String url = imageSpan.getImageSource().toString();
    if (isVideo) {
        if (!TextUtils.isEmpty(mediaFile.getVideoPressShortCode())) {
            content = mediaFile.getVideoPressShortCode();
        } else {
            int xRes = mediaFile.getWidth();
            int yRes = mediaFile.getHeight();
            String mimeType = mediaFile.getMimeType();
            content = String.format(Locale.US, "<video width=\"%s\" height=\"%s\" controls=\"controls\"><source src=\"%s\" type=\"%s\" /><a href=\"%s\">Click to view video</a>.</video>", xRes, yRes, url, mimeType, url);
        }
    } else {
        String alignment = "";
        switch(mediaFile.getHorizontalAlignment()) {
            case 0:
                alignment = "alignnone";
                break;
            case 1:
                alignment = "alignleft";
                break;
            case 2:
                alignment = "aligncenter";
                break;
            case 3:
                alignment = "alignright";
                break;
        }
        String alignmentCSS = "class=\"" + alignment + " size-full\" ";
        String title = mediaFile.getTitle();
        String caption = mediaFile.getCaption();
        int width = mediaFile.getWidth();
        String inlineCSS = " ";
        content = content + "<a href=\"" + url + "\"><img" + inlineCSS + "title=\"" + title + "\" " + alignmentCSS + "alt=\"image\" src=\"" + url + "?w=" + width + "\" /></a>";
        if (!caption.equals("")) {
            content = String.format(Locale.US, "[caption id=\"\" align=\"%s\" width=\"%d\"]%s%s[/caption]", alignment, width, content, TextUtils.htmlEncode(caption));
        }
    }
    return content;
}
Also used : MediaFile(org.wordpress.android.util.helpers.MediaFile) SpannableString(android.text.SpannableString)

Example 13 with MediaFile

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

the class EditPostActivity method addMediaVisualEditor.

private boolean addMediaVisualEditor(Uri uri, boolean isVideo) {
    String path;
    if (uri.toString().contains("content:")) {
        path = getPathFromContentUri(uri);
    } else {
        // File is not in media library
        path = uri.toString().replace("file://", "");
    }
    if (path == null) {
        ToastUtils.showToast(this, R.string.file_not_found, Duration.SHORT);
        return false;
    }
    boolean isOptimized = false;
    if (!NetworkUtils.isWiFiConnected(this) && !isVideo) {
        SiteSettingsInterface siteSettings = SiteSettingsInterface.getInterface(this, mSite, null);
        // Site Settings are implemented on .com/Jetpack sites only
        if (siteSettings != null && siteSettings.init(false).getOptimizedImage()) {
            // Not on WiFi and optimize image is set to ON
            // Max picture size will be 3000px wide. That's the maximum resolution you can set in the current picker.
            String optimizedPath = ImageUtils.optimizeImage(this, path, 3000, 85);
            if (optimizedPath == null) {
                AppLog.e(T.EDITOR, "Optimized picture was null!");
            // TODO: track analytics here
            // AnalyticsTracker.track(Stat.EDITOR_RESIZED_PHOTO_ERROR);
            } else {
                // TODO: track analytics here
                // AnalyticsTracker.track(Stat.EDITOR_RESIZED_PHOTO);
                Uri optimizedImageUri = Uri.parse(optimizedPath);
                if (optimizedImageUri != null) {
                    uri = optimizedImageUri;
                    isOptimized = true;
                }
            }
        }
    }
    MediaModel media = queueFileForUpload(uri, getContentResolver().getType(uri));
    MediaFile mediaFile = FluxCUtils.mediaFileFromMediaModel(media);
    trackAddMediaFromDeviceEvents(isVideo, isOptimized, null, path);
    if (media != null) {
        mEditorFragment.appendMediaFile(mediaFile, path, mImageLoader);
    }
    return true;
}
Also used : MediaFile(org.wordpress.android.util.helpers.MediaFile) MediaModel(org.wordpress.android.fluxc.model.MediaModel) SiteSettingsInterface(org.wordpress.android.ui.prefs.SiteSettingsInterface) Uri(android.net.Uri)

Example 14 with MediaFile

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

the class EditPostActivity method migrateLegacyDraft.

private String migrateLegacyDraft(String content) {
    if (content.contains("<img src=\"null\" android-uri=\"")) {
        // We must replace image tags specific to the legacy editor local drafts:
        // <img src="null" android-uri="file:///..." />
        // And trigger an upload action for the specific image / video
        Pattern pattern = Pattern.compile("<img src=\"null\" android-uri=\"([^\"]*)\".*>");
        Matcher matcher = pattern.matcher(content);
        StringBuffer stringBuffer = new StringBuffer();
        while (matcher.find()) {
            String stringUri = matcher.group(1);
            Uri uri = Uri.parse(stringUri);
            MediaFile mediaFile = FluxCUtils.mediaFileFromMediaModel(queueFileForUpload(uri, getContentResolver().getType(uri), UploadState.FAILED));
            if (mediaFile == null) {
                continue;
            }
            String replacement = getUploadErrorHtml(mediaFile.getMediaId(), mediaFile.getFilePath());
            matcher.appendReplacement(stringBuffer, replacement);
        }
        matcher.appendTail(stringBuffer);
        content = stringBuffer.toString();
    }
    if (content.contains("[caption")) {
        // Convert old legacy post caption formatting to new format, to avoid being stripped by the visual editor
        Pattern pattern = Pattern.compile("(\\[caption[^]]*caption=\"([^\"]*)\"[^]]*].+?)(\\[\\/caption])");
        Matcher matcher = pattern.matcher(content);
        StringBuffer stringBuffer = new StringBuffer();
        while (matcher.find()) {
            String replacement = matcher.group(1) + matcher.group(2) + matcher.group(3);
            matcher.appendReplacement(stringBuffer, replacement);
        }
        matcher.appendTail(stringBuffer);
        content = stringBuffer.toString();
    }
    return content;
}
Also used : MediaFile(org.wordpress.android.util.helpers.MediaFile) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Uri(android.net.Uri)

Example 15 with MediaFile

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

the class EditPostActivity method addMediaLegacyEditor.

private boolean addMediaLegacyEditor(Uri mediaUri, boolean isVideo) {
    trackAddMediaFromDeviceEvents(isVideo, false, mediaUri, null);
    MediaModel mediaModel = buildMediaModel(mediaUri, getContentResolver().getType(mediaUri), UploadState.QUEUED);
    if (isVideo) {
        mediaModel.setTitle(getResources().getString(R.string.video));
    } else {
        mediaModel.setTitle(ImageUtils.getTitleForWPImageSpan(this, mediaUri.getEncodedPath()));
    }
    mediaModel.setPostId(mPost.getId());
    mDispatcher.dispatch(MediaActionBuilder.newUpdateMediaAction(mediaModel));
    MediaFile mediaFile = FluxCUtils.mediaFileFromMediaModel(mediaModel);
    mEditorFragment.appendMediaFile(mediaFile, mediaFile.getFilePath(), mImageLoader);
    return true;
}
Also used : MediaFile(org.wordpress.android.util.helpers.MediaFile) MediaModel(org.wordpress.android.fluxc.model.MediaModel)

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