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.time;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.graphics.Canvas;
22import android.graphics.Paint;
23import android.util.Log;
24import android.view.View;
25
26import com.android.datetimepicker.R;
27
28/**
29 * Draws a simple white circle on which the numbers will be drawn.
30 */
31public class CircleView extends View {
32    private static final String TAG = "CircleView";
33
34    private final Paint mPaint = new Paint();
35    private boolean mIs24HourMode;
36    private int mCircleColor;
37    private int mDotColor;
38    private float mCircleRadiusMultiplier;
39    private float mAmPmCircleRadiusMultiplier;
40    private boolean mIsInitialized;
41
42    private boolean mDrawValuesReady;
43    private int mXCenter;
44    private int mYCenter;
45    private int mCircleRadius;
46
47    public CircleView(Context context) {
48        super(context);
49
50        Resources res = context.getResources();
51        mCircleColor = res.getColor(android.R.color.white);
52        mDotColor = res.getColor(R.color.numbers_text_color);
53        mPaint.setAntiAlias(true);
54
55        mIsInitialized = false;
56    }
57
58    public void initialize(Context context, boolean is24HourMode) {
59        if (mIsInitialized) {
60            Log.e(TAG, "CircleView may only be initialized once.");
61            return;
62        }
63
64        Resources res = context.getResources();
65        mIs24HourMode = is24HourMode;
66        if (is24HourMode) {
67            mCircleRadiusMultiplier = Float.parseFloat(
68                    res.getString(R.string.circle_radius_multiplier_24HourMode));
69        } else {
70            mCircleRadiusMultiplier = Float.parseFloat(
71                    res.getString(R.string.circle_radius_multiplier));
72            mAmPmCircleRadiusMultiplier =
73                    Float.parseFloat(res.getString(R.string.ampm_circle_radius_multiplier));
74        }
75
76        mIsInitialized = true;
77    }
78
79    /* package */ void setTheme(Context context, boolean dark) {
80        Resources res = context.getResources();
81        if (dark) {
82            mCircleColor = res.getColor(R.color.dark_gray);
83            mDotColor = res.getColor(R.color.light_gray);
84        } else {
85            mCircleColor = res.getColor(android.R.color.white);
86            mDotColor = res.getColor(R.color.numbers_text_color);
87        }
88    }
89
90
91    @Override
92    public void onDraw(Canvas canvas) {
93        int viewWidth = getWidth();
94        if (viewWidth == 0 || !mIsInitialized) {
95            return;
96        }
97
98        if (!mDrawValuesReady) {
99            mXCenter = getWidth() / 2;
100            mYCenter = getHeight() / 2;
101            mCircleRadius = (int) (Math.min(mXCenter, mYCenter) * mCircleRadiusMultiplier);
102
103            if (!mIs24HourMode) {
104                // We'll need to draw the AM/PM circles, so the main circle will need to have
105                // a slightly higher center. To keep the entire view centered vertically, we'll
106                // have to push it up by half the radius of the AM/PM circles.
107                int amPmCircleRadius = (int) (mCircleRadius * mAmPmCircleRadiusMultiplier);
108                mYCenter -= amPmCircleRadius / 2;
109            }
110
111            mDrawValuesReady = true;
112        }
113
114        // Draw the white circle.
115        mPaint.setColor(mCircleColor);
116        canvas.drawCircle(mXCenter, mYCenter, mCircleRadius, mPaint);
117
118        // Draw a small black circle in the center.
119        mPaint.setColor(mDotColor);
120        canvas.drawCircle(mXCenter, mYCenter, 2, mPaint);
121    }
122}
123