summaryrefslogtreecommitdiffstats
path: root/src/com/android/mail/browse/BadgeSpan.java
blob: e849f7d1a6ff0e4112bc98e0180a9e0a5212637e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (C) 2014 Google Inc.
 * Licensed to The Android Open Source Project.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.mail.browse;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.RectF;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.CharacterStyle;
import android.text.style.ReplacementSpan;

/**
 * A replacement span to use when displaying a badge in a conversation list item.
 * A badge will be some piece of text with a colored background and rounded
 * corners.
 */
public class BadgeSpan extends ReplacementSpan {

    public interface BadgeSpanDimensions {
        /**
         * Returns the padding value that corresponds to one side of the
         * horizontal padding surrounding the text inside the background color.
         */
        int getHorizontalPadding();

        /**
         * Returns the radius to use for the rounded corners on the background rect.
         */
        float getRoundedCornerRadius();
    }

    private TextPaint mWorkPaint = new TextPaint();
    /**
     * A reference to the enclosing Spanned object to collect other CharacterStyle spans and take
     * them into account when drawing.
     */
    private final Spanned mSpanned;
    private final BadgeSpanDimensions mDims;

    public BadgeSpan(Spanned spanned, BadgeSpanDimensions dims) {
        mSpanned = spanned;
        mDims = dims;
    }

    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, FontMetricsInt fm) {
        if (fm != null) {
            paint.getFontMetricsInt(fm);
            // The magic set of measurements to vertically center text within a colored region!
            fm.top = fm.ascent;
        }
        return measureWidth(paint, text, start, end);
    }

    private int measureWidth(Paint paint, CharSequence text, int start, int end) {
        return (int) paint.measureText(text, start, end) + 2 * mDims.getHorizontalPadding();
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top,
            int y, int bottom, Paint paint) {

        mWorkPaint.set(paint);

        // take into account the foreground/background color spans when painting
        final CharacterStyle[] otherSpans = mSpanned.getSpans(start, end, CharacterStyle.class);
        for (CharacterStyle otherSpan : otherSpans) {
            otherSpan.updateDrawState(mWorkPaint);
        }

        // paint a background if present
        if (mWorkPaint.bgColor != 0) {
            final int prevColor = mWorkPaint.getColor();
            final Paint.Style prevStyle = mWorkPaint.getStyle();

            mWorkPaint.setColor(mWorkPaint.bgColor);
            mWorkPaint.setStyle(Paint.Style.FILL);

            final int bgWidth = measureWidth(mWorkPaint, text, start, end);
            final RectF rect = new RectF(x, top, x + bgWidth, bottom);
            final float radius = mDims.getRoundedCornerRadius();
            canvas.drawRoundRect(rect, radius, radius, mWorkPaint);

            mWorkPaint.setColor(prevColor);
            mWorkPaint.setStyle(prevStyle);
        }

        canvas.drawText(text, start, end, x + mDims.getHorizontalPadding(), y, mWorkPaint);
    }
}