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