OSDN Git Service

Fix the build.
[android-x86/packages-apps-DeskClock.git] / src / com / android / alarmclock / Alarm.java
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.alarmclock;
18
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.media.RingtoneManager;
22 import android.net.Uri;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 import android.provider.BaseColumns;
26 import android.text.format.DateFormat;
27
28 import java.text.DateFormatSymbols;
29 import java.util.Calendar;
30
31 public final class Alarm implements Parcelable {
32
33     //////////////////////////////
34     // Parcelable apis
35     //////////////////////////////
36     public static final Parcelable.Creator<Alarm> CREATOR
37             = new Parcelable.Creator<Alarm>() {
38                 public Alarm createFromParcel(Parcel p) {
39                     return new Alarm(p);
40                 }
41
42                 public Alarm[] newArray(int size) {
43                     return new Alarm[size];
44                 }
45             };
46
47     public int describeContents() {
48         return 0;
49     }
50
51     public void writeToParcel(Parcel p, int flags) {
52         p.writeInt(id);
53         p.writeInt(enabled ? 1 : 0);
54         p.writeInt(hour);
55         p.writeInt(minutes);
56         p.writeInt(daysOfWeek.getCoded());
57         p.writeLong(time);
58         p.writeInt(vibrate ? 1 : 0);
59         p.writeString(label);
60         p.writeParcelable(alert, flags);
61         p.writeInt(silent ? 1 : 0);
62     }
63     //////////////////////////////
64     // end Parcelable apis
65     //////////////////////////////
66
67     //////////////////////////////
68     // Column definitions
69     //////////////////////////////
70     public static class Columns implements BaseColumns {
71         /**
72          * The content:// style URL for this table
73          */
74         public static final Uri CONTENT_URI =
75                 Uri.parse("content://com.android.alarmclock/alarm");
76
77         /**
78          * Hour in 24-hour localtime 0 - 23.
79          * <P>Type: INTEGER</P>
80          */
81         public static final String HOUR = "hour";
82
83         /**
84          * Minutes in localtime 0 - 59
85          * <P>Type: INTEGER</P>
86          */
87         public static final String MINUTES = "minutes";
88
89         /**
90          * Days of week coded as integer
91          * <P>Type: INTEGER</P>
92          */
93         public static final String DAYS_OF_WEEK = "daysofweek";
94
95         /**
96          * Alarm time in UTC milliseconds from the epoch.
97          * <P>Type: INTEGER</P>
98          */
99         public static final String ALARM_TIME = "alarmtime";
100
101         /**
102          * True if alarm is active
103          * <P>Type: BOOLEAN</P>
104          */
105         public static final String ENABLED = "enabled";
106
107         /**
108          * True if alarm should vibrate
109          * <P>Type: BOOLEAN</P>
110          */
111         public static final String VIBRATE = "vibrate";
112
113         /**
114          * Message to show when alarm triggers
115          * Note: not currently used
116          * <P>Type: STRING</P>
117          */
118         public static final String MESSAGE = "message";
119
120         /**
121          * Audio alert to play when alarm triggers
122          * <P>Type: STRING</P>
123          */
124         public static final String ALERT = "alert";
125
126         /**
127          * The default sort order for this table
128          */
129         public static final String DEFAULT_SORT_ORDER = _ID + " ASC";
130
131         // Used when filtering enabled alarms.
132         public static final String WHERE_ENABLED = ENABLED + "=1";
133
134         static final String[] ALARM_QUERY_COLUMNS = {
135             _ID, HOUR, MINUTES, DAYS_OF_WEEK, ALARM_TIME,
136             ENABLED, VIBRATE, MESSAGE, ALERT };
137
138         /**
139          * These save calls to cursor.getColumnIndexOrThrow()
140          * THEY MUST BE KEPT IN SYNC WITH ABOVE QUERY COLUMNS
141          */
142         public static final int ALARM_ID_INDEX = 0;
143         public static final int ALARM_HOUR_INDEX = 1;
144         public static final int ALARM_MINUTES_INDEX = 2;
145         public static final int ALARM_DAYS_OF_WEEK_INDEX = 3;
146         public static final int ALARM_TIME_INDEX = 4;
147         public static final int ALARM_ENABLED_INDEX = 5;
148         public static final int ALARM_VIBRATE_INDEX = 6;
149         public static final int ALARM_MESSAGE_INDEX = 7;
150         public static final int ALARM_ALERT_INDEX = 8;
151     }
152     //////////////////////////////
153     // End column definitions
154     //////////////////////////////
155
156     // Public fields
157     public int        id;
158     public boolean    enabled;
159     public int        hour;
160     public int        minutes;
161     public DaysOfWeek daysOfWeek;
162     public long       time;
163     public boolean    vibrate;
164     public String     label;
165     public Uri        alert;
166     public boolean    silent;
167
168     public Alarm(Cursor c) {
169         id = c.getInt(Columns.ALARM_ID_INDEX);
170         enabled = c.getInt(Columns.ALARM_ENABLED_INDEX) == 1;
171         hour = c.getInt(Columns.ALARM_HOUR_INDEX);
172         minutes = c.getInt(Columns.ALARM_MINUTES_INDEX);
173         daysOfWeek = new DaysOfWeek(c.getInt(Columns.ALARM_DAYS_OF_WEEK_INDEX));
174         time = c.getLong(Columns.ALARM_TIME_INDEX);
175         vibrate = c.getInt(Columns.ALARM_VIBRATE_INDEX) == 1;
176         label = c.getString(Columns.ALARM_MESSAGE_INDEX);
177         String alertString = c.getString(Columns.ALARM_ALERT_INDEX);
178         if (Alarms.ALARM_ALERT_SILENT.equals(alertString)) {
179             if (Log.LOGV) {
180                 Log.v("Alarm is marked as silent");
181             }
182             silent = true;
183         } else {
184             if (alertString != null && alertString.length() != 0) {
185                 alert = Uri.parse(alertString);
186             }
187
188             // If the database alert is null or it failed to parse, use the
189             // default alert.
190             if (alert == null) {
191                 alert = RingtoneManager.getDefaultUri(
192                         RingtoneManager.TYPE_ALARM);
193             }
194         }
195     }
196
197     public Alarm(Parcel p) {
198         id = p.readInt();
199         enabled = p.readInt() == 1;
200         hour = p.readInt();
201         minutes = p.readInt();
202         daysOfWeek = new DaysOfWeek(p.readInt());
203         time = p.readLong();
204         vibrate = p.readInt() == 1;
205         label = p.readString();
206         alert = (Uri) p.readParcelable(null);
207         silent = p.readInt() == 1;
208     }
209
210     public String getLabelOrDefault(Context context) {
211         if (label == null || label.length() == 0) {
212             return context.getString(R.string.default_label);
213         }
214         return label;
215     }
216
217     /*
218      * Days of week code as a single int.
219      * 0x00: no day
220      * 0x01: Monday
221      * 0x02: Tuesday
222      * 0x04: Wednesday
223      * 0x08: Thursday
224      * 0x10: Friday
225      * 0x20: Saturday
226      * 0x40: Sunday
227      */
228     static final class DaysOfWeek {
229
230         private static int[] DAY_MAP = new int[] {
231             Calendar.MONDAY,
232             Calendar.TUESDAY,
233             Calendar.WEDNESDAY,
234             Calendar.THURSDAY,
235             Calendar.FRIDAY,
236             Calendar.SATURDAY,
237             Calendar.SUNDAY,
238         };
239
240         // Bitmask of all repeating days
241         private int mDays;
242
243         DaysOfWeek(int days) {
244             mDays = days;
245         }
246
247         public String toString(Context context, boolean showNever) {
248             StringBuilder ret = new StringBuilder();
249
250             // no days
251             if (mDays == 0) {
252                 return showNever ?
253                         context.getText(R.string.never).toString() : "";
254             }
255
256             // every day
257             if (mDays == 0x7f) {
258                 return context.getText(R.string.every_day).toString();
259             }
260
261             // count selected days
262             int dayCount = 0, days = mDays;
263             while (days > 0) {
264                 if ((days & 1) == 1) dayCount++;
265                 days >>= 1;
266             }
267
268             // short or long form?
269             DateFormatSymbols dfs = new DateFormatSymbols();
270             String[] dayList = (dayCount > 1) ?
271                     dfs.getShortWeekdays() :
272                     dfs.getWeekdays();
273
274             // selected days
275             for (int i = 0; i < 7; i++) {
276                 if ((mDays & (1 << i)) != 0) {
277                     ret.append(dayList[DAY_MAP[i]]);
278                     dayCount -= 1;
279                     if (dayCount > 0) ret.append(
280                             context.getText(R.string.day_concat));
281                 }
282             }
283             return ret.toString();
284         }
285
286         private boolean isSet(int day) {
287             return ((mDays & (1 << day)) > 0);
288         }
289
290         public void set(int day, boolean set) {
291             if (set) {
292                 mDays |= (1 << day);
293             } else {
294                 mDays &= ~(1 << day);
295             }
296         }
297
298         public void set(DaysOfWeek dow) {
299             mDays = dow.mDays;
300         }
301
302         public int getCoded() {
303             return mDays;
304         }
305
306         // Returns days of week encoded in an array of booleans.
307         public boolean[] getBooleanArray() {
308             boolean[] ret = new boolean[7];
309             for (int i = 0; i < 7; i++) {
310                 ret[i] = isSet(i);
311             }
312             return ret;
313         }
314
315         public boolean isRepeatSet() {
316             return mDays != 0;
317         }
318
319         /**
320          * returns number of days from today until next alarm
321          * @param c must be set to today
322          */
323         public int getNextAlarm(Calendar c) {
324             if (mDays == 0) {
325                 return -1;
326             }
327
328             int today = (c.get(Calendar.DAY_OF_WEEK) + 5) % 7;
329
330             int day = 0;
331             int dayCount = 0;
332             for (; dayCount < 7; dayCount++) {
333                 day = (today + dayCount) % 7;
334                 if (isSet(day)) {
335                     break;
336                 }
337             }
338             return dayCount;
339         }
340     }
341 }