Code example for ImageSpan

Methods: getDrawable, setSpan

0
 
        public void onInsertImage(int resID) {
            if (DBG) {
                Log.d(LOG_TAG, "--- onInsertImage by resID");
            } 
            insertImageSpan(new ImageSpan(mEST.getContext(), resID));
            mEST.notifyStateChanged(mMode, mState);
        } 
 
        public void onInsertHorizontalLine() { 
            if (DBG) {
                Log.d(LOG_TAG, "--- onInsertHorizontalLine:");
            } 
            insertImageSpan(new HorizontalLineSpan(0xFF000000, mEST));
            mEST.notifyStateChanged(mMode, mState);
        } 
 
        public void onClearStyles() { 
            if (DBG) {
                Log.d(LOG_TAG, "--- onClearStyles");
            } 
            Editable txt = mEST.getText();
            int len = txt.length();
            Object[] styles = txt.getSpans(0, len, Object.class);
            for (Object style : styles) {
                if (style instanceof ParagraphStyle ||
                        style instanceof QuoteSpan ||
                        style instanceof CharacterStyle) {
                    if (style instanceof ImageSpan) {
                        int start = txt.getSpanStart(style);
                        int end = txt.getSpanEnd(style);
                        txt.replace(start, end, "");
                    } 
                    txt.removeSpan(style);
                } 
            } 
            mEST.setBackgroundDrawable(mEST.mDefaultBackground);
            mEST.mBackgroundColor = DEFAULT_BACKGROUND_COLOR;
        } 
 
        public void setItemSize(int size) {
            if (DBG) {
                Log.d(LOG_TAG, "--- onClickSizeItem");
            } 
            if (mState == STATE_SELECTED || mState == STATE_SELECT_FIX) {
                changeSizeSelectedText(size);
                handleResetEdit(); 
            } 
        } 
 
        public void setItemColor(int color) {
            if (DBG) {
                Log.d(LOG_TAG, "--- onClickColorItem");
            } 
            if (mState == STATE_SELECTED || mState == STATE_SELECT_FIX) {
                changeColorSelectedText(color);
                handleResetEdit(); 
            } 
        } 
 
        public void setAlignment(Layout.Alignment align) {
            if (DBG) {
                Log.d(LOG_TAG, "--- onClickColorItem");
            } 
            if (mState == STATE_SELECTED || mState == STATE_SELECT_FIX) {
                changeAlign(align);
                handleResetEdit(); 
            } 
        } 
 
        public boolean isEditting() { 
            return mEditFlag;
        } 
 
        /* If the style of the span is added, add check case for that style */ 
        public boolean isStyledText() { 
            Editable txt = mEST.getText();
            int len = txt.length();
            if (txt.getSpans(0, len -1, ParagraphStyle.class).length > 0 ||
                    txt.getSpans(0, len -1, QuoteSpan.class).length > 0 ||
                    txt.getSpans(0, len -1, CharacterStyle.class).length > 0 ||
                    mEST.mBackgroundColor != DEFAULT_BACKGROUND_COLOR) {
                return true; 
            } 
            return false; 
        } 
 
        public boolean isSoftKeyBlocked() { 
            return mSoftKeyBlockFlag;
        } 
 
        public int getEditMode() { 
            return mMode;
        } 
 
        public int getSelectState() { 
            return mState;
        } 
 
        public int getSelectionStart() { 
            return mCurStart;
        } 
 
        public int getSelectionEnd() { 
            return mCurEnd;
        } 
 
        private void doNextHandle() { 
            if (DBG) {
                Log.d(LOG_TAG, "--- doNextHandle: " + mMode + "," + mState);
            } 
            switch (mMode) {
            case MODE_COPY:
                handleCopy(); 
                break; 
            case MODE_CUT:
                handleCut(); 
                break; 
            case MODE_PASTE:
                handlePaste(); 
                break; 
            case MODE_SIZE:
                handleSize(); 
                break; 
            case MODE_COLOR:
                handleColor(); 
                break; 
            case MODE_ALIGN:
                handleAlign(); 
                break; 
            default: 
                break; 
            } 
        } 
 
        private void handleCancel() { 
            if (DBG) {
                Log.d(LOG_TAG, "--- handleCancel");
            } 
            mMode = MODE_NOTHING;
            mState = STATE_SELECT_OFF;
            mEditFlag = false;
            Log.d(LOG_TAG, "--- handleCancel:" + mEST.getInputType());
            unblockSoftKey(); 
            unsetSelect(); 
        } 
 
        private void handleComplete() { 
            if (DBG) {
                Log.d(LOG_TAG, "--- handleComplete");
            } 
            if (!mEditFlag) {
                return; 
            } 
            if (mState == STATE_SELECTED) {
                mState = STATE_SELECT_FIX;
            } 
            doNextHandle(); 
        } 
 
        private void handleTextViewFunc(int mode, int id) {
            if (DBG) {
                Log.d(LOG_TAG, "--- handleTextView: " + mMode + "," + mState +
                        "," + id);
            } 
            if (!mEditFlag) {
                return; 
            } 
            if (mMode == MODE_NOTHING || mMode == MODE_SELECT) {
                mMode = mode;
                if (mState == STATE_SELECTED) {
                    mState = STATE_SELECT_FIX;
                    handleTextViewFunc(mode, id);
                } else { 
                    handleSelect(); 
                } 
            } else if (mMode != mode) {
                handleCancel(); 
                mMode = mode;
                handleTextViewFunc(mode, id);
            } else if (mState == STATE_SELECT_FIX) {
                mEST.onTextContextMenuItem(id);
                handleResetEdit(); 
            } 
        } 
 
        private void handleCopy() { 
            if (DBG) {
                Log.d(LOG_TAG, "--- handleCopy: " + mMode + "," + mState);
            } 
            handleTextViewFunc(MODE_COPY, android.R.id.copy);
        } 
 
        private void handleCut() { 
            if (DBG) {
                Log.d(LOG_TAG, "--- handleCopy: " + mMode + "," + mState);
            } 
            handleTextViewFunc(MODE_CUT, android.R.id.cut);
        } 
 
        private void handlePaste() { 
            if (DBG) {
                Log.d(LOG_TAG, "--- handlePaste");
            } 
            if (!mEditFlag) {
                return; 
            } 
            mEST.onTextContextMenuItem(android.R.id.paste);
        } 
 
        private void handleSetSpan(int mode) {
            if (DBG) {
                Log.d(LOG_TAG, "--- handleSetSpan:" + mEditFlag + ","
                        + mState + ',' + mMode);
            } 
            if (!mEditFlag) {
                Log.e(LOG_TAG, "--- handleSetSpan: Editing is not started.");
                return; 
            } 
            if (mMode == MODE_NOTHING || mMode == MODE_SELECT) {
                mMode = mode;
                if (mState == STATE_SELECTED) {
                    mState = STATE_SELECT_FIX;
                    handleSetSpan(mode);
                } else { 
                    handleSelect(); 
                } 
            } else if (mMode != mode) {
                handleCancel(); 
                mMode = mode;
                handleSetSpan(mode);
            } else { 
                if (mState == STATE_SELECT_FIX) {
                    mEST.setHintMessage(HINT_MSG_NULL);
                    switch (mode) {
                    case MODE_COLOR:
                        mEST.onShowForegroundColorAlert();
                        break; 
                    case MODE_SIZE:
                        mEST.onShowSizeAlert();
                        break; 
                    case MODE_ALIGN:
                        mEST.onShowAlignAlert();
                        break; 
                    default: 
                        Log.e(LOG_TAG, "--- handleSetSpan: invalid mode.");
                        break; 
                    } 
                } else { 
                    Log.d(LOG_TAG, "--- handleSetSpan: do nothing.");
                } 
            } 
        } 
 
        private void handleSize() { 
            handleSetSpan(MODE_SIZE);
        } 
 
        private void handleColor() { 
            handleSetSpan(MODE_COLOR);
        } 
 
        private void handleAlign() { 
            handleSetSpan(MODE_ALIGN);
        } 
 
        private void handleSelect() { 
            if (DBG) {
                Log.d(LOG_TAG, "--- handleSelect:" + mEditFlag + "," + mState);
            } 
            if (!mEditFlag) {
                return; 
            } 
            if (mState == STATE_SELECT_OFF) {
                if (isTextSelected()) { 
                    Log.e(LOG_TAG, "Selection is off, but selected");
                } 
                setSelectStartPos(); 
                blockSoftKey(); 
                mEST.setHintMessage(HINT_MSG_SELECT_END);
            } else if (mState == STATE_SELECT_ON) {
                if (isTextSelected()) { 
                    Log.e(LOG_TAG, "Selection now start, but selected");
                } 
                setSelectedEndPos(); 
                mEST.setHintMessage(HINT_MSG_PUSH_COMPETE);
                doNextHandle(); 
            } else if (mState == STATE_SELECTED) {
                if (!isTextSelected()) { 
                    Log.e(LOG_TAG, "Selection is done, but not selected");
                } 
                setSelectedEndPos(); 
                doNextHandle(); 
            } 
        } 
 
        private void handleSelectAll() { 
            if (DBG) {
                Log.d(LOG_TAG, "--- handleSelectAll");
            } 
            if (!mEditFlag) {
                return; 
            } 
            mEST.selectAll();
            mState = STATE_SELECTED;
        } 
 
        private void handleResetEdit() { 
            if (DBG) {
                Log.d(LOG_TAG, "Reset Editor");
            } 
            blockSoftKey(); 
            handleCancel(); 
            mEditFlag = true;
            mEST.setHintMessage(HINT_MSG_SELECT_START);
        } 
 
        private void setSelection() { 
            if (DBG) {
                Log.d(LOG_TAG, "--- onSelect:" + mCurStart + "," + mCurEnd);
            } 
            if (mCurStart >= 0 && mCurStart <= mEST.getText().length()
                    && mCurEnd >= 0 && mCurEnd <= mEST.getText().length()) {
                if (mCurStart < mCurEnd) {
                    mEST.setSelection(mCurStart, mCurEnd);
                } else { 
                    mEST.setSelection(mCurEnd, mCurStart);
                } 
                mState = STATE_SELECTED;
            } else { 
                Log.e(LOG_TAG,
                        "Select is on, but cursor positions are illigal.:" 
                                + mEST.getText().length() + "," + mCurStart
                                + "," + mCurEnd);
            } 
        } 
 
        private void unsetSelect() { 
            if (DBG) {
                Log.d(LOG_TAG, "--- offSelect");
            } 
            int currpos = mEST.getSelectionStart();
            mEST.setSelection(currpos, currpos);
            mState = STATE_SELECT_OFF;
        } 
 
        private void setSelectStartPos() { 
            if (DBG) {
                Log.d(LOG_TAG, "--- setSelectStartPos");
            } 
            mCurStart = mEST.getSelectionStart();
            mState = STATE_SELECT_ON;
        } 
 
        private void setSelectedEndPos() { 
            if (DBG) {
                Log.d(LOG_TAG, "--- setSelectEndPos:");
            } 
            if (mEST.getSelectionStart() == mCurStart) {
                setSelectedEndPos(mEST.getSelectionEnd());
            } else { 
                setSelectedEndPos(mEST.getSelectionStart());
            } 
        } 
 
        public void setSelectedEndPos(int pos) {
            if (DBG) {
                Log.d(LOG_TAG, "--- setSelectedEndPos:");
            } 
            mCurEnd = pos;
            setSelection(); 
        } 
 
        private boolean isTextSelected() { 
            if (DBG) {
                Log.d(LOG_TAG, "--- isTextSelected:" + mCurStart + ","
                        + mCurEnd);
            } 
            return (mCurStart != mCurEnd)
                    && (mState == STATE_SELECTED ||
                            mState == STATE_SELECT_FIX);
        } 
 
        private void setStyledTextSpan(Object span, int start, int end) {
            if (DBG) {
                Log.d(LOG_TAG, "--- setStyledTextSpan:" + mMode + ","
                        + start + "," + end);
            } 
            if (start < end) {
                mEST.getText().setSpan(span, start, end,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else { 
                mEST.getText().setSpan(span, end, start,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } 
        } 
 
        private void changeSizeSelectedText(int size) {
            if (DBG) {
                Log.d(LOG_TAG, "--- changeSize:" + size);
            } 
            setStyledTextSpan(new AbsoluteSizeSpan(size),
                mCurStart, mCurEnd);
        } 
 
        private void changeColorSelectedText(int color) {
            if (DBG) {
                Log.d(LOG_TAG, "--- changeColor:" + color);
            } 
            setStyledTextSpan(new ForegroundColorSpan(color),
                mCurStart, mCurEnd);
        } 
 
        private void changeAlign(Layout.Alignment align) {
            if (DBG) {
                Log.d(LOG_TAG, "--- changeAlign:" + align);
            } 
            setStyledTextSpan(new AlignmentSpan.Standard(align),
                    findLineStart(mEST.getText(), mCurStart),
                    findLineEnd(mEST.getText(), mCurEnd));
        } 
 
        private int findLineStart(Editable text, int current) {
            if (DBG) {
                Log.d(LOG_TAG, "--- findLineStart: curr:" + current +
                        ", length:" + text.length());
            } 
            int pos = current;
            for (; pos > 0; pos--) {
                if (text.charAt(pos - 1) == '\n') {
                    break; 
                } 
            } 
            return pos;
        } 
 
        private void insertImageSpan(ImageSpan span) {
            if (DBG) {
                Log.d(LOG_TAG, "--- insertImageSpan");
            } 
            if (span != null) {
                Log.d(LOG_TAG, "--- insertimagespan:" + span.getDrawable().getIntrinsicHeight() + "," + span.getDrawable().getIntrinsicWidth());
                Log.d(LOG_TAG, "--- insertimagespan:" + span.getDrawable().getClass());
                int curpos = mEST.getSelectionStart();
                mEST.getText().insert(curpos, "\uFFFC");
                mEST.getText().setSpan(span, curpos, curpos + 1,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                mEST.notifyStateChanged(mMode, mState);
            } else { 
                Log.e(LOG_TAG, "--- insertImageSpan: null span was inserted");
            }