blob: 82c13aeea9a479c6ec1d5db948d2e09ef929e28e [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;
Elliott Hughes47a05392012-08-15 14:53:12 -070024import java.util.Locale;
25
26import libcore.icu.LocaleData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
28/**
29 * Sorts dates into the following groups:
30 * Today
31 * Yesterday
Leon Scroggins3608d4a2010-02-19 11:18:53 -050032 * seven days ago
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 * one month ago
34 * older than a month ago
35 */
36
37public 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 Scrogginsc89b13b2010-01-07 18:47:06 -050044 private long [] mBins = new long[DAY_COUNT-1];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 private String [] mLabels = new String[DAY_COUNT];
46
Leon Scroggins3608d4a2010-02-19 11:18:53 -050047 private static final int NUM_DAYS_AGO = 7;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 /**
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 Scrogginsc89b13b2010-01-07 18:47:06 -050060 c.add(Calendar.DAY_OF_YEAR, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 mBins[1] = c.getTimeInMillis(); // Yesterday
Leon Scrogginsc89b13b2010-01-07 18:47:06 -050062 c.add(Calendar.DAY_OF_YEAR, -(NUM_DAYS_AGO - 1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 mBins[2] = c.getTimeInMillis(); // Five days ago
Leon Scrogginsc89b13b2010-01-07 18:47:06 -050064 c.add(Calendar.DAY_OF_YEAR, NUM_DAYS_AGO); // move back to today
65 c.add(Calendar.MONTH, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 mBins[3] = c.getTimeInMillis(); // One month ago
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
68 // build labels
Elliott Hughes47a05392012-08-15 14:53:12 -070069 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 Project9066cfe2009-03-03 19:31:44 -080076
Leon Scroggins4379dca2010-02-19 15:17:21 -050077 int resId = com.android.internal.R.plurals.last_num_days;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 String format = resources.getQuantityString(resId, NUM_DAYS_AGO);
79 mLabels[2] = String.format(format, NUM_DAYS_AGO);
80
Leon Scroggins4379dca2010-02-19 15:17:21 -050081 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 Project9066cfe2009-03-03 19:31:44 -080083 }
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 Scrogginsc89b13b2010-01-07 18:47:06 -050092 int lastDay = DAY_COUNT - 1;
93 for (int i = 0; i < lastDay; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 if (time > mBins[i]) return i;
95 }
Leon Scrogginsc89b13b2010-01-07 18:47:06 -050096 return lastDay;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 }
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 Scrogginsc89b13b2010-01-07 18:47:06 -0500104 if (index < 0 || index >= DAY_COUNT) return "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 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 Scrogginsc89b13b2010-01-07 18:47:06 -0500114 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 Project9066cfe2009-03-03 19:31:44 -0800120 return mBins[index];
121 }
122
123 /**
124 * Calcuate 12:00am by zeroing out hour, minute, second, millisecond
125 */
Leon Scrogginsc89b13b2010-01-07 18:47:06 -0500126 private void beginningOfDay(Calendar c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 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 Project9066cfe2009-03-03 19:31:44 -0800131 }
132}