The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Elliott Hughes | 47a0539 | 2012-08-15 14:53:12 -0700 | [diff] [blame] | 24 | import java.util.Locale; |
| 25 | |
| 26 | import libcore.icu.LocaleData; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Sorts dates into the following groups: |
| 30 | * Today |
| 31 | * Yesterday |
Leon Scroggins | 3608d4a | 2010-02-19 11:18:53 -0500 | [diff] [blame] | 32 | * seven days ago |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | * one month ago |
| 34 | * older than a month ago |
| 35 | */ |
| 36 | |
| 37 | public class DateSorter { |
| 38 | |
| 39 | private static final String LOGTAG = "webkit"; |
| 40 | |
| 41 | /** must be >= 3 */ |
| 42 | public static final int DAY_COUNT = 5; |
| 43 | |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 44 | private long [] mBins = new long[DAY_COUNT-1]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | private String [] mLabels = new String[DAY_COUNT]; |
| 46 | |
Leon Scroggins | 3608d4a | 2010-02-19 11:18:53 -0500 | [diff] [blame] | 47 | private static final int NUM_DAYS_AGO = 7; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 60 | c.add(Calendar.DAY_OF_YEAR, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | mBins[1] = c.getTimeInMillis(); // Yesterday |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 62 | c.add(Calendar.DAY_OF_YEAR, -(NUM_DAYS_AGO - 1)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | mBins[2] = c.getTimeInMillis(); // Five days ago |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 64 | c.add(Calendar.DAY_OF_YEAR, NUM_DAYS_AGO); // move back to today |
| 65 | c.add(Calendar.MONTH, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | mBins[3] = c.getTimeInMillis(); // One month ago |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | |
| 68 | // build labels |
Elliott Hughes | 47a0539 | 2012-08-15 14:53:12 -0700 | [diff] [blame] | 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | |
Leon Scroggins | 4379dca | 2010-02-19 15:17:21 -0500 | [diff] [blame] | 77 | int resId = com.android.internal.R.plurals.last_num_days; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | String format = resources.getQuantityString(resId, NUM_DAYS_AGO); |
| 79 | mLabels[2] = String.format(format, NUM_DAYS_AGO); |
| 80 | |
Leon Scroggins | 4379dca | 2010-02-19 15:17:21 -0500 | [diff] [blame] | 81 | mLabels[3] = context.getString(com.android.internal.R.string.last_month); |
| 82 | mLabels[4] = context.getString(com.android.internal.R.string.older); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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) { |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 92 | int lastDay = DAY_COUNT - 1; |
| 93 | for (int i = 0; i < lastDay; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | if (time > mBins[i]) return i; |
| 95 | } |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 96 | return lastDay; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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) { |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 104 | if (index < 0 || index >= DAY_COUNT) return ""; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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) { |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | return mBins[index]; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Calcuate 12:00am by zeroing out hour, minute, second, millisecond |
| 125 | */ |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 126 | private void beginningOfDay(Calendar c) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | } |
| 132 | } |