DateView.java revision 9f5664b87614d230e181cf0043396b55a38000c1
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 || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) { 55 // need to get a fresh date format 56 mDateFormat = null; 57 } 58 updateClock(); 59 } 60 } 61 }; 62 63 public DateView(Context context, AttributeSet attrs) { 64 super(context, attrs); 65 } 66 67 @Override 68 protected void onAttachedToWindow() { 69 super.onAttachedToWindow(); 70 71 IntentFilter filter = new IntentFilter(); 72 filter.addAction(Intent.ACTION_TIME_TICK); 73 filter.addAction(Intent.ACTION_TIME_CHANGED); 74 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 75 filter.addAction(Intent.ACTION_LOCALE_CHANGED); 76 mContext.registerReceiver(mIntentReceiver, filter, null, null); 77 78 updateClock(); 79 } 80 81 @Override 82 protected void onDetachedFromWindow() { 83 super.onDetachedFromWindow(); 84 85 mDateFormat = null; // reload the locale next time 86 mContext.unregisterReceiver(mIntentReceiver); 87 } 88 89 protected void updateClock() { 90 if (mDateFormat == null) { 91 final String dateFormat = getContext().getString(R.string.system_ui_date_pattern); 92 final Locale l = Locale.getDefault(); 93 final String fmt = ICU.getBestDateTimePattern(dateFormat, l.toString()); 94 mDateFormat = new SimpleDateFormat(fmt, l); 95 } 96 97 mCurrentTime.setTime(System.currentTimeMillis()); 98 99 final String text = mDateFormat.format(mCurrentTime); 100 if (!text.equals(mLastText)) { 101 setText(text); 102 mLastText = text; 103 } 104 } 105} 106