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