Clock.java revision fd52b18d9bf3cd62c7a07058536e9f97db65beea
1/*
2 * Copyright (C) 2006 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
17package com.android.systemui.statusbar.policy;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.content.res.Resources;
24import android.content.res.TypedArray;
25import android.graphics.Canvas;
26import android.graphics.Typeface;
27import android.graphics.drawable.Drawable;
28import android.text.Spannable;
29import android.text.SpannableStringBuilder;
30import android.text.format.DateFormat;
31import android.text.style.CharacterStyle;
32import android.text.style.ForegroundColorSpan;
33import android.text.style.RelativeSizeSpan;
34import android.text.style.RelativeSizeSpan;
35import android.text.style.StyleSpan;
36import android.util.AttributeSet;
37import android.view.View;
38import android.widget.TextView;
39
40import java.text.SimpleDateFormat;
41import java.util.Calendar;
42import java.util.TimeZone;
43
44import com.android.internal.R;
45
46/**
47 * This widget display an analogic clock with two hands for hours and
48 * minutes.
49 */
50public class Clock extends TextView {
51    private boolean mAttached;
52    private Calendar mCalendar;
53    private String mClockFormatString;
54    private SimpleDateFormat mClockFormat;
55
56    private static final int AM_PM_STYLE_NORMAL  = 0;
57    private static final int AM_PM_STYLE_SMALL   = 1;
58    private static final int AM_PM_STYLE_GONE    = 2;
59
60    private static final int AM_PM_STYLE = AM_PM_STYLE_GONE;
61
62    public Clock(Context context) {
63        this(context, null);
64    }
65
66    public Clock(Context context, AttributeSet attrs) {
67        this(context, attrs, 0);
68    }
69
70    public Clock(Context context, AttributeSet attrs, int defStyle) {
71        super(context, attrs, defStyle);
72    }
73
74    @Override
75    protected void onAttachedToWindow() {
76        super.onAttachedToWindow();
77
78        if (!mAttached) {
79            mAttached = true;
80            IntentFilter filter = new IntentFilter();
81
82            filter.addAction(Intent.ACTION_TIME_TICK);
83            filter.addAction(Intent.ACTION_TIME_CHANGED);
84            filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
85            filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
86
87            getContext().registerReceiver(mIntentReceiver, filter, null, getHandler());
88        }
89
90        // NOTE: It's safe to do these after registering the receiver since the receiver always runs
91        // in the main thread, therefore the receiver can't run before this method returns.
92
93        // The time zone may have changed while the receiver wasn't registered, so update the Time
94        mCalendar = Calendar.getInstance(TimeZone.getDefault());
95
96        // Make sure we update to the current time
97        updateClock();
98    }
99
100    @Override
101    protected void onDetachedFromWindow() {
102        super.onDetachedFromWindow();
103        if (mAttached) {
104            getContext().unregisterReceiver(mIntentReceiver);
105            mAttached = false;
106        }
107    }
108
109    private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
110        @Override
111        public void onReceive(Context context, Intent intent) {
112            String action = intent.getAction();
113            if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
114                String tz = intent.getStringExtra("time-zone");
115                mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
116                if (mClockFormat != null) {
117                    mClockFormat.setTimeZone(mCalendar.getTimeZone());
118                }
119            }
120            updateClock();
121        }
122    };
123
124    final void updateClock() {
125        mCalendar.setTimeInMillis(System.currentTimeMillis());
126        setText(getSmallTime());
127    }
128
129    private final CharSequence getSmallTime() {
130        Context context = getContext();
131        boolean b24 = DateFormat.is24HourFormat(context);
132        int res;
133
134        if (b24) {
135            res = R.string.twenty_four_hour_time_format;
136        } else {
137            res = R.string.twelve_hour_time_format;
138        }
139
140        final char MAGIC1 = '\uEF00';
141        final char MAGIC2 = '\uEF01';
142
143        SimpleDateFormat sdf;
144        String format = context.getString(res);
145        if (!format.equals(mClockFormatString)) {
146            /*
147             * Search for an unquoted "a" in the format string, so we can
148             * add dummy characters around it to let us find it again after
149             * formatting and change its size.
150             */
151            if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
152                int a = -1;
153                boolean quoted = false;
154                for (int i = 0; i < format.length(); i++) {
155                    char c = format.charAt(i);
156
157                    if (c == '\'') {
158                        quoted = !quoted;
159                    }
160                    if (!quoted && c == 'a') {
161                        a = i;
162                        break;
163                    }
164                }
165
166                if (a >= 0) {
167                    // Move a back so any whitespace before AM/PM is also in the alternate size.
168                    final int b = a;
169                    while (a > 0 && Character.isWhitespace(format.charAt(a-1))) {
170                        a--;
171                    }
172                    format = format.substring(0, a) + MAGIC1 + format.substring(a, b)
173                        + "a" + MAGIC2 + format.substring(b + 1);
174                }
175            }
176
177            mClockFormat = sdf = new SimpleDateFormat(format);
178            mClockFormatString = format;
179        } else {
180            sdf = mClockFormat;
181        }
182        String result = sdf.format(mCalendar.getTime());
183
184        if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) {
185            int magic1 = result.indexOf(MAGIC1);
186            int magic2 = result.indexOf(MAGIC2);
187            if (magic1 >= 0 && magic2 > magic1) {
188                SpannableStringBuilder formatted = new SpannableStringBuilder(result);
189                if (AM_PM_STYLE == AM_PM_STYLE_GONE) {
190                    formatted.delete(magic1, magic2+1);
191                } else {
192                    if (AM_PM_STYLE == AM_PM_STYLE_SMALL) {
193                        CharacterStyle style = new RelativeSizeSpan(0.7f);
194                        formatted.setSpan(style, magic1, magic2,
195                                          Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
196                    }
197                    formatted.delete(magic2, magic2 + 1);
198                    formatted.delete(magic1, magic1 + 1);
199                }
200                return formatted;
201            }
202        }
203
204        return result;
205
206    }
207}
208
209