OSDN Git Service

383ca7b026174b5bc88efb9761dd645afc5916e8
[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.Typeface;
26 import android.os.Handler;
27 import android.provider.Settings;
28 import android.text.format.DateFormat;
29 import android.util.AttributeSet;
30 import android.view.View;
31 import android.widget.LinearLayout;
32 import android.widget.TextView;
33
34 import java.text.DateFormatSymbols;
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 ContentObserver mFormatChangeObserver;
49     private boolean mLive = true;
50     private boolean mAttached;
51
52     /* called by system on minute ticks */
53     private final Handler mHandler = new Handler();
54     private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
55             @Override
56             public void onReceive(Context context, Intent intent) {
57                 if (mLive && intent.getAction().equals(
58                             Intent.ACTION_TIMEZONE_CHANGED)) {
59                     mCalendar = Calendar.getInstance();
60                 }
61                 updateTime();
62             }
63         };
64
65     static class AmPm {
66         private TextView mAmPm;
67         private String mAmString, mPmString;
68
69         AmPm(View parent, Typeface tf) {
70             mAmPm = (TextView) parent.findViewById(R.id.am_pm);
71             if (tf != null) {
72                 mAmPm.setTypeface(tf);
73             }
74
75             String[] ampm = new DateFormatSymbols().getAmPmStrings();
76             mAmString = ampm[0];
77             mPmString = ampm[1];
78         }
79
80         void setShowAmPm(boolean show) {
81             mAmPm.setVisibility(show ? View.VISIBLE : View.GONE);
82         }
83
84         void setIsMorning(boolean isMorning) {
85             mAmPm.setText(isMorning ? mAmString : mPmString);
86         }
87     }
88
89     private class FormatChangeObserver extends ContentObserver {
90         public FormatChangeObserver() {
91             super(new Handler());
92         }
93         @Override
94         public void onChange(boolean selfChange) {
95             setDateFormat();
96             updateTime();
97         }
98     }
99
100     public DigitalClock(Context context) {
101         this(context, null);
102     }
103
104     public DigitalClock(Context context, AttributeSet attrs) {
105         super(context, attrs);
106     }
107
108     @Override
109     protected void onFinishInflate() {
110         super.onFinishInflate();
111
112         Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
113                 "fonts/Clockopia.ttf");
114         mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
115         mTimeDisplay.setTypeface(tf);
116         mAmPm = new AmPm(this, null);
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 (mLive) {
132             /* monitor time ticks, time changed, timezone */
133             IntentFilter filter = new IntentFilter();
134             filter.addAction(Intent.ACTION_TIME_TICK);
135             filter.addAction(Intent.ACTION_TIME_CHANGED);
136             filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
137             mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
138         }
139
140         /* monitor 12/24-hour display preference */
141         mFormatChangeObserver = new FormatChangeObserver();
142         mContext.getContentResolver().registerContentObserver(
143                 Settings.System.CONTENT_URI, true, mFormatChangeObserver);
144
145         updateTime();
146     }
147
148     @Override
149     protected void onDetachedFromWindow() {
150         super.onDetachedFromWindow();
151
152         if (!mAttached) return;
153         mAttached = false;
154
155         if (mLive) {
156             mContext.unregisterReceiver(mIntentReceiver);
157         }
158         mContext.getContentResolver().unregisterContentObserver(
159                 mFormatChangeObserver);
160     }
161
162
163     void updateTime(Calendar c) {
164         mCalendar = c;
165         updateTime();
166     }
167
168     private void updateTime() {
169         if (mLive) {
170             mCalendar.setTimeInMillis(System.currentTimeMillis());
171         }
172
173         CharSequence newTime = DateFormat.format(mFormat, mCalendar);
174         mTimeDisplay.setText(newTime);
175         mAmPm.setIsMorning(mCalendar.get(Calendar.AM_PM) == 0);
176     }
177
178     private void setDateFormat() {
179         mFormat = Alarms.get24HourMode(mContext) ? Alarms.M24 : M12;
180         mAmPm.setShowAmPm(mFormat == M12);
181     }
182
183     void setLive(boolean live) {
184         mLive = live;
185     }
186 }