Example usage for android.text SpannableStringBuilder valueOf

List of usage examples for android.text SpannableStringBuilder valueOf

Introduction

In this page you can find the example usage for android.text SpannableStringBuilder valueOf.

Prototype

public static SpannableStringBuilder valueOf(CharSequence source) 

Source Link

Usage

From source file:com.renard.documentview.DocumentAdapter.java

@Override
public Object instantiateItem(final View collection, final int position) {
    View view = null;/* ww w.j a  va  2  s. c  o m*/
    if (mCursor.moveToPosition(position)) {
        final int documentId = mCursor.getInt(mIndexId);
        Spanned spanned = mSpannedTexts.get(documentId);

        if (spanned == null) {
            final String text = mCursor.getString(mIndexOCRText);
            if (text == null) {
                spanned = SpannableStringBuilder.valueOf("");
            } else {
                spanned = Html.fromHtml(text);
            }
            mSpannedTexts.put(documentId, spanned);
        }
        view = mInflater.inflate(R.layout.document_fragment, null);
        EditText edit = (EditText) view.findViewById(R.id.editText_document);
        edit.setText(spanned);
        TextWatcher watcher = new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

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

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mChangedDocuments.add(documentId);
                mChangedTexts.put(documentId, s);
            }
        };
        edit.addTextChangedListener(watcher);

        ((ViewPager) collection).addView(view);
        PreferencesUtils.applyTextPreferences(edit, mContext);
    }
    return view;
}

From source file:eu.veldsoft.adsbobball.ActivityStateEnum.java

public SpannableStringBuilder formatPerPlayer(String fixed, playstat query) {
    SpannableStringBuilder sps = SpannableStringBuilder.valueOf(fixed);

    for (Player p : gameManager.getCurrGameState().getPlayers()) {
        if (p.getPlayerId() == 0)
            continue;
        SpannableString s = new SpannableString(String.valueOf(query.call(p)) + " ");
        s.setSpan(new ForegroundColorSpan(p.getColor()), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        sps.append(s);/*  w w  w . j av a2 s  .  co m*/
    }
    return sps;
}

From source file:eu.veldsoft.adsbobball.ActivityStateEnum.java

protected void update(final Canvas canvas, final GameState currGameState, long frameCounter) {

    final Player currPlayer = currGameState.getPlayer(playerId);

    if ((gameView != null)) {
        gameView.draw(canvas, currGameState);
    }/*from   www .java  2  s .c  om*/

    if (frameCounter % displayLoop.ITERATIONS_PER_STATUSUPDATE == 0) {

        SpannableStringBuilder timeLeftStr = SpannableStringBuilder
                .valueOf(getString(R.string.timeLeftLabel, gameManager.timeLeft() / 10));

        SpannableStringBuilder livesStr = formatPerPlayer(getString(R.string.livesLabel), new playstat() {
            @Override
            public int call(Player p) {
                return p.getLives();
            }
        });
        SpannableStringBuilder scoreStr = formatPerPlayer(getString(R.string.scoreLabel), new playstat() {
            @Override
            public int call(Player p) {
                return p.getScore();
            }
        });

        SpannableStringBuilder clearedStr = formatPerPlayer(getString(R.string.areaClearedLabel),
                new playstat() {
                    @Override
                    public int call(Player p) {
                        Grid grid = currGameState.getGrid();
                        if (grid != null)
                            return currGameState.getGrid().getPercentComplete(p.getPlayerId());
                        else
                            return 0;
                    }
                });

        // display fps
        if (secretHandshake >= 3) {

            float fps = displayLoop.getFPS();
            int color = (fps < NUMBER_OF_FRAMES_PER_SECOND * 0.98f ? Color.RED : Color.GREEN);
            SpannableString s = new SpannableString(String.format(" FPS: %2.1f", fps));

            s.setSpan(new ForegroundColorSpan(color), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            timeLeftStr.append(s);

            color = (gameManager.getUPS() < gameManager.NUMBER_OF_UPDATES_PER_SECOND * 0.98f ? Color.RED
                    : Color.GREEN);
            s = new SpannableString(String.format(" UPS: %3.1f", gameManager.getUPS()));

            s.setSpan(new ForegroundColorSpan(color), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            timeLeftStr.append(s);
        }

        statusTopleft.setText(timeLeftStr);
        statusTopright.setText(livesStr);
        statusBotleft.setText(scoreStr);
        statusBotright.setText(clearedStr);

    }
    if (gameManager.hasWonLevel()) {
        showWonScreen();
    } else if (gameManager.isGameLost()) {
        if ((numPlayers == 1) && scores.isTopScore(currPlayer.getScore())) {
            promptUsername();
        }
        showDeadScreen();
    }

}

From source file:org.bobstuff.bobball.ActivityStateEnum.java

private void updateStatus(final GameState currGameState) {

    SpannableStringBuilder timeLeftStr = SpannableStringBuilder
            .valueOf(getString(R.string.timeLeftLabel, gameManager.timeLeft() / 10));

    SpannableStringBuilder livesStr = formatPerPlayer(getString(R.string.livesLabel), new playstat() {
        @Override//from   w  ww.j ava2  s .  c o  m
        public int call(Player p) {
            return p.getLives();
        }
    });
    SpannableStringBuilder scoreStr = formatPerPlayer(getString(R.string.scoreLabel), new playstat() {
        @Override
        public int call(Player p) {
            return p.getScore();
        }
    });

    SpannableStringBuilder clearedStr = formatPerPlayer(getString(R.string.areaClearedLabel), new playstat() {
        @Override
        public int call(Player p) {
            Grid grid = currGameState.getGrid();
            if (grid != null)
                return currGameState.getGrid().getPercentComplete(p.getPlayerId());
            else
                return 0;
        }
    });

    //display fps
    if (secretHandshake >= 3) {

        float fps = displayLoop.getFPS();
        int color = (fps < NUMBER_OF_FRAMES_PER_SECOND * 0.98f ? Color.RED : Color.GREEN);
        SpannableString s = new SpannableString(String.format(" FPS: %2.1f", fps));

        s.setSpan(new ForegroundColorSpan(color), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        timeLeftStr.append(s);

        color = (gameManager.getUPS() < gameManager.NUMBER_OF_UPDATES_PER_SECOND * 0.98f ? Color.RED
                : Color.GREEN);
        s = new SpannableString(String.format(" UPS: %3.1f", gameManager.getUPS()));

        s.setSpan(new ForegroundColorSpan(color), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        timeLeftStr.append(s);
    }

    statusTopleft.setText(timeLeftStr);
    statusTopright.setText(livesStr);
    statusBotleft.setText(scoreStr);
    statusBotright.setText(clearedStr);
}

From source file:tv.acfun.video.CommentsActivity.java

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    int count = mAdapter.getCount();
    if (position > count) {
        if (isreload) {
            mFootview.findViewById(R.id.list_footview_progress).setVisibility(View.VISIBLE);
            TextView textview = (TextView) mFootview.findViewById(R.id.list_footview_text);
            textview.setText(R.string.buffering);
            requestData(pageIndex, false);
        }//w  w  w  . j  a  v  a  2 s. c  o m
        return;
    }
    //        showBar(); //TODO: show input bar when selected comment
    Object o = parent.getItemAtPosition(position);
    if (o == null || !(o instanceof Comment))
        return;
    Comment c = (Comment) o;
    int quoteCount = getQuoteCount();
    removeQuote(mCommentText.getText());
    if (quoteCount == c.count)
        return; // ?
    String pre = ":#" + c.count;
    mQuoteSpan = new Quote(c.count);
    SpannableStringBuilder sb = SpannableStringBuilder.valueOf(mCommentText.getText());
    TextView tv = TextViewUtils.createBubbleTextView(this, pre);
    BitmapDrawable bd = (BitmapDrawable) TextViewUtils.convertViewToDrawable(tv);
    bd.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());
    sb.insert(0, pre);
    mQuoteImage = new ImageSpan(bd);
    sb.setSpan(mQuoteImage, 0, pre.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sb.setSpan(mQuoteSpan, 0, pre.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sb.append("");
    mCommentText.setText(sb);
    mCommentText.setSelection(mCommentText.getText().length());
}

From source file:org.telegram.ui.Cells.DialogCell.java

public void buildLayout() {
    String nameString = "";
    String timeString = "";
    String countString = null;//from ww  w  . j  a  va  2  s  .com
    CharSequence messageString = "";
    CharSequence printingString = null;
    if (isDialogCell) {
        printingString = MessagesController.getInstance().printingStrings.get(currentDialogId);
    }
    TextPaint currentNamePaint = namePaint;
    TextPaint currentMessagePaint = messagePaint;
    boolean checkMessage = true;

    drawNameGroup = false;
    drawNameBroadcast = false;
    drawNameLock = false;
    drawNameBot = false;
    drawVerified = false;

    if (encryptedChat != null) {
        drawNameLock = true;
        nameLockTop = AndroidUtilities.dp(16.5f);
        if (!LocaleController.isRTL) {
            nameLockLeft = AndroidUtilities.dp(AndroidUtilities.leftBaseline);
            nameLeft = AndroidUtilities.dp(AndroidUtilities.leftBaseline + 4)
                    + lockDrawable.getIntrinsicWidth();
        } else {
            nameLockLeft = getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.leftBaseline)
                    - lockDrawable.getIntrinsicWidth();
            nameLeft = AndroidUtilities.dp(14);
        }
    } else {
        if (chat != null) {
            if (chat.id < 0 || ChatObject.isChannel(chat) && !chat.megagroup) {
                drawNameBroadcast = true;
                nameLockTop = AndroidUtilities.dp(16.5f);
            } else {
                drawNameGroup = true;
                nameLockTop = AndroidUtilities.dp(17.5f);
            }
            drawVerified = chat.verified;

            if (!LocaleController.isRTL) {
                nameLockLeft = AndroidUtilities.dp(AndroidUtilities.leftBaseline);
                nameLeft = AndroidUtilities.dp(AndroidUtilities.leftBaseline + 4)
                        + (drawNameGroup ? groupDrawable.getIntrinsicWidth()
                                : broadcastDrawable.getIntrinsicWidth());
            } else {
                nameLockLeft = getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.leftBaseline)
                        - (drawNameGroup ? groupDrawable.getIntrinsicWidth()
                                : broadcastDrawable.getIntrinsicWidth());
                nameLeft = AndroidUtilities.dp(14);
            }
        } else {
            if (!LocaleController.isRTL) {
                nameLeft = AndroidUtilities.dp(AndroidUtilities.leftBaseline);
            } else {
                nameLeft = AndroidUtilities.dp(14);
            }
            if (user != null) {
                if (user.bot) {
                    drawNameBot = true;
                    nameLockTop = AndroidUtilities.dp(16.5f);
                    if (!LocaleController.isRTL) {
                        nameLockLeft = AndroidUtilities.dp(AndroidUtilities.leftBaseline);
                        nameLeft = AndroidUtilities.dp(AndroidUtilities.leftBaseline + 4)
                                + botDrawable.getIntrinsicWidth();
                    } else {
                        nameLockLeft = getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.leftBaseline)
                                - botDrawable.getIntrinsicWidth();
                        nameLeft = AndroidUtilities.dp(14);
                    }
                }
                drawVerified = user.verified;
            }
        }
    }

    int lastDate = lastMessageDate;
    if (lastMessageDate == 0 && message != null) {
        lastDate = message.messageOwner.date;
    }

    if (isDialogCell) {
        draftMessage = DraftQuery.getDraft(currentDialogId);
        if (draftMessage != null
                && (TextUtils.isEmpty(draftMessage.message) && draftMessage.reply_to_msg_id == 0
                        || lastDate > draftMessage.date && unreadCount != 0)
                || ChatObject.isChannel(chat) && !chat.megagroup && !chat.creator && !chat.editor
                || chat != null && (chat.left || chat.kicked)) {
            draftMessage = null;
        }
    } else {
        draftMessage = null;
    }

    if (printingString != null) {
        lastPrintString = messageString = printingString;
        currentMessagePaint = messagePrintingPaint;
    } else {
        lastPrintString = null;

        if (draftMessage != null) {
            checkMessage = false;
            if (TextUtils.isEmpty(draftMessage.message)) {
                String draftString = LocaleController.getString("Draft", R.string.Draft);
                SpannableStringBuilder stringBuilder = SpannableStringBuilder.valueOf(draftString);
                stringBuilder.setSpan(new ForegroundColorSpan(Theme.DIALOGS_DRAFT_TEXT_COLOR), 0,
                        draftString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                messageString = stringBuilder;
            } else {
                String mess = draftMessage.message;
                if (mess.length() > 150) {
                    mess = mess.substring(0, 150);
                }
                String draftString = LocaleController.getString("Draft", R.string.Draft);
                SpannableStringBuilder stringBuilder = SpannableStringBuilder
                        .valueOf(String.format("%s: %s", draftString, mess.replace('\n', ' ')));
                stringBuilder.setSpan(new ForegroundColorSpan(Theme.DIALOGS_DRAFT_TEXT_COLOR), 0,
                        draftString.length() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                messageString = Emoji.replaceEmoji(stringBuilder, messagePaint.getFontMetricsInt(),
                        AndroidUtilities.dp(20), false);
            }
        } else {
            if (message == null) {
                if (encryptedChat != null) {
                    currentMessagePaint = messagePrintingPaint;
                    if (encryptedChat instanceof TLRPC.TL_encryptedChatRequested) {
                        messageString = LocaleController.getString("EncryptionProcessing",
                                R.string.EncryptionProcessing);
                    } else if (encryptedChat instanceof TLRPC.TL_encryptedChatWaiting) {
                        if (user != null && user.first_name != null) {
                            messageString = LocaleController.formatString("AwaitingEncryption",
                                    R.string.AwaitingEncryption, user.first_name);
                        } else {
                            messageString = LocaleController.formatString("AwaitingEncryption",
                                    R.string.AwaitingEncryption, "");
                        }
                    } else if (encryptedChat instanceof TLRPC.TL_encryptedChatDiscarded) {
                        messageString = LocaleController.getString("EncryptionRejected",
                                R.string.EncryptionRejected);
                    } else if (encryptedChat instanceof TLRPC.TL_encryptedChat) {
                        if (encryptedChat.admin_id == UserConfig.getClientUserId()) {
                            if (user != null && user.first_name != null) {
                                messageString = LocaleController.formatString("EncryptedChatStartedOutgoing",
                                        R.string.EncryptedChatStartedOutgoing, user.first_name);
                            } else {
                                messageString = LocaleController.formatString("EncryptedChatStartedOutgoing",
                                        R.string.EncryptedChatStartedOutgoing, "");
                            }
                        } else {
                            messageString = LocaleController.getString("EncryptedChatStartedIncoming",
                                    R.string.EncryptedChatStartedIncoming);
                        }
                    }
                }
            } else {
                TLRPC.User fromUser = null;
                TLRPC.Chat fromChat = null;
                if (message.isFromUser()) {
                    fromUser = MessagesController.getInstance().getUser(message.messageOwner.from_id);
                } else {
                    fromChat = MessagesController.getInstance().getChat(message.messageOwner.to_id.channel_id);
                }
                if (message.messageOwner instanceof TLRPC.TL_messageService) {
                    messageString = message.messageText;
                    currentMessagePaint = messagePrintingPaint;
                } else {
                    if (chat != null && chat.id > 0 && fromChat == null) {
                        String name;
                        if (message.isOutOwner()) {
                            name = LocaleController.getString("FromYou", R.string.FromYou);
                        } else if (fromUser != null) {
                            name = UserObject.getFirstName(fromUser).replace("\n", "");
                        } else if (fromChat != null) {
                            name = fromChat.title.replace("\n", "");
                        } else {
                            name = "DELETED";
                        }
                        checkMessage = false;
                        SpannableStringBuilder stringBuilder;
                        if (message.caption != null) {
                            String mess = message.caption.toString();
                            if (mess.length() > 150) {
                                mess = mess.substring(0, 150);
                            }
                            stringBuilder = SpannableStringBuilder
                                    .valueOf(String.format("%s: %s", name, mess.replace('\n', ' ')));
                        } else if (message.messageOwner.media != null && !message.isMediaEmpty()) {
                            currentMessagePaint = messagePrintingPaint;
                            if (message.messageOwner.media instanceof TLRPC.TL_messageMediaGame) {
                                stringBuilder = SpannableStringBuilder.valueOf(String.format("%s: %s", name,
                                        "\uD83C\uDFAE " + message.messageOwner.media.game.title));
                            } else {
                                stringBuilder = SpannableStringBuilder
                                        .valueOf(String.format("%s: %s", name, message.messageText));
                            }
                            stringBuilder.setSpan(new ForegroundColorSpan(Theme.DIALOGS_ATTACH_TEXT_COLOR),
                                    name.length() + 2, stringBuilder.length(),
                                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        } else if (message.messageOwner.message != null) {
                            String mess = message.messageOwner.message;
                            if (mess.length() > 150) {
                                mess = mess.substring(0, 150);
                            }
                            stringBuilder = SpannableStringBuilder
                                    .valueOf(String.format("%s: %s", name, mess.replace('\n', ' ')));
                        } else {
                            stringBuilder = SpannableStringBuilder.valueOf("");
                        }
                        if (stringBuilder.length() > 0) {
                            stringBuilder.setSpan(new ForegroundColorSpan(Theme.DIALOGS_NAME_TEXT_COLOR), 0,
                                    name.length() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        }
                        messageString = Emoji.replaceEmoji(stringBuilder, messagePaint.getFontMetricsInt(),
                                AndroidUtilities.dp(20), false);
                    } else {
                        if (message.caption != null) {
                            messageString = message.caption;
                        } else {
                            if (message.messageOwner.media instanceof TLRPC.TL_messageMediaGame) {
                                messageString = "\uD83C\uDFAE " + message.messageOwner.media.game.title;
                            } else {
                                messageString = message.messageText;
                            }
                            if (message.messageOwner.media != null && !message.isMediaEmpty()) {
                                currentMessagePaint = messagePrintingPaint;
                            }
                        }
                    }
                }
            }
        }
    }

    if (draftMessage != null) {
        timeString = LocaleController.stringForMessageListDate(draftMessage.date);
    } else if (lastMessageDate != 0) {
        timeString = LocaleController.stringForMessageListDate(lastMessageDate);
    } else if (message != null) {
        timeString = LocaleController.stringForMessageListDate(message.messageOwner.date);
    }

    if (message == null) {
        drawCheck1 = false;
        drawCheck2 = false;
        drawClock = false;
        drawCount = false;
        drawError = false;
    } else {
        if (unreadCount != 0) {
            drawCount = true;
            countString = String.format("%d", unreadCount);
        } else {
            drawCount = false;
        }

        if (message.isOut() && draftMessage == null) {
            if (message.isSending()) {
                drawCheck1 = false;
                drawCheck2 = false;
                drawClock = true;
                drawError = false;
            } else if (message.isSendError()) {
                drawCheck1 = false;
                drawCheck2 = false;
                drawClock = false;
                drawError = true;
                drawCount = false;
            } else if (message.isSent()) {
                drawCheck1 = !message.isUnread() || ChatObject.isChannel(chat) && !chat.megagroup;
                drawCheck2 = true;
                drawClock = false;
                drawError = false;
            }
        } else {
            drawCheck1 = false;
            drawCheck2 = false;
            drawClock = false;
            drawError = false;
        }
    }

    int timeWidth = (int) Math.ceil(timePaint.measureText(timeString));
    timeLayout = new StaticLayout(timeString, timePaint, timeWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
            false);
    if (!LocaleController.isRTL) {
        timeLeft = getMeasuredWidth() - AndroidUtilities.dp(15) - timeWidth;
    } else {
        timeLeft = AndroidUtilities.dp(15);
    }

    if (chat != null) {
        nameString = chat.title;
    } else if (user != null) {
        if (user.id == UserConfig.getClientUserId()) {
            nameString = LocaleController.getString("ChatYourSelfName", R.string.ChatYourSelfName);
        } else if (user.id / 1000 != 777 && user.id / 1000 != 333
                && ContactsController.getInstance().contactsDict.get(user.id) == null) {
            if (ContactsController.getInstance().contactsDict.size() == 0
                    && (!ContactsController.getInstance().contactsLoaded
                            || ContactsController.getInstance().isLoadingContacts())) {
                nameString = UserObject.getUserName(user);
            } else {
                if (user.phone != null && user.phone.length() != 0) {
                    nameString = PhoneFormat.getInstance().format("+" + user.phone);
                } else {
                    nameString = UserObject.getUserName(user);
                }
            }
        } else {
            nameString = UserObject.getUserName(user);
        }
        if (encryptedChat != null) {
            currentNamePaint = nameEncryptedPaint;
        }
    }
    if (nameString.length() == 0) {
        nameString = LocaleController.getString("HiddenName", R.string.HiddenName);
    }

    int nameWidth;

    if (!LocaleController.isRTL) {
        nameWidth = getMeasuredWidth() - nameLeft - AndroidUtilities.dp(14) - timeWidth;
    } else {
        nameWidth = getMeasuredWidth() - nameLeft - AndroidUtilities.dp(AndroidUtilities.leftBaseline)
                - timeWidth;
        nameLeft += timeWidth;
    }
    if (drawNameLock) {
        nameWidth -= AndroidUtilities.dp(4) + lockDrawable.getIntrinsicWidth();
    } else if (drawNameGroup) {
        nameWidth -= AndroidUtilities.dp(4) + groupDrawable.getIntrinsicWidth();
    } else if (drawNameBroadcast) {
        nameWidth -= AndroidUtilities.dp(4) + broadcastDrawable.getIntrinsicWidth();
    } else if (drawNameBot) {
        nameWidth -= AndroidUtilities.dp(4) + botDrawable.getIntrinsicWidth();
    }
    if (drawClock) {
        int w = clockDrawable.getIntrinsicWidth() + AndroidUtilities.dp(5);
        nameWidth -= w;
        if (!LocaleController.isRTL) {
            checkDrawLeft = timeLeft - w;
        } else {
            checkDrawLeft = timeLeft + timeWidth + AndroidUtilities.dp(5);
            nameLeft += w;
        }
    } else if (drawCheck2) {
        int w = checkDrawable.getIntrinsicWidth() + AndroidUtilities.dp(5);
        nameWidth -= w;
        if (drawCheck1) {
            nameWidth -= halfCheckDrawable.getIntrinsicWidth() - AndroidUtilities.dp(8);
            if (!LocaleController.isRTL) {
                halfCheckDrawLeft = timeLeft - w;
                checkDrawLeft = halfCheckDrawLeft - AndroidUtilities.dp(5.5f);
            } else {
                checkDrawLeft = timeLeft + timeWidth + AndroidUtilities.dp(5);
                halfCheckDrawLeft = checkDrawLeft + AndroidUtilities.dp(5.5f);
                nameLeft += w + halfCheckDrawable.getIntrinsicWidth() - AndroidUtilities.dp(8);
            }
        } else {
            if (!LocaleController.isRTL) {
                checkDrawLeft = timeLeft - w;
            } else {
                checkDrawLeft = timeLeft + timeWidth + AndroidUtilities.dp(5);
                nameLeft += w;
            }
        }
    }

    if (dialogMuted && !drawVerified) {
        int w = AndroidUtilities.dp(6) + muteDrawable.getIntrinsicWidth();
        nameWidth -= w;
        if (LocaleController.isRTL) {
            nameLeft += w;
        }
    } else if (drawVerified) {
        int w = AndroidUtilities.dp(6) + verifiedDrawable.getIntrinsicWidth();
        nameWidth -= w;
        if (LocaleController.isRTL) {
            nameLeft += w;
        }
    }

    nameWidth = Math.max(AndroidUtilities.dp(12), nameWidth);
    try {
        CharSequence nameStringFinal = TextUtils.ellipsize(nameString.replace('\n', ' '), currentNamePaint,
                nameWidth - AndroidUtilities.dp(12), TextUtils.TruncateAt.END);
        nameLayout = new StaticLayout(nameStringFinal, currentNamePaint, nameWidth,
                Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }

    int messageWidth = getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.leftBaseline + 16);
    int avatarLeft;
    if (!LocaleController.isRTL) {
        messageLeft = AndroidUtilities.dp(AndroidUtilities.leftBaseline);
        avatarLeft = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 13 : 9);
    } else {
        messageLeft = AndroidUtilities.dp(16);
        avatarLeft = getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.isTablet() ? 65 : 61);
    }
    avatarImage.setImageCoords(avatarLeft, avatarTop, AndroidUtilities.dp(52), AndroidUtilities.dp(52));
    if (drawError) {
        int w = errorDrawable.getIntrinsicWidth() + AndroidUtilities.dp(8);
        messageWidth -= w;
        if (!LocaleController.isRTL) {
            errorLeft = getMeasuredWidth() - errorDrawable.getIntrinsicWidth() - AndroidUtilities.dp(11);
        } else {
            errorLeft = AndroidUtilities.dp(11);
            messageLeft += w;
        }
    } else if (countString != null) {
        countWidth = Math.max(AndroidUtilities.dp(12), (int) Math.ceil(countPaint.measureText(countString)));
        countLayout = new StaticLayout(countString, countPaint, countWidth, Layout.Alignment.ALIGN_CENTER, 1.0f,
                0.0f, false);
        int w = countWidth + AndroidUtilities.dp(18);
        messageWidth -= w;
        if (!LocaleController.isRTL) {
            countLeft = getMeasuredWidth() - countWidth - AndroidUtilities.dp(19);
        } else {
            countLeft = AndroidUtilities.dp(19);
            messageLeft += w;
        }
        drawCount = true;
    } else {
        drawCount = false;
    }

    if (checkMessage) {
        if (messageString == null) {
            messageString = "";
        }
        String mess = messageString.toString();
        if (mess.length() > 150) {
            mess = mess.substring(0, 150);
        }
        mess = mess.replace('\n', ' ');
        messageString = Emoji.replaceEmoji(mess, messagePaint.getFontMetricsInt(), AndroidUtilities.dp(17),
                false);
    }
    messageWidth = Math.max(AndroidUtilities.dp(12), messageWidth);
    CharSequence messageStringFinal = TextUtils.ellipsize(messageString, currentMessagePaint,
            messageWidth - AndroidUtilities.dp(12), TextUtils.TruncateAt.END);
    try {
        messageLayout = new StaticLayout(messageStringFinal, currentMessagePaint, messageWidth,
                Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }

    double widthpx;
    float left;
    if (LocaleController.isRTL) {
        if (nameLayout != null && nameLayout.getLineCount() > 0) {
            left = nameLayout.getLineLeft(0);
            widthpx = Math.ceil(nameLayout.getLineWidth(0));
            if (dialogMuted && !drawVerified) {
                nameMuteLeft = (int) (nameLeft + (nameWidth - widthpx) - AndroidUtilities.dp(6)
                        - muteDrawable.getIntrinsicWidth());
            } else if (drawVerified) {
                nameMuteLeft = (int) (nameLeft + (nameWidth - widthpx) - AndroidUtilities.dp(6)
                        - verifiedDrawable.getIntrinsicWidth());
            }
            if (left == 0) {
                if (widthpx < nameWidth) {
                    nameLeft += (nameWidth - widthpx);
                }
            }
        }
        if (messageLayout != null && messageLayout.getLineCount() > 0) {
            left = messageLayout.getLineLeft(0);
            if (left == 0) {
                widthpx = Math.ceil(messageLayout.getLineWidth(0));
                if (widthpx < messageWidth) {
                    messageLeft += (messageWidth - widthpx);
                }
            }
        }
    } else {
        if (nameLayout != null && nameLayout.getLineCount() > 0) {
            left = nameLayout.getLineRight(0);
            if (left == nameWidth) {
                widthpx = Math.ceil(nameLayout.getLineWidth(0));
                if (widthpx < nameWidth) {
                    nameLeft -= (nameWidth - widthpx);
                }
            }
            if (dialogMuted || drawVerified) {
                nameMuteLeft = (int) (nameLeft + left + AndroidUtilities.dp(6));
            }
        }
        if (messageLayout != null && messageLayout.getLineCount() > 0) {
            left = messageLayout.getLineRight(0);
            if (left == messageWidth) {
                widthpx = Math.ceil(messageLayout.getLineWidth(0));
                if (widthpx < messageWidth) {
                    messageLeft -= (messageWidth - widthpx);
                }
            }
        }
    }
}

From source file:tv.acfun.a63.CommentsActivity.java

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    int count = mAdapter.getCount();
    if (position > count) {
        if (isreload) {
            mFootview.findViewById(R.id.list_footview_progress).setVisibility(View.VISIBLE);
            TextView textview = (TextView) mFootview.findViewById(R.id.list_footview_text);
            textview.setText(R.string.loading);
            requestData(pageIndex, false);
        }//  w  w w  .  java  2s .  c o m
        return;
    }
    showBar(); // show input bar when selected comment
    Object o = parent.getItemAtPosition(position);
    if (o == null || !(o instanceof Comment))
        return;
    Comment c = (Comment) o;
    int quoteCount = getQuoteCount();
    removeQuote(mCommentText.getText());
    if (quoteCount == c.count)
        return; // ?
    String pre = ":#" + c.count;
    mQuoteSpan = new Quote(c.count);
    /**
     * @see http 
     *      ://www.kpbird.com/2013/02/android-chips-edittext-token-edittext
     *      .html
     */
    SpannableStringBuilder sb = SpannableStringBuilder.valueOf(mCommentText.getText());
    TextView tv = TextViewUtils.createBubbleTextView(this, pre);
    BitmapDrawable bd = (BitmapDrawable) TextViewUtils.convertViewToDrawable(tv);
    bd.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());
    sb.insert(0, pre);
    mQuoteImage = new ImageSpan(bd);
    sb.setSpan(mQuoteImage, 0, pre.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sb.setSpan(mQuoteSpan, 0, pre.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sb.append("");
    mCommentText.setText(sb);
    mCommentText.setSelection(mCommentText.getText().length());
}

From source file:tv.acfun.a63.CommentsActivity.java

String getComment() {
    Editable text = SpannableStringBuilder.valueOf(mCommentText.getText());
    Quote quote = TextViewUtils.getLast(text, Quote.class);
    int start = text.getSpanStart(quote);
    int end = text.getSpanEnd(quote);
    if (start < 0)
        return text.toString();
    else if (start == 0) {
        return text.subSequence(end, text.length()).toString();
    } else// w  w  w. jav  a2 s. c o  m
        return text.subSequence(0, start).toString() + text.subSequence(end, text.length()).toString();
}

From source file:org.mariotaku.twidere.fragment.UserFragment.java

@UiThread
public void displayUser(final ParcelableUser user, ParcelableAccount account) {
    final FragmentActivity activity = getActivity();
    if (activity == null)
        return;/*from  w ww .j av a2  s.  co  m*/
    mUser = user;
    mAccount = account;
    if (user == null || user.key == null) {
        mProfileImageView.setVisibility(View.GONE);
        mProfileTypeView.setVisibility(View.GONE);
        if (activity instanceof ATEActivity) {
            setUiColor(Config.primaryColor(activity, ((ATEActivity) activity).getATEKey()));
        }
        return;
    }
    mProfileImageView.setVisibility(View.VISIBLE);
    final Resources resources = getResources();
    final LoaderManager lm = getLoaderManager();
    lm.destroyLoader(LOADER_ID_USER);
    lm.destroyLoader(LOADER_ID_FRIENDSHIP);
    mCardContent.setVisibility(View.VISIBLE);
    mHeaderErrorContainer.setVisibility(View.GONE);
    mProgressContainer.setVisibility(View.GONE);
    mUser = user;
    mProfileImageView.setBorderColor(user.color != 0 ? user.color : Color.WHITE);
    mProfileNameContainer.drawEnd(user.account_color);
    mNameView.setText(mBidiFormatter.unicodeWrap(TextUtils.isEmpty(user.nickname) ? user.name
            : getString(R.string.name_with_nickname, user.name, user.nickname)));
    final int typeIconRes = Utils.getUserTypeIconRes(user.is_verified, user.is_protected);
    if (typeIconRes != 0) {
        mProfileTypeView.setImageResource(typeIconRes);
        mProfileTypeView.setVisibility(View.VISIBLE);
    } else {
        mProfileTypeView.setImageDrawable(null);
        mProfileTypeView.setVisibility(View.GONE);
    }
    mScreenNameView.setText(String.format("@%s", user.screen_name));
    final TwidereLinkify linkify = new TwidereLinkify(this);
    if (user.description_unescaped != null) {
        final SpannableStringBuilder text = SpannableStringBuilder.valueOf(user.description_unescaped);
        ParcelableStatusUtils.applySpans(text, user.description_spans);
        linkify.applyAllLinks(text, user.account_key, false, false);
        mDescriptionView.setText(text);
    } else {
        mDescriptionView.setText(user.description_plain);
        Linkify.addLinks(mDescriptionView, Linkify.WEB_URLS);
    }
    mDescriptionContainer.setVisibility(mDescriptionView.length() > 0 ? View.VISIBLE : View.GONE);

    mLocationContainer.setVisibility(TextUtils.isEmpty(user.location) ? View.GONE : View.VISIBLE);
    mLocationView.setText(user.location);
    mURLContainer.setVisibility(
            TextUtils.isEmpty(user.url) && TextUtils.isEmpty(user.url_expanded) ? View.GONE : View.VISIBLE);
    mURLView.setText(TextUtils.isEmpty(user.url_expanded) ? user.url : user.url_expanded);
    final String createdAt = Utils.formatToLongTimeString(activity, user.created_at);
    final float daysSinceCreation = (System.currentTimeMillis() - user.created_at) / 1000 / 60 / 60 / 24;
    final int dailyTweets = Math.round(user.statuses_count / Math.max(1, daysSinceCreation));
    mCreatedAtView.setText(resources.getQuantityString(R.plurals.created_at_with_N_tweets_per_day, dailyTweets,
            createdAt, dailyTweets));
    mListedCount.setText(Utils.getLocalizedNumber(mLocale, user.listed_count));
    final long groupsCount = user.extras != null ? user.extras.groups_count : -1;
    mGroupsCount.setText(Utils.getLocalizedNumber(mLocale, groupsCount));
    mFollowersCount.setText(Utils.getLocalizedNumber(mLocale, user.followers_count));
    mFriendsCount.setText(Utils.getLocalizedNumber(mLocale, user.friends_count));

    mListedContainer.setVisibility(user.listed_count < 0 ? View.GONE : View.VISIBLE);
    mGroupsContainer.setVisibility(groupsCount < 0 ? View.GONE : View.VISIBLE);

    mMediaLoader.displayOriginalProfileImage(mProfileImageView, user);
    if (user.color != 0) {
        setUiColor(user.color);
    } else if (user.link_color != 0) {
        setUiColor(user.link_color);
    } else if (activity instanceof ATEActivity) {
        setUiColor(Config.primaryColor(activity, ((ATEActivity) activity).getATEKey()));
    }
    final int defWidth = resources.getDisplayMetrics().widthPixels;
    final int width = mBannerWidth > 0 ? mBannerWidth : defWidth;
    final String bannerUrl = ParcelableUserUtils.getProfileBannerUrl(user);
    if (ObjectUtils.notEqual(mProfileBannerView.getTag(), bannerUrl)
            || mProfileBannerView.getDrawable() == null) {
        mProfileBannerView.setTag(bannerUrl);
        mMediaLoader.displayProfileBanner(mProfileBannerView, bannerUrl, width);
    }
    final UserRelationship relationship = mRelationship;
    if (relationship == null) {
        getFriendship();
    }
    activity.setTitle(
            UserColorNameManager.decideDisplayName(user.nickname, user.name, user.screen_name, mNameFirst));

    Calendar cal = Calendar.getInstance();
    final int currentMonth = cal.get(Calendar.MONTH), currentDay = cal.get(Calendar.DAY_OF_MONTH);
    cal.setTimeInMillis(user.created_at);
    if (cal.get(Calendar.MONTH) == currentMonth && cal.get(Calendar.DAY_OF_MONTH) == currentDay
            && !mHideBirthdayView) {
        mProfileBirthdayBannerView.setVisibility(View.VISIBLE);
    } else {
        mProfileBirthdayBannerView.setVisibility(View.GONE);
    }
    updateTitleAlpha();
    invalidateOptionsMenu();
    updateSubtitle();
}

From source file:de.vanita5.twittnuker.util.ThemeUtils.java

public static void applyParagraphSpacing(TextView textView, float multiplier) {
    final SpannableStringBuilder builder = SpannableStringBuilder.valueOf(textView.getText());
    int prevLineBreak, currLineBreak = 0;
    for (int i = 0, j = builder.length(); i < j; i++) {
        if (builder.charAt(i) == '\n') {
            prevLineBreak = currLineBreak;
            currLineBreak = i;//from ww  w  .j  a v  a  2  s.co  m
            if (currLineBreak > 0) {
                builder.setSpan(new ParagraphSpacingSpan(multiplier), prevLineBreak, currLineBreak,
                        Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            }
        }
    }
    textView.setText(builder);
}