1 /* 2 * Copyright (C) 2010 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 android.widget; 18 19 import static android.text.format.DateUtils.DAY_IN_MILLIS; 20 import static android.text.format.DateUtils.HOUR_IN_MILLIS; 21 import static android.text.format.DateUtils.MINUTE_IN_MILLIS; 22 import static android.text.format.DateUtils.YEAR_IN_MILLIS; 23 import static android.text.format.Time.getJulianDay; 24 25 import android.app.ActivityThread; 26 import android.content.BroadcastReceiver; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.content.IntentFilter; 30 import android.content.res.Configuration; 31 import android.content.res.TypedArray; 32 import android.database.ContentObserver; 33 import android.icu.util.Calendar; 34 import android.os.Handler; 35 import android.text.format.Time; 36 import android.util.AttributeSet; 37 import android.view.accessibility.AccessibilityNodeInfo; 38 import android.widget.RemoteViews.RemoteView; 39 40 import com.android.internal.R; 41 42 import java.text.DateFormat; 43 import java.util.ArrayList; 44 import java.util.Date; 45 import java.util.TimeZone; 46 47 // 48 // TODO 49 // - listen for the next threshold time to update the view. 50 // - listen for date format pref changed 51 // - put the AM/PM in a smaller font 52 // 53 54 /** 55 * Displays a given time in a convenient human-readable foramt. 56 * 57 * @hide 58 */ 59 @RemoteView 60 public class DateTimeView extends TextView { 61 private static final int SHOW_TIME = 0; 62 private static final int SHOW_MONTH_DAY_YEAR = 1; 63 64 Date mTime; 65 long mTimeMillis; 66 67 int mLastDisplay = -1; 68 DateFormat mLastFormat; 69 70 private long mUpdateTimeMillis; 71 private static final ThreadLocal<ReceiverInfo> sReceiverInfo = new ThreadLocal<ReceiverInfo>(); 72 private String mNowText; 73 private boolean mShowRelativeTime; 74 DateTimeView(Context context)75 public DateTimeView(Context context) { 76 this(context, null); 77 } 78 DateTimeView(Context context, AttributeSet attrs)79 public DateTimeView(Context context, AttributeSet attrs) { 80 super(context, attrs); 81 final TypedArray a = context.obtainStyledAttributes(attrs, 82 com.android.internal.R.styleable.DateTimeView, 0, 83 0); 84 85 final int N = a.getIndexCount(); 86 for (int i = 0; i < N; i++) { 87 int attr = a.getIndex(i); 88 switch (attr) { 89 case R.styleable.DateTimeView_showRelative: 90 boolean relative = a.getBoolean(i, false); 91 setShowRelativeTime(relative); 92 break; 93 } 94 } 95 a.recycle(); 96 } 97 98 @Override onAttachedToWindow()99 protected void onAttachedToWindow() { 100 super.onAttachedToWindow(); 101 ReceiverInfo ri = sReceiverInfo.get(); 102 if (ri == null) { 103 ri = new ReceiverInfo(); 104 sReceiverInfo.set(ri); 105 } 106 ri.addView(this); 107 } 108 109 @Override onDetachedFromWindow()110 protected void onDetachedFromWindow() { 111 super.onDetachedFromWindow(); 112 final ReceiverInfo ri = sReceiverInfo.get(); 113 if (ri != null) { 114 ri.removeView(this); 115 } 116 } 117 118 @android.view.RemotableViewMethod setTime(long time)119 public void setTime(long time) { 120 Time t = new Time(); 121 t.set(time); 122 mTimeMillis = t.toMillis(false); 123 mTime = new Date(t.year-1900, t.month, t.monthDay, t.hour, t.minute, 0); 124 update(); 125 } 126 127 @android.view.RemotableViewMethod setShowRelativeTime(boolean showRelativeTime)128 public void setShowRelativeTime(boolean showRelativeTime) { 129 mShowRelativeTime = showRelativeTime; 130 updateNowText(); 131 update(); 132 } 133 134 @Override 135 @android.view.RemotableViewMethod setVisibility(@isibility int visibility)136 public void setVisibility(@Visibility int visibility) { 137 boolean gotVisible = visibility != GONE && getVisibility() == GONE; 138 super.setVisibility(visibility); 139 if (gotVisible) { 140 update(); 141 } 142 } 143 update()144 void update() { 145 if (mTime == null || getVisibility() == GONE) { 146 return; 147 } 148 if (mShowRelativeTime) { 149 updateRelativeTime(); 150 return; 151 } 152 153 int display; 154 Date time = mTime; 155 156 Time t = new Time(); 157 t.set(mTimeMillis); 158 t.second = 0; 159 160 t.hour -= 12; 161 long twelveHoursBefore = t.toMillis(false); 162 t.hour += 12; 163 long twelveHoursAfter = t.toMillis(false); 164 t.hour = 0; 165 t.minute = 0; 166 long midnightBefore = t.toMillis(false); 167 t.monthDay++; 168 long midnightAfter = t.toMillis(false); 169 170 long nowMillis = System.currentTimeMillis(); 171 t.set(nowMillis); 172 t.second = 0; 173 nowMillis = t.normalize(false); 174 175 // Choose the display mode 176 choose_display: { 177 if ((nowMillis >= midnightBefore && nowMillis < midnightAfter) 178 || (nowMillis >= twelveHoursBefore && nowMillis < twelveHoursAfter)) { 179 display = SHOW_TIME; 180 break choose_display; 181 } 182 // Else, show month day and year. 183 display = SHOW_MONTH_DAY_YEAR; 184 break choose_display; 185 } 186 187 // Choose the format 188 DateFormat format; 189 if (display == mLastDisplay && mLastFormat != null) { 190 // use cached format 191 format = mLastFormat; 192 } else { 193 switch (display) { 194 case SHOW_TIME: 195 format = getTimeFormat(); 196 break; 197 case SHOW_MONTH_DAY_YEAR: 198 format = DateFormat.getDateInstance(DateFormat.SHORT); 199 break; 200 default: 201 throw new RuntimeException("unknown display value: " + display); 202 } 203 mLastFormat = format; 204 } 205 206 // Set the text 207 String text = format.format(mTime); 208 setText(text); 209 210 // Schedule the next update 211 if (display == SHOW_TIME) { 212 // Currently showing the time, update at the later of twelve hours after or midnight. 213 mUpdateTimeMillis = twelveHoursAfter > midnightAfter ? twelveHoursAfter : midnightAfter; 214 } else { 215 // Currently showing the date 216 if (mTimeMillis < nowMillis) { 217 // If the time is in the past, don't schedule an update 218 mUpdateTimeMillis = 0; 219 } else { 220 // If hte time is in the future, schedule one at the earlier of twelve hours 221 // before or midnight before. 222 mUpdateTimeMillis = twelveHoursBefore < midnightBefore 223 ? twelveHoursBefore : midnightBefore; 224 } 225 } 226 } 227 228 private void updateRelativeTime() { 229 long now = System.currentTimeMillis(); 230 long duration = Math.abs(now - mTimeMillis); 231 int count; 232 long millisIncrease; 233 boolean past = (now >= mTimeMillis); 234 String result; 235 if (duration < MINUTE_IN_MILLIS) { 236 setText(mNowText); 237 mUpdateTimeMillis = mTimeMillis + MINUTE_IN_MILLIS + 1; 238 return; 239 } else if (duration < HOUR_IN_MILLIS) { 240 count = (int)(duration / MINUTE_IN_MILLIS); 241 result = String.format(getContext().getResources().getQuantityString(past 242 ? com.android.internal.R.plurals.duration_minutes_shortest 243 : com.android.internal.R.plurals.duration_minutes_shortest_future, 244 count), 245 count); 246 millisIncrease = MINUTE_IN_MILLIS; 247 } else if (duration < DAY_IN_MILLIS) { 248 count = (int)(duration / HOUR_IN_MILLIS); 249 result = String.format(getContext().getResources().getQuantityString(past 250 ? com.android.internal.R.plurals.duration_hours_shortest 251 : com.android.internal.R.plurals.duration_hours_shortest_future, 252 count), 253 count); 254 millisIncrease = HOUR_IN_MILLIS; 255 } else if (duration < YEAR_IN_MILLIS) { 256 // In weird cases it can become 0 because of daylight savings 257 TimeZone timeZone = TimeZone.getDefault(); 258 count = Math.max(Math.abs(dayDistance(timeZone, mTimeMillis, now)), 1); 259 result = String.format(getContext().getResources().getQuantityString(past 260 ? com.android.internal.R.plurals.duration_days_shortest 261 : com.android.internal.R.plurals.duration_days_shortest_future, 262 count), 263 count); 264 if (past || count != 1) { 265 mUpdateTimeMillis = computeNextMidnight(timeZone); 266 millisIncrease = -1; 267 } else { 268 millisIncrease = DAY_IN_MILLIS; 269 } 270 271 } else { 272 count = (int)(duration / YEAR_IN_MILLIS); 273 result = String.format(getContext().getResources().getQuantityString(past 274 ? com.android.internal.R.plurals.duration_years_shortest 275 : com.android.internal.R.plurals.duration_years_shortest_future, 276 count), 277 count); 278 millisIncrease = YEAR_IN_MILLIS; 279 } 280 if (millisIncrease != -1) { 281 if (past) { 282 mUpdateTimeMillis = mTimeMillis + millisIncrease * (count + 1) + 1; 283 } else { 284 mUpdateTimeMillis = mTimeMillis - millisIncrease * count + 1; 285 } 286 } 287 setText(result); 288 } 289 290 /** 291 * @param timeZone the timezone we are in 292 * @return the timepoint in millis at UTC at midnight in the current timezone 293 */ computeNextMidnight(TimeZone timeZone)294 private long computeNextMidnight(TimeZone timeZone) { 295 Calendar c = Calendar.getInstance(); 296 c.setTimeZone(libcore.icu.DateUtilsBridge.icuTimeZone(timeZone)); 297 c.add(Calendar.DAY_OF_MONTH, 1); 298 c.set(Calendar.HOUR_OF_DAY, 0); 299 c.set(Calendar.MINUTE, 0); 300 c.set(Calendar.SECOND, 0); 301 c.set(Calendar.MILLISECOND, 0); 302 return c.getTimeInMillis(); 303 } 304 305 @Override onConfigurationChanged(Configuration newConfig)306 protected void onConfigurationChanged(Configuration newConfig) { 307 super.onConfigurationChanged(newConfig); 308 updateNowText(); 309 update(); 310 } 311 updateNowText()312 private void updateNowText() { 313 if (!mShowRelativeTime) { 314 return; 315 } 316 mNowText = getContext().getResources().getString( 317 com.android.internal.R.string.now_string_shortest); 318 } 319 320 // Return the date difference for the two times in a given timezone. dayDistance(TimeZone timeZone, long startTime, long endTime)321 private static int dayDistance(TimeZone timeZone, long startTime, 322 long endTime) { 323 return getJulianDay(endTime, timeZone.getOffset(endTime) / 1000) 324 - getJulianDay(startTime, timeZone.getOffset(startTime) / 1000); 325 } 326 getTimeFormat()327 private DateFormat getTimeFormat() { 328 return android.text.format.DateFormat.getTimeFormat(getContext()); 329 } 330 clearFormatAndUpdate()331 void clearFormatAndUpdate() { 332 mLastFormat = null; 333 update(); 334 } 335 336 @Override onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info)337 public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { 338 super.onInitializeAccessibilityNodeInfoInternal(info); 339 if (mShowRelativeTime) { 340 // The short version of the time might not be completely understandable and for 341 // accessibility we rather have a longer version. 342 long now = System.currentTimeMillis(); 343 long duration = Math.abs(now - mTimeMillis); 344 int count; 345 boolean past = (now >= mTimeMillis); 346 String result; 347 if (duration < MINUTE_IN_MILLIS) { 348 result = mNowText; 349 } else if (duration < HOUR_IN_MILLIS) { 350 count = (int)(duration / MINUTE_IN_MILLIS); 351 result = String.format(getContext().getResources().getQuantityString(past 352 ? com.android.internal. 353 R.plurals.duration_minutes_relative 354 : com.android.internal. 355 R.plurals.duration_minutes_relative_future, 356 count), 357 count); 358 } else if (duration < DAY_IN_MILLIS) { 359 count = (int)(duration / HOUR_IN_MILLIS); 360 result = String.format(getContext().getResources().getQuantityString(past 361 ? com.android.internal. 362 R.plurals.duration_hours_relative 363 : com.android.internal. 364 R.plurals.duration_hours_relative_future, 365 count), 366 count); 367 } else if (duration < YEAR_IN_MILLIS) { 368 // In weird cases it can become 0 because of daylight savings 369 TimeZone timeZone = TimeZone.getDefault(); 370 count = Math.max(Math.abs(dayDistance(timeZone, mTimeMillis, now)), 1); 371 result = String.format(getContext().getResources().getQuantityString(past 372 ? com.android.internal. 373 R.plurals.duration_days_relative 374 : com.android.internal. 375 R.plurals.duration_days_relative_future, 376 count), 377 count); 378 379 } else { 380 count = (int)(duration / YEAR_IN_MILLIS); 381 result = String.format(getContext().getResources().getQuantityString(past 382 ? com.android.internal. 383 R.plurals.duration_years_relative 384 : com.android.internal. 385 R.plurals.duration_years_relative_future, 386 count), 387 count); 388 } 389 info.setText(result); 390 } 391 } 392 393 /** 394 * @hide 395 */ setReceiverHandler(Handler handler)396 public static void setReceiverHandler(Handler handler) { 397 ReceiverInfo ri = sReceiverInfo.get(); 398 if (ri == null) { 399 ri = new ReceiverInfo(); 400 sReceiverInfo.set(ri); 401 } 402 ri.setHandler(handler); 403 } 404 405 private static class ReceiverInfo { 406 private final ArrayList<DateTimeView> mAttachedViews = new ArrayList<DateTimeView>(); 407 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 408 @Override 409 public void onReceive(Context context, Intent intent) { 410 String action = intent.getAction(); 411 if (Intent.ACTION_TIME_TICK.equals(action)) { 412 if (System.currentTimeMillis() < getSoonestUpdateTime()) { 413 // The update() function takes a few milliseconds to run because of 414 // all of the time conversions it needs to do, so we can't do that 415 // every minute. 416 return; 417 } 418 } 419 // ACTION_TIME_CHANGED can also signal a change of 12/24 hr. format. 420 updateAll(); 421 } 422 }; 423 424 private final ContentObserver mObserver = new ContentObserver(new Handler()) { 425 @Override 426 public void onChange(boolean selfChange) { 427 updateAll(); 428 } 429 }; 430 431 private Handler mHandler = new Handler(); 432 addView(DateTimeView v)433 public void addView(DateTimeView v) { 434 synchronized (mAttachedViews) { 435 final boolean register = mAttachedViews.isEmpty(); 436 mAttachedViews.add(v); 437 if (register) { 438 register(getApplicationContextIfAvailable(v.getContext())); 439 } 440 } 441 } 442 removeView(DateTimeView v)443 public void removeView(DateTimeView v) { 444 synchronized (mAttachedViews) { 445 mAttachedViews.remove(v); 446 if (mAttachedViews.isEmpty()) { 447 unregister(getApplicationContextIfAvailable(v.getContext())); 448 } 449 } 450 } 451 updateAll()452 void updateAll() { 453 synchronized (mAttachedViews) { 454 final int count = mAttachedViews.size(); 455 for (int i = 0; i < count; i++) { 456 DateTimeView view = mAttachedViews.get(i); 457 view.post(() -> view.clearFormatAndUpdate()); 458 } 459 } 460 } 461 getSoonestUpdateTime()462 long getSoonestUpdateTime() { 463 long result = Long.MAX_VALUE; 464 synchronized (mAttachedViews) { 465 final int count = mAttachedViews.size(); 466 for (int i = 0; i < count; i++) { 467 final long time = mAttachedViews.get(i).mUpdateTimeMillis; 468 if (time < result) { 469 result = time; 470 } 471 } 472 } 473 return result; 474 } 475 getApplicationContextIfAvailable(Context context)476 static final Context getApplicationContextIfAvailable(Context context) { 477 final Context ac = context.getApplicationContext(); 478 return ac != null ? ac : ActivityThread.currentApplication().getApplicationContext(); 479 } 480 register(Context context)481 void register(Context context) { 482 final IntentFilter filter = new IntentFilter(); 483 filter.addAction(Intent.ACTION_TIME_TICK); 484 filter.addAction(Intent.ACTION_TIME_CHANGED); 485 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); 486 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 487 context.registerReceiver(mReceiver, filter, null, mHandler); 488 } 489 unregister(Context context)490 void unregister(Context context) { 491 context.unregisterReceiver(mReceiver); 492 } 493 setHandler(Handler handler)494 public void setHandler(Handler handler) { 495 mHandler = handler; 496 synchronized (mAttachedViews) { 497 if (!mAttachedViews.isEmpty()) { 498 unregister(mAttachedViews.get(0).getContext()); 499 register(mAttachedViews.get(0).getContext()); 500 } 501 } 502 } 503 } 504 } 505