DateView.java revision 6090995951c6e2e4dcf38102f01793f8a94166e1
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 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.util.AttributeSet; 24import android.widget.TextView; 25 26import com.android.systemui.R; 27 28import java.text.SimpleDateFormat; 29import java.util.Date; 30import java.util.Locale; 31 32import libcore.icu.ICU; 33 34public class DateView extends TextView { 35 private static final String TAG = "DateView"; 36 37 private final Date mCurrentTime = new Date(); 38 39 private SimpleDateFormat mDateFormat; 40 private String mLastText; 41 42 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 43 @Override 44 public void onReceive(Context context, Intent intent) { 45 final String action = intent.getAction(); 46 if (Intent.ACTION_TIME_TICK.equals(action) 47 || Intent.ACTION_TIME_CHANGED.equals(action) 48 || Intent.ACTION_TIMEZONE_CHANGED.equals(action) 49 || Intent.ACTION_LOCALE_CHANGED.equals(action)) { 50 if (Intent.ACTION_LOCALE_CHANGED.equals(action) 51 || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) { 52 // need to get a fresh date format 53 mDateFormat = null; 54 } 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(); 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); 73 mContext.registerReceiver(mIntentReceiver, filter, null, null); 74 75 updateClock(); 76 } 77 78 @Override 79 protected void onDetachedFromWindow() { 80 super.onDetachedFromWindow(); 81 82 mDateFormat = null; // reload the locale next time 83 mContext.unregisterReceiver(mIntentReceiver); 84 } 85 86 protected void updateClock() { 87 if (mDateFormat == null) { 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); 92 } 93 94 mCurrentTime.setTime(System.currentTimeMillis()); 95 96 final String text = mDateFormat.format(mCurrentTime); 97 if (!text.equals(mLastText)) { 98 setText(text); 99 mLastText = text; 100 } 101 } 102} 103