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.util.AttributeSet;
21 import android.view.LayoutInflater;
22 import android.view.ViewGroup;
23 import android.widget.ImageView;
24 import android.widget.LinearLayout;
25 
26 import com.android.deskclock.CircleTimerView;
27 import com.android.deskclock.R;
28 
29 
30 public class TimerListItem extends LinearLayout {
31 
32     CountingTimerView mTimerText;
33     CircleTimerView mCircleView;
34     ImageView mResetAddButton;
35 
36     long mTimerLength;
37 
TimerListItem(Context context)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 
TimerListItem(Context context, AttributeSet attrs)48     public TimerListItem(Context context, AttributeSet attrs) {
49         super(context, attrs);
50     }
51 
52     @Override
onFinishInflate()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 
set(long timerLength, long timeLeft, boolean drawRed)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 
start()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 
pause()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 
stop()90     public void stop() {
91         mCircleView.stopIntervalAnimation();
92         mTimerText.setTimeStrTextColor(false, true);
93         mTimerText.showTime(true);
94         mCircleView.setVisibility(VISIBLE);
95     }
96 
timesUp()97     public void timesUp() {
98         mCircleView.abortIntervalAnimation();
99         mTimerText.setTimeStrTextColor(true, true);
100     }
101 
done()102     public void done() {
103         mCircleView.stopIntervalAnimation();
104         mCircleView.setVisibility(VISIBLE);
105         mCircleView.invalidate();
106         mTimerText.setTimeStrTextColor(true, false);
107     }
108 
setLength(long timerLength)109     public void setLength(long timerLength) {
110         mTimerLength = timerLength;
111         mCircleView.setIntervalTime(mTimerLength);
112         mCircleView.invalidate();
113     }
114 
setTextBlink(boolean blink)115     public void setTextBlink(boolean blink) {
116         mTimerText.showTime(!blink);
117     }
118 
setCircleBlink(boolean blink)119     public void setCircleBlink(boolean blink) {
120         mCircleView.setVisibility(blink ? INVISIBLE : VISIBLE);
121     }
122 
setResetAddButton(boolean isRunning, OnClickListener listener)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 
setTime(long time, boolean forceUpdate)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")
setAnimatedHeight(int height)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