DateSorter.java revision c89b13b962c6634db536e563a42d058702ce045b
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-1];
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.add(Calendar.DAY_OF_YEAR, -1);
58        mBins[1] = c.getTimeInMillis();  // Yesterday
59        c.add(Calendar.DAY_OF_YEAR, -(NUM_DAYS_AGO - 1));
60        mBins[2] = c.getTimeInMillis();  // Five days ago
61        c.add(Calendar.DAY_OF_YEAR, NUM_DAYS_AGO); // move back to today
62        c.add(Calendar.MONTH, -1);
63        mBins[3] = c.getTimeInMillis();  // One month ago
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
69        int resId = com.android.internal.R.plurals.num_days_ago;
70        String format = resources.getQuantityString(resId, NUM_DAYS_AGO);
71        mLabels[2] = String.format(format, NUM_DAYS_AGO);
72
73        mLabels[3] = context.getText(com.android.internal.R.string.oneMonthDurationPast).toString();
74        mLabels[4] = context.getText(com.android.internal.R.string.beforeOneMonthDurationPast)
75                .toString();
76    }
77
78    /**
79     * @param time time since the Epoch in milliseconds, such as that
80     * returned by Calendar.getTimeInMillis()
81     * @return an index from 0 to (DAY_COUNT - 1) that identifies which
82     * date bin this date belongs to
83     */
84    public int getIndex(long time) {
85        int lastDay = DAY_COUNT - 1;
86        for (int i = 0; i < lastDay; i++) {
87            if (time > mBins[i]) return i;
88        }
89        return lastDay;
90    }
91
92    /**
93     * @param index date bin index as returned by getIndex()
94     * @return string label suitable for display to user
95     */
96    public String getLabel(int index) {
97        if (index < 0 || index >= DAY_COUNT) return "";
98        return mLabels[index];
99    }
100
101
102    /**
103     * @param index date bin index as returned by getIndex()
104     * @return date boundary at given index
105     */
106    public long getBoundary(int index) {
107        int lastDay = DAY_COUNT - 1;
108        // Error case
109        if (index < 0 || index > lastDay) index = 0;
110        // Since this provides a lower boundary on dates that will be included
111        // in the given bin, provide the smallest value
112        if (index == lastDay) return Long.MIN_VALUE;
113        return mBins[index];
114    }
115
116    /**
117     * Calcuate 12:00am by zeroing out hour, minute, second, millisecond
118     */
119    private void beginningOfDay(Calendar c) {
120        c.set(Calendar.HOUR_OF_DAY, 0);
121        c.set(Calendar.MINUTE, 0);
122        c.set(Calendar.SECOND, 0);
123        c.set(Calendar.MILLISECOND, 0);
124    }
125}
126