QuoteSpan.java revision 80756e38882720860db52f1fcc21fa1505a02abf
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.text.style;
18
19import android.annotation.ColorInt;
20import android.graphics.Paint;
21import android.graphics.Canvas;
22import android.os.Parcel;
23import android.text.Layout;
24import android.text.ParcelableSpan;
25import android.text.TextUtils;
26
27public class QuoteSpan implements LeadingMarginSpan, ParcelableSpan {
28    private static final int STRIPE_WIDTH = 2;
29    private static final int GAP_WIDTH = 2;
30
31    private final int mColor;
32
33    public QuoteSpan() {
34        super();
35        mColor = 0xff0000ff;
36    }
37
38    public QuoteSpan(@ColorInt int color) {
39        super();
40        mColor = color;
41    }
42
43    public QuoteSpan(Parcel src) {
44        mColor = src.readInt();
45    }
46
47    public int getSpanTypeId() {
48        return TextUtils.QUOTE_SPAN;
49    }
50
51    public int describeContents() {
52        return 0;
53    }
54
55    public void writeToParcel(Parcel dest, int flags) {
56        dest.writeInt(mColor);
57    }
58
59    @ColorInt
60    public int getColor() {
61        return mColor;
62    }
63
64    public int getLeadingMargin(boolean first) {
65        return STRIPE_WIDTH + GAP_WIDTH;
66    }
67
68    public void drawLeadingMargin(Canvas c, Paint p, int x, int dir,
69                                  int top, int baseline, int bottom,
70                                  CharSequence text, int start, int end,
71                                  boolean first, Layout layout) {
72        Paint.Style style = p.getStyle();
73        int color = p.getColor();
74
75        p.setStyle(Paint.Style.FILL);
76        p.setColor(mColor);
77
78        c.drawRect(x, top, x + dir * STRIPE_WIDTH, bottom, p);
79
80        p.setStyle(style);
81        p.setColor(color);
82    }
83}
84