2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.statusbar.policy;
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;
24 import android.view.View;
25 import android.view.ViewParent;
26 import android.widget.TextView;
28 import com.android.systemui.R;
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
32 import java.util.Locale;
34 import libcore.icu.ICU;
36 public class DateView extends TextView {
37 private static final String TAG = "DateView";
39 private boolean mAttachedToWindow;
40 private boolean mWindowVisible;
41 private boolean mUpdating;
43 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
45 public void onReceive(Context context, Intent intent) {
46 final String action = intent.getAction();
47 if (Intent.ACTION_TIME_TICK.equals(action)
48 || Intent.ACTION_TIME_CHANGED.equals(action)
49 || Intent.ACTION_TIMEZONE_CHANGED.equals(action)
50 || Intent.ACTION_LOCALE_CHANGED.equals(action)) {
56 public DateView(Context context, AttributeSet attrs) {
57 super(context, attrs);
61 protected void onAttachedToWindow() {
62 super.onAttachedToWindow();
63 mAttachedToWindow = true;
68 protected void onDetachedFromWindow() {
69 super.onDetachedFromWindow();
70 mAttachedToWindow = false;
75 protected void onWindowVisibilityChanged(int visibility) {
76 super.onWindowVisibilityChanged(visibility);
77 mWindowVisible = visibility == VISIBLE;
82 protected void onVisibilityChanged(View changedView, int visibility) {
83 super.onVisibilityChanged(changedView, visibility);
88 protected int getSuggestedMinimumWidth() {
89 // makes the large background bitmap not force us to full width
93 protected void updateClock() {
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()));
101 private boolean isVisible() {
104 if (v.getVisibility() != VISIBLE) {
107 final ViewParent parent = v.getParent();
108 if (parent instanceof View) {
116 private void setUpdates() {
117 boolean update = mAttachedToWindow && mWindowVisible && isVisible();
118 if (update != mUpdating) {
121 // Register for Intent broadcasts for the clock and battery
122 IntentFilter filter = new IntentFilter();
123 filter.addAction(Intent.ACTION_TIME_TICK);
124 filter.addAction(Intent.ACTION_TIME_CHANGED);
125 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
126 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
127 mContext.registerReceiver(mIntentReceiver, filter, null, null);
130 mContext.unregisterReceiver(mIntentReceiver);