blob: 95f1004331377ffe6aafdf65279114972337e7cf [file] [log] [blame]
Robert Snoeberger046ee9c2019-01-10 18:29:38 -05001/*
2 * Copyright (C) 2019 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 */
16package com.android.keyguard.clock;
17
18import android.content.Context;
19import android.text.format.DateFormat;
20import android.util.AttributeSet;
21import android.widget.FrameLayout;
22import android.widget.ImageView;
23
24import com.android.keyguard.R;
25
26import java.text.SimpleDateFormat;
27import java.util.Calendar;
28import java.util.TimeZone;
29
30/**
31 * Clock composed of two images that rotate with the time.
32 *
33 * The images are the clock hands. ImageClock expects two child ImageViews
34 * with ids hour_hand and minute_hand.
35 */
36public class ImageClock extends FrameLayout {
37
38 private ImageView mHourHand;
39 private ImageView mMinuteHand;
Robert Snoeberger71e50792019-02-15 15:48:01 -050040 private final Calendar mTime = Calendar.getInstance(TimeZone.getDefault());
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050041 private String mDescFormat;
42 private TimeZone mTimeZone;
43
44 public ImageClock(Context context) {
45 this(context, null);
46 }
47
48 public ImageClock(Context context, AttributeSet attrs) {
49 this(context, attrs, 0);
50 }
51
52 public ImageClock(Context context, AttributeSet attrs, int defStyleAttr) {
53 super(context, attrs, defStyleAttr);
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050054 mDescFormat = ((SimpleDateFormat) DateFormat.getTimeFormat(context)).toLocalizedPattern();
55 }
56
57 /**
58 * Call when the time changes to update the rotation of the clock hands.
59 */
60 public void onTimeChanged() {
61 mTime.setTimeInMillis(System.currentTimeMillis());
Robert Snoeberger09c6d492019-03-08 16:57:40 -050062 final float hourAngle = mTime.get(Calendar.HOUR) * 30f + mTime.get(Calendar.MINUTE) * 0.5f;
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050063 mHourHand.setRotation(hourAngle);
64 final float minuteAngle = mTime.get(Calendar.MINUTE) * 6f;
65 mMinuteHand.setRotation(minuteAngle);
66 setContentDescription(DateFormat.format(mDescFormat, mTime));
67 invalidate();
68 }
69
70 /**
71 * Call when the time zone has changed to update clock hands.
72 *
73 * @param timeZone The updated time zone that will be used.
74 */
75 public void onTimeZoneChanged(TimeZone timeZone) {
76 mTimeZone = timeZone;
77 mTime.setTimeZone(timeZone);
78 }
79
Robert Snoeberger0397c842019-02-07 14:25:46 -050080 /**
81 * Sets the colors to use on the clock face.
82 * @param dark Darker color obtained from color palette.
83 * @param light Lighter color obtained from color palette.
84 */
85 public void setClockColors(int dark, int light) {
86 mHourHand.setColorFilter(dark);
87 mMinuteHand.setColorFilter(light);
88 }
89
Robert Snoeberger046ee9c2019-01-10 18:29:38 -050090 @Override
91 protected void onFinishInflate() {
92 super.onFinishInflate();
93 mHourHand = findViewById(R.id.hour_hand);
94 mMinuteHand = findViewById(R.id.minute_hand);
95 }
96
97 @Override
98 protected void onAttachedToWindow() {
99 super.onAttachedToWindow();
Robert Snoeberger71e50792019-02-15 15:48:01 -0500100 mTime.setTimeZone(mTimeZone != null ? mTimeZone : TimeZone.getDefault());
Robert Snoeberger046ee9c2019-01-10 18:29:38 -0500101 onTimeChanged();
102 }
103}