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.deskclock;
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.deskclock", "alarm", ALARMS);
42 sURLMatcher.addURI("com.android.deskclock", "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 + "(8, 30, 31, 0, 0, 1, '', '');");
71 db.execSQL(insertMe + "(9, 00, 96, 0, 0, 1, '', '');");
75 public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
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");
85 public AlarmProvider() {
89 public boolean onCreate() {
90 mOpenHelper = new DatabaseHelper(getContext());
95 public Cursor query(Uri url, String[] projectionIn, String selection,
96 String[] selectionArgs, String sort) {
97 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
99 // Generate the body of the query
100 int match = sURLMatcher.match(url);
103 qb.setTables("alarms");
106 qb.setTables("alarms");
107 qb.appendWhere("_id=");
108 qb.appendWhere(url.getPathSegments().get(1));
111 throw new IllegalArgumentException("Unknown URL " + url);
114 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
115 Cursor ret = qb.query(db, projectionIn, selection, selectionArgs,
119 if (Log.LOGV) Log.v("Alarms.query: failed");
121 ret.setNotificationUri(getContext().getContentResolver(), url);
128 public String getType(Uri url) {
129 int match = sURLMatcher.match(url);
132 return "vnd.android.cursor.dir/alarms";
134 return "vnd.android.cursor.item/alarms";
136 throw new IllegalArgumentException("Unknown URL");
141 public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
144 int match = sURLMatcher.match(url);
145 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
148 String segment = url.getPathSegments().get(1);
149 rowId = Long.parseLong(segment);
150 count = db.update("alarms", values, "_id=" + rowId, null);
154 throw new UnsupportedOperationException(
155 "Cannot update URL: " + url);
158 if (Log.LOGV) Log.v("*** notifyChange() rowId: " + rowId + " url " + url);
159 getContext().getContentResolver().notifyChange(url, null);
164 public Uri insert(Uri url, ContentValues initialValues) {
165 if (sURLMatcher.match(url) != ALARMS) {
166 throw new IllegalArgumentException("Cannot insert into URL: " + url);
169 ContentValues values;
170 if (initialValues != null)
171 values = new ContentValues(initialValues);
173 values = new ContentValues();
175 if (!values.containsKey(Alarm.Columns.HOUR))
176 values.put(Alarm.Columns.HOUR, 0);
178 if (!values.containsKey(Alarm.Columns.MINUTES))
179 values.put(Alarm.Columns.MINUTES, 0);
181 if (!values.containsKey(Alarm.Columns.DAYS_OF_WEEK))
182 values.put(Alarm.Columns.DAYS_OF_WEEK, 0);
184 if (!values.containsKey(Alarm.Columns.ALARM_TIME))
185 values.put(Alarm.Columns.ALARM_TIME, 0);
187 if (!values.containsKey(Alarm.Columns.ENABLED))
188 values.put(Alarm.Columns.ENABLED, 0);
190 if (!values.containsKey(Alarm.Columns.VIBRATE))
191 values.put(Alarm.Columns.VIBRATE, 1);
193 if (!values.containsKey(Alarm.Columns.MESSAGE))
194 values.put(Alarm.Columns.MESSAGE, "");
196 if (!values.containsKey(Alarm.Columns.ALERT))
197 values.put(Alarm.Columns.ALERT, "");
199 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
200 long rowId = db.insert("alarms", Alarm.Columns.MESSAGE, values);
202 throw new SQLException("Failed to insert row into " + url);
204 if (Log.LOGV) Log.v("Added alarm rowId = " + rowId);
206 Uri newUrl = ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, rowId);
207 getContext().getContentResolver().notifyChange(newUrl, null);
211 public int delete(Uri url, String where, String[] whereArgs) {
212 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
215 switch (sURLMatcher.match(url)) {
217 count = db.delete("alarms", where, whereArgs);
220 String segment = url.getPathSegments().get(1);
221 rowId = Long.parseLong(segment);
222 if (TextUtils.isEmpty(where)) {
223 where = "_id=" + segment;
225 where = "_id=" + segment + " AND (" + where + ")";
227 count = db.delete("alarms", where, whereArgs);
230 throw new IllegalArgumentException("Cannot delete from URL: " + url);
233 getContext().getContentResolver().notifyChange(url, null);