Home | History | Annotate | Download | only in webkit
      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 android.webkit;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 
     22 import java.util.Calendar;
     23 import java.util.Locale;
     24 
     25 import libcore.icu.LocaleData;
     26 
     27 /**
     28  * Sorts dates into the following groups:
     29  *   Today
     30  *   Yesterday
     31  *   seven days ago
     32  *   one month ago
     33  *   older than a month ago
     34  */
     35 
     36 public class DateSorter {
     37 
     38     private static final String LOGTAG = "webkit";
     39 
     40     /** must be >= 3 */
     41     public static final int DAY_COUNT = 5;
     42 
     43     private long [] mBins = new long[DAY_COUNT-1];
     44     private String [] mLabels = new String[DAY_COUNT];
     45 
     46     private static final int NUM_DAYS_AGO = 7;
     47 
     48     /**
     49      * @param context Application context
     50      */
     51     public DateSorter(Context context) {
     52         Resources resources = context.getResources();
     53 
     54         Calendar c = Calendar.getInstance();
     55         beginningOfDay(c);
     56 
     57         // Create the bins
     58         mBins[0] = c.getTimeInMillis(); // Today
     59         c.add(Calendar.DAY_OF_YEAR, -1);
     60         mBins[1] = c.getTimeInMillis();  // Yesterday
     61         c.add(Calendar.DAY_OF_YEAR, -(NUM_DAYS_AGO - 1));
     62         mBins[2] = c.getTimeInMillis();  // Five days ago
     63         c.add(Calendar.DAY_OF_YEAR, NUM_DAYS_AGO); // move back to today
     64         c.add(Calendar.MONTH, -1);
     65         mBins[3] = c.getTimeInMillis();  // One month ago
     66 
     67         // build labels
     68         Locale locale = resources.getConfiguration().locale;
     69         if (locale == null) {
     70             locale = Locale.getDefault();
     71         }
     72         LocaleData localeData = LocaleData.get(locale);
     73         mLabels[0] = localeData.today;
     74         mLabels[1] = localeData.yesterday;
     75 
     76         int resId = com.android.internal.R.plurals.last_num_days;
     77         String format = resources.getQuantityString(resId, NUM_DAYS_AGO);
     78         mLabels[2] = String.format(format, NUM_DAYS_AGO);
     79 
     80         mLabels[3] = context.getString(com.android.internal.R.string.last_month);
     81         mLabels[4] = context.getString(com.android.internal.R.string.older);
     82     }
     83 
     84     /**
     85      * @param time time since the Epoch in milliseconds, such as that
     86      * returned by Calendar.getTimeInMillis()
     87      * @return an index from 0 to (DAY_COUNT - 1) that identifies which
     88      * date bin this date belongs to
     89      */
     90     public int getIndex(long time) {
     91         int lastDay = DAY_COUNT - 1;
     92         for (int i = 0; i < lastDay; i++) {
     93             if (time > mBins[i]) return i;
     94         }
     95         return lastDay;
     96     }
     97 
     98     /**
     99      * @param index date bin index as returned by getIndex()
    100      * @return string label suitable for display to user
    101      */
    102     public String getLabel(int index) {
    103         if (index < 0 || index >= DAY_COUNT) return "";
    104         return mLabels[index];
    105     }
    106 
    107 
    108     /**
    109      * @param index date bin index as returned by getIndex()
    110      * @return date boundary at given index
    111      */
    112     public long getBoundary(int index) {
    113         int lastDay = DAY_COUNT - 1;
    114         // Error case
    115         if (index < 0 || index > lastDay) index = 0;
    116         // Since this provides a lower boundary on dates that will be included
    117         // in the given bin, provide the smallest value
    118         if (index == lastDay) return Long.MIN_VALUE;
    119         return mBins[index];
    120     }
    121 
    122     /**
    123      * Calcuate 12:00am by zeroing out hour, minute, second, millisecond
    124      */
    125     private void beginningOfDay(Calendar c) {
    126         c.set(Calendar.HOUR_OF_DAY, 0);
    127         c.set(Calendar.MINUTE, 0);
    128         c.set(Calendar.SECOND, 0);
    129         c.set(Calendar.MILLISECOND, 0);
    130     }
    131 }
    132