android.support.v4.text.BidiFormatter Java Examples

The following examples show how to use android.support.v4.text.BidiFormatter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SnippetArticleViewHolder.java    From 365browser with Apache License 2.0 6 votes vote down vote up
private static String getArticleAge(SnippetArticle article) {
    if (article.mPublishTimestampMilliseconds == 0) return "";

    // DateUtils.getRelativeTimeSpanString(...) calls through to TimeZone.getDefault(). If this
    // has never been called before it loads the current time zone from disk. In most likelihood
    // this will have been called previously and the current time zone will have been cached,
    // but in some cases (eg instrumentation tests) it will cause a strict mode violation.
    StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
    CharSequence relativeTimeSpan;
    try {
        long time = SystemClock.elapsedRealtime();
        relativeTimeSpan =
                DateUtils.getRelativeTimeSpanString(article.mPublishTimestampMilliseconds,
                        System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS);
        RecordHistogram.recordTimesHistogram("Android.StrictMode.SnippetUIBuildTime",
                SystemClock.elapsedRealtime() - time, TimeUnit.MILLISECONDS);
    } finally {
        StrictMode.setThreadPolicy(oldPolicy);
    }

    // We add a dash before the elapsed time, e.g. " - 14 minutes ago".
    return String.format(ARTICLE_AGE_FORMAT_STRING,
            BidiFormatter.getInstance().unicodeWrap(relativeTimeSpan));
}
 
Example #2
Source File: BidiFormatterSupport.java    From V.FlyoutTest with MIT License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.bidiformater_support);

    String formattedText = String.format(text, phone);

    TextView tv_sample = (TextView) findViewById(R.id.textview_without_bidiformatter);
    tv_sample.setText(formattedText);

    TextView tv_bidiformatter = (TextView) findViewById(R.id.textview_with_bidiformatter);
    String wrappedPhone = BidiFormatter.getInstance(true /* rtlContext */).unicodeWrap(phone);
    formattedText = String.format(text, wrappedPhone);
    tv_bidiformatter.setText(formattedText);
}
 
Example #3
Source File: AppsListAdapter.java    From android-cache-cleaner with MIT License 6 votes vote down vote up
public void updateStorageUsage(long totalMemory, long lowMemory, long medMemory,
                               long highMemory) {
    Context context = mColorBar.getContext();

    BidiFormatter bidiFormatter = BidiFormatter.getInstance();

    String sizeStr = bidiFormatter.unicodeWrap(
            Formatter.formatShortFileSize(context, lowMemory));
    mFreeSizeText.setText(context.getString(R.string.apps_list_header_memory, sizeStr));

    sizeStr = bidiFormatter.unicodeWrap(
            Formatter.formatShortFileSize(context, medMemory));
    mCacheSizeText.setText(context.getString(R.string.apps_list_header_memory, sizeStr));

    sizeStr = bidiFormatter.unicodeWrap(
            Formatter.formatShortFileSize(context, highMemory));
    mSystemSizeText.setText(context.getString(R.string.apps_list_header_memory, sizeStr));

    mColorBar.setRatios((float) highMemory / (float) totalMemory,
            (float) medMemory / (float) totalMemory,
            (float) lowMemory / (float) totalMemory);
}
 
Example #4
Source File: SnippetArticleViewHolder.java    From delion with Apache License 2.0 4 votes vote down vote up
@Override
public void onBindViewHolder(NewTabPageListItem article) {
    super.onBindViewHolder(article);

    mArticle = (SnippetArticle) article;

    mHeadlineTextView.setText(mArticle.mTitle);

    // We format the publisher here so that having a publisher name in an RTL language doesn't
    // mess up the formatting on an LTR device and vice versa.
    String publisherAttribution = String.format(PUBLISHER_FORMAT_STRING,
            BidiFormatter.getInstance().unicodeWrap(mArticle.mPublisher),
            DateUtils.getRelativeTimeSpanString(mArticle.mPublishTimestampMilliseconds,
                    System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS));
    mPublisherTextView.setText(publisherAttribution);

    // The favicon of the publisher should match the textview height.
    int widthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    mPublisherTextView.measure(widthSpec, heightSpec);
    mPublisherFaviconSizePx = mPublisherTextView.getMeasuredHeight();

    mArticleSnippetTextView.setText(mArticle.mPreviewText);

    // If there's still a pending thumbnail fetch, cancel it.
    cancelImageFetch();

    // If the article has a thumbnail already, reuse it. Otherwise start a fetch.
    if (mArticle.getThumbnailBitmap() != null) {
        mThumbnailView.setImageBitmap(mArticle.getThumbnailBitmap());
    } else {
        mThumbnailView.setImageResource(R.drawable.ic_snippet_thumbnail_placeholder);
        mImageCallback = new FetchImageCallback(this, mArticle);
        mSnippetsBridge.fetchSnippetImage(mArticle, mImageCallback);
    }

    // Set the favicon of the publisher.
    try {
        fetchFaviconFromLocalCache(new URI(mArticle.mUrl), true);
    } catch (URISyntaxException e) {
        setDefaultFaviconOnView();
    }
}
 
Example #5
Source File: SnippetArticleViewHolder.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
public void onBindViewHolder(SnippetArticle article) {
    super.onBindViewHolder();

    // No longer listen for offline status changes to the old article.
    if (mArticle != null) mArticle.setOfflineStatusChangeRunnable(null);

    mArticle = article;
    updateLayout();

    mHeadlineTextView.setText(mArticle.mTitle);

    // DateUtils.getRelativeTimeSpanString(...) calls through to TimeZone.getDefault(). If this
    // has never been called before it loads the current time zone from disk. In most likelihood
    // this will have been called previously and the current time zone will have been cached,
    // but in some cases (eg instrumentation tests) it will cause a strict mode violation.
    StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
    try {
        long time = SystemClock.elapsedRealtime();
        CharSequence relativeTimeSpan = DateUtils.getRelativeTimeSpanString(
                mArticle.mPublishTimestampMilliseconds, System.currentTimeMillis(),
                DateUtils.MINUTE_IN_MILLIS);
        RecordHistogram.recordTimesHistogram("Android.StrictMode.SnippetUIBuildTime",
                SystemClock.elapsedRealtime() - time, TimeUnit.MILLISECONDS);

        // We format the publisher here so that having a publisher name in an RTL language
        // doesn't mess up the formatting on an LTR device and vice versa.
        String publisherAttribution = String.format(PUBLISHER_FORMAT_STRING,
                BidiFormatter.getInstance().unicodeWrap(mArticle.mPublisher), relativeTimeSpan);
        mPublisherTextView.setText(publisherAttribution);
    } finally {
        StrictMode.setThreadPolicy(oldPolicy);
    }

    // The favicon of the publisher should match the TextView height.
    int widthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    mPublisherTextView.measure(widthSpec, heightSpec);
    mPublisherFaviconSizePx = mPublisherTextView.getMeasuredHeight();

    mArticleSnippetTextView.setText(mArticle.mPreviewText);

    // If there's still a pending thumbnail fetch, cancel it.
    cancelImageFetch();

    // If the article has a thumbnail already, reuse it. Otherwise start a fetch.
    // mThumbnailView's visibility is modified in updateLayout().
    if (mThumbnailView.getVisibility() == View.VISIBLE) {
        if (mArticle.getThumbnailBitmap() != null) {
            mThumbnailView.setImageBitmap(mArticle.getThumbnailBitmap());
        } else {
            mThumbnailView.setImageResource(R.drawable.ic_snippet_thumbnail_placeholder);
            mImageCallback = new FetchImageCallback(this, mArticle);
            mNewTabPageManager.getSuggestionsSource()
                    .fetchSuggestionImage(mArticle, mImageCallback);
        }
    }

    // Set the favicon of the publisher.
    try {
        fetchFaviconFromLocalCache(new URI(mArticle.mUrl), true);
    } catch (URISyntaxException e) {
        setDefaultFaviconOnView();
    }

    mOfflineBadge.setVisibility(View.GONE);
    if (SnippetsConfig.isOfflineBadgeEnabled()) {
        Runnable offlineChecker = new Runnable() {
            @Override
            public void run() {
                if (mArticle.getOfflinePageOfflineId() != null || mArticle.mIsDownloadedAsset) {
                    mOfflineBadge.setVisibility(View.VISIBLE);
                }
            }
        };
        mArticle.setOfflineStatusChangeRunnable(offlineChecker);
        offlineChecker.run();
    }

    mRecyclerView.onSnippetBound(itemView);
}
 
Example #6
Source File: SnippetArticleViewHolder.java    From 365browser with Apache License 2.0 4 votes vote down vote up
private static String getPublisherString(SnippetArticle article) {
    // We format the publisher here so that having a publisher name in an RTL language
    // doesn't mess up the formatting on an LTR device and vice versa.
    return BidiFormatter.getInstance().unicodeWrap(article.mPublisher);
}
 
Example #7
Source File: BubbleTextContainer.java    From actor-platform with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Rect bounds = new Rect();
    Drawable background = getBackground();
    if (background != null) {
        background.getPadding(bounds);
    }

    int wMode = MeasureSpec.getMode(widthMeasureSpec);
    int maxW = MeasureSpec.getSize(widthMeasureSpec) - bounds.left - bounds.right;

    TextView messageView = (TextView) getChildAt(0);
    messageView.measure(MeasureSpec.makeMeasureSpec(maxW, wMode), heightMeasureSpec);
    View timeView = getChildAt(1);
    timeView.measure(MeasureSpec.makeMeasureSpec(maxW, wMode), heightMeasureSpec);

    Layout textLayout = messageView.getLayout();

    int contentW = messageView.getMeasuredWidth();
    int timeW = timeView.getMeasuredWidth();
    boolean isRtl = BidiFormatter.getInstance().isRtl(messageView.getText().toString());

    if (messageView.getLayout().getLineCount() < 5 && !isRtl) {
        contentW = 0;
        for (int i = 0; i < textLayout.getLineCount(); i++) {
            contentW = Math.max(contentW, (int) textLayout.getLineWidth(i));
        }
    }

    int lastLineW = (int) textLayout.getLineWidth(textLayout.getLineCount() - 1);

    if (isRtl) {
        lastLineW = contentW;
    }

    int fullContentW, fullContentH;

    if (isRtl) {
        fullContentW = contentW;
        fullContentH = messageView.getMeasuredHeight() + timeView.getMeasuredHeight();
    } else {
        if (lastLineW + timeW < contentW) {
            // Nothing to do
            fullContentW = contentW;
            fullContentH = messageView.getMeasuredHeight();
        } else if (lastLineW + timeW < maxW) {
            fullContentW = lastLineW + timeW;
            fullContentH = messageView.getMeasuredHeight();
        } else {
            fullContentW = contentW;
            fullContentH = messageView.getMeasuredHeight() + timeView.getMeasuredHeight();
        }
    }

    setMeasuredDimension(fullContentW + bounds.left + bounds.right, fullContentH + bounds.top + bounds.bottom);
}