DateView.java revision d2bf42a96e052019d71f10cba652246fe09e09cb
1package com.android.server.status; 2 3import android.content.BroadcastReceiver; 4import android.content.Context; 5import android.content.Intent; 6import android.content.IntentFilter; 7import android.util.AttributeSet; 8import android.util.Log; 9import android.widget.TextView; 10import android.view.MotionEvent; 11 12import java.text.DateFormat; 13import java.util.Date; 14 15public final class DateView extends TextView { 16 private static final String TAG = "DateView"; 17 18 private boolean mUpdating = false; 19 20 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 21 @Override 22 public void onReceive(Context context, Intent intent) { 23 String action = intent.getAction(); 24 if (action.equals(Intent.ACTION_TIME_TICK) 25 || action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { 26 updateClock(); 27 } 28 } 29 }; 30 31 public DateView(Context context, AttributeSet attrs) { 32 super(context, attrs); 33 } 34 35 @Override 36 protected void onAttachedToWindow() { 37 super.onAttachedToWindow(); 38 } 39 40 @Override 41 protected void onDetachedFromWindow() { 42 super.onDetachedFromWindow(); 43 setUpdates(false); 44 } 45 46 @Override 47 protected int getSuggestedMinimumWidth() { 48 // makes the large background bitmap not force us to full width 49 return 0; 50 } 51 52 private final void updateClock() { 53 Date now = new Date(); 54 setText(DateFormat.getDateInstance(DateFormat.LONG).format(now)); 55 } 56 57 void setUpdates(boolean update) { 58 if (update != mUpdating) { 59 mUpdating = update; 60 if (update) { 61 // Register for Intent broadcasts for the clock and battery 62 IntentFilter filter = new IntentFilter(); 63 filter.addAction(Intent.ACTION_TIME_TICK); 64 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 65 mContext.registerReceiver(mIntentReceiver, filter, null, null); 66 updateClock(); 67 } else { 68 mContext.unregisterReceiver(mIntentReceiver); 69 } 70 } 71 } 72} 73 74