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