blob: 1716dbd86f2b9c4dee8cf1ed7e25ec0795e43ae3 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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 android.widget;
18
19import android.content.Context;
20import android.content.Intent;
21import android.content.IntentFilter;
22import android.content.BroadcastReceiver;
23import android.content.res.Resources;
24import android.content.res.TypedArray;
25import android.graphics.Canvas;
26import android.graphics.drawable.Drawable;
27import android.os.Handler;
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070028import android.text.format.DateUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.text.format.Time;
30import android.util.AttributeSet;
31import android.view.View;
32import android.widget.RemoteViews.RemoteView;
33
34import java.util.TimeZone;
35
36/**
37 * This widget display an analogic clock with two hands for hours and
38 * minutes.
Scott Mained5ff3b2012-09-26 11:42:09 -070039 *
40 * @attr ref android.R.styleable#AnalogClock_dial
41 * @attr ref android.R.styleable#AnalogClock_hand_hour
42 * @attr ref android.R.styleable#AnalogClock_hand_minute
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 */
44@RemoteView
45public class AnalogClock extends View {
46 private Time mCalendar;
47
48 private Drawable mHourHand;
49 private Drawable mMinuteHand;
50 private Drawable mDial;
51
52 private int mDialWidth;
53 private int mDialHeight;
54
55 private boolean mAttached;
56
57 private final Handler mHandler = new Handler();
58 private float mMinutes;
59 private float mHour;
60 private boolean mChanged;
61
62 public AnalogClock(Context context) {
63 this(context, null);
64 }
65
66 public AnalogClock(Context context, AttributeSet attrs) {
67 this(context, attrs, 0);
68 }
69
Alan Viverette617feb92013-09-09 18:09:13 -070070 public AnalogClock(Context context, AttributeSet attrs, int defStyleAttr) {
71 this(context, attrs, defStyleAttr, 0);
72 }
73
74 public AnalogClock(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
75 super(context, attrs, defStyleAttr, defStyleRes);
76
77 final Resources r = context.getResources();
78 final TypedArray a = context.obtainStyledAttributes(
79 attrs, com.android.internal.R.styleable.AnalogClock, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
81 mDial = a.getDrawable(com.android.internal.R.styleable.AnalogClock_dial);
82 if (mDial == null) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -080083 mDial = context.getDrawable(com.android.internal.R.drawable.clock_dial);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 }
85
86 mHourHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_hour);
87 if (mHourHand == null) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -080088 mHourHand = context.getDrawable(com.android.internal.R.drawable.clock_hand_hour);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 }
90
91 mMinuteHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_minute);
92 if (mMinuteHand == null) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -080093 mMinuteHand = context.getDrawable(com.android.internal.R.drawable.clock_hand_minute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 }
95
96 mCalendar = new Time();
97
98 mDialWidth = mDial.getIntrinsicWidth();
99 mDialHeight = mDial.getIntrinsicHeight();
100 }
101
102 @Override
103 protected void onAttachedToWindow() {
104 super.onAttachedToWindow();
105
106 if (!mAttached) {
107 mAttached = true;
108 IntentFilter filter = new IntentFilter();
109
110 filter.addAction(Intent.ACTION_TIME_TICK);
111 filter.addAction(Intent.ACTION_TIME_CHANGED);
112 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
113
Svetoslav976e8bd2014-07-16 15:12:03 -0700114 // OK, this is gross but needed. This class is supported by the
115 // remote views machanism and as a part of that the remote views
116 // can be inflated by a context for another user without the app
117 // having interact users permission - just for loading resources.
118 // For exmaple, when adding widgets from a user profile to the
119 // home screen. Therefore, we register the receiver as the current
120 // user not the one the context is for.
121 getContext().registerReceiverAsUser(mIntentReceiver,
122 android.os.Process.myUserHandle(), filter, null, mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 }
124
125 // NOTE: It's safe to do these after registering the receiver since the receiver always runs
126 // in the main thread, therefore the receiver can't run before this method returns.
127
128 // The time zone may have changed while the receiver wasn't registered, so update the Time
129 mCalendar = new Time();
130
131 // Make sure we update to the current time
132 onTimeChanged();
133 }
134
135 @Override
136 protected void onDetachedFromWindow() {
137 super.onDetachedFromWindow();
138 if (mAttached) {
139 getContext().unregisterReceiver(mIntentReceiver);
140 mAttached = false;
141 }
142 }
143
144 @Override
145 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
146
147 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
148 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
149 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
150 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
151
152 float hScale = 1.0f;
153 float vScale = 1.0f;
154
155 if (widthMode != MeasureSpec.UNSPECIFIED && widthSize < mDialWidth) {
156 hScale = (float) widthSize / (float) mDialWidth;
157 }
158
159 if (heightMode != MeasureSpec.UNSPECIFIED && heightSize < mDialHeight) {
160 vScale = (float )heightSize / (float) mDialHeight;
161 }
162
163 float scale = Math.min(hScale, vScale);
164
Dianne Hackborn189ee182010-12-02 21:48:53 -0800165 setMeasuredDimension(resolveSizeAndState((int) (mDialWidth * scale), widthMeasureSpec, 0),
166 resolveSizeAndState((int) (mDialHeight * scale), heightMeasureSpec, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 }
168
169 @Override
170 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
171 super.onSizeChanged(w, h, oldw, oldh);
172 mChanged = true;
173 }
174
175 @Override
176 protected void onDraw(Canvas canvas) {
177 super.onDraw(canvas);
178
179 boolean changed = mChanged;
180 if (changed) {
181 mChanged = false;
182 }
183
184 int availableWidth = mRight - mLeft;
185 int availableHeight = mBottom - mTop;
186
187 int x = availableWidth / 2;
188 int y = availableHeight / 2;
189
190 final Drawable dial = mDial;
191 int w = dial.getIntrinsicWidth();
192 int h = dial.getIntrinsicHeight();
193
194 boolean scaled = false;
195
196 if (availableWidth < w || availableHeight < h) {
197 scaled = true;
198 float scale = Math.min((float) availableWidth / (float) w,
199 (float) availableHeight / (float) h);
200 canvas.save();
201 canvas.scale(scale, scale, x, y);
202 }
203
204 if (changed) {
205 dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
206 }
207 dial.draw(canvas);
208
209 canvas.save();
210 canvas.rotate(mHour / 12.0f * 360.0f, x, y);
211 final Drawable hourHand = mHourHand;
212 if (changed) {
213 w = hourHand.getIntrinsicWidth();
214 h = hourHand.getIntrinsicHeight();
215 hourHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
216 }
217 hourHand.draw(canvas);
218 canvas.restore();
219
220 canvas.save();
221 canvas.rotate(mMinutes / 60.0f * 360.0f, x, y);
222
223 final Drawable minuteHand = mMinuteHand;
224 if (changed) {
225 w = minuteHand.getIntrinsicWidth();
226 h = minuteHand.getIntrinsicHeight();
227 minuteHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
228 }
229 minuteHand.draw(canvas);
230 canvas.restore();
231
232 if (scaled) {
233 canvas.restore();
234 }
235 }
236
237 private void onTimeChanged() {
238 mCalendar.setToNow();
239
240 int hour = mCalendar.hour;
241 int minute = mCalendar.minute;
242 int second = mCalendar.second;
243
244 mMinutes = minute + second / 60.0f;
245 mHour = hour + mMinutes / 60.0f;
246 mChanged = true;
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -0700247
248 updateContentDescription(mCalendar);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 }
250
251 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
252 @Override
253 public void onReceive(Context context, Intent intent) {
254 if (intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
255 String tz = intent.getStringExtra("time-zone");
256 mCalendar = new Time(TimeZone.getTimeZone(tz).getID());
257 }
258
259 onTimeChanged();
260
261 invalidate();
262 }
263 };
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -0700264
265 private void updateContentDescription(Time time) {
266 final int flags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_24HOUR;
267 String contentDescription = DateUtils.formatDateTime(mContext,
268 time.toMillis(false), flags);
269 setContentDescription(contentDescription);
270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271}