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.icu.text.DateFormat; 25import android.icu.text.DisplayContext; 26import android.util.AttributeSet; 27import android.widget.TextView; 28 29import com.android.systemui.R; 30 31import java.text.SimpleDateFormat; 32import java.util.Date; 33import java.util.Locale; 34 35public class DateView extends TextView { 36 private static final String TAG = "DateView"; 37 38 private final Date mCurrentTime = new Date(); 39 40 private DateFormat mDateFormat; 41 private String mLastText; 42 private String mDatePattern; 43 44 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 45 @Override 46 public void onReceive(Context context, Intent intent) { 47 final String action = intent.getAction(); 48 if (Intent.ACTION_TIME_TICK.equals(action) 49 || Intent.ACTION_TIME_CHANGED.equals(action) 50 || Intent.ACTION_TIMEZONE_CHANGED.equals(action) 51 || Intent.ACTION_LOCALE_CHANGED.equals(action)) { 52 if (Intent.ACTION_LOCALE_CHANGED.equals(action) 53 || Intent.ACTION_TIMEZONE_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 TypedArray a = context.getTheme().obtainStyledAttributes( 65 attrs, 66 R.styleable.DateView, 67 0, 0); 68 69 try { 70 mDatePattern = a.getString(R.styleable.DateView_datePattern); 71 } finally { 72 a.recycle(); 73 } 74 if (mDatePattern == null) { 75 mDatePattern = getContext().getString(R.string.system_ui_date_pattern); 76 } 77 } 78 79 @Override 80 protected void onAttachedToWindow() { 81 super.onAttachedToWindow(); 82 83 IntentFilter filter = new IntentFilter(); 84 filter.addAction(Intent.ACTION_TIME_TICK); 85 filter.addAction(Intent.ACTION_TIME_CHANGED); 86 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 87 filter.addAction(Intent.ACTION_LOCALE_CHANGED); 88 getContext().registerReceiver(mIntentReceiver, filter, null, null); 89 90 updateClock(); 91 } 92 93 @Override 94 protected void onDetachedFromWindow() { 95 super.onDetachedFromWindow(); 96 97 mDateFormat = null; // reload the locale next time 98 getContext().unregisterReceiver(mIntentReceiver); 99 } 100 101 protected void updateClock() { 102 if (mDateFormat == null) { 103 final Locale l = Locale.getDefault(); 104 DateFormat format = DateFormat.getInstanceForSkeleton(mDatePattern, l); 105 format.setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE); 106 mDateFormat = format; 107 } 108 109 mCurrentTime.setTime(System.currentTimeMillis()); 110 111 final String text = mDateFormat.format(mCurrentTime); 112 if (!text.equals(mLastText)) { 113 setText(text); 114 mLastText = text; 115 } 116 } 117} 118