2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.alarmclock;
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;
32 public class AlarmProvider extends ContentProvider {
33 private SQLiteOpenHelper mOpenHelper;
35 private static final int ALARMS = 1;
36 private static final int ALARMS_ID = 2;
37 private static final UriMatcher sURLMatcher = new UriMatcher(
41 sURLMatcher.addURI("com.android.alarmclock", "alarm", ALARMS);
42 sURLMatcher.addURI("com.android.alarmclock", "alarm/#", ALARMS_ID);
45 private static class DatabaseHelper extends SQLiteOpenHelper {
46 private static final String DATABASE_NAME = "alarms.db";
47 private static final int DATABASE_VERSION = 5;
49 public DatabaseHelper(Context context) {
50 super(context, DATABASE_NAME, null, DATABASE_VERSION);
54 public void onCreate(SQLiteDatabase db) {
55 db.execSQL("CREATE TABLE alarms (" +
56 "_id INTEGER PRIMARY KEY," +
59 "daysofweek INTEGER, " +
60 "alarmtime INTEGER, " +
66 // insert default alarms
67 String insertMe = "INSERT INTO alarms " +
68 "(hour, minutes, daysofweek, alarmtime, enabled, vibrate, message, alert) " +
70 db.execSQL(insertMe + "(7, 0, 127, 0, 0, 1, '', '');");
71 db.execSQL(insertMe + "(8, 30, 31, 0, 0, 1, '', '');");
72 db.execSQL(insertMe + "(9, 00, 0, 0, 0, 1, '', '');");
76 public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
78 "Upgrading alarms database from version " +
79 oldVersion + " to " + currentVersion +
80 ", which will destroy all old data");
81 db.execSQL("DROP TABLE IF EXISTS alarms");
86 public AlarmProvider() {
90 public boolean onCreate() {
91 mOpenHelper = new DatabaseHelper(getContext());
96 public Cursor query(Uri url, String[] projectionIn, String selection,
97 String[] selectionArgs, String sort) {
98 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
100 // Generate the body of the query
101 int match = sURLMatcher.match(url);
104 qb.setTables("alarms");
107 qb.setTables("alarms");
108 qb.appendWhere("_id=");
109 qb.appendWhere(url.getPathSegments().get(1));
112 throw new IllegalArgumentException("Unknown URL " + url);
115 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
116 Cursor ret = qb.query(db, projectionIn, selection, selectionArgs,
120 if (Log.LOGV) Log.v("Alarms.query: failed");
122 ret.setNotificationUri(getContext().getContentResolver(), url);
129 public String getType(Uri url) {
130 int match = sURLMatcher.match(url);
133 return "vnd.android.cursor.dir/alarms";
135 return "vnd.android.cursor.item/alarms";
137 throw new IllegalArgumentException("Unknown URL");
142 public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
145 int match = sURLMatcher.match(url);
146 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
149 String segment = url.getPathSegments().get(1);
150 rowId = Long.parseLong(segment);
151 count = db.update("alarms", values, "_id=" + rowId, null);
155 throw new UnsupportedOperationException(
156 "Cannot update URL: " + url);
159 if (Log.LOGV) Log.v("*** notifyChange() rowId: " + rowId + " url " + url);
160 getContext().getContentResolver().notifyChange(url, null);
165 public Uri insert(Uri url, ContentValues initialValues) {
166 if (sURLMatcher.match(url) != ALARMS) {
167 throw new IllegalArgumentException("Cannot insert into URL: " + url);
170 ContentValues values;
171 if (initialValues != null)
172 values = new ContentValues(initialValues);
174 values = new ContentValues();
176 if (!values.containsKey(Alarm.Columns.HOUR))
177 values.put(Alarm.Columns.HOUR, 0);
179 if (!values.containsKey(Alarm.Columns.MINUTES))
180 values.put(Alarm.Columns.MINUTES, 0);
182 if (!values.containsKey(Alarm.Columns.DAYS_OF_WEEK))
183 values.put(Alarm.Columns.DAYS_OF_WEEK, 0);
185 if (!values.containsKey(Alarm.Columns.ALARM_TIME))
186 values.put(Alarm.Columns.ALARM_TIME, 0);
188 if (!values.containsKey(Alarm.Columns.ENABLED))
189 values.put(Alarm.Columns.ENABLED, 0);
191 if (!values.containsKey(Alarm.Columns.VIBRATE))
192 values.put(Alarm.Columns.VIBRATE, 1);
194 if (!values.containsKey(Alarm.Columns.MESSAGE))
195 values.put(Alarm.Columns.MESSAGE, "");
197 if (!values.containsKey(Alarm.Columns.ALERT))
198 values.put(Alarm.Columns.ALERT, "");
200 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
201 long rowId = db.insert("alarms", Alarm.Columns.MESSAGE, values);
203 throw new SQLException("Failed to insert row into " + url);
205 if (Log.LOGV) Log.v("Added alarm rowId = " + rowId);
207 Uri newUrl = ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, rowId);
208 getContext().getContentResolver().notifyChange(newUrl, null);
212 public int delete(Uri url, String where, String[] whereArgs) {
213 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
216 switch (sURLMatcher.match(url)) {
218 count = db.delete("alarms", where, whereArgs);
221 String segment = url.getPathSegments().get(1);
222 rowId = Long.parseLong(segment);
223 if (TextUtils.isEmpty(where)) {
224 where = "_id=" + segment;
226 where = "_id=" + segment + " AND (" + where + ")";
228 count = db.delete("alarms", where, whereArgs);
231 throw new IllegalArgumentException("Cannot delete from URL: " + url);
234 getContext().getContentResolver().notifyChange(url, null);