OSDN Git Service

d28cc7de78c0e742c306cf40235da89aa1c828f4
[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             mAmPm.setTypeface(tf);
72
73             String[] ampm = new DateFormatSymbols().getAmPmStrings();
74             mAmString = ampm[0];
75             mPmString = ampm[1];
76         }
77
78         void setShowAmPm(boolean show) {
79             mAmPm.setVisibility(show ? View.VISIBLE : View.GONE);
80         }
81
82         void setIsMorning(boolean isMorning) {
83             mAmPm.setText(isMorning ? mAmString : mPmString);
84         }
85     }
86
87     private class FormatChangeObserver extends ContentObserver {
88         public FormatChangeObserver() {
89             super(new Handler());
90         }
91         @Override
92         public void onChange(boolean selfChange) {
93             setDateFormat();
94             updateTime();
95         }
96     }
97
98     public DigitalClock(Context context) {
99         this(context, null);
100     }
101
102     public DigitalClock(Context context, AttributeSet attrs) {
103         super(context, attrs);
104     }
105
106     @Override
107     protected void onFinishInflate() {
108         super.onFinishInflate();
109
110         Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
111                 "fonts/Clockopia.ttf");
112         mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
113         mTimeDisplay.setTypeface(tf);
114         mAmPm = new AmPm(this, tf);
115         mCalendar = Calendar.getInstance();
116
117         setDateFormat();
118     }
119
120     @Override
121     protected void onAttachedToWindow() {
122         super.onAttachedToWindow();
123
124         if (Log.LOGV) Log.v("onAttachedToWindow " + this);
125
126         if (mAttached) return;
127         mAttached = true;
128
129         if (mLive) {
130             /* monitor time ticks, time changed, timezone */
131             IntentFilter filter = new IntentFilter();
132             filter.addAction(Intent.ACTION_TIME_TICK);
133             filter.addAction(Intent.ACTION_TIME_CHANGED);
134             filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
135             mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
136         }
137
138         /* monitor 12/24-hour display preference */
139         mFormatChangeObserver = new FormatChangeObserver();
140         mContext.getContentResolver().registerContentObserver(
141                 Settings.System.CONTENT_URI, true, mFormatChangeObserver);
142
143         updateTime();
144     }
145
146     @Override
147     protected void onDetachedFromWindow() {
148         super.onDetachedFromWindow();
149
150         if (!mAttached) return;
151         mAttached = false;
152
153         if (mLive) {
154             mContext.unregisterReceiver(mIntentReceiver);
155         }
156         mContext.getContentResolver().unregisterContentObserver(
157                 mFormatChangeObserver);
158     }
159
160
161     void updateTime(Calendar c) {
162         mCalendar = c;
163         updateTime();
164     }
165
166     private void updateTime() {
167         if (mLive) {
168             mCalendar.setTimeInMillis(System.currentTimeMillis());
169         }
170
171         CharSequence newTime = DateFormat.format(mFormat, mCalendar);
172         mTimeDisplay.setText(newTime);
173         mAmPm.setIsMorning(mCalendar.get(Calendar.AM_PM) == 0);
174     }
175
176     private void setDateFormat() {
177         mFormat = Alarms.get24HourMode(mContext) ? Alarms.M24 : M12;
178         mAmPm.setShowAmPm(mFormat == M12);
179     }
180
181     void setLive(boolean live) {
182         mLive = live;
183     }
184 }