• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 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.tablet;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.content.res.AssetManager;
24 import android.content.res.Resources;
25 import android.content.res.TypedArray;
26 import android.graphics.Canvas;
27 import android.graphics.Typeface;
28 import android.graphics.drawable.Drawable;
29 import android.text.Spannable;
30 import android.text.SpannableStringBuilder;
31 import android.text.format.DateFormat;
32 import android.text.style.CharacterStyle;
33 import android.text.style.ForegroundColorSpan;
34 import android.text.style.RelativeSizeSpan;
35 import android.text.style.RelativeSizeSpan;
36 import android.text.style.StyleSpan;
37 import android.util.AttributeSet;
38 import android.view.View;
39 import android.view.ViewGroup;
40 import android.widget.FrameLayout;
41 import android.widget.TextView;
42 
43 import java.text.SimpleDateFormat;
44 import java.util.Calendar;
45 import java.util.TimeZone;
46 
47 import com.android.systemui.R;
48 
49 public class HoloClock extends FrameLayout {
50     private boolean mAttached;
51     private Calendar mCalendar;
52     private String mClockFormatString;
53     private SimpleDateFormat mClockFormat;
54 
55     private static final String FONT_DIR = "/system/fonts/";
56     private static final String CLOCK_FONT = FONT_DIR + "AndroidClock_Solid.ttf";
57     private static final String CLOCK_FG_FONT = FONT_DIR + "AndroidClock.ttf";
58     private static final String CLOCK_BG_FONT = FONT_DIR + "AndroidClock_Highlight.ttf";
59 
60     private static Typeface sBackgroundType, sForegroundType, sSolidType;
61     private TextView mSolidText, mBgText, mFgText;
62 
HoloClock(Context context)63     public HoloClock(Context context) {
64         this(context, null);
65     }
66 
HoloClock(Context context, AttributeSet attrs)67     public HoloClock(Context context, AttributeSet attrs) {
68         this(context, attrs, 0);
69     }
70 
HoloClock(Context context, AttributeSet attrs, int defStyle)71     public HoloClock(Context context, AttributeSet attrs, int defStyle) {
72         super(context, attrs, defStyle);
73     }
74 
75     @Override
onFinishInflate()76     protected void onFinishInflate() {
77         super.onFinishInflate();
78 
79         if (sSolidType == null) {
80             sSolidType = Typeface.createFromFile(CLOCK_FONT);
81             sBackgroundType = Typeface.createFromFile(CLOCK_BG_FONT);
82             sForegroundType = Typeface.createFromFile(CLOCK_FG_FONT);
83         }
84         mBgText = (TextView) findViewById(R.id.time_bg);
85         if (mBgText != null) {
86             mBgText.setTypeface(sBackgroundType);
87         }
88         mFgText = (TextView) findViewById(R.id.time_fg);
89         if (mFgText != null) {
90             mFgText.setTypeface(sForegroundType);
91         }
92         mSolidText = (TextView) findViewById(R.id.time_solid);
93         if (mSolidText != null) {
94             mSolidText.setTypeface(sSolidType);
95         }
96     }
97 
98     @Override
onAttachedToWindow()99     protected void onAttachedToWindow() {
100         super.onAttachedToWindow();
101 
102         if (!mAttached) {
103             mAttached = true;
104             IntentFilter filter = new IntentFilter();
105 
106             filter.addAction(Intent.ACTION_TIME_TICK);
107             filter.addAction(Intent.ACTION_TIME_CHANGED);
108             filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
109             filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
110 
111             getContext().registerReceiver(mIntentReceiver, filter, null, getHandler());
112         }
113 
114         // NOTE: It's safe to do these after registering the receiver since the receiver always runs
115         // in the main thread, therefore the receiver can't run before this method returns.
116 
117         // The time zone may have changed while the receiver wasn't registered, so update the Time
118         mCalendar = Calendar.getInstance(TimeZone.getDefault());
119 
120         // Make sure we update to the current time
121         updateClock();
122     }
123 
124     @Override
onDetachedFromWindow()125     protected void onDetachedFromWindow() {
126         super.onDetachedFromWindow();
127         if (mAttached) {
128             getContext().unregisterReceiver(mIntentReceiver);
129             mAttached = false;
130         }
131     }
132 
133     private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
134         @Override
135         public void onReceive(Context context, Intent intent) {
136             String action = intent.getAction();
137             if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
138                 String tz = intent.getStringExtra("time-zone");
139                 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz));
140                 if (mClockFormat != null) {
141                     mClockFormat.setTimeZone(mCalendar.getTimeZone());
142                 }
143             }
144             updateClock();
145         }
146     };
147 
updateClock()148     final void updateClock() {
149         mCalendar.setTimeInMillis(System.currentTimeMillis());
150         CharSequence txt = getTimeText();
151         if (mBgText != null) mBgText.setText(txt);
152         if (mFgText != null) mFgText.setText(txt);
153         if (mSolidText != null) mSolidText.setText(txt);
154     }
155 
getTimeText()156     private final CharSequence getTimeText() {
157         Context context = getContext();
158         int res = DateFormat.is24HourFormat(context)
159             ? com.android.internal.R.string.twenty_four_hour_time_format
160             : com.android.internal.R.string.twelve_hour_time_format;
161 
162         SimpleDateFormat sdf;
163         String format = context.getString(res);
164         if (!format.equals(mClockFormatString)) {
165             // we don't want AM/PM showing up in our statusbar, even in 12h mode
166             format = format.replaceAll("a", "").trim();
167             mClockFormat = sdf = new SimpleDateFormat(format);
168             mClockFormatString = format;
169         } else {
170             sdf = mClockFormat;
171         }
172         String result = sdf.format(mCalendar.getTime());
173         return result;
174     }
175 }
176 
177