source: trunk/hiptop/pester/net/sabi/pester/Alarm.java@ 239

Last change on this file since 239 was 239, checked in by Nicholas Riley, 18 years ago

Final initial control set for edit dialog.

File size: 2.5 KB
RevLine 
[237]1package net.sabi.pester;
2
3import java.io.ByteArrayInputStream;
4import java.io.ByteArrayOutputStream;
5import java.io.DataInputStream;
6import java.io.DataOutputStream;
7import danger.internal.Date;
8import danger.util.StdActiveObject;
9
10public class Alarm extends StdActiveObject {
11 private static final int VERSION_1 = 1;
12
13 // persisted
14 private String mMessage;
[239]15 private int mType;
[237]16 private long mPeriod;
17 private Date mDate;
18
19 // transient
20 private int mStatus;
21
22 public Alarm() {
23 mStatus = STATUS_INVALID;
24 }
25
26 public String getMessage() {
27 return mMessage;
28 }
29 public long getPeriod() {
30 return mPeriod;
31 }
32 public boolean getUsesPeriod() {
[239]33 return mType != TYPE_DATE;
[237]34 }
35 public Date getDate() {
36 return mDate;
37 }
38
39 public void setMessage(String message) {
40 mMessage = message;
41 }
[239]42 public void setPeriod(long period, boolean repeating) {
43 mType = repeating ? TYPE_PERIODIC_REPEATING : TYPE_PERIODIC;
[237]44 mPeriod = period;
45 }
46 public void setDate(Date date) {
[239]47 mType = TYPE_DATE;
[237]48 mDate = date;
49 }
50
51 public byte[] toByteArray() {
52 try {
53 ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
54 DataOutputStream dataStream = new DataOutputStream(byteStream);
55
56 dataStream.writeByte(VERSION_1);
57 dataStream.writeUTF(mMessage);
58 dataStream.writeLong(mPeriod);
59 dataStream.writeInt(mDate.getUnixTimeGMT());
[239]60 dataStream.writeInt(mType);
[237]61 dataStream.flush();
62 return byteStream.toByteArray();
63 } catch (Exception e) {
64 // XXX do something
65 }
66 return null;
67 }
68
69 public void fromByteArray(byte[] data) {
70 try {
71 ByteArrayInputStream byteStream = new ByteArrayInputStream(data);
72 DataInputStream dataStream = new DataInputStream(byteStream);
73
74 byte version = dataStream.readByte();
75 if (version != VERSION_1) {
76 // XXX barf
77 }
78 mMessage = dataStream.readUTF();
79 mPeriod = dataStream.readLong();
[239]80 mType = dataStream.readInt();
[237]81 } catch (Exception e) {
82 // XXX do something
83 }
84 }
85
86 void beginEditing() {
87 mStatus = STATUS_EDITING;
88 }
89
90 void endEditing() {
91 mStatus = STATUS_SCHEDULED;
92 }
93
94 public String toString() {
95 return mMessage;
96 }
97
[239]98 public static final int TYPE_PERIODIC = 0;
99 public static final int TYPE_PERIODIC_REPEATING = 1;
100 public static final int TYPE_DATE = 2;
101
[237]102 public static final int STATUS_INVALID = 0;
103 public static final int STATUS_EDITING = 1;
104 public static final int STATUS_SCHEDULED = 2;
105 public static final int STATUS_EXPIRED = 3;
106}
Note: See TracBrowser for help on using the repository browser.