/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.deskclock.timer;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View;

import com.android.deskclock.R;
import com.android.deskclock.ZeroTopPaddingTextView;


public class TimerView extends LinearLayout {

    private ZeroTopPaddingTextView mHoursOnes, mMinutesOnes;
    private ZeroTopPaddingTextView mHoursTens, mMinutesTens;
    private TextView mSeconds;
    private final Typeface mAndroidClockMonoThin;
    private Typeface mOriginalHoursTypeface;
    private final int mWhiteColor, mGrayColor;

    public TimerView(Context context) {
        this(context, null);
    }

    public TimerView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mAndroidClockMonoThin = Typeface.createFromAsset(context.getAssets(),"fonts/AndroidClockMono-Thin.ttf");
        mWhiteColor = context.getResources().getColor(R.color.clock_white);
        mGrayColor = context.getResources().getColor(R.color.clock_gray);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mHoursTens = (ZeroTopPaddingTextView)findViewById(R.id.hours_tens);
        mMinutesTens = (ZeroTopPaddingTextView)findViewById(R.id.minutes_tens);
        mHoursOnes = (ZeroTopPaddingTextView)findViewById(R.id.hours_ones);
        mMinutesOnes = (ZeroTopPaddingTextView)findViewById(R.id.minutes_ones);
        mSeconds = (TextView)findViewById(R.id.seconds);
        if (mHoursOnes != null) {
            mOriginalHoursTypeface = mHoursOnes.getTypeface();
        }
        // Set the lowest time unit with thin font (excluding hundredths)
        if (mSeconds != null) {
            mSeconds.setTypeface(mAndroidClockMonoThin);
        } else  {
            if (mMinutesTens != null) {
                mMinutesTens.setTypeface(mAndroidClockMonoThin);
                mMinutesTens.updatePadding();
            }
            if (mMinutesOnes != null) {
                mMinutesOnes.setTypeface(mAndroidClockMonoThin);
                mMinutesOnes.updatePadding();
            }
        }
    }


    public void setTime(int hoursTensDigit, int hoursOnesDigit, int minutesTensDigit,
            int minutesOnesDigit, int seconds) {
        if (mHoursTens != null) {
            // Hide digit
            if (hoursTensDigit == -2) {
                mHoursTens.setVisibility(View.INVISIBLE);
            } else if (hoursTensDigit == -1) {
                mHoursTens.setText("-");
                mHoursTens.setTypeface(mAndroidClockMonoThin);
                mHoursTens.setTextColor(mGrayColor);
                mHoursTens.updatePadding();
                mHoursTens.setVisibility(View.VISIBLE);
            } else {
                mHoursTens.setText(String.format("%d",hoursTensDigit));
                mHoursTens.setTypeface(mOriginalHoursTypeface);
                mHoursTens.setTextColor(mWhiteColor);
                mHoursTens.updatePadding();
                mHoursTens.setVisibility(View.VISIBLE);
            }
        }
        if (mHoursOnes != null) {
            if (hoursOnesDigit == -1) {
                mHoursOnes.setText("-");
                mHoursOnes.setTypeface(mAndroidClockMonoThin);
                mHoursOnes.setTextColor(mGrayColor);
                mHoursOnes.updatePadding();
            } else {
                mHoursOnes.setText(String.format("%d",hoursOnesDigit));
                mHoursOnes.setTypeface(mOriginalHoursTypeface);
                mHoursOnes.setTextColor(mWhiteColor);
                mHoursOnes.updatePadding();
            }
        }
        if (mMinutesTens != null) {
            if (minutesTensDigit == -1) {
                mMinutesTens.setText("-");
                mMinutesTens.setTextColor(mGrayColor);
            } else {
                mMinutesTens.setTextColor(mWhiteColor);
                mMinutesTens.setText(String.format("%d",minutesTensDigit));
            }
        }
        if (mMinutesOnes != null) {
            if (minutesOnesDigit == -1) {
                mMinutesOnes.setText("-");
                mMinutesOnes.setTextColor(mGrayColor);
            } else {
                mMinutesOnes.setText(String.format("%d",minutesOnesDigit));
                mMinutesOnes.setTextColor(mWhiteColor);
            }
        }

        if (mSeconds != null) {
            mSeconds.setText(String.format("%02d",seconds));
        }
    }
}

@@ -1187,53 +1187,8 @@
 in;%0A
-    private Typeface mOriginalHoursTypeface;%0A
     
@@ -1230,16 +1230,16 @@
 yColor;%0A
+
 %0A    pub
@@ -1430,16 +1430,32 @@
 noThin =
+%0A               
  Typefac
@@ -1492,16 +1492,17 @@
 ssets(),
+ 
 %22fonts/A
@@ -2138,24 +2138,190 @@
 d.seconds);%0A
+        // If we have hours tens, we are in the alarm time picker, set the hours font to thin%0A        // to prevent the need to set the top paddings (see b/7407383).%0A
         if (
@@ -2318,35 +2318,35 @@
       if (mHours
-One
+Ten
 s != null) %7B%0A   
@@ -2359,31 +2359,147 @@
    m
-OriginalHoursTypeface =
+HoursTens.setTypeface(mAndroidClockMonoThin);%0A            mHoursTens.updatePadding();%0A            if (mHoursOnes != null) %7B%0A               
  mHo
@@ -2506,17 +2506,17 @@
 ursOnes.
-g
+s
 etTypefa
@@ -2518,18 +2518,97 @@
 ypeface(
-);
+mAndroidClockMonoThin);%0A                mHoursOnes.updatePadding();%0A            %7D
 %0A       
@@ -3524,71 +3524,8 @@
 %22);%0A
-                mHoursTens.setTypeface(mAndroidClockMonoThin);%0A
     
@@ -3577,52 +3577,8 @@
 r);%0A
-                mHoursTens.updatePadding();%0A
     
@@ -3726,72 +3726,8 @@
 ));%0A
-                mHoursTens.setTypeface(mOriginalHoursTypeface);%0A
     
@@ -3780,52 +3780,8 @@
 r);%0A
-                mHoursTens.updatePadding();%0A
     
@@ -3975,71 +3975,8 @@
 %22);%0A
-                mHoursOnes.setTypeface(mAndroidClockMonoThin);%0A
     
@@ -4028,52 +4028,8 @@
 r);%0A
-                mHoursOnes.updatePadding();%0A
     
@@ -4121,72 +4121,8 @@
 ));%0A
-                mHoursOnes.setTypeface(mOriginalHoursTypeface);%0A
     
@@ -4175,52 +4175,8 @@
 r);%0A
-                mHoursOnes.updatePadding();%0A
     

Fix flickers in numeric pad of time picker