The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 26 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import java.util.Calendar; |
| 28 | |
| 29 | /** |
| 30 | * Like AnalogClock, but digital. Shows seconds. |
| 31 | * |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 32 | * @deprecated It is recommended you use {@link TextClock} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | */ |
Romain Guy | 86ca5d3 | 2012-07-03 17:23:30 -0700 | [diff] [blame] | 34 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | public class DigitalClock extends TextView { |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 36 | // FIXME: implement separate views for hours/minutes/seconds, so |
| 37 | // proportional fonts don't shake rendering |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
| 39 | Calendar mCalendar; |
Romain Guy | 86ca5d3 | 2012-07-03 17:23:30 -0700 | [diff] [blame] | 40 | @SuppressWarnings("FieldCanBeLocal") // We must keep a reference to this observer |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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); |
Romain Guy | 86ca5d3 | 2012-07-03 17:23:30 -0700 | [diff] [blame] | 52 | initClock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | public DigitalClock(Context context, AttributeSet attrs) { |
| 56 | super(context, attrs); |
Romain Guy | 86ca5d3 | 2012-07-03 17:23:30 -0700 | [diff] [blame] | 57 | initClock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Romain Guy | 86ca5d3 | 2012-07-03 17:23:30 -0700 | [diff] [blame] | 60 | private void initClock() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | if (mCalendar == null) { |
| 62 | mCalendar = Calendar.getInstance(); |
| 63 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
| 67 | protected void onAttachedToWindow() { |
| 68 | mTickerStopped = false; |
| 69 | super.onAttachedToWindow(); |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 70 | |
| 71 | mFormatChangeObserver = new FormatChangeObserver(); |
| 72 | getContext().getContentResolver().registerContentObserver( |
| 73 | Settings.System.CONTENT_URI, true, mFormatChangeObserver); |
| 74 | setFormat(); |
| 75 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | mHandler = new Handler(); |
| 77 | |
| 78 | /** |
| 79 | * requests a tick on the next hard-second boundary |
| 80 | */ |
| 81 | mTicker = new Runnable() { |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 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 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | mTicker.run(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | protected void onDetachedFromWindow() { |
| 97 | super.onDetachedFromWindow(); |
| 98 | mTickerStopped = true; |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 99 | getContext().getContentResolver().unregisterContentObserver( |
| 100 | mFormatChangeObserver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | } |
| 102 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | private void setFormat() { |
Elliott Hughes | cdafd37 | 2013-03-18 17:21:33 -0700 | [diff] [blame] | 104 | mFormat = DateFormat.getTimeFormatString(getContext()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 117 | |
| 118 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 119 | public CharSequence getAccessibilityClassName() { |
Romain Guy | 3d1728c | 2012-10-31 20:31:58 -0700 | [diff] [blame] | 120 | //noinspection deprecation |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 121 | return DigitalClock.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 122 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | } |