1/*
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.database.ContentObserver;
21import android.os.Handler;
22import android.os.SystemClock;
23import android.provider.Settings;
24import android.text.format.DateFormat;
25import android.util.AttributeSet;
26import android.view.accessibility.AccessibilityEvent;
27import android.view.accessibility.AccessibilityNodeInfo;
28
29import java.util.Calendar;
30
31/**
32 * Like AnalogClock, but digital.  Shows seconds.
33 *
34 * @deprecated It is recommended you use {@link TextClock} instead.
35 */
36@Deprecated
37public class DigitalClock extends TextView {
38    // FIXME: implement separate views for hours/minutes/seconds, so
39    // proportional fonts don't shake rendering
40
41    Calendar mCalendar;
42    private final static String m12 = "h:mm:ss aa";
43    private final static String m24 = "k:mm:ss";
44    @SuppressWarnings("FieldCanBeLocal") // We must keep a reference to this observer
45    private FormatChangeObserver mFormatChangeObserver;
46
47    private Runnable mTicker;
48    private Handler mHandler;
49
50    private boolean mTickerStopped = false;
51
52    String mFormat;
53
54    public DigitalClock(Context context) {
55        super(context);
56        initClock();
57    }
58
59    public DigitalClock(Context context, AttributeSet attrs) {
60        super(context, attrs);
61        initClock();
62    }
63
64    private void initClock() {
65        if (mCalendar == null) {
66            mCalendar = Calendar.getInstance();
67        }
68
69        mFormatChangeObserver = new FormatChangeObserver();
70        getContext().getContentResolver().registerContentObserver(
71                Settings.System.CONTENT_URI, true, mFormatChangeObserver);
72
73        setFormat();
74    }
75
76    @Override
77    protected void onAttachedToWindow() {
78        mTickerStopped = false;
79        super.onAttachedToWindow();
80        mHandler = new Handler();
81
82        /**
83         * requests a tick on the next hard-second boundary
84         */
85        mTicker = new Runnable() {
86            public void run() {
87                if (mTickerStopped) return;
88                mCalendar.setTimeInMillis(System.currentTimeMillis());
89                setText(DateFormat.format(mFormat, mCalendar));
90                invalidate();
91                long now = SystemClock.uptimeMillis();
92                long next = now + (1000 - now % 1000);
93                mHandler.postAtTime(mTicker, next);
94            }
95        };
96        mTicker.run();
97    }
98
99    @Override
100    protected void onDetachedFromWindow() {
101        super.onDetachedFromWindow();
102        mTickerStopped = true;
103    }
104
105    /**
106     * Pulls 12/24 mode from system settings
107     */
108    private boolean get24HourMode() {
109        return android.text.format.DateFormat.is24HourFormat(getContext());
110    }
111
112    private void setFormat() {
113        if (get24HourMode()) {
114            mFormat = m24;
115        } else {
116            mFormat = m12;
117        }
118    }
119
120    private class FormatChangeObserver extends ContentObserver {
121        public FormatChangeObserver() {
122            super(new Handler());
123        }
124
125        @Override
126        public void onChange(boolean selfChange) {
127            setFormat();
128        }
129    }
130
131    @Override
132    public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
133        super.onInitializeAccessibilityEvent(event);
134        //noinspection deprecation
135        event.setClassName(DigitalClock.class.getName());
136    }
137
138    @Override
139    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
140        super.onInitializeAccessibilityNodeInfo(info);
141        //noinspection deprecation
142        info.setClassName(DigitalClock.class.getName());
143    }
144}
145