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