Search in sources :

Example 26 with Editable

use of android.text.Editable in project android_frameworks_base by ResurrectionRemix.

the class SpellChecker method spellCheck.

private void spellCheck() {
    if (mSpellCheckerSession == null)
        return;
    Editable editable = (Editable) mTextView.getText();
    final int selectionStart = Selection.getSelectionStart(editable);
    final int selectionEnd = Selection.getSelectionEnd(editable);
    TextInfo[] textInfos = new TextInfo[mLength];
    int textInfosCount = 0;
    for (int i = 0; i < mLength; i++) {
        final SpellCheckSpan spellCheckSpan = mSpellCheckSpans[i];
        if (mIds[i] < 0 || spellCheckSpan.isSpellCheckInProgress())
            continue;
        final int start = editable.getSpanStart(spellCheckSpan);
        final int end = editable.getSpanEnd(spellCheckSpan);
        // Do not check this word if the user is currently editing it
        final boolean isEditing;
        // Defer spell check when typing a word with an interior apostrophe.
        // TODO: a better solution to this would be to make the word
        // iterator locale-sensitive and include the apostrophe in
        // languages that use it (such as English).
        final boolean apostrophe = (selectionStart == end + 1 && editable.charAt(end) == '\'');
        if (mIsSentenceSpellCheckSupported) {
            // Allow the overlap of the cursor and the first boundary of the spell check span
            // no to skip the spell check of the following word because the
            // following word will never be spell-checked even if the user finishes composing
            isEditing = !apostrophe && (selectionEnd <= start || selectionStart > end);
        } else {
            isEditing = !apostrophe && (selectionEnd < start || selectionStart > end);
        }
        if (start >= 0 && end > start && isEditing) {
            spellCheckSpan.setSpellCheckInProgress(true);
            final TextInfo textInfo = new TextInfo(editable, start, end, mCookie, mIds[i]);
            textInfos[textInfosCount++] = textInfo;
            if (DBG) {
                Log.d(TAG, "create TextInfo: (" + i + "/" + mLength + ") text = " + textInfo.getSequence() + ", cookie = " + mCookie + ", seq = " + mIds[i] + ", sel start = " + selectionStart + ", sel end = " + selectionEnd + ", start = " + start + ", end = " + end);
            }
        }
    }
    if (textInfosCount > 0) {
        if (textInfosCount < textInfos.length) {
            TextInfo[] textInfosCopy = new TextInfo[textInfosCount];
            System.arraycopy(textInfos, 0, textInfosCopy, 0, textInfosCount);
            textInfos = textInfosCopy;
        }
        if (mIsSentenceSpellCheckSupported) {
            mSpellCheckerSession.getSentenceSuggestions(textInfos, SuggestionSpan.SUGGESTIONS_MAX_SIZE);
        } else {
            mSpellCheckerSession.getSuggestions(textInfos, SuggestionSpan.SUGGESTIONS_MAX_SIZE, false);
        }
    }
}
Also used : SpellCheckSpan(android.text.style.SpellCheckSpan) TextInfo(android.view.textservice.TextInfo) Editable(android.text.Editable)

Example 27 with Editable

use of android.text.Editable in project Tusky by Vavassor.

the class ComposeActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_compose);
    ButterKnife.bind(this);
    // Setup the toolbar.
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setTitle(null);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
        Drawable closeIcon = AppCompatResources.getDrawable(this, R.drawable.ic_close_24dp);
        ThemeUtils.setDrawableTint(this, closeIcon, R.attr.compose_close_button_tint);
        actionBar.setHomeAsUpIndicator(closeIcon);
    }
    // Setup the interface buttons.
    floatingBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            onSendClicked();
        }
    });
    pickBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            onMediaPick();
        }
    });
    takeBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            initiateCameraApp();
        }
    });
    nsfwBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            toggleNsfw();
        }
    });
    visibilityBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showComposeOptions();
        }
    });
    /* Initialise all the state, or restore it from a previous run, to determine a "starting"
         * state. */
    SharedPreferences preferences = getPrivatePreferences();
    String startingVisibility;
    boolean startingHideText;
    String startingContentWarning = null;
    ArrayList<SavedQueuedMedia> savedMediaQueued = null;
    if (savedInstanceState != null) {
        showMarkSensitive = savedInstanceState.getBoolean("showMarkSensitive");
        startingVisibility = savedInstanceState.getString("statusVisibility");
        statusMarkSensitive = savedInstanceState.getBoolean("statusMarkSensitive");
        startingHideText = savedInstanceState.getBoolean("statusHideText");
        // Keep these until everything needed to put them in the queue is finished initializing.
        savedMediaQueued = savedInstanceState.getParcelableArrayList("savedMediaQueued");
        // These are for restoring an in-progress commit content operation.
        InputContentInfoCompat previousInputContentInfo = InputContentInfoCompat.wrap(savedInstanceState.getParcelable("commitContentInputContentInfo"));
        int previousFlags = savedInstanceState.getInt("commitContentFlags");
        if (previousInputContentInfo != null) {
            onCommitContentInternal(previousInputContentInfo, previousFlags);
        }
    } else {
        showMarkSensitive = false;
        startingVisibility = preferences.getString("rememberedVisibility", "public");
        statusMarkSensitive = false;
        startingHideText = false;
    }
    /* If the composer is started up as a reply to another post, override the "starting" state
         * based on what the intent from the reply request passes. */
    Intent intent = getIntent();
    String[] mentionedUsernames = null;
    inReplyToId = null;
    if (intent != null) {
        inReplyToId = intent.getStringExtra("in_reply_to_id");
        String replyVisibility = intent.getStringExtra("reply_visibility");
        if (replyVisibility != null && startingVisibility != null) {
            // Lowest possible visibility setting in response
            if (startingVisibility.equals("direct") || replyVisibility.equals("direct")) {
                startingVisibility = "direct";
            } else if (startingVisibility.equals("private") || replyVisibility.equals("private")) {
                startingVisibility = "private";
            } else if (startingVisibility.equals("unlisted") || replyVisibility.equals("unlisted")) {
                startingVisibility = "unlisted";
            } else {
                startingVisibility = replyVisibility;
            }
        }
        mentionedUsernames = intent.getStringArrayExtra("mentioned_usernames");
        if (inReplyToId != null) {
            startingHideText = !intent.getStringExtra("content_warning").equals("");
            if (startingHideText) {
                startingContentWarning = intent.getStringExtra("content_warning");
            }
        }
    }
    /* If the currently logged in account is locked, its posts should default to private. This
         * should override even the reply settings, so this must be done after those are set up. */
    if (preferences.getBoolean("loggedInAccountLocked", false)) {
        startingVisibility = "private";
    }
    // After the starting state is finalised, the interface can be set to reflect this state.
    setStatusVisibility(startingVisibility);
    postProgress.setVisibility(View.INVISIBLE);
    updateNsfwButtonColor();
    final ParserUtils parser = new ParserUtils(this);
    // Setup the main text field.
    // new String[] { "image/gif", "image/webp" }
    setEditTextMimeTypes(null);
    final int mentionColour = ThemeUtils.getColor(this, R.attr.compose_mention_color);
    SpanUtils.highlightSpans(textEditor.getText(), mentionColour);
    textEditor.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            updateVisibleCharactersLeft();
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            SpanUtils.highlightSpans(editable, mentionColour);
        }
    });
    textEditor.addOnPasteListener(new EditTextTyped.OnPasteListener() {

        @Override
        public void onPaste() {
            parser.getPastedURLText(ComposeActivity.this);
        }
    });
    // Add any mentions to the text field when a reply is first composed.
    if (mentionedUsernames != null) {
        StringBuilder builder = new StringBuilder();
        for (String name : mentionedUsernames) {
            builder.append('@');
            builder.append(name);
            builder.append(' ');
        }
        textEditor.setText(builder);
        textEditor.setSelection(textEditor.length());
    }
    // Initialise the content warning editor.
    contentWarningEditor.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            updateVisibleCharactersLeft();
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
    showContentWarning(startingHideText);
    if (startingContentWarning != null) {
        contentWarningEditor.setText(startingContentWarning);
    }
    // Initialise the empty media queue state.
    mediaQueued = new ArrayList<>();
    waitForMediaLatch = new CountUpDownLatch();
    statusAlreadyInFlight = false;
    // These can only be added after everything affected by the media queue is initialized.
    if (savedMediaQueued != null) {
        for (SavedQueuedMedia item : savedMediaQueued) {
            addMediaToQueue(item.type, item.preview, item.uri, item.mediaSize);
        }
    } else if (intent != null && savedInstanceState == null) {
        /* Get incoming images being sent through a share action from another app. Only do this
             * when savedInstanceState is null, otherwise both the images from the intent and the
             * instance state will be re-queued. */
        String type = intent.getType();
        if (type != null) {
            if (type.startsWith("image/")) {
                List<Uri> uriList = new ArrayList<>();
                switch(intent.getAction()) {
                    case Intent.ACTION_SEND:
                        {
                            Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
                            if (uri != null) {
                                uriList.add(uri);
                            }
                            break;
                        }
                    case Intent.ACTION_SEND_MULTIPLE:
                        {
                            ArrayList<Uri> list = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
                            if (list != null) {
                                for (Uri uri : list) {
                                    if (uri != null) {
                                        uriList.add(uri);
                                    }
                                }
                            }
                            break;
                        }
                }
                for (Uri uri : uriList) {
                    long mediaSize = getMediaSize(getContentResolver(), uri);
                    pickMedia(uri, mediaSize);
                }
            } else if (type.equals("text/plain")) {
                String action = intent.getAction();
                if (action != null && action.equals(Intent.ACTION_SEND)) {
                    String text = intent.getStringExtra(Intent.EXTRA_TEXT);
                    if (text != null) {
                        int start = Math.max(textEditor.getSelectionStart(), 0);
                        int end = Math.max(textEditor.getSelectionEnd(), 0);
                        int left = Math.min(start, end);
                        int right = Math.max(start, end);
                        textEditor.getText().replace(left, right, text, 0, text.length());
                        parser.putInClipboardManager(this, text);
                        textEditor.onPaste();
                    }
                }
            }
        }
    }
}
Also used : SpannableStringBuilder(android.text.SpannableStringBuilder) CountUpDownLatch(com.keylesspalace.tusky.util.CountUpDownLatch) StringUtils.randomAlphanumericString(com.keylesspalace.tusky.util.StringUtils.randomAlphanumericString) Uri(android.net.Uri) InputContentInfoCompat(android.support.v13.view.inputmethod.InputContentInfoCompat) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) List(java.util.List) ArrayList(java.util.ArrayList) ParserUtils(com.keylesspalace.tusky.util.ParserUtils) ActionBar(android.support.v7.app.ActionBar) Toolbar(android.support.v7.widget.Toolbar) SharedPreferences(android.content.SharedPreferences) Drawable(android.graphics.drawable.Drawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Intent(android.content.Intent) ImageView(android.widget.ImageView) BindView(butterknife.BindView) View(android.view.View) TextView(android.widget.TextView) EditTextTyped(com.keylesspalace.tusky.view.EditTextTyped)

Example 28 with Editable

use of android.text.Editable in project Tusky by Vavassor.

the class ComposeActivity method removeMediaFromQueue.

private void removeMediaFromQueue(QueuedMedia item) {
    mediaPreviewBar.removeView(item.preview);
    mediaQueued.remove(item);
    if (mediaQueued.size() == 0) {
        showMarkSensitive(false);
        /* If there are no image previews to show, the extra padding that was added to the
             * EditText can be removed so there isn't unnecessary empty space. */
        textEditor.setPadding(textEditor.getPaddingLeft(), textEditor.getPaddingTop(), textEditor.getPaddingRight(), 0);
    }
    // Remove the text URL associated with this media.
    if (item.uploadUrl != null) {
        Editable text = textEditor.getText();
        int start = text.getSpanStart(item.uploadUrl);
        int end = text.getSpanEnd(item.uploadUrl);
        if (start != -1 && end != -1) {
            text.delete(start, end);
        }
    }
    enableMediaButtons();
    cancelReadyingMedia(item);
}
Also used : Editable(android.text.Editable)

Example 29 with Editable

use of android.text.Editable in project LshUtils by SenhLinsh.

the class ViewUtils method setEditTextSelectionToEnd.

/**
     * 设置输入框的光标到末尾
     */
public static final void setEditTextSelectionToEnd(EditText editText) {
    Editable editable = editText.getEditableText();
    Selection.setSelection((Spannable) editable, editable.toString().length());
}
Also used : Editable(android.text.Editable)

Example 30 with Editable

use of android.text.Editable in project Rocket.Chat.Android by RocketChat.

the class MessageFormLayout method init.

private void init() {
    composer = (ViewGroup) LayoutInflater.from(getContext()).inflate(R.layout.message_composer, this, false);
    btnExtra = composer.findViewById(R.id.btn_extras);
    btnExtra.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            onExtraActionSelectionClick();
        }
    });
    btnSubmit = composer.findViewById(R.id.btn_submit);
    btnSubmit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            String messageText = getText();
            if (messageText.length() > 0 && submitTextListener != null) {
                submitTextListener.onSubmitText(messageText);
            }
        }
    });
    btnSubmit.setScaleX(0);
    btnSubmit.setScaleY(0);
    btnSubmit.setVisibility(GONE);
    ImageKeyboardEditText editText = (ImageKeyboardEditText) composer.findViewById(R.id.editor);
    editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (TextUtils.getTrimmedLength(s) > 0) {
                animateHide(btnExtra);
                animateShow(btnSubmit);
            } else {
                animateShow(btnExtra);
                animateHide(btnSubmit);
            }
        }
    });
    editText.setContentListener(new ImageKeyboardEditText.OnCommitContentListener() {

        @Override
        public boolean onCommitContent(InputContentInfoCompat inputContentInfo, int flags, Bundle opts, String[] supportedMimeTypes) {
            if (listener != null) {
                return listener.onCommitContent(inputContentInfo, flags, opts, supportedMimeTypes);
            }
            return false;
        }
    });
    addView(composer);
}
Also used : Bundle(android.os.Bundle) TextView(android.widget.TextView) View(android.view.View) InputContentInfoCompat(android.support.v13.view.inputmethod.InputContentInfoCompat) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable)

Aggregations

Editable (android.text.Editable)881 TextWatcher (android.text.TextWatcher)414 View (android.view.View)293 TextView (android.widget.TextView)217 EditText (android.widget.EditText)144 Paint (android.graphics.Paint)102 ImageView (android.widget.ImageView)94 Intent (android.content.Intent)81 AdapterView (android.widget.AdapterView)81 KeyEvent (android.view.KeyEvent)74 TextPaint (android.text.TextPaint)69 InputMethodManager (android.view.inputmethod.InputMethodManager)64 ListView (android.widget.ListView)64 DialogInterface (android.content.DialogInterface)57 SuppressLint (android.annotation.SuppressLint)56 Spannable (android.text.Spannable)51 AlertDialog (android.app.AlertDialog)46 RecyclerView (android.support.v7.widget.RecyclerView)45 ArrayList (java.util.ArrayList)43 Button (android.widget.Button)41