blob: 8ebf303136ef961a829e3f3deea86a9040ad69b1 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.database.ContentObserver;
21import android.os.Handler;
22import android.os.SystemClock;
23import android.provider.Settings;
24import android.text.format.DateFormat;
25import android.util.AttributeSet;
Aurimas Liutikas99441c52016-10-11 16:48:32 -070026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import java.util.Calendar;
28
29/**
David Castro3e103552018-09-17 09:38:14 -040030 * Like AnalogClock, but digital.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031 *
Romain Guy3d1728c2012-10-31 20:31:58 -070032 * @deprecated It is recommended you use {@link TextClock} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 */
Romain Guy86ca5d32012-07-03 17:23:30 -070034@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035public class DigitalClock extends TextView {
Romain Guy3d1728c2012-10-31 20:31:58 -070036 // FIXME: implement separate views for hours/minutes/seconds, so
37 // proportional fonts don't shake rendering
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39 Calendar mCalendar;
Romain Guy86ca5d32012-07-03 17:23:30 -070040 @SuppressWarnings("FieldCanBeLocal") // We must keep a reference to this observer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 private FormatChangeObserver mFormatChangeObserver;
42
43 private Runnable mTicker;
44 private Handler mHandler;
45
46 private boolean mTickerStopped = false;
47
48 String mFormat;
49
50 public DigitalClock(Context context) {
51 super(context);
Romain Guy86ca5d32012-07-03 17:23:30 -070052 initClock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 }
54
55 public DigitalClock(Context context, AttributeSet attrs) {
56 super(context, attrs);
Romain Guy86ca5d32012-07-03 17:23:30 -070057 initClock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 }
59
Romain Guy86ca5d32012-07-03 17:23:30 -070060 private void initClock() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 if (mCalendar == null) {
62 mCalendar = Calendar.getInstance();
63 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 }
65
66 @Override
67 protected void onAttachedToWindow() {
68 mTickerStopped = false;
69 super.onAttachedToWindow();
John Reckd0374c62015-10-20 13:25:01 -070070
71 mFormatChangeObserver = new FormatChangeObserver();
72 getContext().getContentResolver().registerContentObserver(
73 Settings.System.CONTENT_URI, true, mFormatChangeObserver);
74 setFormat();
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 mHandler = new Handler();
77
78 /**
79 * requests a tick on the next hard-second boundary
80 */
81 mTicker = new Runnable() {
Romain Guy3d1728c2012-10-31 20:31:58 -070082 public void run() {
83 if (mTickerStopped) return;
84 mCalendar.setTimeInMillis(System.currentTimeMillis());
85 setText(DateFormat.format(mFormat, mCalendar));
86 invalidate();
87 long now = SystemClock.uptimeMillis();
88 long next = now + (1000 - now % 1000);
89 mHandler.postAtTime(mTicker, next);
90 }
91 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 mTicker.run();
93 }
94
95 @Override
96 protected void onDetachedFromWindow() {
97 super.onDetachedFromWindow();
98 mTickerStopped = true;
John Reckd0374c62015-10-20 13:25:01 -070099 getContext().getContentResolver().unregisterContentObserver(
100 mFormatChangeObserver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 }
102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 private void setFormat() {
Elliott Hughescdafd372013-03-18 17:21:33 -0700104 mFormat = DateFormat.getTimeFormatString(getContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 }
106
107 private class FormatChangeObserver extends ContentObserver {
108 public FormatChangeObserver() {
109 super(new Handler());
110 }
111
112 @Override
113 public void onChange(boolean selfChange) {
114 setFormat();
115 }
116 }
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800117
118 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800119 public CharSequence getAccessibilityClassName() {
Romain Guy3d1728c2012-10-31 20:31:58 -0700120 //noinspection deprecation
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800121 return DigitalClock.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123}