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