TimerView.java revision 014575aefd55845714d0a614213d432ee5d9474b
1/*
2 * Copyright (C) 2012 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.deskclock.timer;
18
19import android.content.Context;
20import android.graphics.Typeface;
21import android.util.AttributeSet;
22import android.widget.LinearLayout;
23import android.widget.TextView;
24import android.view.View;
25
26import com.android.deskclock.R;
27import com.android.deskclock.ZeroTopPaddingTextView;
28
29
30public class TimerView extends LinearLayout {
31
32    private ZeroTopPaddingTextView mHoursOnes, mMinutesOnes;
33    private ZeroTopPaddingTextView mHoursTens, mMinutesTens;
34    private TextView mSeconds;
35    private final Typeface mAndroidClockMonoThin;
36    private Typeface mOriginalHoursTypeface;
37    private final int mWhiteColor, mGrayColor;
38
39    public TimerView(Context context) {
40        this(context, null);
41    }
42
43    public TimerView(Context context, AttributeSet attrs) {
44        super(context, attrs);
45
46        mAndroidClockMonoThin = Typeface.createFromAsset(context.getAssets(),"fonts/AndroidClockMono-Thin.ttf");
47        mWhiteColor = context.getResources().getColor(R.color.clock_white);
48        mGrayColor = context.getResources().getColor(R.color.clock_gray);
49    }
50
51    @Override
52    protected void onFinishInflate() {
53        super.onFinishInflate();
54
55        mHoursTens = (ZeroTopPaddingTextView)findViewById(R.id.hours_tens);
56        mMinutesTens = (ZeroTopPaddingTextView)findViewById(R.id.minutes_tens);
57        mHoursOnes = (ZeroTopPaddingTextView)findViewById(R.id.hours_ones);
58        mMinutesOnes = (ZeroTopPaddingTextView)findViewById(R.id.minutes_ones);
59        mSeconds = (TextView)findViewById(R.id.seconds);
60        if (mHoursOnes != null) {
61            mOriginalHoursTypeface = mHoursOnes.getTypeface();
62        }
63        // Set the lowest time unit with thin font (excluding hundredths)
64        if (mSeconds != null) {
65            mSeconds.setTypeface(mAndroidClockMonoThin);
66        } else  {
67            if (mMinutesTens != null) {
68                mMinutesTens.setTypeface(mAndroidClockMonoThin);
69                mMinutesTens.updatePadding();
70            }
71            if (mMinutesOnes != null) {
72                mMinutesOnes.setTypeface(mAndroidClockMonoThin);
73                mMinutesOnes.updatePadding();
74            }
75        }
76    }
77
78
79    public void setTime(int hoursTensDigit, int hoursOnesDigit, int minutesTensDigit,
80            int minutesOnesDigit, int seconds) {
81        if (mHoursTens != null) {
82            // Hide digit
83            if (hoursTensDigit == -2) {
84                mHoursTens.setVisibility(View.INVISIBLE);
85            } else if (hoursTensDigit == -1) {
86                mHoursTens.setText("-");
87                mHoursTens.setTypeface(mAndroidClockMonoThin);
88                mHoursTens.setTextColor(mGrayColor);
89                mHoursTens.updatePadding();
90                mHoursTens.setVisibility(View.VISIBLE);
91            } else {
92                mHoursTens.setText(String.format("%d",hoursTensDigit));
93                mHoursTens.setTypeface(mOriginalHoursTypeface);
94                mHoursTens.setTextColor(mWhiteColor);
95                mHoursTens.updatePadding();
96                mHoursTens.setVisibility(View.VISIBLE);
97            }
98        }
99        if (mHoursOnes != null) {
100            if (hoursOnesDigit == -1) {
101                mHoursOnes.setText("-");
102                mHoursOnes.setTypeface(mAndroidClockMonoThin);
103                mHoursOnes.setTextColor(mGrayColor);
104                mHoursOnes.updatePadding();
105            } else {
106                mHoursOnes.setText(String.format("%d",hoursOnesDigit));
107                mHoursOnes.setTypeface(mOriginalHoursTypeface);
108                mHoursOnes.setTextColor(mWhiteColor);
109                mHoursOnes.updatePadding();
110            }
111        }
112        if (mMinutesTens != null) {
113            if (minutesTensDigit == -1) {
114                mMinutesTens.setText("-");
115                mMinutesTens.setTextColor(mGrayColor);
116            } else {
117                mMinutesTens.setTextColor(mWhiteColor);
118                mMinutesTens.setText(String.format("%d",minutesTensDigit));
119            }
120        }
121        if (mMinutesOnes != null) {
122            if (minutesOnesDigit == -1) {
123                mMinutesOnes.setText("-");
124                mMinutesOnes.setTextColor(mGrayColor);
125            } else {
126                mMinutesOnes.setText(String.format("%d",minutesOnesDigit));
127                mMinutesOnes.setTextColor(mWhiteColor);
128            }
129        }
130
131        if (mSeconds != null) {
132            mSeconds.setText(String.format("%02d",seconds));
133        }
134    }
135}
136