• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.widget;
18 
19 import android.content.Context;
20 import android.database.ContentObserver;
21 import android.os.Handler;
22 import android.os.SystemClock;
23 import android.provider.Settings;
24 import android.text.format.DateFormat;
25 import android.util.AttributeSet;
26 import java.util.Calendar;
27 
28 /**
29  * Like AnalogClock, but digital.  Shows seconds.
30  *
31  * @deprecated It is recommended you use {@link TextClock} instead.
32  */
33 @Deprecated
34 public class DigitalClock extends TextView {
35     // FIXME: implement separate views for hours/minutes/seconds, so
36     // proportional fonts don't shake rendering
37 
38     Calendar mCalendar;
39     @SuppressWarnings("FieldCanBeLocal") // We must keep a reference to this observer
40     private FormatChangeObserver mFormatChangeObserver;
41 
42     private Runnable mTicker;
43     private Handler mHandler;
44 
45     private boolean mTickerStopped = false;
46 
47     String mFormat;
48 
DigitalClock(Context context)49     public DigitalClock(Context context) {
50         super(context);
51         initClock();
52     }
53 
DigitalClock(Context context, AttributeSet attrs)54     public DigitalClock(Context context, AttributeSet attrs) {
55         super(context, attrs);
56         initClock();
57     }
58 
initClock()59     private void initClock() {
60         if (mCalendar == null) {
61             mCalendar = Calendar.getInstance();
62         }
63 
64         mFormatChangeObserver = new FormatChangeObserver();
65         getContext().getContentResolver().registerContentObserver(
66                 Settings.System.CONTENT_URI, true, mFormatChangeObserver);
67 
68         setFormat();
69     }
70 
71     @Override
onAttachedToWindow()72     protected void onAttachedToWindow() {
73         mTickerStopped = false;
74         super.onAttachedToWindow();
75         mHandler = new Handler();
76 
77         /**
78          * requests a tick on the next hard-second boundary
79          */
80         mTicker = new Runnable() {
81             public void run() {
82                 if (mTickerStopped) return;
83                 mCalendar.setTimeInMillis(System.currentTimeMillis());
84                 setText(DateFormat.format(mFormat, mCalendar));
85                 invalidate();
86                 long now = SystemClock.uptimeMillis();
87                 long next = now + (1000 - now % 1000);
88                 mHandler.postAtTime(mTicker, next);
89             }
90         };
91         mTicker.run();
92     }
93 
94     @Override
onDetachedFromWindow()95     protected void onDetachedFromWindow() {
96         super.onDetachedFromWindow();
97         mTickerStopped = true;
98     }
99 
setFormat()100     private void setFormat() {
101         mFormat = DateFormat.getTimeFormatString(getContext());
102     }
103 
104     private class FormatChangeObserver extends ContentObserver {
FormatChangeObserver()105         public FormatChangeObserver() {
106             super(new Handler());
107         }
108 
109         @Override
onChange(boolean selfChange)110         public void onChange(boolean selfChange) {
111             setFormat();
112         }
113     }
114 
115     @Override
getAccessibilityClassName()116     public CharSequence getAccessibilityClassName() {
117         //noinspection deprecation
118         return DigitalClock.class.getName();
119     }
120 }
121