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