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.content.res.Resources; 21 import android.graphics.Paint; 22 import android.graphics.Typeface; 23 import android.util.AttributeSet; 24 import android.widget.LinearLayout; 25 import android.widget.TextView; 26 27 import com.android.deskclock.R; 28 29 30 public class TimerView extends LinearLayout { 31 32 private TextView mHoursTens, mHoursOnes; 33 private TextView mMinutesTens, mMinutesOnes; 34 private TextView mSeconds; 35 private final Typeface mAndroidClockMonoThin; 36 private Typeface mOriginalHoursTypeface; 37 private Typeface mOriginalMinutesTypeface; 38 private final int mWhiteColor, mGrayColor; 39 40 @SuppressWarnings("unused") TimerView(Context context)41 public TimerView(Context context) { 42 this(context, null); 43 } 44 TimerView(Context context, AttributeSet attrs)45 public TimerView(Context context, AttributeSet attrs) { 46 super(context, attrs); 47 48 mAndroidClockMonoThin = 49 Typeface.createFromAsset(context.getAssets(), "fonts/AndroidClockMono-Thin.ttf"); 50 51 final Resources resources = context.getResources(); 52 mWhiteColor = resources.getColor(R.color.clock_white); 53 mGrayColor = resources.getColor(R.color.clock_gray); 54 } 55 56 @Override onFinishInflate()57 protected void onFinishInflate() { 58 super.onFinishInflate(); 59 60 mHoursTens = (TextView) findViewById(R.id.hours_tens); 61 62 mHoursOnes = (TextView) findViewById(R.id.hours_ones); 63 mOriginalHoursTypeface = mHoursOnes.getTypeface(); 64 65 mMinutesTens = (TextView) findViewById(R.id.minutes_tens); 66 addStartPadding(mMinutesTens); 67 68 mMinutesOnes = (TextView) findViewById(R.id.minutes_ones); 69 mOriginalMinutesTypeface = mMinutesOnes.getTypeface(); 70 71 mSeconds = (TextView) findViewById(R.id.seconds); 72 addStartPadding(mSeconds); 73 } 74 75 /** 76 * Measure the text and add a start padding to the view 77 * @param textView view to measure and onb to which add start padding 78 */ addStartPadding(TextView textView)79 private void addStartPadding(TextView textView) { 80 final float gapPadding = 0.45f; 81 // allDigits will contain ten digits: "0123456789" in the default locale 82 String allDigits = String.format("%010d", 123456789); 83 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 84 paint.setTextSize(textView.getTextSize()); 85 paint.setTypeface(textView.getTypeface()); 86 87 float widths[] = new float[allDigits.length()]; 88 int ll = paint.getTextWidths(allDigits, widths); 89 int largest = 0; 90 for (int ii = 1; ii < ll; ii++) { 91 if (widths[ii] > widths[largest]) { 92 largest = ii; 93 } 94 } 95 // Add left padding to the view - Note: layout inherits LTR 96 textView.setPadding((int) (gapPadding * widths[largest]), 0, 0, 0); 97 } 98 setTime(int hoursTensDigit, int hoursOnesDigit, int minutesTensDigit, int minutesOnesDigit, int seconds)99 public void setTime(int hoursTensDigit, int hoursOnesDigit, int minutesTensDigit, 100 int minutesOnesDigit, int seconds) { 101 if (hoursTensDigit == -1) { 102 mHoursTens.setText("-"); 103 mHoursTens.setTypeface(mAndroidClockMonoThin); 104 mHoursTens.setTextColor(mGrayColor); 105 } else { 106 mHoursTens.setText(String.valueOf(hoursTensDigit)); 107 mHoursTens.setTypeface(mOriginalHoursTypeface); 108 mHoursTens.setTextColor(mWhiteColor); 109 } 110 111 if (hoursOnesDigit == -1) { 112 mHoursOnes.setText("-"); 113 mHoursOnes.setTypeface(mAndroidClockMonoThin); 114 mHoursOnes.setTextColor(mGrayColor); 115 } else { 116 mHoursOnes.setText(String.valueOf(hoursOnesDigit)); 117 mHoursOnes.setTypeface(mOriginalHoursTypeface); 118 mHoursOnes.setTextColor(mWhiteColor); 119 } 120 121 if (minutesTensDigit == -1) { 122 mMinutesTens.setText("-"); 123 mMinutesTens.setTypeface(mAndroidClockMonoThin); 124 mMinutesTens.setTextColor(mGrayColor); 125 } else { 126 mMinutesTens.setText(String.valueOf(minutesTensDigit)); 127 mMinutesTens.setTypeface(mOriginalMinutesTypeface); 128 mMinutesTens.setTextColor(mWhiteColor); 129 } 130 131 if (minutesOnesDigit == -1) { 132 mMinutesOnes.setText("-"); 133 mMinutesOnes.setTypeface(mAndroidClockMonoThin); 134 mMinutesOnes.setTextColor(mGrayColor); 135 } else { 136 mMinutesOnes.setText(String.valueOf(minutesOnesDigit)); 137 mMinutesOnes.setTypeface(mOriginalMinutesTypeface); 138 mMinutesOnes.setTextColor(mWhiteColor); 139 } 140 141 mSeconds.setText(String.format("%02d", seconds)); 142 } 143 } 144