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 updateClock(); 48 } 49 } 50 }; 51 52 public DateView(Context context, AttributeSet attrs) { 53 super(context, attrs); 54 } 55 56 @Override 57 protected void onAttachedToWindow() { 58 super.onAttachedToWindow(); 59 mAttachedToWindow = true; 60 setUpdates(); 61 } 62 63 @Override 64 protected void onDetachedFromWindow() { 65 super.onDetachedFromWindow(); 66 mAttachedToWindow = false; 67 setUpdates(); 68 } 69 70 @Override 71 protected void onWindowVisibilityChanged(int visibility) { 72 super.onWindowVisibilityChanged(visibility); 73 mWindowVisible = visibility == VISIBLE; 74 setUpdates(); 75 } 76 77 @Override 78 protected void onVisibilityChanged(View changedView, int visibility) { 79 super.onVisibilityChanged(changedView, visibility); 80 setUpdates(); 81 } 82 83 @Override 84 protected int getSuggestedMinimumWidth() { 85 // makes the large background bitmap not force us to full width 86 return 0; 87 } 88 89 private final void updateClock() { 90 final Context context = getContext(); 91 Date now = new Date(); 92 CharSequence dow = DateFormat.format("EEEE", now); 93 CharSequence date = DateFormat.getLongDateFormat(context).format(now); 94 setText(context.getString(R.string.status_bar_date_formatter, dow, date)); 95 } 96 97 private boolean isVisible() { 98 View v = this; 99 while (true) { 100 if (v.getVisibility() != VISIBLE) { 101 return false; 102 } 103 final ViewParent parent = v.getParent(); 104 if (parent instanceof View) { 105 v = (View)parent; 106 } else { 107 return true; 108 } 109 } 110 } 111 112 private void setUpdates() { 113 boolean update = mAttachedToWindow && mWindowVisible && isVisible(); 114 if (update != mUpdating) { 115 mUpdating = update; 116 if (update) { 117 // Register for Intent broadcasts for the clock and battery 118 IntentFilter filter = new IntentFilter(); 119 filter.addAction(Intent.ACTION_TIME_TICK); 120 filter.addAction(Intent.ACTION_TIME_CHANGED); 121 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 122 mContext.registerReceiver(mIntentReceiver, filter, null, null); 123 updateClock(); 124 } else { 125 mContext.unregisterReceiver(mIntentReceiver); 126 } 127 } 128 } 129} 130