1/*
2 * Copyright (C) 2013 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 com.android.datetimepicker.date;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.graphics.Canvas;
22import android.graphics.Paint;
23import android.graphics.Paint.Align;
24import android.graphics.Paint.Style;
25import android.util.AttributeSet;
26import android.widget.TextView;
27
28import com.android.datetimepicker.R;
29
30/**
31 * A text view which, when pressed or activated, displays a blue circle around the text.
32 */
33public class TextViewWithCircularIndicator extends TextView {
34
35    private static final int SELECTED_CIRCLE_ALPHA = 60;
36
37    Paint mCirclePaint = new Paint();
38
39    private final int mRadius;
40    private final int mCircleColor;
41    private final String mItemIsSelectedText;
42
43    private boolean mDrawCircle;
44
45    public TextViewWithCircularIndicator(Context context, AttributeSet attrs) {
46        super(context, attrs);
47        Resources res = context.getResources();
48        mCircleColor = res.getColor(R.color.blue);
49        mRadius = res.getDimensionPixelOffset(R.dimen.month_select_circle_radius);
50        mItemIsSelectedText = context.getResources().getString(R.string.item_is_selected);
51
52        init();
53    }
54
55    private void init() {
56        mCirclePaint.setFakeBoldText(true);
57        mCirclePaint.setAntiAlias(true);
58        mCirclePaint.setColor(mCircleColor);
59        mCirclePaint.setTextAlign(Align.CENTER);
60        mCirclePaint.setStyle(Style.FILL);
61        mCirclePaint.setAlpha(SELECTED_CIRCLE_ALPHA);
62    }
63
64    public void drawIndicator(boolean drawCircle) {
65        mDrawCircle = drawCircle;
66    }
67
68    @Override
69    public void onDraw(Canvas canvas) {
70        super.onDraw(canvas);
71        if (mDrawCircle) {
72            final int width = getWidth();
73            final int height = getHeight();
74            int radius = Math.min(width, height) / 2;
75            canvas.drawCircle(width / 2, height / 2, radius, mCirclePaint);
76        }
77    }
78
79    @Override
80    public CharSequence getContentDescription() {
81        CharSequence itemText = getText();
82        if (mDrawCircle) {
83            return String.format(mItemIsSelectedText, itemText);
84        } else {
85            return itemText;
86        }
87    }
88}
89