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