OSDN Git Service

am fb2b5e04: am 1fd77470: am 1a1e9b31: Merge "DateView: Use Intent.ACTION_LOCALE_CHAN...
[android-x86/frameworks-base.git] / packages / SystemUI / src / com / android / systemui / statusbar / policy / DateView.java
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
17 package com.android.systemui.statusbar.policy;
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;
24 import android.view.View;
25 import android.view.ViewParent;
26 import android.widget.TextView;
27
28 import com.android.systemui.R;
29
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
32 import java.util.Locale;
33
34 import libcore.icu.ICU;
35
36 public class DateView extends TextView {
37     private static final String TAG = "DateView";
38
39     private boolean mAttachedToWindow;
40     private boolean mWindowVisible;
41     private boolean mUpdating;
42
43     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
44         @Override
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)) {
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();
63         mAttachedToWindow = true;
64         setUpdates();
65     }
66     
67     @Override
68     protected void onDetachedFromWindow() {
69         super.onDetachedFromWindow();
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();
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
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()));
99     }
100
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();
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);
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);
128                 updateClock();
129             } else {
130                 mContext.unregisterReceiver(mIntentReceiver);
131             }
132         }
133     }
134 }