Home | History | Annotate | Download | only in widget
      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 
     49     public DigitalClock(Context context) {
     50         super(context);
     51         initClock();
     52     }
     53 
     54     public DigitalClock(Context context, AttributeSet attrs) {
     55         super(context, attrs);
     56         initClock();
     57     }
     58 
     59     private void initClock() {
     60         if (mCalendar == null) {
     61             mCalendar = Calendar.getInstance();
     62         }
     63     }
     64 
     65     @Override
     66     protected void onAttachedToWindow() {
     67         mTickerStopped = false;
     68         super.onAttachedToWindow();
     69 
     70         mFormatChangeObserver = new FormatChangeObserver();
     71         getContext().getContentResolver().registerContentObserver(
     72                 Settings.System.CONTENT_URI, true, mFormatChangeObserver);
     73         setFormat();
     74 
     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
     95     protected void onDetachedFromWindow() {
     96         super.onDetachedFromWindow();
     97         mTickerStopped = true;
     98         getContext().getContentResolver().unregisterContentObserver(
     99                 mFormatChangeObserver);
    100     }
    101 
    102     private void setFormat() {
    103         mFormat = DateFormat.getTimeFormatString(getContext());
    104     }
    105 
    106     private class FormatChangeObserver extends ContentObserver {
    107         public FormatChangeObserver() {
    108             super(new Handler());
    109         }
    110 
    111         @Override
    112         public void onChange(boolean selfChange) {
    113             setFormat();
    114         }
    115     }
    116 
    117     @Override
    118     public CharSequence getAccessibilityClassName() {
    119         //noinspection deprecation
    120         return DigitalClock.class.getName();
    121     }
    122 }
    123