Home | History | Annotate | Download | only in timer
      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 mHoursOnes, mMinutesOnes;
     33     private TextView mMinutesTens;
     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")
     41     public TimerView(Context context) {
     42         this(context, null);
     43     }
     44 
     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         Resources resources = context.getResources();
     52         mWhiteColor = resources.getColor(R.color.clock_white);
     53         mGrayColor = resources.getColor(R.color.clock_gray);
     54     }
     55 
     56     @Override
     57     protected void onFinishInflate() {
     58         super.onFinishInflate();
     59 
     60         mHoursOnes = (TextView) findViewById(R.id.hours_ones);
     61         if (mHoursOnes != null) {
     62             mOriginalHoursTypeface = mHoursOnes.getTypeface();
     63         }
     64         mMinutesTens = (TextView) findViewById(R.id.minutes_tens);
     65         if (mHoursOnes != null && mMinutesTens != null) {
     66             addStartPadding(mMinutesTens);
     67         }
     68         mMinutesOnes = (TextView) findViewById(R.id.minutes_ones);
     69         if (mMinutesOnes != null) {
     70             mOriginalMinutesTypeface = mMinutesOnes.getTypeface();
     71         }
     72         mSeconds = (TextView) findViewById(R.id.seconds);
     73         if (mSeconds != null) {
     74             addStartPadding(mSeconds);
     75         }
     76     }
     77 
     78     /**
     79      * Measure the text and add a start padding to the view
     80      * @param textView view to measure and onb to which add start padding
     81      */
     82     private void addStartPadding(TextView textView) {
     83         final float gapPadding = 0.45f;
     84         // allDigits will contain ten digits: "0123456789" in the default locale
     85         String allDigits = String.format("%010d", 123456789);
     86         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
     87         paint.setTextSize(textView.getTextSize());
     88         paint.setTypeface(textView.getTypeface());
     89 
     90         float widths[] = new float[allDigits.length()];
     91         int ll = paint.getTextWidths(allDigits, widths);
     92         int largest = 0;
     93         for (int ii = 1; ii < ll; ii++) {
     94             if (widths[ii] > widths[largest]) {
     95                 largest = ii;
     96             }
     97         }
     98         // Add left padding to the view - Note: layout inherits LTR
     99         textView.setPadding((int) (gapPadding * widths[largest]), 0, 0, 0);
    100     }
    101 
    102 
    103     public void setTime(int hoursOnesDigit, int minutesTensDigit,
    104                         int minutesOnesDigit, int seconds) {
    105         if (mHoursOnes != null) {
    106             if (hoursOnesDigit == -1) {
    107                 mHoursOnes.setText("-");
    108                 mHoursOnes.setTypeface(mAndroidClockMonoThin);
    109                 mHoursOnes.setTextColor(mGrayColor);
    110             } else {
    111                 mHoursOnes.setText(String.format("%d", hoursOnesDigit));
    112                 mHoursOnes.setTypeface(mOriginalHoursTypeface);
    113                 mHoursOnes.setTextColor(mWhiteColor);
    114             }
    115         }
    116 
    117         if (mMinutesTens != null) {
    118             if (minutesTensDigit == -1) {
    119                 mMinutesTens.setText("-");
    120                 mMinutesTens.setTypeface(mAndroidClockMonoThin);
    121                 mMinutesTens.setTextColor(mGrayColor);
    122             } else {
    123                 mMinutesTens.setText(String.format("%d", minutesTensDigit));
    124                 mMinutesTens.setTypeface(mOriginalMinutesTypeface);
    125                 mMinutesTens.setTextColor(mWhiteColor);
    126             }
    127         }
    128         if (mMinutesOnes != null) {
    129             if (minutesOnesDigit == -1) {
    130                 mMinutesOnes.setText("-");
    131                 mMinutesOnes.setTypeface(mAndroidClockMonoThin);
    132                 mMinutesOnes.setTextColor(mGrayColor);
    133             } else {
    134                 mMinutesOnes.setText(String.format("%d", minutesOnesDigit));
    135                 mMinutesOnes.setTypeface(mOriginalMinutesTypeface);
    136                 mMinutesOnes.setTextColor(mWhiteColor);
    137             }
    138         }
    139 
    140         if (mSeconds != null) {
    141             mSeconds.setText(String.format("%02d", seconds));
    142         }
    143     }
    144 }
    145