DateView.java revision b478a71625235f554263324dbf0501f6b6c87520
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.util.Log; 25import android.view.View; 26import android.view.ViewParent; 27import android.widget.TextView; 28 29import com.android.systemui.R; 30 31import java.text.SimpleDateFormat; 32import java.util.Date; 33import java.util.Locale; 34 35import libcore.icu.ICU; 36 37public class DateView extends TextView { 38 private static final String TAG = "DateView"; 39 40 private final Date mCurrentTime = new Date(); 41 42 private SimpleDateFormat mDateFormat; 43 private String mLastText; 44 45 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 46 @Override 47 public void onReceive(Context context, Intent intent) { 48 final String action = intent.getAction(); 49 if (Intent.ACTION_TIME_TICK.equals(action) 50 || Intent.ACTION_TIME_CHANGED.equals(action) 51 || Intent.ACTION_TIMEZONE_CHANGED.equals(action) 52 || Intent.ACTION_LOCALE_CHANGED.equals(action)) { 53 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) { 54 // need to get a fresh date format 55 mDateFormat = null; 56 } 57 updateClock(); 58 } 59 } 60 }; 61 62 public DateView(Context context, AttributeSet attrs) { 63 super(context, attrs); 64 } 65 66 @Override 67 protected void onAttachedToWindow() { 68 super.onAttachedToWindow(); 69 70 IntentFilter filter = new IntentFilter(); 71 filter.addAction(Intent.ACTION_TIME_TICK); 72 filter.addAction(Intent.ACTION_TIME_CHANGED); 73 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 74 filter.addAction(Intent.ACTION_LOCALE_CHANGED); 75 mContext.registerReceiver(mIntentReceiver, filter, null, null); 76 77 updateClock(); 78 } 79 80 @Override 81 protected void onDetachedFromWindow() { 82 super.onDetachedFromWindow(); 83 84 mDateFormat = null; // reload the locale next time 85 mContext.unregisterReceiver(mIntentReceiver); 86 } 87 88 protected void updateClock() { 89 if (mDateFormat == null) { 90 final String dateFormat = getContext().getString(R.string.system_ui_date_pattern); 91 final Locale l = Locale.getDefault(); 92 final String fmt = ICU.getBestDateTimePattern(dateFormat, l.toString()); 93 mDateFormat = new SimpleDateFormat(fmt, l); 94 } 95 96 mCurrentTime.setTime(System.currentTimeMillis()); 97 98 final String text = mDateFormat.format(mCurrentTime); 99 if (!text.equals(mLastText)) { 100 setText(text); 101 mLastText = text; 102 } 103 } 104} 105