Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 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 | |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 17 | package com.android.systemui.statusbar.policy; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.IntentFilter; |
| 23 | import android.util.AttributeSet; |
Jeff Sharkey | 4519a02 | 2011-09-07 23:24:53 -0700 | [diff] [blame] | 24 | import android.widget.TextView; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 25 | |
Mike Lockwood | ec9a50b | 2011-08-03 15:26:57 -0400 | [diff] [blame] | 26 | import com.android.systemui.R; |
| 27 | |
Chris Wren | 56018e5 | 2013-01-15 10:59:43 -0500 | [diff] [blame] | 28 | import java.text.SimpleDateFormat; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 29 | import java.util.Date; |
Chris Wren | 56018e5 | 2013-01-15 10:59:43 -0500 | [diff] [blame] | 30 | import java.util.Locale; |
| 31 | |
| 32 | import libcore.icu.ICU; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 33 | |
Winson Chung | d63c5978 | 2012-09-05 17:34:41 -0700 | [diff] [blame] | 34 | public class DateView extends TextView { |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 35 | private static final String TAG = "DateView"; |
| 36 | |
Alan Viverette | 6e9b047 | 2013-07-29 13:46:43 -0700 | [diff] [blame] | 37 | private final Date mCurrentTime = new Date(); |
| 38 | |
| 39 | private SimpleDateFormat mDateFormat; |
Daniel Sandler | b478a71 | 2013-09-29 23:25:50 -0400 | [diff] [blame] | 40 | private String mLastText; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 41 | |
| 42 | private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 43 | @Override |
| 44 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | 4519a02 | 2011-09-07 23:24:53 -0700 | [diff] [blame] | 45 | final String action = intent.getAction(); |
| 46 | if (Intent.ACTION_TIME_TICK.equals(action) |
| 47 | || Intent.ACTION_TIME_CHANGED.equals(action) |
Jorge Ruesga | 46b369f | 2012-09-19 00:24:05 +0200 | [diff] [blame] | 48 | || Intent.ACTION_TIMEZONE_CHANGED.equals(action) |
| 49 | || Intent.ACTION_LOCALE_CHANGED.equals(action)) { |
Chet Haase | 9f5664b | 2013-10-10 09:03:19 -0700 | [diff] [blame] | 50 | if (Intent.ACTION_LOCALE_CHANGED.equals(action) |
| 51 | || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) { |
Daniel Sandler | b478a71 | 2013-09-29 23:25:50 -0400 | [diff] [blame] | 52 | // need to get a fresh date format |
| 53 | mDateFormat = null; |
Alan Viverette | 6e9b047 | 2013-07-29 13:46:43 -0700 | [diff] [blame] | 54 | } |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 55 | 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 Sandler | b478a71 | 2013-09-29 23:25:50 -0400 | [diff] [blame] | 67 | |
| 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 Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 73 | getContext().registerReceiver(mIntentReceiver, filter, null, null); |
Daniel Sandler | b478a71 | 2013-09-29 23:25:50 -0400 | [diff] [blame] | 74 | |
| 75 | updateClock(); |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 76 | } |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 77 | |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 78 | @Override |
| 79 | protected void onDetachedFromWindow() { |
| 80 | super.onDetachedFromWindow(); |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 81 | |
Daniel Sandler | b478a71 | 2013-09-29 23:25:50 -0400 | [diff] [blame] | 82 | mDateFormat = null; // reload the locale next time |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 83 | getContext().unregisterReceiver(mIntentReceiver); |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Winson Chung | d63c5978 | 2012-09-05 17:34:41 -0700 | [diff] [blame] | 86 | protected void updateClock() { |
Daniel Sandler | b478a71 | 2013-09-29 23:25:50 -0400 | [diff] [blame] | 87 | if (mDateFormat == null) { |
Alan Viverette | 6e9b047 | 2013-07-29 13:46:43 -0700 | [diff] [blame] | 88 | 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 Viverette | 6e9b047 | 2013-07-29 13:46:43 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | mCurrentTime.setTime(System.currentTimeMillis()); |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 95 | |
Daniel Sandler | b478a71 | 2013-09-29 23:25:50 -0400 | [diff] [blame] | 96 | final String text = mDateFormat.format(mCurrentTime); |
| 97 | if (!text.equals(mLastText)) { |
| 98 | setText(text); |
| 99 | mLastText = text; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |