Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / things-manager / sdk / java / src / org / iotivity / service / tm / Time.java
1 package org.iotivity.service.tm;
2
3 import android.util.Log;
4
5 /**
6  * This class provides time-related information used for
7  * scheduled/recursive group action features. Along with time-related
8  * variables, it also provides various useful functionality including
9  * translating time to second unit
10  */
11 public class Time {
12
13     public enum ActionSetType {
14         NONE, SCHEDULED, RECURSIVE
15     }
16
17     public int            mYear      = 0;
18     public int            mMonth     = 0;
19     public int            mDay       = 0;
20     public int            mHour      = 0;
21     public int            mMin       = 0;
22     public int            mSec       = 0;
23     public int            mDayOfWeek = 0;
24
25     private ActionSetType mType      = ActionSetType.NONE;
26     private long          mDelay     = 0;
27
28     private final String  LOG_TAG    = this.getClass().getSimpleName();
29
30     /**
31      * Set the time for executing ActionSet.
32      *
33      * @param year
34      *            Year to be set
35      * @param month
36      *            Month of the year to be set
37      * @param day
38      *            Day of the month to be set
39      * @param hour
40      *            Hour to be set
41      * @param min
42      *            Minute to be set
43      * @param sec
44      *            Second to be set
45      * @param dayOfTheWeek
46      *            Day of the week to be set
47      *
48      */
49     public void setTime(int year, int month, int day, int hour, int min,
50             int sec, int dayOfTheWeek) {
51         if (year < 0 || month < 0 || day < 0 || hour < 0 || min < 0 || sec < 0
52                 || dayOfTheWeek < 0) {
53             Log.e(LOG_TAG, "Input time is invalid");
54             return;
55         }
56
57         year -= 1900;
58         month -= 1;
59
60         mDelay = 0;
61         mYear = year;
62         mMonth = month;
63         mDay = day;
64         mHour = hour;
65         mMin = month;
66         mSec = sec;
67         mDayOfWeek = dayOfTheWeek;
68         mType = ActionSetType.NONE;
69     }
70
71     /**
72      * Set type of ActionSet.
73      *
74      * @param type
75      *            Type of ActionSet
76      */
77     public void setType(ActionSetType type) {
78         mType = type;
79     }
80
81     /**
82      * Set day of the week for recursively executing ActionSet.
83      *
84      * @param day
85      *            day of the week
86      */
87     public void setDayOfWeekForRecursive(int day) {
88         if (day != -1) {
89             mType = ActionSetType.RECURSIVE;
90             setTime(0, 0, 0, 0, 0, 0, day);
91         }
92     }
93
94     /**
95      * Set the time delay in seconds for executing ActionSet.
96      *
97      * @param seconds
98      *            time delay in seconds
99      *
100      */
101     public void setDelay(long seconds) {
102         if (mType != ActionSetType.NONE) {
103             mDelay = seconds;
104         }
105     }
106
107     /**
108      * Get absolute time in seconds.
109      *
110      * @return long - Absolute time in seconds.
111      *
112      */
113     public long getSecAbsTime() {
114         long interval;
115
116         interval = (mHour * 60 * 60);
117         interval += (mMin * 60);
118         interval += (mSec * 1);
119
120         return interval;
121     }
122
123     /**
124      * Get the type of ActionSet.
125      *
126      * @return ActionSetType - Type of ActionSet.
127      *
128      */
129     public ActionSetType getType() {
130         return mType;
131     }
132
133     /**
134      * Get the time delay in seconds set in the ActionSet.
135      *
136      * @return long - Delay in seconds.
137      *
138      */
139     public long getDelay() {
140         return mDelay;
141     }
142 }