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 libcore.icu.LocaleData; 20 21 import android.app.ActivityManager; 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.content.res.TypedArray; 27 import android.os.Bundle; 28 import android.os.Handler; 29 import android.os.SystemClock; 30 import android.os.UserHandle; 31 import android.text.Spannable; 32 import android.text.SpannableStringBuilder; 33 import android.text.format.DateFormat; 34 import android.text.style.CharacterStyle; 35 import android.text.style.RelativeSizeSpan; 36 import android.util.ArraySet; 37 import android.util.AttributeSet; 38 import android.view.Display; 39 import android.view.View; 40 import android.widget.TextView; 41 42 import com.android.systemui.DemoMode; 43 import com.android.systemui.R; 44 import com.android.systemui.statusbar.phone.StatusBarIconController; 45 import com.android.systemui.tuner.TunerService; 46 import com.android.systemui.tuner.TunerService.Tunable; 47 48 import java.text.SimpleDateFormat; 49 import java.util.Calendar; 50 import java.util.Locale; 51 import java.util.TimeZone; 52 53 /** 54 * Digital clock for the status bar. 55 */ 56 public class Clock extends TextView implements DemoMode, Tunable { 57 58 public static final String CLOCK_SECONDS = "clock_seconds"; 59 60 private boolean mAttached; 61 private Calendar mCalendar; 62 private String mClockFormatString; 63 private SimpleDateFormat mClockFormat; 64 private SimpleDateFormat mContentDescriptionFormat; 65 private Locale mLocale; 66 67 private static final int AM_PM_STYLE_NORMAL = 0; 68 private static final int AM_PM_STYLE_SMALL = 1; 69 private static final int AM_PM_STYLE_GONE = 2; 70 71 private final int mAmPmStyle; 72 private boolean mShowSeconds; 73 private Handler mSecondsHandler; 74 Clock(Context context)75 public Clock(Context context) { 76 this(context, null); 77 } 78 Clock(Context context, AttributeSet attrs)79 public Clock(Context context, AttributeSet attrs) { 80 this(context, attrs, 0); 81 } 82 Clock(Context context, AttributeSet attrs, int defStyle)83 public Clock(Context context, AttributeSet attrs, int defStyle) { 84 super(context, attrs, defStyle); 85 TypedArray a = context.getTheme().obtainStyledAttributes( 86 attrs, 87 R.styleable.Clock, 88 0, 0); 89 try { 90 mAmPmStyle = a.getInt(R.styleable.Clock_amPmStyle, AM_PM_STYLE_GONE); 91 } finally { 92 a.recycle(); 93 } 94 } 95 96 @Override onAttachedToWindow()97 protected void onAttachedToWindow() { 98 super.onAttachedToWindow(); 99 100 if (!mAttached) { 101 mAttached = true; 102 IntentFilter filter = new IntentFilter(); 103 104 filter.addAction(Intent.ACTION_TIME_TICK); 105 filter.addAction(Intent.ACTION_TIME_CHANGED); 106 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 107 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); 108 filter.addAction(Intent.ACTION_USER_SWITCHED); 109 110 getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter, 111 null, getHandler()); 112 TunerService.get(getContext()).addTunable(this, CLOCK_SECONDS, 113 StatusBarIconController.ICON_BLACKLIST); 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 updateShowSeconds(); 125 } 126 127 @Override onDetachedFromWindow()128 protected void onDetachedFromWindow() { 129 super.onDetachedFromWindow(); 130 if (mAttached) { 131 getContext().unregisterReceiver(mIntentReceiver); 132 mAttached = false; 133 TunerService.get(getContext()).removeTunable(this); 134 } 135 } 136 137 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 138 @Override 139 public void onReceive(Context context, Intent intent) { 140 String action = intent.getAction(); 141 if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { 142 String tz = intent.getStringExtra("time-zone"); 143 mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz)); 144 if (mClockFormat != null) { 145 mClockFormat.setTimeZone(mCalendar.getTimeZone()); 146 } 147 } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { 148 final Locale newLocale = getResources().getConfiguration().locale; 149 if (! newLocale.equals(mLocale)) { 150 mLocale = newLocale; 151 mClockFormatString = ""; // force refresh 152 } 153 } 154 updateClock(); 155 } 156 }; 157 updateClock()158 final void updateClock() { 159 if (mDemoMode) return; 160 mCalendar.setTimeInMillis(System.currentTimeMillis()); 161 setText(getSmallTime()); 162 setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime())); 163 } 164 165 @Override onTuningChanged(String key, String newValue)166 public void onTuningChanged(String key, String newValue) { 167 if (CLOCK_SECONDS.equals(key)) { 168 mShowSeconds = newValue != null && Integer.parseInt(newValue) != 0; 169 updateShowSeconds(); 170 } else if (StatusBarIconController.ICON_BLACKLIST.equals(key)) { 171 ArraySet<String> list = StatusBarIconController.getIconBlacklist(newValue); 172 setVisibility(list.contains("clock") ? View.GONE : View.VISIBLE); 173 } 174 } 175 updateShowSeconds()176 private void updateShowSeconds() { 177 if (mShowSeconds) { 178 // Wait until we have a display to start trying to show seconds. 179 if (mSecondsHandler == null && getDisplay() != null) { 180 mSecondsHandler = new Handler(); 181 if (getDisplay().getState() == Display.STATE_ON) { 182 mSecondsHandler.postAtTime(mSecondTick, 183 SystemClock.uptimeMillis() / 1000 * 1000 + 1000); 184 } 185 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); 186 filter.addAction(Intent.ACTION_SCREEN_ON); 187 mContext.registerReceiver(mScreenReceiver, filter); 188 } 189 } else { 190 if (mSecondsHandler != null) { 191 mContext.unregisterReceiver(mScreenReceiver); 192 mSecondsHandler.removeCallbacks(mSecondTick); 193 mSecondsHandler = null; 194 updateClock(); 195 } 196 } 197 } 198 getSmallTime()199 private final CharSequence getSmallTime() { 200 Context context = getContext(); 201 boolean is24 = DateFormat.is24HourFormat(context, ActivityManager.getCurrentUser()); 202 LocaleData d = LocaleData.get(context.getResources().getConfiguration().locale); 203 204 final char MAGIC1 = '\uEF00'; 205 final char MAGIC2 = '\uEF01'; 206 207 SimpleDateFormat sdf; 208 String format = mShowSeconds 209 ? is24 ? d.timeFormat_Hms : d.timeFormat_hms 210 : is24 ? d.timeFormat_Hm : d.timeFormat_hm; 211 if (!format.equals(mClockFormatString)) { 212 mContentDescriptionFormat = new SimpleDateFormat(format); 213 /* 214 * Search for an unquoted "a" in the format string, so we can 215 * add dummy characters around it to let us find it again after 216 * formatting and change its size. 217 */ 218 if (mAmPmStyle != AM_PM_STYLE_NORMAL) { 219 int a = -1; 220 boolean quoted = false; 221 for (int i = 0; i < format.length(); i++) { 222 char c = format.charAt(i); 223 224 if (c == '\'') { 225 quoted = !quoted; 226 } 227 if (!quoted && c == 'a') { 228 a = i; 229 break; 230 } 231 } 232 233 if (a >= 0) { 234 // Move a back so any whitespace before AM/PM is also in the alternate size. 235 final int b = a; 236 while (a > 0 && Character.isWhitespace(format.charAt(a-1))) { 237 a--; 238 } 239 format = format.substring(0, a) + MAGIC1 + format.substring(a, b) 240 + "a" + MAGIC2 + format.substring(b + 1); 241 } 242 } 243 mClockFormat = sdf = new SimpleDateFormat(format); 244 mClockFormatString = format; 245 } else { 246 sdf = mClockFormat; 247 } 248 String result = sdf.format(mCalendar.getTime()); 249 250 if (mAmPmStyle != AM_PM_STYLE_NORMAL) { 251 int magic1 = result.indexOf(MAGIC1); 252 int magic2 = result.indexOf(MAGIC2); 253 if (magic1 >= 0 && magic2 > magic1) { 254 SpannableStringBuilder formatted = new SpannableStringBuilder(result); 255 if (mAmPmStyle == AM_PM_STYLE_GONE) { 256 formatted.delete(magic1, magic2+1); 257 } else { 258 if (mAmPmStyle == AM_PM_STYLE_SMALL) { 259 CharacterStyle style = new RelativeSizeSpan(0.7f); 260 formatted.setSpan(style, magic1, magic2, 261 Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 262 } 263 formatted.delete(magic2, magic2 + 1); 264 formatted.delete(magic1, magic1 + 1); 265 } 266 return formatted; 267 } 268 } 269 270 return result; 271 272 } 273 274 private boolean mDemoMode; 275 276 @Override dispatchDemoCommand(String command, Bundle args)277 public void dispatchDemoCommand(String command, Bundle args) { 278 if (!mDemoMode && command.equals(COMMAND_ENTER)) { 279 mDemoMode = true; 280 } else if (mDemoMode && command.equals(COMMAND_EXIT)) { 281 mDemoMode = false; 282 updateClock(); 283 } else if (mDemoMode && command.equals(COMMAND_CLOCK)) { 284 String millis = args.getString("millis"); 285 String hhmm = args.getString("hhmm"); 286 if (millis != null) { 287 mCalendar.setTimeInMillis(Long.parseLong(millis)); 288 } else if (hhmm != null && hhmm.length() == 4) { 289 int hh = Integer.parseInt(hhmm.substring(0, 2)); 290 int mm = Integer.parseInt(hhmm.substring(2)); 291 boolean is24 = DateFormat.is24HourFormat( 292 getContext(), ActivityManager.getCurrentUser()); 293 if (is24) { 294 mCalendar.set(Calendar.HOUR_OF_DAY, hh); 295 } else { 296 mCalendar.set(Calendar.HOUR, hh); 297 } 298 mCalendar.set(Calendar.MINUTE, mm); 299 } 300 setText(getSmallTime()); 301 setContentDescription(mContentDescriptionFormat.format(mCalendar.getTime())); 302 } 303 } 304 305 private final BroadcastReceiver mScreenReceiver = new BroadcastReceiver() { 306 @Override 307 public void onReceive(Context context, Intent intent) { 308 String action = intent.getAction(); 309 if (Intent.ACTION_SCREEN_OFF.equals(action)) { 310 if (mSecondsHandler != null) { 311 mSecondsHandler.removeCallbacks(mSecondTick); 312 } 313 } else if (Intent.ACTION_SCREEN_ON.equals(action)) { 314 if (mSecondsHandler != null) { 315 mSecondsHandler.postAtTime(mSecondTick, 316 SystemClock.uptimeMillis() / 1000 * 1000 + 1000); 317 } 318 } 319 } 320 }; 321 322 private final Runnable mSecondTick = new Runnable() { 323 @Override 324 public void run() { 325 if (mCalendar != null) { 326 updateClock(); 327 } 328 mSecondsHandler.postAtTime(this, SystemClock.uptimeMillis() / 1000 * 1000 + 1000); 329 } 330 }; 331 } 332 333