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.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.content.res.Resources; 24 import android.content.res.TypedArray; 25 import android.graphics.Canvas; 26 import android.graphics.Typeface; 27 import android.graphics.drawable.Drawable; 28 import android.text.Spannable; 29 import android.text.SpannableStringBuilder; 30 import android.text.format.DateFormat; 31 import android.text.style.CharacterStyle; 32 import android.text.style.ForegroundColorSpan; 33 import android.text.style.RelativeSizeSpan; 34 import android.text.style.RelativeSizeSpan; 35 import android.text.style.StyleSpan; 36 import android.util.AttributeSet; 37 import android.util.Slog; 38 import android.view.View; 39 import android.widget.TextView; 40 41 import java.text.SimpleDateFormat; 42 import java.util.Calendar; 43 import java.util.Locale; 44 import java.util.TimeZone; 45 46 import libcore.icu.LocaleData; 47 48 import com.android.internal.R; 49 50 /** 51 * Digital clock for the status bar. 52 */ 53 public class Clock extends TextView { 54 private boolean mAttached; 55 private Calendar mCalendar; 56 private String mClockFormatString; 57 private SimpleDateFormat mClockFormat; 58 private Locale mLocale; 59 60 private static final int AM_PM_STYLE_NORMAL = 0; 61 private static final int AM_PM_STYLE_SMALL = 1; 62 private static final int AM_PM_STYLE_GONE = 2; 63 64 private static final int AM_PM_STYLE = AM_PM_STYLE_GONE; 65 66 public Clock(Context context) { 67 this(context, null); 68 } 69 70 public Clock(Context context, AttributeSet attrs) { 71 this(context, attrs, 0); 72 } 73 74 public Clock(Context context, AttributeSet attrs, int defStyle) { 75 super(context, attrs, defStyle); 76 } 77 78 @Override 79 protected void onAttachedToWindow() { 80 super.onAttachedToWindow(); 81 82 if (!mAttached) { 83 mAttached = true; 84 IntentFilter filter = new IntentFilter(); 85 86 filter.addAction(Intent.ACTION_TIME_TICK); 87 filter.addAction(Intent.ACTION_TIME_CHANGED); 88 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 89 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); 90 filter.addAction(Intent.ACTION_USER_SWITCHED); 91 92 getContext().registerReceiver(mIntentReceiver, filter, null, getHandler()); 93 } 94 95 // NOTE: It's safe to do these after registering the receiver since the receiver always runs 96 // in the main thread, therefore the receiver can't run before this method returns. 97 98 // The time zone may have changed while the receiver wasn't registered, so update the Time 99 mCalendar = Calendar.getInstance(TimeZone.getDefault()); 100 101 // Make sure we update to the current time 102 updateClock(); 103 } 104 105 @Override 106 protected void onDetachedFromWindow() { 107 super.onDetachedFromWindow(); 108 if (mAttached) { 109 getContext().unregisterReceiver(mIntentReceiver); 110 mAttached = false; 111 } 112 } 113 114 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 115 @Override 116 public void onReceive(Context context, Intent intent) { 117 String action = intent.getAction(); 118 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { 119 String tz = intent.getStringExtra("time-zone"); 120 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz)); 121 if (mClockFormat != null) { 122 mClockFormat.setTimeZone(mCalendar.getTimeZone()); 123 } 124 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { 125 final Locale newLocale = getResources().getConfiguration().locale; 126 if (! newLocale.equals(mLocale)) { 127 mLocale = newLocale; 128 mClockFormatString = ""; // force refresh 129 } 130 } 131 updateClock(); 132 } 133 }; 134 135 final void updateClock() { 136 mCalendar.setTimeInMillis(System.currentTimeMillis()); 137 setText(getSmallTime()); 138 } 139 140 private final CharSequence getSmallTime() { 141 Context context = getContext(); 142 boolean is24 = DateFormat.is24HourFormat(context); 143 LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); 144 145 final char MAGIC1 = '\uEF00'; 146 final char MAGIC2 = '\uEF01'; 147 148 SimpleDateFormat sdf; 149 String format = is24 ? d.timeFormat24 : d.timeFormat12; 150 if (!format.equals(mClockFormatString)) { 151 /* 152 * Search for an unquoted "a" in the format string, so we can 153 * add dummy characters around it to let us find it again after 154 * formatting and change its size. 155 */ 156 if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) { 157 int a = -1; 158 boolean quoted = false; 159 for (int i = 0; i < format.length(); i++) { 160 char c = format.charAt(i); 161 162 if (c == '\'') { 163 quoted = !quoted; 164 } 165 if (!quoted && c == 'a') { 166 a = i; 167 break; 168 } 169 } 170 171 if (a >= 0) { 172 // Move a back so any whitespace before AM/PM is also in the alternate size. 173 final int b = a; 174 while (a > 0 && Character.isWhitespace(format.charAt(a-1))) { 175 a--; 176 } 177 format = format.substring(0, a) + MAGIC1 + format.substring(a, b) 178 + "a" + MAGIC2 + format.substring(b + 1); 179 } 180 } 181 mClockFormat = sdf = new SimpleDateFormat(format); 182 mClockFormatString = format; 183 } else { 184 sdf = mClockFormat; 185 } 186 String result = sdf.format(mCalendar.getTime()); 187 188 if (AM_PM_STYLE != AM_PM_STYLE_NORMAL) { 189 int magic1 = result.indexOf(MAGIC1); 190 int magic2 = result.indexOf(MAGIC2); 191 if (magic1 >= 0 && magic2 > magic1) { 192 SpannableStringBuilder formatted = new SpannableStringBuilder(result); 193 if (AM_PM_STYLE == AM_PM_STYLE_GONE) { 194 formatted.delete(magic1, magic2+1); 195 } else { 196 if (AM_PM_STYLE == AM_PM_STYLE_SMALL) { 197 CharacterStyle style = new RelativeSizeSpan(0.7f); 198 formatted.setSpan(style, magic1, magic2, 199 Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 200 } 201 formatted.delete(magic2, magic2 + 1); 202 formatted.delete(magic1, magic1 + 1); 203 } 204 return formatted; 205 } 206 } 207 208 return result; 209 210 } 211 } 212 213