DateSorter.java revision a589419c04f278a5d94811091a1100d284f3f2ec
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 17package android.webkit; 18 19import android.content.Context; 20import android.content.res.Resources; 21 22import java.util.Calendar; 23import java.util.Date; 24 25/** 26 * Sorts dates into the following groups: 27 * Today 28 * Yesterday 29 * five days ago 30 * one month ago 31 * older than a month ago 32 */ 33 34public 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]; 42 private String [] mLabels = new String[DAY_COUNT]; 43 44 private static final int NUM_DAYS_AGO = 5; 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.roll(Calendar.DAY_OF_YEAR, -1); 58 mBins[1] = c.getTimeInMillis(); // Yesterday 59 c.roll(Calendar.DAY_OF_YEAR, -(NUM_DAYS_AGO - 1)); 60 mBins[2] = c.getTimeInMillis(); // Five days ago 61 c.roll(Calendar.DAY_OF_YEAR, NUM_DAYS_AGO); // move back to today 62 c.roll(Calendar.MONTH, -1); 63 mBins[3] = c.getTimeInMillis(); // One month ago 64 c.roll(Calendar.MONTH, -1); 65 mBins[4] = c.getTimeInMillis(); // Over one month ago 66 67 // build labels 68 mLabels[0] = context.getText(com.android.internal.R.string.today).toString(); 69 mLabels[1] = context.getText(com.android.internal.R.string.yesterday).toString(); 70 71 int resId = com.android.internal.R.plurals.num_days_ago; 72 String format = resources.getQuantityString(resId, NUM_DAYS_AGO); 73 mLabels[2] = String.format(format, NUM_DAYS_AGO); 74 75 mLabels[3] = context.getText(com.android.internal.R.string.oneMonthDurationPast).toString(); 76 mLabels[4] = context.getText(com.android.internal.R.string.beforeOneMonthDurationPast) 77 .toString(); 78 } 79 80 /** 81 * @param time time since the Epoch in milliseconds, such as that 82 * returned by Calendar.getTimeInMillis() 83 * @return an index from 0 to (DAY_COUNT - 1) that identifies which 84 * date bin this date belongs to 85 */ 86 public int getIndex(long time) { 87 // Lame linear search 88 for (int i = 0; i < DAY_COUNT; i++) { 89 if (time > mBins[i]) return i; 90 } 91 return DAY_COUNT - 1; 92 } 93 94 /** 95 * @param index date bin index as returned by getIndex() 96 * @return string label suitable for display to user 97 */ 98 public String getLabel(int index) { 99 return mLabels[index]; 100 } 101 102 103 /** 104 * @param index date bin index as returned by getIndex() 105 * @return date boundary at given index 106 */ 107 public long getBoundary(int index) { 108 return mBins[index]; 109 } 110 111 /** 112 * Calcuate 12:00am by zeroing out hour, minute, second, millisecond 113 */ 114 private Calendar beginningOfDay(Calendar c) { 115 c.set(Calendar.HOUR_OF_DAY, 0); 116 c.set(Calendar.MINUTE, 0); 117 c.set(Calendar.SECOND, 0); 118 c.set(Calendar.MILLISECOND, 0); 119 return c; 120 } 121} 122