OSDN Git Service

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