blob: fede244f2720462d3a6ae18149024e54adfd9085 [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;
Elliott Hughes47a05392012-08-15 14:53:12 -070023import java.util.Locale;
24
25import libcore.icu.LocaleData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
27/**
28 * Sorts dates into the following groups:
29 * Today
30 * Yesterday
Leon Scroggins3608d4a2010-02-19 11:18:53 -050031 * seven days ago
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032 * one month ago
33 * older than a month ago
34 */
35
36public class DateSorter {
37
38 private static final String LOGTAG = "webkit";
39
40 /** must be >= 3 */
41 public static final int DAY_COUNT = 5;
42
Leon Scrogginsc89b13b2010-01-07 18:47:06 -050043 private long [] mBins = new long[DAY_COUNT-1];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 private String [] mLabels = new String[DAY_COUNT];
45
Leon Scroggins3608d4a2010-02-19 11:18:53 -050046 private static final int NUM_DAYS_AGO = 7;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 /**
49 * @param context Application context
50 */
51 public DateSorter(Context context) {
52 Resources resources = context.getResources();
53
54 Calendar c = Calendar.getInstance();
55 beginningOfDay(c);
56
57 // Create the bins
58 mBins[0] = c.getTimeInMillis(); // Today
Leon Scrogginsc89b13b2010-01-07 18:47:06 -050059 c.add(Calendar.DAY_OF_YEAR, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 mBins[1] = c.getTimeInMillis(); // Yesterday
Leon Scrogginsc89b13b2010-01-07 18:47:06 -050061 c.add(Calendar.DAY_OF_YEAR, -(NUM_DAYS_AGO - 1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 mBins[2] = c.getTimeInMillis(); // Five days ago
Leon Scrogginsc89b13b2010-01-07 18:47:06 -050063 c.add(Calendar.DAY_OF_YEAR, NUM_DAYS_AGO); // move back to today
64 c.add(Calendar.MONTH, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 mBins[3] = c.getTimeInMillis(); // One month ago
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
67 // build labels
Elliott Hughes47a05392012-08-15 14:53:12 -070068 Locale locale = resources.getConfiguration().locale;
69 if (locale == null) {
70 locale = Locale.getDefault();
71 }
72 LocaleData localeData = LocaleData.get(locale);
73 mLabels[0] = localeData.today;
74 mLabels[1] = localeData.yesterday;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075
Leon Scroggins4379dca2010-02-19 15:17:21 -050076 int resId = com.android.internal.R.plurals.last_num_days;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 String format = resources.getQuantityString(resId, NUM_DAYS_AGO);
78 mLabels[2] = String.format(format, NUM_DAYS_AGO);
79
Leon Scroggins4379dca2010-02-19 15:17:21 -050080 mLabels[3] = context.getString(com.android.internal.R.string.last_month);
81 mLabels[4] = context.getString(com.android.internal.R.string.older);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 }
83
84 /**
85 * @param time time since the Epoch in milliseconds, such as that
86 * returned by Calendar.getTimeInMillis()
87 * @return an index from 0 to (DAY_COUNT - 1) that identifies which
88 * date bin this date belongs to
89 */
90 public int getIndex(long time) {
Leon Scrogginsc89b13b2010-01-07 18:47:06 -050091 int lastDay = DAY_COUNT - 1;
92 for (int i = 0; i < lastDay; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 if (time > mBins[i]) return i;
94 }
Leon Scrogginsc89b13b2010-01-07 18:47:06 -050095 return lastDay;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 }
97
98 /**
99 * @param index date bin index as returned by getIndex()
100 * @return string label suitable for display to user
101 */
102 public String getLabel(int index) {
Leon Scrogginsc89b13b2010-01-07 18:47:06 -0500103 if (index < 0 || index >= DAY_COUNT) return "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 return mLabels[index];
105 }
106
107
108 /**
109 * @param index date bin index as returned by getIndex()
110 * @return date boundary at given index
111 */
112 public long getBoundary(int index) {
Leon Scrogginsc89b13b2010-01-07 18:47:06 -0500113 int lastDay = DAY_COUNT - 1;
114 // Error case
115 if (index < 0 || index > lastDay) index = 0;
116 // Since this provides a lower boundary on dates that will be included
117 // in the given bin, provide the smallest value
118 if (index == lastDay) return Long.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 return mBins[index];
120 }
121
122 /**
123 * Calcuate 12:00am by zeroing out hour, minute, second, millisecond
124 */
Leon Scrogginsc89b13b2010-01-07 18:47:06 -0500125 private void beginningOfDay(Calendar c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 c.set(Calendar.HOUR_OF_DAY, 0);
127 c.set(Calendar.MINUTE, 0);
128 c.set(Calendar.SECOND, 0);
129 c.set(Calendar.MILLISECOND, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 }
131}