2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.deskclock;
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.content.res.Resources;
24 import android.database.ContentObserver;
25 import android.graphics.drawable.AnimationDrawable;
26 import android.graphics.drawable.Drawable;
27 import android.os.Handler;
28 import android.provider.Settings;
29 import android.text.format.DateFormat;
30 import android.util.AttributeSet;
31 import android.view.View;
32 import android.widget.LinearLayout;
33 import android.widget.TextView;
35 import java.util.Calendar;
40 public class DigitalClock extends LinearLayout {
42 private final static String M12 = "h:mm";
44 private Calendar mCalendar;
45 private String mFormat;
46 private TextView mTimeDisplay;
48 private boolean mAnimate;
49 private ContentObserver mFormatChangeObserver;
50 private boolean mLive = true;
51 private boolean mAttached;
53 /* called by system on minute ticks */
54 private final Handler mHandler = new Handler();
55 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
57 public void onReceive(Context context, Intent intent) {
58 if (mLive && intent.getAction().equals(
59 Intent.ACTION_TIMEZONE_CHANGED)) {
60 mCalendar = Calendar.getInstance();
67 private int mColorOn, mColorOff;
69 private LinearLayout mAmPmLayout;
70 private TextView mAm, mPm;
73 mAmPmLayout = (LinearLayout) parent.findViewById(R.id.am_pm);
74 mAm = (TextView)mAmPmLayout.findViewById(R.id.am);
75 mPm = (TextView)mAmPmLayout.findViewById(R.id.pm);
77 Resources r = parent.getResources();
78 mColorOn = r.getColor(R.color.ampm_on);
79 mColorOff = r.getColor(R.color.ampm_off);
82 void setShowAmPm(boolean show) {
83 mAmPmLayout.setVisibility(show ? View.VISIBLE : View.GONE);
86 void setIsMorning(boolean isMorning) {
87 mAm.setTextColor(isMorning ? mColorOn : mColorOff);
88 mPm.setTextColor(isMorning ? mColorOff : mColorOn);
92 private class FormatChangeObserver extends ContentObserver {
93 public FormatChangeObserver() {
97 public void onChange(boolean selfChange) {
103 public DigitalClock(Context context) {
107 public DigitalClock(Context context, AttributeSet attrs) {
108 super(context, attrs);
112 protected void onFinishInflate() {
113 super.onFinishInflate();
115 mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
116 mAmPm = new AmPm(this);
117 mCalendar = Calendar.getInstance();
123 protected void onAttachedToWindow() {
124 super.onAttachedToWindow();
126 if (Log.LOGV) Log.v("onAttachedToWindow " + this);
128 if (mAttached) return;
132 setBackgroundResource(R.drawable.animate_circle);
133 /* Start the animation (looped playback by default). */
134 ((AnimationDrawable) getBackground()).start();
138 /* monitor time ticks, time changed, timezone */
139 IntentFilter filter = new IntentFilter();
140 filter.addAction(Intent.ACTION_TIME_TICK);
141 filter.addAction(Intent.ACTION_TIME_CHANGED);
142 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
143 mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
146 /* monitor 12/24-hour display preference */
147 mFormatChangeObserver = new FormatChangeObserver();
148 mContext.getContentResolver().registerContentObserver(
149 Settings.System.CONTENT_URI, true, mFormatChangeObserver);
155 protected void onDetachedFromWindow() {
156 super.onDetachedFromWindow();
158 if (!mAttached) return;
161 Drawable background = getBackground();
162 if (background instanceof AnimationDrawable) {
163 ((AnimationDrawable) background).stop();
167 mContext.unregisterReceiver(mIntentReceiver);
169 mContext.getContentResolver().unregisterContentObserver(
170 mFormatChangeObserver);
174 void updateTime(Calendar c) {
179 private void updateTime() {
181 mCalendar.setTimeInMillis(System.currentTimeMillis());
184 CharSequence newTime = DateFormat.format(mFormat, mCalendar);
185 mTimeDisplay.setText(newTime);
186 mAmPm.setIsMorning(mCalendar.get(Calendar.AM_PM) == 0);
189 private void setDateFormat() {
190 mFormat = Alarms.get24HourMode(mContext) ? Alarms.M24 : M12;
191 mAmPm.setShowAmPm(mFormat == M12);
198 void setLive(boolean live) {