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