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 63 public HoloClock(Context context) { 64 this(context, null); 65 } 66 67 public HoloClock(Context context, AttributeSet attrs) { 68 this(context, attrs, 0); 69 } 70 71 public HoloClock(Context context, AttributeSet attrs, int defStyle) { 72 super(context, attrs, defStyle); 73 } 74 75 @Override 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 mBgText.setVisibility(View.INVISIBLE); 88 } 89 90 mFgText = (TextView) findViewById(R.id.time_fg); 91 if (mFgText != null) { 92 mFgText.setTypeface(sForegroundType); 93 } 94 mSolidText = (TextView) findViewById(R.id.time_solid); 95 if (mSolidText != null) { 96 mSolidText.setTypeface(sSolidType); 97 } 98 } 99 100 @Override 101 protected void onAttachedToWindow() { 102 super.onAttachedToWindow(); 103 104 if (!mAttached) { 105 mAttached = true; 106 IntentFilter filter = new IntentFilter(); 107 108 filter.addAction(Intent.ACTION_TIME_TICK); 109 filter.addAction(Intent.ACTION_TIME_CHANGED); 110 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 111 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); 112 113 getContext().registerReceiver(mIntentReceiver, filter, null, getHandler()); 114 } 115 116 // NOTE: It's safe to do these after registering the receiver since the receiver always runs 117 // in the main thread, therefore the receiver can't run before this method returns. 118 119 // The time zone may have changed while the receiver wasn't registered, so update the Time 120 mCalendar = Calendar.getInstance(TimeZone.getDefault()); 121 122 // Make sure we update to the current time 123 updateClock(); 124 } 125 126 @Override 127 protected void onDetachedFromWindow() { 128 super.onDetachedFromWindow(); 129 if (mAttached) { 130 getContext().unregisterReceiver(mIntentReceiver); 131 mAttached = false; 132 } 133 } 134 135 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 136 @Override 137 public void onReceive(Context context, Intent intent) { 138 String action = intent.getAction(); 139 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { 140 String tz = intent.getStringExtra("time-zone"); 141 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz)); 142 if (mClockFormat != null) { 143 mClockFormat.setTimeZone(mCalendar.getTimeZone()); 144 } 145 } 146 updateClock(); 147 } 148 }; 149 150 final void updateClock() { 151 mCalendar.setTimeInMillis(System.currentTimeMillis()); 152 CharSequence txt = getTimeText(); 153 if (mBgText != null) mBgText.setText(txt); 154 if (mFgText != null) mFgText.setText(txt); 155 if (mSolidText != null) mSolidText.setText(txt); 156 } 157 158 private final CharSequence getTimeText() { 159 Context context = getContext(); 160 int res = DateFormat.is24HourFormat(context) 161 ? com.android.internal.R.string.twenty_four_hour_time_format 162 : com.android.internal.R.string.twelve_hour_time_format; 163 164 SimpleDateFormat sdf; 165 String format = context.getString(res); 166 if (!format.equals(mClockFormatString)) { 167 // we don't want AM/PM showing up in our statusbar, even in 12h mode 168 format = format.replaceAll("a", "").trim(); 169 mClockFormat = sdf = new SimpleDateFormat(format); 170 mClockFormatString = format; 171 } else { 172 sdf = mClockFormat; 173 } 174 String result = sdf.format(mCalendar.getTime()); 175 return result; 176 } 177 } 178 179