Search in sources :

Example 1 with MediaGalleryImageSpan

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

the class WPHtml method withinParagraph.

private static void withinParagraph(StringBuilder out, Spanned text, int start, int end, int nl, boolean last) {
    int next;
    for (int i = start; i < end; i = next) {
        next = text.nextSpanTransition(i, end, CharacterStyle.class);
        CharacterStyle[] style = text.getSpans(i, next, CharacterStyle.class);
        for (int j = 0; j < style.length; j++) {
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("<strong>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("<em>");
                }
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if (s.equals("monospace")) {
                    out.append("<tt>");
                }
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("<sup>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("<sub>");
            }
            if (style[j] instanceof WPUnderlineSpan) {
                out.append("<u>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("<strike>");
            }
            if (style[j] instanceof URLSpan) {
                out.append("<a href=\"");
                out.append(((URLSpan) style[j]).getURL());
                out.append("\">");
            }
            if (style[j] instanceof MediaGalleryImageSpan) {
                out.append(getGalleryShortcode((MediaGalleryImageSpan) style[j]));
            } else if (style[j] instanceof WPImageSpan && ((WPImageSpan) style[j]).getMediaFile().getMediaId() != null) {
                out.append(getContent((WPImageSpan) style[j]));
            } else if (style[j] instanceof WPImageSpan) {
                out.append("<img src=\"");
                out.append(((WPImageSpan) style[j]).getSource());
                out.append("\" android-uri=\"" + ((WPImageSpan) style[j]).getImageSource().toString() + "\"");
                out.append(" />");
                // Don't output the dummy character underlying the image.
                i = next;
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                out.append("<font size =\"");
                out.append(((AbsoluteSizeSpan) style[j]).getSize() / 6);
                out.append("\">");
            }
            if (style[j] instanceof ForegroundColorSpan) {
                out.append("<font color =\"#");
                String color = Integer.toHexString(((ForegroundColorSpan) style[j]).getForegroundColor() + 0x01000000);
                while (color.length() < 6) {
                    color = "0" + color;
                }
                out.append(color);
                out.append("\">");
            }
        }
        processWPImage(out, text, i, next);
        for (int j = style.length - 1; j >= 0; j--) {
            if (style[j] instanceof ForegroundColorSpan) {
                out.append("</font>");
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                out.append("</font>");
            }
            if (style[j] instanceof URLSpan) {
                out.append("</a>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("</strike>");
            }
            if (style[j] instanceof WPUnderlineSpan) {
                out.append("</u>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("</sub>");
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("</sup>");
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if (s.equals("monospace")) {
                    out.append("</tt>");
                }
            }
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("</strong>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("</em>");
                }
            }
        }
    }
    String p = last ? "" : "</p>\n<p>";
    if (nl == 1) {
        out.append("<br>\n");
    } else if (nl == 2) {
        out.append(p);
    } else {
        for (int i = 2; i < nl; i++) {
            out.append("<br>");
        }
        out.append(p);
    }
}
Also used : SuperscriptSpan(android.text.style.SuperscriptSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) WPUnderlineSpan(org.wordpress.android.util.helpers.WPUnderlineSpan) WPImageSpan(org.wordpress.android.util.helpers.WPImageSpan) SpannableString(android.text.SpannableString) URLSpan(android.text.style.URLSpan) CharacterStyle(android.text.style.CharacterStyle) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan) StyleSpan(android.text.style.StyleSpan) SubscriptSpan(android.text.style.SubscriptSpan) MediaGalleryImageSpan(org.wordpress.android.util.helpers.MediaGalleryImageSpan) TypefaceSpan(android.text.style.TypefaceSpan) StrikethroughSpan(android.text.style.StrikethroughSpan)

Example 2 with MediaGalleryImageSpan

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

the class LegacyEditorFragment method onTouch.

@Override
public boolean onTouch(View v, MotionEvent event) {
    float pos = event.getY();
    if (event.getAction() == 0)
        mLastYPos = pos;
    if (event.getAction() > 1) {
        int scrollThreshold = DisplayUtils.dpToPx(getActivity(), 2);
        if (((mLastYPos - pos) > scrollThreshold) || ((pos - mLastYPos) > scrollThreshold))
            mScrollDetected = true;
    }
    mLastYPos = pos;
    if (event.getAction() == MotionEvent.ACTION_UP) {
        ActionBar actionBar = getActionBar();
        if (actionBar != null && actionBar.isShowing()) {
            setContentEditingModeVisible(true);
            return false;
        }
    }
    if (event.getAction() == MotionEvent.ACTION_UP && !mScrollDetected) {
        Layout layout = ((TextView) v).getLayout();
        int x = (int) event.getX();
        int y = (int) event.getY();
        x += v.getScrollX();
        y += v.getScrollY();
        if (layout != null) {
            int line = layout.getLineForVertical(y);
            int charPosition = layout.getOffsetForHorizontal(line, x);
            Spannable spannable = mContentEditText.getText();
            if (spannable == null) {
                return false;
            }
            // check if image span was tapped
            WPImageSpan[] imageSpans = spannable.getSpans(charPosition, charPosition, WPImageSpan.class);
            if (imageSpans.length != 0) {
                final WPImageSpan imageSpan = imageSpans[0];
                MediaFile mediaFile = imageSpan.getMediaFile();
                if (mediaFile == null)
                    return false;
                if (!mediaFile.isVideo()) {
                    LayoutInflater factory = LayoutInflater.from(getActivity());
                    final View alertView = factory.inflate(R.layout.alert_image_options, null);
                    if (alertView == null)
                        return false;
                    final EditText imageWidthText = (EditText) alertView.findViewById(R.id.imageWidthText);
                    final EditText titleText = (EditText) alertView.findViewById(R.id.title);
                    final EditText caption = (EditText) alertView.findViewById(R.id.caption);
                    final CheckBox featuredCheckBox = (CheckBox) alertView.findViewById(R.id.featuredImage);
                    final CheckBox featuredInPostCheckBox = (CheckBox) alertView.findViewById(R.id.featuredInPost);
                    // show featured image checkboxes if supported
                    if (mFeaturedImageSupported) {
                        featuredCheckBox.setVisibility(View.VISIBLE);
                        featuredInPostCheckBox.setVisibility(View.VISIBLE);
                    }
                    featuredCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            if (isChecked) {
                                featuredInPostCheckBox.setVisibility(View.VISIBLE);
                            } else {
                                featuredInPostCheckBox.setVisibility(View.GONE);
                            }
                        }
                    });
                    final SeekBar seekBar = (SeekBar) alertView.findViewById(R.id.imageWidth);
                    final Spinner alignmentSpinner = (Spinner) alertView.findViewById(R.id.alignment_spinner);
                    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.alignment_array, android.R.layout.simple_spinner_item);
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    alignmentSpinner.setAdapter(adapter);
                    seekBar.setProgress(mediaFile.getWidth());
                    titleText.setText(mediaFile.getTitle());
                    caption.setText(mediaFile.getCaption());
                    featuredCheckBox.setChecked(mediaFile.isFeatured());
                    if (mediaFile.isFeatured()) {
                        featuredInPostCheckBox.setVisibility(View.VISIBLE);
                    } else {
                        featuredInPostCheckBox.setVisibility(View.GONE);
                    }
                    featuredInPostCheckBox.setChecked(mediaFile.isFeaturedInPost());
                    alignmentSpinner.setSelection(mediaFile.getHorizontalAlignment(), true);
                    final int maxWidth = MediaUtils.getMaximumImageWidth(getActivity(), imageSpan.getImageSource(), mBlogSettingMaxImageWidth);
                    seekBar.setMax(maxWidth / 10);
                    imageWidthText.setText(String.format(Locale.US, "%dpx", maxWidth));
                    if (mediaFile.getWidth() != 0) {
                        seekBar.setProgress(mediaFile.getWidth() / 10);
                    }
                    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

                        @Override
                        public void onStopTrackingTouch(SeekBar seekBar) {
                        }

                        @Override
                        public void onStartTrackingTouch(SeekBar seekBar) {
                        }

                        @Override
                        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                            if (progress == 0) {
                                progress = 1;
                            }
                            imageWidthText.setText(String.format(Locale.US, "%dpx", progress * 10));
                        }
                    });
                    imageWidthText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

                        @Override
                        public void onFocusChange(View v, boolean hasFocus) {
                            if (hasFocus) {
                                imageWidthText.setText("");
                            }
                        }
                    });
                    imageWidthText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

                        @Override
                        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                            int width = getEditTextIntegerClamped(imageWidthText, 10, maxWidth);
                            seekBar.setProgress(width / 10);
                            imageWidthText.setSelection((String.valueOf(width).length()));
                            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(imageWidthText.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
                            return true;
                        }
                    });
                    showImageSettings(alertView, titleText, caption, imageWidthText, featuredCheckBox, featuredInPostCheckBox, maxWidth, alignmentSpinner, imageSpan);
                    mScrollDetected = false;
                    return true;
                }
            } else {
                mContentEditText.setMovementMethod(ArrowKeyMovementMethod.getInstance());
                int selectionStart = mContentEditText.getSelectionStart();
                if (selectionStart >= 0 && mContentEditText.getSelectionEnd() >= selectionStart)
                    mContentEditText.setSelection(selectionStart, mContentEditText.getSelectionEnd());
            }
            // get media gallery spans
            MediaGalleryImageSpan[] gallerySpans = spannable.getSpans(charPosition, charPosition, MediaGalleryImageSpan.class);
            if (gallerySpans.length > 0) {
                final MediaGalleryImageSpan gallerySpan = gallerySpans[0];
                Intent intent = new Intent(ACTION_MEDIA_GALLERY_TOUCHED);
                intent.putExtra(EXTRA_MEDIA_GALLERY, gallerySpan.getMediaGallery());
                getActivity().sendBroadcast(intent);
            }
        }
    } else if (event.getAction() == 1) {
        mScrollDetected = false;
    }
    return false;
}
Also used : Spinner(android.widget.Spinner) InputMethodManager(android.view.inputmethod.InputMethodManager) KeyEvent(android.view.KeyEvent) TextView(android.widget.TextView) ActionBar(android.support.v7.app.ActionBar) MediaFile(org.wordpress.android.util.helpers.MediaFile) WPEditText(org.wordpress.android.util.widgets.WPEditText) EditText(android.widget.EditText) SeekBar(android.widget.SeekBar) WPImageSpan(org.wordpress.android.util.helpers.WPImageSpan) Intent(android.content.Intent) View(android.view.View) TextView(android.widget.TextView) LinearLayout(android.widget.LinearLayout) Layout(android.text.Layout) CheckBox(android.widget.CheckBox) LayoutInflater(android.view.LayoutInflater) MediaGalleryImageSpan(org.wordpress.android.util.helpers.MediaGalleryImageSpan) Spannable(android.text.Spannable) CompoundButton(android.widget.CompoundButton)

Example 3 with MediaGalleryImageSpan

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

the class LegacyEditorFragment method appendGallery.

@Override
public void appendGallery(MediaGallery mediaGallery) {
    Editable editableText = mContentEditText.getText();
    if (editableText == null) {
        return;
    }
    int selectionStart = mContentEditText.getSelectionStart();
    int selectionEnd = mContentEditText.getSelectionEnd();
    if (selectionStart > selectionEnd) {
        int temp = selectionEnd;
        selectionEnd = selectionStart;
        selectionStart = temp;
    }
    int line, column = 0;
    if (mContentEditText.getLayout() != null) {
        line = mContentEditText.getLayout().getLineForOffset(selectionStart);
        column = mContentEditText.getSelectionStart() - mContentEditText.getLayout().getLineStart(line);
    }
    if (column != 0) {
        // insert one line break if the cursor is not at the first column
        editableText.insert(selectionEnd, "\n");
        selectionStart = selectionStart + 1;
        selectionEnd = selectionEnd + 1;
    }
    editableText.insert(selectionStart, " ");
    MediaGalleryImageSpan is = new MediaGalleryImageSpan(getActivity(), mediaGallery, R.drawable.legacy_icon_mediagallery_placeholder);
    editableText.setSpan(is, selectionStart, selectionEnd + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    AlignmentSpan.Standard as = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER);
    editableText.setSpan(as, selectionStart, selectionEnd + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    editableText.insert(selectionEnd + 1, "\n\n");
}
Also used : AlignmentSpan(android.text.style.AlignmentSpan) Editable(android.text.Editable) MediaGalleryImageSpan(org.wordpress.android.util.helpers.MediaGalleryImageSpan)

Example 4 with MediaGalleryImageSpan

use of org.wordpress.android.util.helpers.MediaGalleryImageSpan 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)

Aggregations

MediaGalleryImageSpan (org.wordpress.android.util.helpers.MediaGalleryImageSpan)4 WPImageSpan (org.wordpress.android.util.helpers.WPImageSpan)3 CharacterStyle (android.text.style.CharacterStyle)2 MediaFile (org.wordpress.android.util.helpers.MediaFile)2 Intent (android.content.Intent)1 ActionBar (android.support.v7.app.ActionBar)1 Editable (android.text.Editable)1 Layout (android.text.Layout)1 Spannable (android.text.Spannable)1 SpannableString (android.text.SpannableString)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)1 AlignmentSpan (android.text.style.AlignmentSpan)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 StrikethroughSpan (android.text.style.StrikethroughSpan)1 StyleSpan (android.text.style.StyleSpan)1 SubscriptSpan (android.text.style.SubscriptSpan)1 SuggestionSpan (android.text.style.SuggestionSpan)1 SuperscriptSpan (android.text.style.SuperscriptSpan)1 TypefaceSpan (android.text.style.TypefaceSpan)1