OSDN Git Service

Moving things around a little in the new DeskClock.
[android-x86/packages-apps-DeskClock.git] / src / com / android / deskclock / DigitalClock.java
1 /*
2  * Copyright (C) 2008 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 com.android.deskclock;
18
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;
34
35 import java.util.Calendar;
36
37 /**
38  * Displays the time
39  */
40 public class DigitalClock extends LinearLayout {
41
42     private final static String M12 = "h:mm";
43
44     private Calendar mCalendar;
45     private String mFormat;
46     private TextView mTimeDisplay;
47     private AmPm mAmPm;
48     private boolean mAnimate;
49     private ContentObserver mFormatChangeObserver;
50     private boolean mLive = true;
51     private boolean mAttached;
52
53     /* called by system on minute ticks */
54     private final Handler mHandler = new Handler();
55     private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
56             @Override
57             public void onReceive(Context context, Intent intent) {
58                 if (mLive && intent.getAction().equals(
59                             Intent.ACTION_TIMEZONE_CHANGED)) {
60                     mCalendar = Calendar.getInstance();
61                 }
62                 updateTime();
63             }
64         };
65
66     static class AmPm {
67         private int mColorOn, mColorOff;
68
69         private LinearLayout mAmPmLayout;
70         private TextView mAm, mPm;
71
72         AmPm(View parent) {
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);
76
77             Resources r = parent.getResources();
78             mColorOn = r.getColor(R.color.ampm_on);
79             mColorOff = r.getColor(R.color.ampm_off);
80         }
81
82         void setShowAmPm(boolean show) {
83             mAmPmLayout.setVisibility(show ? View.VISIBLE : View.GONE);
84         }
85
86         void setIsMorning(boolean isMorning) {
87             mAm.setTextColor(isMorning ? mColorOn : mColorOff);
88             mPm.setTextColor(isMorning ? mColorOff : mColorOn);
89         }
90     }
91
92     private class FormatChangeObserver extends ContentObserver {
93         public FormatChangeObserver() {
94             super(new Handler());
95         }
96         @Override
97         public void onChange(boolean selfChange) {
98             setDateFormat();
99             updateTime();
100         }
101     }
102
103     public DigitalClock(Context context) {
104         this(context, null);
105     }
106
107     public DigitalClock(Context context, AttributeSet attrs) {
108         super(context, attrs);
109     }
110
111     @Override
112     protected void onFinishInflate() {
113         super.onFinishInflate();
114
115         mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
116         mAmPm = new AmPm(this);
117         mCalendar = Calendar.getInstance();
118
119         setDateFormat();
120     }
121
122     @Override
123     protected void onAttachedToWindow() {
124         super.onAttachedToWindow();
125
126         if (Log.LOGV) Log.v("onAttachedToWindow " + this);
127
128         if (mAttached) return;
129         mAttached = true;
130
131         if (mAnimate) {
132             setBackgroundResource(R.drawable.animate_circle);
133             /* Start the animation (looped playback by default). */
134             ((AnimationDrawable) getBackground()).start();
135         }
136
137         if (mLive) {
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);
144         }
145
146         /* monitor 12/24-hour display preference */
147         mFormatChangeObserver = new FormatChangeObserver();
148         mContext.getContentResolver().registerContentObserver(
149                 Settings.System.CONTENT_URI, true, mFormatChangeObserver);
150
151         updateTime();
152     }
153
154     @Override
155     protected void onDetachedFromWindow() {
156         super.onDetachedFromWindow();
157
158         if (!mAttached) return;
159         mAttached = false;
160
161         Drawable background = getBackground();
162         if (background instanceof AnimationDrawable) {
163             ((AnimationDrawable) background).stop();
164         }
165
166         if (mLive) {
167             mContext.unregisterReceiver(mIntentReceiver);
168         }
169         mContext.getContentResolver().unregisterContentObserver(
170                 mFormatChangeObserver);
171     }
172
173
174     void updateTime(Calendar c) {
175         mCalendar = c;
176         updateTime();
177     }
178
179     private void updateTime() {
180         if (mLive) {
181             mCalendar.setTimeInMillis(System.currentTimeMillis());
182         }
183
184         CharSequence newTime = DateFormat.format(mFormat, mCalendar);
185         mTimeDisplay.setText(newTime);
186         mAmPm.setIsMorning(mCalendar.get(Calendar.AM_PM) == 0);
187     }
188
189     private void setDateFormat() {
190         mFormat = Alarms.get24HourMode(mContext) ? Alarms.M24 : M12;
191         mAmPm.setShowAmPm(mFormat == M12);
192     }
193
194     void setAnimate() {
195         mAnimate = true;
196     }
197
198     void setLive(boolean live) {
199         mLive = live;
200     }
201 }