1 /*
2  * Copyright (C) 2012 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 
17 package android.bordeaux.services;
18 
19 import android.text.format.Time;
20 import android.util.Log;
21 
22 import java.util.Arrays;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 
27 // import java.util.Date;
28 
29 // TODO: use build in functions in
30 // import android.text.format.Time;
31 public class TimeStatsAggregator extends Aggregator {
32     final String TAG = "TimeStatsAggregator";
33 
34     public static final String TIME_OF_WEEK = "Time of Week";
35     public static final String DAY_OF_WEEK = "Day of Week";
36     public static final String TIME_OF_DAY = "Time of Day";
37     public static final String PERIOD_OF_DAY = "Period of Day";
38 
39     static final String WEEKEND = "Weekend";
40     static final String WEEKDAY = "Weekday";
41     static final String MONDAY = "Monday";
42     static final String TUESDAY = "Tuesday";
43     static final String WEDNESDAY = "Wednesday";
44     static final String THURSDAY = "Thursday";
45     static final String FRIDAY = "Friday";
46     static final String SATURDAY = "Saturday";
47     static final String SUNDAY = "Sunday";
48     static final String MORNING = "Morning";
49     static final String NOON = "Noon";
50     static final String AFTERNOON = "AfterNoon";
51     static final String EVENING = "Evening";
52     static final String NIGHT = "Night";
53     static final String LATENIGHT = "LateNight";
54     static final String DAYTIME = "Daytime";
55     static final String NIGHTTIME = "Nighttime";
56 
57     static String mFakeTimeOfDay = null;
58     static String mFakeDayOfWeek = null;
59 
60     static final String[] TIME_OF_DAY_VALUES =
61        {MORNING, NOON, AFTERNOON, EVENING, NIGHT, LATENIGHT};
62 
63     static final String[] DAY_OF_WEEK_VALUES =
64        {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};
65 
66     static final String[] DAYTIME_VALUES = {MORNING, NOON, AFTERNOON, EVENING};
67 
getListOfFeatures()68     public String[] getListOfFeatures(){
69         String [] list = new String[4];
70         list[0] = TIME_OF_WEEK;
71         list[1] = DAY_OF_WEEK;
72         list[2] = TIME_OF_DAY;
73         list[3] = PERIOD_OF_DAY;
74         return list;
75     }
76 
getFeatureValue(String featureName)77     public Map<String,String> getFeatureValue(String featureName) {
78         HashMap<String,String> feature = new HashMap<String,String>();
79 
80         HashMap<String, String> features =
81             getAllTimeFeatures(System.currentTimeMillis());
82         if (features.containsKey(featureName)) {
83             feature.put(featureName, features.get(featureName));
84         } else {
85             Log.e(TAG, "There is no Time feature called " + featureName);
86         }
87         return (Map)feature;
88     }
89 
getTimeOfDay(int hour)90     private static String getTimeOfDay(int hour) {
91         if (hour >= 5 && hour < 11) {
92             return MORNING;
93         } else if (hour >= 11 && hour < 14) {
94             return NOON;
95         } else if (hour >= 14 && hour < 18) {
96             return AFTERNOON;
97         } else if (hour >= 18 && hour < 21) {
98             return EVENING;
99         } else if ((hour >= 21 && hour < 24) ||
100                    (hour >= 0 && hour < 1))  {
101             return NIGHT;
102         } else {
103             return LATENIGHT;
104         }
105     }
106 
getDayOfWeek(int day)107     private static String getDayOfWeek(int day) {
108         switch (day) {
109             case Time.SATURDAY:
110                 return SATURDAY;
111             case Time.SUNDAY:
112                 return SUNDAY;
113             case Time.MONDAY:
114                 return MONDAY;
115             case Time.TUESDAY:
116                 return TUESDAY;
117             case Time.WEDNESDAY:
118                 return WEDNESDAY;
119             case Time.THURSDAY:
120                 return THURSDAY;
121             default:
122                 return FRIDAY;
123         }
124     }
125 
getPeriodOfDay(int hour)126     private static String getPeriodOfDay(int hour) {
127         if (hour > 6 && hour < 19) {
128             return DAYTIME;
129         } else {
130             return NIGHTTIME;
131         }
132     }
133 
getAllTimeFeatures(long utcTime)134     static HashMap<String, String> getAllTimeFeatures(long utcTime) {
135         HashMap<String, String> features = new HashMap<String, String>();
136         Time time = new Time();
137         time.set(utcTime);
138 
139         if (mFakeTimeOfDay != null && mFakeTimeOfDay.length() != 0) {
140             List<String> day_list = Arrays.asList(DAYTIME_VALUES);
141 
142             if (day_list.contains(mFakeTimeOfDay)) {
143                 features.put(PERIOD_OF_DAY, DAYTIME);
144             } else {
145                 features.put(PERIOD_OF_DAY, NIGHTTIME);
146             }
147             features.put(TIME_OF_DAY, mFakeTimeOfDay);
148         } else {
149             features.put(PERIOD_OF_DAY, getPeriodOfDay(time.hour));
150             features.put(TIME_OF_DAY, getTimeOfDay(time.hour));
151         }
152 
153         if (mFakeDayOfWeek != null && mFakeDayOfWeek.length() != 0) {
154             features.put(DAY_OF_WEEK, mFakeDayOfWeek);
155             if (mFakeDayOfWeek.equals(SUNDAY) ||
156                 mFakeDayOfWeek.equals(SATURDAY) ||
157                 mFakeDayOfWeek.equals(FRIDAY) &&
158                     features.get(PERIOD_OF_DAY).equals(NIGHTTIME)) {
159                 features.put(TIME_OF_WEEK, WEEKEND);
160             } else {
161                 features.put(TIME_OF_WEEK, WEEKDAY);
162             }
163         }
164         else {
165             features.put(DAY_OF_WEEK, getDayOfWeek(time.weekDay));
166             if (time.weekDay == Time.SUNDAY || time.weekDay == Time.SATURDAY ||
167                     (time.weekDay == Time.FRIDAY &&
168                     features.get(PERIOD_OF_DAY).equals(NIGHTTIME))) {
169                 features.put(TIME_OF_WEEK, WEEKEND);
170             } else {
171                 features.put(TIME_OF_WEEK, WEEKDAY);
172             }
173         }
174 
175         return features;
176     }
177 
178     // get all possible time_of_day values
getTimeOfDayValues()179     public static List<String> getTimeOfDayValues() {
180         return Arrays.asList(TIME_OF_DAY_VALUES);
181     }
182 
183     // get all possible day values
getDayOfWeekValues()184     public static List<String> getDayOfWeekValues() {
185         return Arrays.asList(DAY_OF_WEEK_VALUES);
186     }
187 
188     // set the fake time of day
189     // set to "" to disable the fake time
setFakeTimeOfDay(String time_of_day)190     public static void setFakeTimeOfDay(String time_of_day) {
191         mFakeTimeOfDay = time_of_day;
192     }
193 
194     // set the fake day of week
195     // set to "" to disable the fake day
setFakeDayOfWeek(String day_of_week)196     public static void setFakeDayOfWeek(String day_of_week) {
197         mFakeDayOfWeek = day_of_week;
198     }
199 }
200