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; |
| 24 | |
| 25 | /** |
| 26 | * Sorts dates into the following groups: |
| 27 | * Today |
| 28 | * Yesterday |
Leon Scroggins | 3608d4a | 2010-02-19 11:18:53 -0500 | [diff] [blame] | 29 | * seven days ago |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 41 | private long [] mBins = new long[DAY_COUNT-1]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | private String [] mLabels = new String[DAY_COUNT]; |
| 43 | |
Leon Scroggins | 3608d4a | 2010-02-19 11:18:53 -0500 | [diff] [blame] | 44 | private static final int NUM_DAYS_AGO = 7; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 57 | c.add(Calendar.DAY_OF_YEAR, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | mBins[1] = c.getTimeInMillis(); // Yesterday |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 59 | 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] | 60 | mBins[2] = c.getTimeInMillis(); // Five days ago |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 61 | c.add(Calendar.DAY_OF_YEAR, NUM_DAYS_AGO); // move back to today |
| 62 | c.add(Calendar.MONTH, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | mBins[3] = c.getTimeInMillis(); // One month ago |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Leon Scroggins | 4379dca | 2010-02-19 15:17:21 -0500 | [diff] [blame] | 69 | 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] | 70 | String format = resources.getQuantityString(resId, NUM_DAYS_AGO); |
| 71 | mLabels[2] = String.format(format, NUM_DAYS_AGO); |
| 72 | |
Leon Scroggins | 4379dca | 2010-02-19 15:17:21 -0500 | [diff] [blame] | 73 | mLabels[3] = context.getString(com.android.internal.R.string.last_month); |
| 74 | 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] | 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) { |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 84 | int lastDay = DAY_COUNT - 1; |
| 85 | for (int i = 0; i < lastDay; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | if (time > mBins[i]) return i; |
| 87 | } |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 88 | return lastDay; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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) { |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 96 | if (index < 0 || index >= DAY_COUNT) return ""; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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) { |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | return mBins[index]; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Calcuate 12:00am by zeroing out hour, minute, second, millisecond |
| 117 | */ |
Leon Scroggins | c89b13b | 2010-01-07 18:47:06 -0500 | [diff] [blame] | 118 | private void beginningOfDay(Calendar c) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | } |
| 124 | } |