summaryrefslogtreecommitdiff
path: root/bordeaux/service/src/android/bordeaux/services/TimeStatsAggregator.java
blob: 2b94cd44b3b08af5877ea3405dff60a46f972c86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.bordeaux.services;

import android.text.format.Time;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;

// import java.util.Date;

// TODO: use build in functions in
// import android.text.format.Time;
public class TimeStatsAggregator extends Aggregator {
    final String TAG = "TimeStatsAggregator";

    public static final String TIME_OF_WEEK = "Time of Week";
    public static final String DAY_OF_WEEK = "Day of Week";
    public static final String TIME_OF_DAY = "Time of Day";
    public static final String PERIOD_OF_DAY = "Period of Day";

    static final String WEEKEND = "Weekend";
    static final String WEEKDAY = "Weekday";
    static final String MONDAY = "Monday";
    static final String TUESDAY = "Tuesday";
    static final String WEDNESDAY = "Wednesday";
    static final String THURSDAY = "Thursday";
    static final String FRIDAY = "Friday";
    static final String SATURDAY = "Saturday";
    static final String SUNDAY = "Sunday";
    static final String MORNING = "Morning";
    static final String NOON = "Noon";
    static final String AFTERNOON = "AfterNoon";
    static final String EVENING = "Evening";
    static final String NIGHT = "Night";
    static final String LATENIGHT = "LateNight";
    static final String DAYTIME = "Daytime";
    static final String NIGHTTIME = "Nighttime";

    public String[] getListOfFeatures(){
        String [] list = new String[4];
        list[0] = TIME_OF_WEEK;
        list[1] = DAY_OF_WEEK;
        list[2] = TIME_OF_DAY;
        list[3] = PERIOD_OF_DAY;
        return list;
    }

    public Map<String,String> getFeatureValue(String featureName) {
        HashMap<String,String> feature = new HashMap<String,String>();

        HashMap<String, String> features =
            getAllTimeFeatures(System.currentTimeMillis());
        if (features.containsKey(featureName)) {
            feature.put(featureName, features.get(featureName));
        } else {
            Log.e(TAG, "There is no Time feature called " + featureName);
        }
        return (Map)feature;
    }

    private static String getTimeOfDay(int hour) {
        if (hour >= 5 && hour < 11) {
            return MORNING;
        } else if (hour >= 11 && hour < 14) {
            return NOON;
        } else if (hour >= 14 && hour < 18) {
            return AFTERNOON;
        } else if (hour >= 18 && hour < 21) {
            return EVENING;
        } else if ((hour >= 21 && hour < 24) ||
                   (hour >= 0 && hour < 1))  {
            return NIGHT;
        } else {
            return LATENIGHT;
        }
    }

    private static String getDayOfWeek(int day) {
        switch (day) {
            case Time.SATURDAY:
                return SATURDAY;
            case Time.SUNDAY:
                return SUNDAY;
            case Time.MONDAY:
                return MONDAY;
            case Time.TUESDAY:
                return TUESDAY;
            case Time.WEDNESDAY:
                return WEDNESDAY;
            case Time.THURSDAY:
                return THURSDAY;
            default:
                return FRIDAY;
        }
    }

    private static String getPeriodOfDay(int hour) {
        if (hour > 6 && hour < 19) {
            return DAYTIME;
        } else {
            return NIGHTTIME;
        }
    }

    static HashMap<String, String> getAllTimeFeatures(long utcTime) {
        HashMap<String, String> features = new HashMap<String, String>();
        Time time = new Time();
        time.set(utcTime);

        features.put(DAY_OF_WEEK, getDayOfWeek(time.weekDay));
        features.put(PERIOD_OF_DAY, getPeriodOfDay(time.hour));
        features.put(TIME_OF_DAY, getTimeOfDay(time.hour));

        if (time.weekDay == Time.SUNDAY || time.weekDay == Time.SATURDAY ||
                (time.weekDay == Time.FRIDAY &&
                features.get(PERIOD_OF_DAY).equals(NIGHTTIME))) {
            features.put(TIME_OF_WEEK, WEEKEND);
        } else {
            features.put(TIME_OF_WEEK, WEEKDAY);
        }
        return features;
    }
}