DateView.java revision a3dba34dd211f8fff0a75bda1cc5f4fb0c723ac8
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.util.Slog; 26import android.widget.TextView; 27import android.view.MotionEvent; 28import android.view.View; 29import android.view.ViewParent; 30 31import com.android.systemui.R; 32 33import java.util.Date; 34 35public final class DateView extends TextView { 36 private static final String TAG = "DateView"; 37 38 private boolean mAttachedToWindow; 39 private boolean mWindowVisible; 40 private boolean mUpdating; 41 42 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 43 @Override 44 public void onReceive(Context context, Intent intent) { 45 String action = intent.getAction(); 46 if (action.equals(Intent.ACTION_TIME_TICK) 47 || action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { 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_TIMEZONE_CHANGED); 122 mContext.registerReceiver(mIntentReceiver, filter, null, null); 123 updateClock(); 124 } else { 125 mContext.unregisterReceiver(mIntentReceiver); 126 } 127 } 128 } 129} 130 131