-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlarm.java
231 lines (194 loc) · 5.07 KB
/
Alarm.java
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**************************************************************************
*
* Copyright (C) 2012-2015 Alex Taradov <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************/
package com.application.brainfit;
import android.content.Context;
import android.content.Intent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Calendar;
public class Alarm implements Comparable<Alarm>
{
private Context mContext;
private long mId;
private String mTitle;
private long mDate;
private boolean mEnabled;
private int mOccurence;
private int mDays;
private long mNextOccurence;
public static final int ONCE = 0;
public static final int WEEKLY = 1;
public static final int NEVER = 0;
public static final int EVERY_DAY = 0x7f;
public Alarm(Context context)
{
mContext = context;
mId = 0;
mTitle = "";
mDate = System.currentTimeMillis();
mEnabled = true;
mOccurence = ONCE;
mDays = EVERY_DAY;
update();
}
public long getId()
{
return mId;
}
public void setId(long id)
{
mId = id;
}
public String getTitle()
{
return mTitle;
}
public void setTitle(String title)
{
mTitle = title;
}
public int getOccurence()
{
return mOccurence;
}
public void setOccurence(int occurence)
{
mOccurence = occurence;
update();
}
public long getDate()
{
return mDate;
}
public void setDate(long date)
{
mDate = date;
update();
}
public boolean getEnabled()
{
return mEnabled;
}
public void setEnabled(boolean enabled)
{
mEnabled = enabled;
}
public int getDays()
{
return mDays;
}
public void setDays(int days)
{
mDays = days;
update();
}
public long getNextOccurence()
{
return mNextOccurence;
}
public boolean getOutdated()
{
return mNextOccurence < System.currentTimeMillis();
}
public int compareTo(Alarm aThat)
{
final long thisNext = getNextOccurence();
final long thatNext = aThat.getNextOccurence();
final int BEFORE = -1;
final int EQUAL = 0;
final int AFTER = 1;
if (this == aThat)
return EQUAL;
if (thisNext > thatNext)
return AFTER;
else if (thisNext < thatNext)
return BEFORE;
else
return EQUAL;
}
public void update()
{
Calendar now = Calendar.getInstance();
if (mOccurence == WEEKLY)
{
Calendar alarm = Calendar.getInstance();
alarm.setTimeInMillis(mDate);
alarm.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
if (mDays != NEVER)
{
while (true)
{
int day = (alarm.get(Calendar.DAY_OF_WEEK) + 5) % 7;
if (alarm.getTimeInMillis() > now.getTimeInMillis() && (mDays & (1 << day)) > 0)
break;
alarm.add(Calendar.DAY_OF_MONTH, 1);
}
}
else
{
alarm.add(Calendar.YEAR, 10);
}
mNextOccurence = alarm.getTimeInMillis();
}
else
{
mNextOccurence = mDate;
}
mDate = mNextOccurence;
}
public void toIntent(Intent intent)
{
intent.putExtra("com.taradov.alarmme.id", mId);
intent.putExtra("com.taradov.alarmme.title", mTitle);
intent.putExtra("com.taradov.alarmme.date", mDate);
intent.putExtra("com.taradov.alarmme.alarm", mEnabled);
intent.putExtra("com.taradov.alarmme.occurence", mOccurence);
intent.putExtra("com.taradov.alarmme.days", mDays);
}
public void fromIntent(Intent intent)
{
mId = intent.getLongExtra("com.taradov.alarmme.id", 0);
mTitle = intent.getStringExtra("com.taradov.alarmme.title");
mDate = intent.getLongExtra("com.taradov.alarmme.date", 0);
mEnabled = intent.getBooleanExtra("com.taradov.alarmme.alarm", true);
mOccurence = intent.getIntExtra("com.taradov.alarmme.occurence", 0);
mDays = intent.getIntExtra("com.taradov.alarmme.days", 0);
update();
}
public void serialize(DataOutputStream dos) throws IOException
{
dos.writeLong(mId);
dos.writeUTF(mTitle);
dos.writeLong(mDate);
dos.writeBoolean(mEnabled);
dos.writeInt(mOccurence);
dos.writeInt(mDays);
}
public void deserialize(DataInputStream dis) throws IOException
{
mId = dis.readLong();
mTitle = dis.readUTF();
mDate = dis.readLong();
mEnabled = dis.readBoolean();
mOccurence = dis.readInt();
mDays = dis.readInt();
update();
}
}