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