Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 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 | |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 17 | package com.android.systemui.statusbar.policy; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.IntentFilter; |
| 23 | import android.util.AttributeSet; |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 24 | import android.view.View; |
| 25 | import android.view.ViewParent; |
Jeff Sharkey | 4519a02 | 2011-09-07 23:24:53 -0700 | [diff] [blame] | 26 | import android.widget.TextView; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 27 | |
Mike Lockwood | ec9a50b | 2011-08-03 15:26:57 -0400 | [diff] [blame] | 28 | import com.android.systemui.R; |
| 29 | |
Chris Wren | 56018e5 | 2013-01-15 10:59:43 -0500 | [diff] [blame] | 30 | import java.text.SimpleDateFormat; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 31 | import java.util.Date; |
Chris Wren | 56018e5 | 2013-01-15 10:59:43 -0500 | [diff] [blame] | 32 | import java.util.Locale; |
| 33 | |
| 34 | import libcore.icu.ICU; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 35 | |
Winson Chung | d63c5978 | 2012-09-05 17:34:41 -0700 | [diff] [blame] | 36 | public class DateView extends TextView { |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 37 | private static final String TAG = "DateView"; |
| 38 | |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 39 | private boolean mAttachedToWindow; |
| 40 | private boolean mWindowVisible; |
| 41 | private boolean mUpdating; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 42 | |
| 43 | private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 44 | @Override |
| 45 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | 4519a02 | 2011-09-07 23:24:53 -0700 | [diff] [blame] | 46 | final String action = intent.getAction(); |
| 47 | if (Intent.ACTION_TIME_TICK.equals(action) |
| 48 | || Intent.ACTION_TIME_CHANGED.equals(action) |
Jorge Ruesga | 46b369f | 2012-09-19 00:24:05 +0200 | [diff] [blame] | 49 | || Intent.ACTION_TIMEZONE_CHANGED.equals(action) |
| 50 | || Intent.ACTION_LOCALE_CHANGED.equals(action)) { |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 51 | updateClock(); |
| 52 | } |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | public DateView(Context context, AttributeSet attrs) { |
| 57 | super(context, attrs); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected void onAttachedToWindow() { |
| 62 | super.onAttachedToWindow(); |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 63 | mAttachedToWindow = true; |
| 64 | setUpdates(); |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | protected void onDetachedFromWindow() { |
| 69 | super.onDetachedFromWindow(); |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 70 | mAttachedToWindow = false; |
| 71 | setUpdates(); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | protected void onWindowVisibilityChanged(int visibility) { |
| 76 | super.onWindowVisibilityChanged(visibility); |
| 77 | mWindowVisible = visibility == VISIBLE; |
| 78 | setUpdates(); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | protected void onVisibilityChanged(View changedView, int visibility) { |
| 83 | super.onVisibilityChanged(changedView, visibility); |
| 84 | setUpdates(); |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | protected int getSuggestedMinimumWidth() { |
| 89 | // makes the large background bitmap not force us to full width |
| 90 | return 0; |
| 91 | } |
| 92 | |
Winson Chung | d63c5978 | 2012-09-05 17:34:41 -0700 | [diff] [blame] | 93 | protected void updateClock() { |
Chris Wren | 56018e5 | 2013-01-15 10:59:43 -0500 | [diff] [blame] | 94 | final String dateFormat = getContext().getString(R.string.system_ui_date_pattern); |
| 95 | final Locale l = Locale.getDefault(); |
| 96 | String fmt = ICU.getBestDateTimePattern(dateFormat, l.toString()); |
| 97 | SimpleDateFormat sdf = new SimpleDateFormat(fmt, l); |
| 98 | setText(sdf.format(new Date())); |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 101 | private boolean isVisible() { |
| 102 | View v = this; |
| 103 | while (true) { |
| 104 | if (v.getVisibility() != VISIBLE) { |
| 105 | return false; |
| 106 | } |
| 107 | final ViewParent parent = v.getParent(); |
| 108 | if (parent instanceof View) { |
| 109 | v = (View)parent; |
| 110 | } else { |
| 111 | return true; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private void setUpdates() { |
| 117 | boolean update = mAttachedToWindow && mWindowVisible && isVisible(); |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 118 | if (update != mUpdating) { |
| 119 | mUpdating = update; |
| 120 | if (update) { |
| 121 | // Register for Intent broadcasts for the clock and battery |
| 122 | IntentFilter filter = new IntentFilter(); |
| 123 | filter.addAction(Intent.ACTION_TIME_TICK); |
Jeff Sharkey | 4519a02 | 2011-09-07 23:24:53 -0700 | [diff] [blame] | 124 | filter.addAction(Intent.ACTION_TIME_CHANGED); |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 125 | filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); |
Jorge Ruesga | 46b369f | 2012-09-19 00:24:05 +0200 | [diff] [blame] | 126 | filter.addAction(Intent.ACTION_LOCALE_CHANGED); |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 127 | mContext.registerReceiver(mIntentReceiver, filter, null, null); |
| 128 | updateClock(); |
| 129 | } else { |
| 130 | mContext.unregisterReceiver(mIntentReceiver); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |