blob: cadb44a065c2ff26b43ab44e3b0b8b76fda11ba8 [file] [log] [blame]
Joe Onorato503007d2010-04-16 09:20:55 -07001/*
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
Joe Onoratofd52b182010-11-10 18:00:52 -080017package com.android.systemui.statusbar.policy;
Joe Onorato503007d2010-04-16 09:20:55 -070018
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.util.AttributeSet;
Jeff Sharkey4519a022011-09-07 23:24:53 -070024import android.widget.TextView;
Joe Onorato503007d2010-04-16 09:20:55 -070025
Mike Lockwoodec9a50b2011-08-03 15:26:57 -040026import com.android.systemui.R;
27
Chris Wren56018e52013-01-15 10:59:43 -050028import java.text.SimpleDateFormat;
Joe Onorato503007d2010-04-16 09:20:55 -070029import java.util.Date;
Chris Wren56018e52013-01-15 10:59:43 -050030import java.util.Locale;
31
32import libcore.icu.ICU;
Joe Onorato503007d2010-04-16 09:20:55 -070033
Winson Chungd63c59782012-09-05 17:34:41 -070034public class DateView extends TextView {
Joe Onorato503007d2010-04-16 09:20:55 -070035 private static final String TAG = "DateView";
36
Alan Viverette6e9b0472013-07-29 13:46:43 -070037 private final Date mCurrentTime = new Date();
38
39 private SimpleDateFormat mDateFormat;
Daniel Sandlerb478a712013-09-29 23:25:50 -040040 private String mLastText;
Joe Onorato503007d2010-04-16 09:20:55 -070041
42 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
43 @Override
44 public void onReceive(Context context, Intent intent) {
Jeff Sharkey4519a022011-09-07 23:24:53 -070045 final String action = intent.getAction();
46 if (Intent.ACTION_TIME_TICK.equals(action)
47 || Intent.ACTION_TIME_CHANGED.equals(action)
Jorge Ruesga46b369f2012-09-19 00:24:05 +020048 || Intent.ACTION_TIMEZONE_CHANGED.equals(action)
49 || Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Chet Haase9f5664b2013-10-10 09:03:19 -070050 if (Intent.ACTION_LOCALE_CHANGED.equals(action)
51 || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
Daniel Sandlerb478a712013-09-29 23:25:50 -040052 // need to get a fresh date format
53 mDateFormat = null;
Alan Viverette6e9b0472013-07-29 13:46:43 -070054 }
Joe Onorato503007d2010-04-16 09:20:55 -070055 updateClock();
56 }
57 }
58 };
59
60 public DateView(Context context, AttributeSet attrs) {
61 super(context, attrs);
62 }
63
64 @Override
65 protected void onAttachedToWindow() {
66 super.onAttachedToWindow();
Daniel Sandlerb478a712013-09-29 23:25:50 -040067
68 IntentFilter filter = new IntentFilter();
69 filter.addAction(Intent.ACTION_TIME_TICK);
70 filter.addAction(Intent.ACTION_TIME_CHANGED);
71 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
72 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
John Spurlock01534782014-01-13 11:59:22 -050073 getContext().registerReceiver(mIntentReceiver, filter, null, null);
Daniel Sandlerb478a712013-09-29 23:25:50 -040074
75 updateClock();
Joe Onorato503007d2010-04-16 09:20:55 -070076 }
John Spurlock209bede2013-07-17 12:23:27 -040077
Joe Onorato503007d2010-04-16 09:20:55 -070078 @Override
79 protected void onDetachedFromWindow() {
80 super.onDetachedFromWindow();
Joe Onoratofd52b182010-11-10 18:00:52 -080081
Daniel Sandlerb478a712013-09-29 23:25:50 -040082 mDateFormat = null; // reload the locale next time
John Spurlock01534782014-01-13 11:59:22 -050083 getContext().unregisterReceiver(mIntentReceiver);
Joe Onorato503007d2010-04-16 09:20:55 -070084 }
85
Winson Chungd63c59782012-09-05 17:34:41 -070086 protected void updateClock() {
Daniel Sandlerb478a712013-09-29 23:25:50 -040087 if (mDateFormat == null) {
Alan Viverette6e9b0472013-07-29 13:46:43 -070088 final String dateFormat = getContext().getString(R.string.system_ui_date_pattern);
89 final Locale l = Locale.getDefault();
90 final String fmt = ICU.getBestDateTimePattern(dateFormat, l.toString());
91 mDateFormat = new SimpleDateFormat(fmt, l);
Alan Viverette6e9b0472013-07-29 13:46:43 -070092 }
93
94 mCurrentTime.setTime(System.currentTimeMillis());
Joe Onorato503007d2010-04-16 09:20:55 -070095
Daniel Sandlerb478a712013-09-29 23:25:50 -040096 final String text = mDateFormat.format(mCurrentTime);
97 if (!text.equals(mLastText)) {
98 setText(text);
99 mLastText = text;
Joe Onorato503007d2010-04-16 09:20:55 -0700100 }
101 }
102}