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.util.AttributeSet;
21import android.view.LayoutInflater;
22import android.view.ViewGroup;
23import android.widget.ImageView;
24import android.widget.LinearLayout;
25
26import com.android.deskclock.CircleTimerView;
27import com.android.deskclock.R;
28
29
30public class TimerListItem extends LinearLayout {
31
32    CountingTimerView mTimerText;
33    CircleTimerView mCircleView;
34    ImageView mResetAddButton;
35
36    long mTimerLength;
37
38    public TimerListItem(Context context) {
39        this(context, null);
40    }
41
42//    public void TimerListItem newInstance(Context context) {
43//        final LayoutInflater layoutInflater =
44//                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
45//        layoutInflater.inflate(R.layout.timer_list_item, this);
46//    }
47
48    public TimerListItem(Context context, AttributeSet attrs) {
49        super(context, attrs);
50    }
51
52    @Override
53    protected void onFinishInflate() {
54        super.onFinishInflate();
55        mTimerText = (CountingTimerView) findViewById(R.id.timer_time_text);
56        mCircleView = (CircleTimerView) findViewById(R.id.timer_time);
57        mResetAddButton = (ImageView) findViewById(R.id.reset_add);
58        mCircleView.setTimerMode(true);
59    }
60
61    public void set(long timerLength, long timeLeft, boolean drawRed) {
62        if (mCircleView == null) {
63            mCircleView = (CircleTimerView) findViewById(R.id.timer_time);
64            mCircleView.setTimerMode(true);
65        }
66        mTimerLength = timerLength;
67        mCircleView.setIntervalTime(mTimerLength);
68        mCircleView.setPassedTime(timerLength - timeLeft, drawRed);
69        invalidate();
70    }
71
72    public void start() {
73        mResetAddButton.setImageResource(R.drawable.ic_plusone);
74        mResetAddButton.setContentDescription(getResources().getString(R.string.timer_plus_one));
75        mCircleView.startIntervalAnimation();
76        mTimerText.setTimeStrTextColor(false, true);
77        mTimerText.showTime(true);
78        mCircleView.setVisibility(VISIBLE);
79    }
80
81    public void pause() {
82        mResetAddButton.setImageResource(R.drawable.ic_reset);
83        mResetAddButton.setContentDescription(getResources().getString(R.string.timer_reset));
84        mCircleView.pauseIntervalAnimation();
85        mTimerText.setTimeStrTextColor(false, true);
86        mTimerText.showTime(true);
87        mCircleView.setVisibility(VISIBLE);
88    }
89
90    public void stop() {
91        mCircleView.stopIntervalAnimation();
92        mTimerText.setTimeStrTextColor(false, true);
93        mTimerText.showTime(true);
94        mCircleView.setVisibility(VISIBLE);
95    }
96
97    public void timesUp() {
98        mCircleView.abortIntervalAnimation();
99        mTimerText.setTimeStrTextColor(true, true);
100    }
101
102    public void done() {
103        mCircleView.stopIntervalAnimation();
104        mCircleView.setVisibility(VISIBLE);
105        mCircleView.invalidate();
106        mTimerText.setTimeStrTextColor(true, false);
107    }
108
109    public void setLength(long timerLength) {
110        mTimerLength = timerLength;
111        mCircleView.setIntervalTime(mTimerLength);
112        mCircleView.invalidate();
113    }
114
115    public void setTextBlink(boolean blink) {
116        mTimerText.showTime(!blink);
117    }
118
119    public void setCircleBlink(boolean blink) {
120        mCircleView.setVisibility(blink ? INVISIBLE : VISIBLE);
121    }
122
123    public void setResetAddButton(boolean isRunning, OnClickListener listener) {
124        if (mResetAddButton == null) {
125            mResetAddButton = (ImageView) findViewById(R.id.reset_add);
126        }
127        mResetAddButton.setImageResource(isRunning ? R.drawable.ic_plusone :
128                R.drawable.ic_reset);
129        mResetAddButton.setContentDescription(getResources().getString(
130                isRunning ? R.string.timer_plus_one : R.string.timer_reset));
131        mResetAddButton.setOnClickListener(listener);
132    }
133
134    public void setTime(long time, boolean forceUpdate) {
135        if (mTimerText == null) {
136            mTimerText = (CountingTimerView) findViewById(R.id.timer_time_text);
137        }
138        mTimerText.setTime(time, false, forceUpdate);
139    }
140
141    // Used by animator to animate the size of a timer
142    @SuppressWarnings("unused")
143    public void setAnimatedHeight(int height) {
144        ViewGroup.LayoutParams layoutParams = getLayoutParams();
145        if (layoutParams != null) {
146            layoutParams.height = height;
147            requestLayout();
148        }
149    }
150
151}
152