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.content.res.TypedArray; 24import android.text.format.DateFormat; 25import android.util.AttributeSet; 26import android.widget.TextView; 27 28import com.android.systemui.R; 29 30import java.text.SimpleDateFormat; 31import java.util.Date; 32import java.util.Locale; 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 private String mDatePattern; 42 43 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 44 @Override 45 public void onReceive(Context context, Intent intent) { 46 final String action = intent.getAction(); 47 if (Intent.ACTION_TIME_TICK.equals(action) 48 || Intent.ACTION_TIME_CHANGED.equals(action) 49 || Intent.ACTION_TIMEZONE_CHANGED.equals(action) 50 || Intent.ACTION_LOCALE_CHANGED.equals(action)) { 51 if (Intent.ACTION_LOCALE_CHANGED.equals(action) 52 || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) { 53 // need to get a fresh date format 54 mDateFormat = null; 55 } 56 updateClock(); 57 } 58 } 59 }; 60 61 public DateView(Context context, AttributeSet attrs) { 62 super(context, attrs); 63 TypedArray a = context.getTheme().obtainStyledAttributes( 64 attrs, 65 R.styleable.DateView, 66 0, 0); 67 68 try { 69 mDatePattern = a.getString(R.styleable.DateView_datePattern); 70 } finally { 71 a.recycle(); 72 } 73 if (mDatePattern == null) { 74 mDatePattern = getContext().getString(R.string.system_ui_date_pattern); 75 } 76 } 77 78 @Override 79 protected void onAttachedToWindow() { 80 super.onAttachedToWindow(); 81 82 IntentFilter filter = new IntentFilter(); 83 filter.addAction(Intent.ACTION_TIME_TICK); 84 filter.addAction(Intent.ACTION_TIME_CHANGED); 85 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 86 filter.addAction(Intent.ACTION_LOCALE_CHANGED); 87 getContext().registerReceiver(mIntentReceiver, filter, null, null); 88 89 updateClock(); 90 } 91 92 @Override 93 protected void onDetachedFromWindow() { 94 super.onDetachedFromWindow(); 95 96 mDateFormat = null; // reload the locale next time 97 getContext().unregisterReceiver(mIntentReceiver); 98 } 99 100 protected void updateClock() { 101 if (mDateFormat == null) { 102 final Locale l = Locale.getDefault(); 103 final String fmt = DateFormat.getBestDateTimePattern(l, mDatePattern); 104 mDateFormat = new SimpleDateFormat(fmt, l); 105 } 106 107 mCurrentTime.setTime(System.currentTimeMillis()); 108 109 final String text = mDateFormat.format(mCurrentTime); 110 if (!text.equals(mLastText)) { 111 setText(text); 112 mLastText = text; 113 } 114 } 115} 116