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