blob: 847f997f1f92b67eca4b37a1227993bf7d039658 [file] [log] [blame]
Joe Onorato503007d2010-04-16 09:20:55 -07001/*
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 Onoratofd52b182010-11-10 18:00:52 -080017package com.android.systemui.statusbar.policy;
Joe Onorato503007d2010-04-16 09:20:55 -070018
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.util.AttributeSet;
Joe Onoratofd52b182010-11-10 18:00:52 -080024import android.view.View;
25import android.view.ViewParent;
Jeff Sharkey4519a022011-09-07 23:24:53 -070026import android.widget.TextView;
Joe Onorato503007d2010-04-16 09:20:55 -070027
Mike Lockwoodec9a50b2011-08-03 15:26:57 -040028import com.android.systemui.R;
29
Chris Wren56018e52013-01-15 10:59:43 -050030import java.text.SimpleDateFormat;
Joe Onorato503007d2010-04-16 09:20:55 -070031import java.util.Date;
Chris Wren56018e52013-01-15 10:59:43 -050032import java.util.Locale;
33
34import libcore.icu.ICU;
Joe Onorato503007d2010-04-16 09:20:55 -070035
Winson Chungd63c59782012-09-05 17:34:41 -070036public class DateView extends TextView {
Joe Onorato503007d2010-04-16 09:20:55 -070037 private static final String TAG = "DateView";
38
Joe Onoratofd52b182010-11-10 18:00:52 -080039 private boolean mAttachedToWindow;
40 private boolean mWindowVisible;
41 private boolean mUpdating;
Joe Onorato503007d2010-04-16 09:20:55 -070042
43 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
44 @Override
45 public void onReceive(Context context, Intent intent) {
Jeff Sharkey4519a022011-09-07 23:24:53 -070046 final String action = intent.getAction();
47 if (Intent.ACTION_TIME_TICK.equals(action)
48 || Intent.ACTION_TIME_CHANGED.equals(action)
Jorge Ruesga46b369f2012-09-19 00:24:05 +020049 || Intent.ACTION_TIMEZONE_CHANGED.equals(action)
50 || Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Joe Onorato503007d2010-04-16 09:20:55 -070051 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 Onoratofd52b182010-11-10 18:00:52 -080063 mAttachedToWindow = true;
64 setUpdates();
Joe Onorato503007d2010-04-16 09:20:55 -070065 }
John Spurlock209bede2013-07-17 12:23:27 -040066
Joe Onorato503007d2010-04-16 09:20:55 -070067 @Override
68 protected void onDetachedFromWindow() {
69 super.onDetachedFromWindow();
Joe Onoratofd52b182010-11-10 18:00:52 -080070 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 Onorato503007d2010-04-16 09:20:55 -070085 }
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 Chungd63c59782012-09-05 17:34:41 -070093 protected void updateClock() {
Chris Wren56018e52013-01-15 10:59:43 -050094 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 Onorato503007d2010-04-16 09:20:55 -070099 }
100
Joe Onoratofd52b182010-11-10 18:00:52 -0800101 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 Onorato503007d2010-04-16 09:20:55 -0700118 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 Sharkey4519a022011-09-07 23:24:53 -0700124 filter.addAction(Intent.ACTION_TIME_CHANGED);
Joe Onorato503007d2010-04-16 09:20:55 -0700125 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
Jorge Ruesga46b369f2012-09-19 00:24:05 +0200126 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Joe Onorato503007d2010-04-16 09:20:55 -0700127 mContext.registerReceiver(mIntentReceiver, filter, null, null);
128 updateClock();
129 } else {
130 mContext.unregisterReceiver(mIntentReceiver);
131 }
132 }
133 }
134}