blob: 0e8ad7e07511017b7af8bc856cc36214a0dd5d7a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Leon Scroggins3608d4a2010-02-19 11:18:53 -050029 * seven days ago
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030 * 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
Leon Scrogginsc89b13b2010-01-07 18:47:06 -050041 private long [] mBins = new long[DAY_COUNT-1];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 private String [] mLabels = new String[DAY_COUNT];
43
Leon Scroggins3608d4a2010-02-19 11:18:53 -050044 private static final int NUM_DAYS_AGO = 7;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 /**
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 Scrogginsc89b13b2010-01-07 18:47:06 -050057 c.add(Calendar.DAY_OF_YEAR, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 mBins[1] = c.getTimeInMillis(); // Yesterday
Leon Scrogginsc89b13b2010-01-07 18:47:06 -050059 c.add(Calendar.DAY_OF_YEAR, -(NUM_DAYS_AGO - 1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 mBins[2] = c.getTimeInMillis(); // Five days ago
Leon Scrogginsc89b13b2010-01-07 18:47:06 -050061 c.add(Calendar.DAY_OF_YEAR, NUM_DAYS_AGO); // move back to today
62 c.add(Calendar.MONTH, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 mBins[3] = c.getTimeInMillis(); // One month ago
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
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 Scroggins4379dca2010-02-19 15:17:21 -050069 int resId = com.android.internal.R.plurals.last_num_days;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 String format = resources.getQuantityString(resId, NUM_DAYS_AGO);
71 mLabels[2] = String.format(format, NUM_DAYS_AGO);
72
Leon Scroggins4379dca2010-02-19 15:17:21 -050073 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 Project9066cfe2009-03-03 19:31:44 -080075 }
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 Scrogginsc89b13b2010-01-07 18:47:06 -050084 int lastDay = DAY_COUNT - 1;
85 for (int i = 0; i < lastDay; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 if (time > mBins[i]) return i;
87 }
Leon Scrogginsc89b13b2010-01-07 18:47:06 -050088 return lastDay;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 }
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 Scrogginsc89b13b2010-01-07 18:47:06 -050096 if (index < 0 || index >= DAY_COUNT) return "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 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 Scrogginsc89b13b2010-01-07 18:47:06 -0500106 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 Project9066cfe2009-03-03 19:31:44 -0800112 return mBins[index];
113 }
114
115 /**
116 * Calcuate 12:00am by zeroing out hour, minute, second, millisecond
117 */
Leon Scrogginsc89b13b2010-01-07 18:47:06 -0500118 private void beginningOfDay(Calendar c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 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 Project9066cfe2009-03-03 19:31:44 -0800123 }
124}