OSDN Git Service

More tweaks per spec.
[android-x86/packages-apps-DeskClock.git] / src / com / android / deskclock / AlarmProvider.java
1 /*
2  * Copyright (C) 2007 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.deskclock;
18
19 import android.content.ContentProvider;
20 import android.content.ContentUris;
21 import android.content.ContentValues;
22 import android.content.Context;
23 import android.content.UriMatcher;
24 import android.database.Cursor;
25 import android.database.SQLException;
26 import android.database.sqlite.SQLiteDatabase;
27 import android.database.sqlite.SQLiteOpenHelper;
28 import android.database.sqlite.SQLiteQueryBuilder;
29 import android.net.Uri;
30 import android.text.TextUtils;
31
32 public class AlarmProvider extends ContentProvider {
33     private SQLiteOpenHelper mOpenHelper;
34
35     private static final int ALARMS = 1;
36     private static final int ALARMS_ID = 2;
37     private static final UriMatcher sURLMatcher = new UriMatcher(
38             UriMatcher.NO_MATCH);
39
40     static {
41         sURLMatcher.addURI("com.android.deskclock", "alarm", ALARMS);
42         sURLMatcher.addURI("com.android.deskclock", "alarm/#", ALARMS_ID);
43     }
44
45     private static class DatabaseHelper extends SQLiteOpenHelper {
46         private static final String DATABASE_NAME = "alarms.db";
47         private static final int DATABASE_VERSION = 5;
48
49         public DatabaseHelper(Context context) {
50             super(context, DATABASE_NAME, null, DATABASE_VERSION);
51         }
52
53         @Override
54         public void onCreate(SQLiteDatabase db) {
55             db.execSQL("CREATE TABLE alarms (" +
56                        "_id INTEGER PRIMARY KEY," +
57                        "hour INTEGER, " +
58                        "minutes INTEGER, " +
59                        "daysofweek INTEGER, " +
60                        "alarmtime INTEGER, " +
61                        "enabled INTEGER, " +
62                        "vibrate INTEGER, " +
63                        "message TEXT, " +
64                        "alert TEXT);");
65
66             // insert default alarms
67             String insertMe = "INSERT INTO alarms " +
68                     "(hour, minutes, daysofweek, alarmtime, enabled, vibrate, message, alert) " +
69                     "VALUES ";
70             db.execSQL(insertMe + "(8, 30, 31, 0, 0, 1, '', '');");
71             db.execSQL(insertMe + "(9, 00, 96, 0, 0, 1, '', '');");
72         }
73
74         @Override
75         public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
76             if (Log.LOGV) Log.v(
77                     "Upgrading alarms database from version " +
78                     oldVersion + " to " + currentVersion +
79                     ", which will destroy all old data");
80             db.execSQL("DROP TABLE IF EXISTS alarms");
81             onCreate(db);
82         }
83     }
84
85     public AlarmProvider() {
86     }
87
88     @Override
89     public boolean onCreate() {
90         mOpenHelper = new DatabaseHelper(getContext());
91         return true;
92     }
93
94     @Override
95     public Cursor query(Uri url, String[] projectionIn, String selection,
96             String[] selectionArgs, String sort) {
97         SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
98
99         // Generate the body of the query
100         int match = sURLMatcher.match(url);
101         switch (match) {
102             case ALARMS:
103                 qb.setTables("alarms");
104                 break;
105             case ALARMS_ID:
106                 qb.setTables("alarms");
107                 qb.appendWhere("_id=");
108                 qb.appendWhere(url.getPathSegments().get(1));
109                 break;
110             default:
111                 throw new IllegalArgumentException("Unknown URL " + url);
112         }
113
114         SQLiteDatabase db = mOpenHelper.getReadableDatabase();
115         Cursor ret = qb.query(db, projectionIn, selection, selectionArgs,
116                               null, null, sort);
117
118         if (ret == null) {
119             if (Log.LOGV) Log.v("Alarms.query: failed");
120         } else {
121             ret.setNotificationUri(getContext().getContentResolver(), url);
122         }
123
124         return ret;
125     }
126
127     @Override
128     public String getType(Uri url) {
129         int match = sURLMatcher.match(url);
130         switch (match) {
131             case ALARMS:
132                 return "vnd.android.cursor.dir/alarms";
133             case ALARMS_ID:
134                 return "vnd.android.cursor.item/alarms";
135             default:
136                 throw new IllegalArgumentException("Unknown URL");
137         }
138     }
139
140     @Override
141     public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
142         int count;
143         long rowId = 0;
144         int match = sURLMatcher.match(url);
145         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
146         switch (match) {
147             case ALARMS_ID: {
148                 String segment = url.getPathSegments().get(1);
149                 rowId = Long.parseLong(segment);
150                 count = db.update("alarms", values, "_id=" + rowId, null);
151                 break;
152             }
153             default: {
154                 throw new UnsupportedOperationException(
155                         "Cannot update URL: " + url);
156             }
157         }
158         if (Log.LOGV) Log.v("*** notifyChange() rowId: " + rowId + " url " + url);
159         getContext().getContentResolver().notifyChange(url, null);
160         return count;
161     }
162
163     @Override
164     public Uri insert(Uri url, ContentValues initialValues) {
165         if (sURLMatcher.match(url) != ALARMS) {
166             throw new IllegalArgumentException("Cannot insert into URL: " + url);
167         }
168
169         ContentValues values;
170         if (initialValues != null)
171             values = new ContentValues(initialValues);
172         else
173             values = new ContentValues();
174
175         if (!values.containsKey(Alarm.Columns.HOUR))
176             values.put(Alarm.Columns.HOUR, 0);
177
178         if (!values.containsKey(Alarm.Columns.MINUTES))
179             values.put(Alarm.Columns.MINUTES, 0);
180
181         if (!values.containsKey(Alarm.Columns.DAYS_OF_WEEK))
182             values.put(Alarm.Columns.DAYS_OF_WEEK, 0);
183
184         if (!values.containsKey(Alarm.Columns.ALARM_TIME))
185             values.put(Alarm.Columns.ALARM_TIME, 0);
186
187         if (!values.containsKey(Alarm.Columns.ENABLED))
188             values.put(Alarm.Columns.ENABLED, 0);
189
190         if (!values.containsKey(Alarm.Columns.VIBRATE))
191             values.put(Alarm.Columns.VIBRATE, 1);
192
193         if (!values.containsKey(Alarm.Columns.MESSAGE))
194             values.put(Alarm.Columns.MESSAGE, "");
195
196         if (!values.containsKey(Alarm.Columns.ALERT))
197             values.put(Alarm.Columns.ALERT, "");
198
199         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
200         long rowId = db.insert("alarms", Alarm.Columns.MESSAGE, values);
201         if (rowId < 0) {
202             throw new SQLException("Failed to insert row into " + url);
203         }
204         if (Log.LOGV) Log.v("Added alarm rowId = " + rowId);
205
206         Uri newUrl = ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, rowId);
207         getContext().getContentResolver().notifyChange(newUrl, null);
208         return newUrl;
209     }
210
211     public int delete(Uri url, String where, String[] whereArgs) {
212         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
213         int count;
214         long rowId = 0;
215         switch (sURLMatcher.match(url)) {
216             case ALARMS:
217                 count = db.delete("alarms", where, whereArgs);
218                 break;
219             case ALARMS_ID:
220                 String segment = url.getPathSegments().get(1);
221                 rowId = Long.parseLong(segment);
222                 if (TextUtils.isEmpty(where)) {
223                     where = "_id=" + segment;
224                 } else {
225                     where = "_id=" + segment + " AND (" + where + ")";
226                 }
227                 count = db.delete("alarms", where, whereArgs);
228                 break;
229             default:
230                 throw new IllegalArgumentException("Cannot delete from URL: " + url);
231         }
232
233         getContext().getContentResolver().notifyChange(url, null);
234         return count;
235     }
236 }