[237] | 1 | package net.sabi.pester;
|
---|
| 2 |
|
---|
| 3 | import java.io.ByteArrayInputStream;
|
---|
| 4 | import java.io.ByteArrayOutputStream;
|
---|
| 5 | import java.io.DataInputStream;
|
---|
| 6 | import java.io.DataOutputStream;
|
---|
| 7 | import danger.internal.Date;
|
---|
| 8 | import danger.util.StdActiveObject;
|
---|
[240] | 9 | import danger.util.DEBUG;
|
---|
[237] | 10 |
|
---|
| 11 | public class Alarm extends StdActiveObject {
|
---|
| 12 | private static final int VERSION_1 = 1;
|
---|
| 13 |
|
---|
| 14 | // persisted
|
---|
| 15 | private String mMessage;
|
---|
[239] | 16 | private int mType;
|
---|
[240] | 17 | private int mPeriod; // XXX rename to 'interval'
|
---|
[237] | 18 | private Date mDate;
|
---|
| 19 |
|
---|
| 20 | // transient
|
---|
[241] | 21 | private int mState;
|
---|
[237] | 22 |
|
---|
| 23 | public Alarm() {
|
---|
[241] | 24 | mState = STATE_INVALID;
|
---|
[237] | 25 | }
|
---|
| 26 |
|
---|
| 27 | public String getMessage() {
|
---|
| 28 | return mMessage;
|
---|
| 29 | }
|
---|
[240] | 30 | public int getPeriod() {
|
---|
[237] | 31 | return mPeriod;
|
---|
| 32 | }
|
---|
| 33 | public boolean getUsesPeriod() {
|
---|
[239] | 34 | return mType != TYPE_DATE;
|
---|
[237] | 35 | }
|
---|
[240] | 36 | public boolean getRepeating() {
|
---|
| 37 | return mType == TYPE_PERIODIC_REPEATING;
|
---|
| 38 | }
|
---|
[237] | 39 | public Date getDate() {
|
---|
| 40 | return mDate;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public void setMessage(String message) {
|
---|
| 44 | mMessage = message;
|
---|
| 45 | }
|
---|
[240] | 46 | public void setPeriod(int period, boolean repeating) {
|
---|
[239] | 47 | mType = repeating ? TYPE_PERIODIC_REPEATING : TYPE_PERIODIC;
|
---|
[237] | 48 | mPeriod = period;
|
---|
[240] | 49 | mDate = new Date();
|
---|
| 50 | mDate.addSeconds(period);
|
---|
[237] | 51 | }
|
---|
| 52 | public void setDate(Date date) {
|
---|
[239] | 53 | mType = TYPE_DATE;
|
---|
[237] | 54 | mDate = date;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public byte[] toByteArray() {
|
---|
| 58 | try {
|
---|
| 59 | ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
|
---|
| 60 | DataOutputStream dataStream = new DataOutputStream(byteStream);
|
---|
| 61 |
|
---|
| 62 | dataStream.writeByte(VERSION_1);
|
---|
| 63 | dataStream.writeUTF(mMessage);
|
---|
[240] | 64 | dataStream.writeInt(mPeriod);
|
---|
[237] | 65 | dataStream.writeInt(mDate.getUnixTimeGMT());
|
---|
[239] | 66 | dataStream.writeInt(mType);
|
---|
[237] | 67 | dataStream.flush();
|
---|
| 68 | return byteStream.toByteArray();
|
---|
| 69 | } catch (Exception e) {
|
---|
| 70 | // XXX do something
|
---|
| 71 | }
|
---|
| 72 | return null;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public void fromByteArray(byte[] data) {
|
---|
| 76 | try {
|
---|
| 77 | ByteArrayInputStream byteStream = new ByteArrayInputStream(data);
|
---|
| 78 | DataInputStream dataStream = new DataInputStream(byteStream);
|
---|
| 79 |
|
---|
| 80 | byte version = dataStream.readByte();
|
---|
| 81 | if (version != VERSION_1) {
|
---|
| 82 | // XXX barf
|
---|
| 83 | }
|
---|
| 84 | mMessage = dataStream.readUTF();
|
---|
[240] | 85 | mPeriod = dataStream.readInt();
|
---|
[239] | 86 | mType = dataStream.readInt();
|
---|
[237] | 87 | } catch (Exception e) {
|
---|
| 88 | // XXX do something
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | void beginEditing() {
|
---|
[241] | 93 | mState = STATE_EDITING;
|
---|
[237] | 94 | }
|
---|
| 95 |
|
---|
| 96 | void endEditing() {
|
---|
[241] | 97 | mState = STATE_SCHEDULED;
|
---|
[240] | 98 | DEBUG.p("schedule alarm: " + mMessage);
|
---|
| 99 | switch (mType) {
|
---|
| 100 | case TYPE_PERIODIC:
|
---|
| 101 | DEBUG.p(" - periodic: " + mPeriod); break;
|
---|
| 102 | case TYPE_PERIODIC_REPEATING:
|
---|
| 103 | DEBUG.p(" - periodic repeating: " + mPeriod); break;
|
---|
| 104 | case TYPE_DATE:
|
---|
| 105 | DEBUG.p(" - date: " + mDate); break;
|
---|
| 106 | }
|
---|
[237] | 107 | }
|
---|
| 108 |
|
---|
| 109 | public String toString() {
|
---|
| 110 | return mMessage;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[239] | 113 | public static final int TYPE_PERIODIC = 0;
|
---|
| 114 | public static final int TYPE_PERIODIC_REPEATING = 1;
|
---|
| 115 | public static final int TYPE_DATE = 2;
|
---|
| 116 |
|
---|
[241] | 117 | public static final int STATE_INVALID = 0;
|
---|
| 118 | public static final int STATE_EDITING = 1;
|
---|
| 119 | public static final int STATE_SCHEDULED = 2;
|
---|
| 120 | public static final int STATE_EXPIRED = 3;
|
---|
[237] | 121 | } |
---|