DateView.java revision 46b369f09e7f0a9873a77312fdf49de937b97e69
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.text.format.DateFormat; 24import android.util.AttributeSet; 25import android.view.View; 26import android.view.ViewParent; 27import android.widget.TextView; 28 29import com.android.systemui.R; 30 31import java.util.Date; 32 33public final class DateView extends TextView { 34 private static final String TAG = "DateView"; 35 36 private boolean mAttachedToWindow; 37 private boolean mWindowVisible; 38 private boolean mUpdating; 39 40 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 41 @Override 42 public void onReceive(Context context, Intent intent) { 43 final String action = intent.getAction(); 44 if (Intent.ACTION_TIME_TICK.equals(action) 45 || Intent.ACTION_TIME_CHANGED.equals(action) 46 || Intent.ACTION_TIMEZONE_CHANGED.equals(action) 47 || Intent.ACTION_LOCALE_CHANGED.equals(action)) { 48 updateClock(); 49 } 50 } 51 }; 52 53 public DateView(Context context, AttributeSet attrs) { 54 super(context, attrs); 55 } 56 57 @Override 58 protected void onAttachedToWindow() { 59 super.onAttachedToWindow(); 60 mAttachedToWindow = true; 61 setUpdates(); 62 } 63 64 @Override 65 protected void onDetachedFromWindow() { 66 super.onDetachedFromWindow(); 67 mAttachedToWindow = false; 68 setUpdates(); 69 } 70 71 @Override 72 protected void onWindowVisibilityChanged(int visibility) { 73 super.onWindowVisibilityChanged(visibility); 74 mWindowVisible = visibility == VISIBLE; 75 setUpdates(); 76 } 77 78 @Override 79 protected void onVisibilityChanged(View changedView, int visibility) { 80 super.onVisibilityChanged(changedView, visibility); 81 setUpdates(); 82 } 83 84 @Override 85 protected int getSuggestedMinimumWidth() { 86 // makes the large background bitmap not force us to full width 87 return 0; 88 } 89 90 private final void updateClock() { 91 final Context context = getContext(); 92 Date now = new Date(); 93 CharSequence dow = DateFormat.format("EEEE", now); 94 CharSequence date = DateFormat.getLongDateFormat(context).format(now); 95 setText(context.getString(R.string.status_bar_date_formatter, dow, date)); 96 } 97 98 private boolean isVisible() { 99 View v = this; 100 while (true) { 101 if (v.getVisibility() != VISIBLE) { 102 return false; 103 } 104 final ViewParent parent = v.getParent(); 105 if (parent instanceof View) { 106 v = (View)parent; 107 } else { 108 return true; 109 } 110 } 111 } 112 113 private void setUpdates() { 114 boolean update = mAttachedToWindow && mWindowVisible && isVisible(); 115 if (update != mUpdating) { 116 mUpdating = update; 117 if (update) { 118 // Register for Intent broadcasts for the clock and battery 119 IntentFilter filter = new IntentFilter(); 120 filter.addAction(Intent.ACTION_TIME_TICK); 121 filter.addAction(Intent.ACTION_TIME_CHANGED); 122 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 123 filter.addAction(Intent.ACTION_LOCALE_CHANGED); 124 mContext.registerReceiver(mIntentReceiver, filter, null, null); 125 updateClock(); 126 } else { 127 mContext.unregisterReceiver(mIntentReceiver); 128 } 129 } 130 } 131} 132