DateSorter.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
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    Date mDate = new Date();
47    Calendar mCal = Calendar.getInstance();
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.roll(Calendar.DAY_OF_YEAR, -1);
61        mBins[1] = c.getTimeInMillis();  // Yesterday
62        c.roll(Calendar.DAY_OF_YEAR, -(NUM_DAYS_AGO - 1));
63        mBins[2] = c.getTimeInMillis();  // Five days ago
64        c.roll(Calendar.DAY_OF_YEAR, NUM_DAYS_AGO); // move back to today
65        c.roll(Calendar.MONTH, -1);
66        mBins[3] = c.getTimeInMillis();  // One month ago
67        c.roll(Calendar.MONTH, -1);
68        mBins[4] = c.getTimeInMillis();  // Over one month ago
69
70        // build labels
71        mLabels[0] = context.getText(com.android.internal.R.string.today).toString();
72        mLabels[1] = context.getText(com.android.internal.R.string.yesterday).toString();
73
74        int resId = com.android.internal.R.plurals.num_days_ago;
75        String format = resources.getQuantityString(resId, NUM_DAYS_AGO);
76        mLabels[2] = String.format(format, NUM_DAYS_AGO);
77
78        mLabels[3] = context.getText(com.android.internal.R.string.oneMonthDurationPast).toString();
79        mLabels[4] = context.getText(com.android.internal.R.string.beforeOneMonthDurationPast)
80                .toString();
81    }
82
83    /**
84     * @param time time since the Epoch in milliseconds, such as that
85     * returned by Calendar.getTimeInMillis()
86     * @return an index from 0 to (DAY_COUNT - 1) that identifies which
87     * date bin this date belongs to
88     */
89    public int getIndex(long time) {
90        // Lame linear search
91        for (int i = 0; i < DAY_COUNT; i++) {
92            if (time > mBins[i]) return i;
93        }
94        return DAY_COUNT - 1;
95    }
96
97    /**
98     * @param index date bin index as returned by getIndex()
99     * @return string label suitable for display to user
100     */
101    public String getLabel(int index) {
102        return mLabels[index];
103    }
104
105
106    /**
107     * @param index date bin index as returned by getIndex()
108     * @return date boundary at given index
109     */
110    public long getBoundary(int index) {
111        return mBins[index];
112    }
113
114    /**
115     * Calcuate 12:00am by zeroing out hour, minute, second, millisecond
116     */
117    private Calendar beginningOfDay(Calendar c) {
118        c.set(Calendar.HOUR_OF_DAY, 0);
119        c.set(Calendar.MINUTE, 0);
120        c.set(Calendar.SECOND, 0);
121        c.set(Calendar.MILLISECOND, 0);
122        return c;
123    }
124}
125