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 
24 import android.app.ActivityThread;
25 import android.compat.annotation.UnsupportedAppUsage;
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.os.Build;
34 import android.os.Handler;
35 import android.text.TextUtils;
36 import android.util.AttributeSet;
37 import android.util.PluralsMessageFormatter;
38 import android.view.accessibility.AccessibilityNodeInfo;
39 import android.view.inspector.InspectableProperty;
40 import android.widget.RemoteViews.RemoteView;
41 
42 import com.android.internal.R;
43 
44 import java.text.DateFormat;
45 import java.time.Instant;
46 import java.time.LocalDate;
47 import java.time.LocalDateTime;
48 import java.time.LocalTime;
49 import java.time.ZoneId;
50 import java.time.temporal.JulianFields;
51 import java.util.ArrayList;
52 import java.util.Date;
53 import java.util.HashMap;
54 import java.util.Map;
55 
56 //
57 // TODO
58 // - listen for the next threshold time to update the view.
59 // - listen for date format pref changed
60 // - put the AM/PM in a smaller font
61 //
62 
63 /**
64  * Displays a given time in a convenient human-readable foramt.
65  *
66  * @hide
67  */
68 @RemoteView
69 public class DateTimeView extends TextView {
70     private static final int SHOW_TIME = 0;
71     private static final int SHOW_MONTH_DAY_YEAR = 1;
72 
73     private long mTimeMillis;
74     // The LocalDateTime equivalent of mTimeMillis but truncated to minute, i.e. no seconds / nanos.
75     private LocalDateTime mLocalTime;
76 
77     int mLastDisplay = -1;
78     DateFormat mLastFormat;
79 
80     private long mUpdateTimeMillis;
81     private static final ThreadLocal<ReceiverInfo> sReceiverInfo = new ThreadLocal<ReceiverInfo>();
82     private String mNowText;
83     private boolean mShowRelativeTime;
84 
DateTimeView(Context context)85     public DateTimeView(Context context) {
86         this(context, null);
87     }
88 
89     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
DateTimeView(Context context, AttributeSet attrs)90     public DateTimeView(Context context, AttributeSet attrs) {
91         super(context, attrs);
92         final TypedArray a = context.obtainStyledAttributes(attrs,
93                 com.android.internal.R.styleable.DateTimeView, 0,
94                 0);
95 
96         final int N = a.getIndexCount();
97         for (int i = 0; i < N; i++) {
98             int attr = a.getIndex(i);
99             switch (attr) {
100                 case R.styleable.DateTimeView_showRelative:
101                     boolean relative = a.getBoolean(i, false);
102                     setShowRelativeTime(relative);
103                     break;
104             }
105         }
106         a.recycle();
107     }
108 
109     @Override
onAttachedToWindow()110     protected void onAttachedToWindow() {
111         super.onAttachedToWindow();
112         ReceiverInfo ri = sReceiverInfo.get();
113         if (ri == null) {
114             ri = new ReceiverInfo();
115             sReceiverInfo.set(ri);
116         }
117         ri.addView(this);
118         // The view may not be added to the view hierarchy immediately right after setTime()
119         // is called which means it won't get any update from intents before being added.
120         // In such case, the view might show the incorrect relative time after being added to the
121         // view hierarchy until the next update intent comes.
122         // So we update the time here if mShowRelativeTime is enabled to prevent this case.
123         if (mShowRelativeTime) {
124             update();
125         }
126     }
127 
128     @Override
onDetachedFromWindow()129     protected void onDetachedFromWindow() {
130         super.onDetachedFromWindow();
131         final ReceiverInfo ri = sReceiverInfo.get();
132         if (ri != null) {
133             ri.removeView(this);
134         }
135     }
136 
137     @android.view.RemotableViewMethod
138     @UnsupportedAppUsage
setTime(long timeMillis)139     public void setTime(long timeMillis) {
140         mTimeMillis = timeMillis;
141         LocalDateTime dateTime = toLocalDateTime(timeMillis, ZoneId.systemDefault());
142         mLocalTime = dateTime.withSecond(0);
143         update();
144     }
145 
146     @android.view.RemotableViewMethod
setShowRelativeTime(boolean showRelativeTime)147     public void setShowRelativeTime(boolean showRelativeTime) {
148         mShowRelativeTime = showRelativeTime;
149         updateNowText();
150         update();
151     }
152 
153     /**
154      * Returns whether this view shows relative time
155      *
156      * @return True if it shows relative time, false otherwise
157      */
158     @InspectableProperty(name = "showReleative", hasAttributeId = false)
isShowRelativeTime()159     public boolean isShowRelativeTime() {
160         return mShowRelativeTime;
161     }
162 
163     @Override
164     @android.view.RemotableViewMethod
setVisibility(@isibility int visibility)165     public void setVisibility(@Visibility int visibility) {
166         boolean gotVisible = visibility != GONE && getVisibility() == GONE;
167         super.setVisibility(visibility);
168         if (gotVisible) {
169             update();
170         }
171     }
172 
173     @UnsupportedAppUsage
update()174     void update() {
175         if (mLocalTime == null || getVisibility() == GONE) {
176             return;
177         }
178         if (mShowRelativeTime) {
179             updateRelativeTime();
180             return;
181         }
182 
183         int display;
184         ZoneId zoneId = ZoneId.systemDefault();
185 
186         // localTime is the local time for mTimeMillis but at zero seconds past the minute.
187         LocalDateTime localTime = mLocalTime;
188         LocalDateTime localStartOfDay =
189                 LocalDateTime.of(localTime.toLocalDate(), LocalTime.MIDNIGHT);
190         LocalDateTime localTomorrowStartOfDay = localStartOfDay.plusDays(1);
191         // now is current local time but at zero seconds past the minute.
192         LocalDateTime localNow = LocalDateTime.now(zoneId).withSecond(0);
193 
194         long twelveHoursBefore = toEpochMillis(localTime.minusHours(12), zoneId);
195         long twelveHoursAfter = toEpochMillis(localTime.plusHours(12), zoneId);
196         long midnightBefore = toEpochMillis(localStartOfDay, zoneId);
197         long midnightAfter = toEpochMillis(localTomorrowStartOfDay, zoneId);
198         long time = toEpochMillis(localTime, zoneId);
199         long now = toEpochMillis(localNow, zoneId);
200 
201         // Choose the display mode
202         choose_display: {
203             if ((now >= midnightBefore && now < midnightAfter)
204                     || (now >= twelveHoursBefore && now < twelveHoursAfter)) {
205                 display = SHOW_TIME;
206                 break choose_display;
207             }
208             // Else, show month day and year.
209             display = SHOW_MONTH_DAY_YEAR;
210             break choose_display;
211         }
212 
213         // Choose the format
214         DateFormat format;
215         if (display == mLastDisplay && mLastFormat != null) {
216             // use cached format
217             format = mLastFormat;
218         } else {
219             switch (display) {
220                 case SHOW_TIME:
221                     format = getTimeFormat();
222                     break;
223                 case SHOW_MONTH_DAY_YEAR:
224                     format = DateFormat.getDateInstance(DateFormat.SHORT);
225                     break;
226                 default:
227                     throw new RuntimeException("unknown display value: " + display);
228             }
229             mLastFormat = format;
230         }
231 
232         // Set the text
233         String text = format.format(new Date(time));
234         maybeSetText(text);
235 
236         // Schedule the next update
237         if (display == SHOW_TIME) {
238             // Currently showing the time, update at the later of twelve hours after or midnight.
239             mUpdateTimeMillis = twelveHoursAfter > midnightAfter ? twelveHoursAfter : midnightAfter;
240         } else {
241             // Currently showing the date
242             if (mTimeMillis < now) {
243                 // If the time is in the past, don't schedule an update
244                 mUpdateTimeMillis = 0;
245             } else {
246                 // If hte time is in the future, schedule one at the earlier of twelve hours
247                 // before or midnight before.
248                 mUpdateTimeMillis = twelveHoursBefore < midnightBefore
249                         ? twelveHoursBefore : midnightBefore;
250             }
251         }
252     }
253 
254     private void updateRelativeTime() {
255         long now = System.currentTimeMillis();
256         long duration = Math.abs(now - mTimeMillis);
257         int count;
258         long millisIncrease;
259         boolean past = (now >= mTimeMillis);
260         String result;
261         if (duration < MINUTE_IN_MILLIS) {
262             maybeSetText(mNowText);
263             mUpdateTimeMillis = mTimeMillis + MINUTE_IN_MILLIS + 1;
264             return;
265         } else if (duration < HOUR_IN_MILLIS) {
266             count = (int)(duration / MINUTE_IN_MILLIS);
267             result = getContext().getResources().getString(past
268                     ? com.android.internal.R.string.duration_minutes_shortest
269                     : com.android.internal.R.string.duration_minutes_shortest_future,
270                     count);
271             millisIncrease = MINUTE_IN_MILLIS;
272         } else if (duration < DAY_IN_MILLIS) {
273             count = (int)(duration / HOUR_IN_MILLIS);
274             result = getContext().getResources().getString(past
275                             ? com.android.internal.R.string.duration_hours_shortest
276                             : com.android.internal.R.string.duration_hours_shortest_future,
277                             count);
278             millisIncrease = HOUR_IN_MILLIS;
279         } else if (duration < YEAR_IN_MILLIS) {
280             // In weird cases it can become 0 because of daylight savings
281             LocalDateTime localDateTime = mLocalTime;
282             ZoneId zoneId = ZoneId.systemDefault();
283             LocalDateTime localNow = toLocalDateTime(now, zoneId);
284 
285             count = Math.max(Math.abs(dayDistance(localDateTime, localNow)), 1);
286             result = getContext().getResources().getString(past
287                     ? com.android.internal.R.string.duration_days_shortest
288                     : com.android.internal.R.string.duration_days_shortest_future,
289                     count);
290             if (past || count != 1) {
291                 mUpdateTimeMillis = computeNextMidnight(localNow, zoneId);
292                 millisIncrease = -1;
293             } else {
294                 millisIncrease = DAY_IN_MILLIS;
295             }
296 
297         } else {
298             count = (int)(duration / YEAR_IN_MILLIS);
299             result = getContext().getResources().getString(past
300                     ? com.android.internal.R.string.duration_years_shortest
301                     : com.android.internal.R.string.duration_years_shortest_future,
302                     count);
303             millisIncrease = YEAR_IN_MILLIS;
304         }
305         if (millisIncrease != -1) {
306             if (past) {
307                 mUpdateTimeMillis = mTimeMillis + millisIncrease * (count + 1) + 1;
308             } else {
309                 mUpdateTimeMillis = mTimeMillis - millisIncrease * count + 1;
310             }
311         }
312         maybeSetText(result);
313     }
314 
315     /**
316      * Sets text only if the text has actually changed. This prevents needles relayouts of this
317      * view when set to wrap_content.
318      */
maybeSetText(String text)319     private void maybeSetText(String text) {
320         if (TextUtils.equals(getText(), text)) {
321             return;
322         }
323 
324         setText(text);
325     }
326 
327     /**
328      * Returns the epoch millis for the next midnight in the specified timezone.
329      */
computeNextMidnight(LocalDateTime time, ZoneId zoneId)330     private static long computeNextMidnight(LocalDateTime time, ZoneId zoneId) {
331         // This ignores the chance of overflow: it should never happen.
332         LocalDate tomorrow = time.toLocalDate().plusDays(1);
333         LocalDateTime nextMidnight = LocalDateTime.of(tomorrow, LocalTime.MIDNIGHT);
334         return toEpochMillis(nextMidnight, zoneId);
335     }
336 
337     @Override
onConfigurationChanged(Configuration newConfig)338     protected void onConfigurationChanged(Configuration newConfig) {
339         super.onConfigurationChanged(newConfig);
340         updateNowText();
341         update();
342     }
343 
updateNowText()344     private void updateNowText() {
345         if (!mShowRelativeTime) {
346             return;
347         }
348         mNowText = getContext().getResources().getString(
349                 com.android.internal.R.string.now_string_shortest);
350     }
351 
352     // Return the number of days between the two dates.
dayDistance(LocalDateTime start, LocalDateTime end)353     private static int dayDistance(LocalDateTime start, LocalDateTime end) {
354         return (int) (end.getLong(JulianFields.JULIAN_DAY)
355                 - start.getLong(JulianFields.JULIAN_DAY));
356     }
357 
getTimeFormat()358     private DateFormat getTimeFormat() {
359         return android.text.format.DateFormat.getTimeFormat(getContext());
360     }
361 
clearFormatAndUpdate()362     void clearFormatAndUpdate() {
363         mLastFormat = null;
364         update();
365     }
366 
367     @Override
onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info)368     public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
369         super.onInitializeAccessibilityNodeInfoInternal(info);
370         if (mShowRelativeTime) {
371             // The short version of the time might not be completely understandable and for
372             // accessibility we rather have a longer version.
373             long now = System.currentTimeMillis();
374             long duration = Math.abs(now - mTimeMillis);
375             int count;
376             boolean past = (now >= mTimeMillis);
377             String result;
378             Map<String, Object> arguments = new HashMap<>();
379             if (duration < MINUTE_IN_MILLIS) {
380                 result = mNowText;
381             } else if (duration < HOUR_IN_MILLIS) {
382                 count = (int)(duration / MINUTE_IN_MILLIS);
383                 arguments.put("count", count);
384                 result = PluralsMessageFormatter.format(
385                         getContext().getResources(),
386                         arguments,
387                         past ? R.string.duration_minutes_relative
388                                 : R.string.duration_minutes_relative_future);
389             } else if (duration < DAY_IN_MILLIS) {
390                 count = (int)(duration / HOUR_IN_MILLIS);
391                 arguments.put("count", count);
392                 result = PluralsMessageFormatter.format(
393                         getContext().getResources(),
394                         arguments,
395                         past ? R.string.duration_hours_relative
396                                 : R.string.duration_hours_relative_future);
397             } else if (duration < YEAR_IN_MILLIS) {
398                 // In weird cases it can become 0 because of daylight savings
399                 LocalDateTime localDateTime = mLocalTime;
400                 ZoneId zoneId = ZoneId.systemDefault();
401                 LocalDateTime localNow = toLocalDateTime(now, zoneId);
402 
403                 count = Math.max(Math.abs(dayDistance(localDateTime, localNow)), 1);
404                 arguments.put("count", count);
405                 result = PluralsMessageFormatter.format(
406                         getContext().getResources(),
407                         arguments,
408                         past ? R.string.duration_days_relative
409                                 : R.string.duration_days_relative_future);
410             } else {
411                 count = (int)(duration / YEAR_IN_MILLIS);
412                 arguments.put("count", count);
413                 result = PluralsMessageFormatter.format(
414                         getContext().getResources(),
415                         arguments,
416                         past ? R.string.duration_years_relative
417                                 : R.string.duration_years_relative_future);
418             }
419             info.setText(result);
420         }
421     }
422 
423     /**
424      * @hide
425      */
setReceiverHandler(Handler handler)426     public static void setReceiverHandler(Handler handler) {
427         ReceiverInfo ri = sReceiverInfo.get();
428         if (ri == null) {
429             ri = new ReceiverInfo();
430             sReceiverInfo.set(ri);
431         }
432         ri.setHandler(handler);
433     }
434 
435     private static class ReceiverInfo {
436         private final ArrayList<DateTimeView> mAttachedViews = new ArrayList<DateTimeView>();
437         private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
438             @Override
439             public void onReceive(Context context, Intent intent) {
440                 String action = intent.getAction();
441                 if (Intent.ACTION_TIME_TICK.equals(action)) {
442                     if (System.currentTimeMillis() < getSoonestUpdateTime()) {
443                         // The update() function takes a few milliseconds to run because of
444                         // all of the time conversions it needs to do, so we can't do that
445                         // every minute.
446                         return;
447                     }
448                 }
449                 // ACTION_TIME_CHANGED can also signal a change of 12/24 hr. format.
450                 updateAll();
451             }
452         };
453 
454         private final ContentObserver mObserver = new ContentObserver(new Handler()) {
455             @Override
456             public void onChange(boolean selfChange) {
457                 updateAll();
458             }
459         };
460 
461         private Handler mHandler = new Handler();
462 
addView(DateTimeView v)463         public void addView(DateTimeView v) {
464             synchronized (mAttachedViews) {
465                 final boolean register = mAttachedViews.isEmpty();
466                 mAttachedViews.add(v);
467                 if (register) {
468                     register(getApplicationContextIfAvailable(v.getContext()));
469                 }
470             }
471         }
472 
removeView(DateTimeView v)473         public void removeView(DateTimeView v) {
474             synchronized (mAttachedViews) {
475                 final boolean removed = mAttachedViews.remove(v);
476                 // Only unregister once when we remove the last view in the list otherwise we risk
477                 // trying to unregister a receiver that is no longer registered.
478                 if (removed && mAttachedViews.isEmpty()) {
479                     unregister(getApplicationContextIfAvailable(v.getContext()));
480                 }
481             }
482         }
483 
updateAll()484         void updateAll() {
485             synchronized (mAttachedViews) {
486                 final int count = mAttachedViews.size();
487                 for (int i = 0; i < count; i++) {
488                     DateTimeView view = mAttachedViews.get(i);
489                     view.post(() -> view.clearFormatAndUpdate());
490                 }
491             }
492         }
493 
getSoonestUpdateTime()494         long getSoonestUpdateTime() {
495             long result = Long.MAX_VALUE;
496             synchronized (mAttachedViews) {
497                 final int count = mAttachedViews.size();
498                 for (int i = 0; i < count; i++) {
499                     final long time = mAttachedViews.get(i).mUpdateTimeMillis;
500                     if (time < result) {
501                         result = time;
502                     }
503                 }
504             }
505             return result;
506         }
507 
getApplicationContextIfAvailable(Context context)508         static final Context getApplicationContextIfAvailable(Context context) {
509             final Context ac = context.getApplicationContext();
510             return ac != null ? ac : ActivityThread.currentApplication().getApplicationContext();
511         }
512 
register(Context context)513         void register(Context context) {
514             final IntentFilter filter = new IntentFilter();
515             filter.addAction(Intent.ACTION_TIME_TICK);
516             filter.addAction(Intent.ACTION_TIME_CHANGED);
517             filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
518             filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
519             context.registerReceiver(mReceiver, filter, null, mHandler);
520         }
521 
unregister(Context context)522         void unregister(Context context) {
523             context.unregisterReceiver(mReceiver);
524         }
525 
setHandler(Handler handler)526         public void setHandler(Handler handler) {
527             mHandler = handler;
528             synchronized (mAttachedViews) {
529                 if (!mAttachedViews.isEmpty()) {
530                     unregister(mAttachedViews.get(0).getContext());
531                     register(mAttachedViews.get(0).getContext());
532                 }
533             }
534         }
535     }
536 
toLocalDateTime(long timeMillis, ZoneId zoneId)537     private static LocalDateTime toLocalDateTime(long timeMillis, ZoneId zoneId) {
538         // java.time types like LocalDateTime / Instant can support the full range of "long millis"
539         // with room to spare so we do not need to worry about overflow / underflow and the rsulting
540         // exceptions while the input to this class is a long.
541         Instant instant = Instant.ofEpochMilli(timeMillis);
542         return LocalDateTime.ofInstant(instant, zoneId);
543     }
544 
toEpochMillis(LocalDateTime time, ZoneId zoneId)545     private static long toEpochMillis(LocalDateTime time, ZoneId zoneId) {
546         Instant instant = time.toInstant(zoneId.getRules().getOffset(time));
547         return instant.toEpochMilli();
548     }
549 }
550