1 /* 2 * Copyright (C) 2010 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.ContentUris; 20 import android.content.ContentValues; 21 import android.content.Context; 22 import android.database.Cursor; 23 import android.database.SQLException; 24 import android.database.sqlite.SQLiteDatabase; 25 import android.database.sqlite.SQLiteOpenHelper; 26 import android.net.Uri; 27 28 /** 29 * Helper class for opening the database from multiple providers. Also provides 30 * some common functionality. 31 */ 32 class AlarmDatabaseHelper extends SQLiteOpenHelper { 33 34 private static final String DATABASE_NAME = "alarms.db"; 35 private static final int DATABASE_VERSION = 5; 36 37 public AlarmDatabaseHelper(Context context) { 38 super(context, DATABASE_NAME, null, DATABASE_VERSION); 39 } 40 41 @Override 42 public void onCreate(SQLiteDatabase db) { 43 db.execSQL("CREATE TABLE alarms (" + 44 "_id INTEGER PRIMARY KEY," + 45 "hour INTEGER, " + 46 "minutes INTEGER, " + 47 "daysofweek INTEGER, " + 48 "alarmtime INTEGER, " + 49 "enabled INTEGER, " + 50 "vibrate INTEGER, " + 51 "message TEXT, " + 52 "alert TEXT);"); 53 54 // insert default alarms 55 String insertMe = "INSERT INTO alarms " + 56 "(hour, minutes, daysofweek, alarmtime, enabled, vibrate, " + 57 " message, alert) VALUES "; 58 db.execSQL(insertMe + "(8, 30, 31, 0, 0, 1, '', '');"); 59 db.execSQL(insertMe + "(9, 00, 96, 0, 0, 1, '', '');"); 60 } 61 62 @Override 63 public void onUpgrade(SQLiteDatabase db, int oldVersion, 64 int currentVersion) { 65 if (Log.LOGV) Log.v( 66 "Upgrading alarms database from version " + 67 oldVersion + " to " + currentVersion + 68 ", which will destroy all old data"); 69 db.execSQL("DROP TABLE IF EXISTS alarms"); 70 onCreate(db); 71 } 72 73 Uri commonInsert(ContentValues values) { 74 SQLiteDatabase db = getWritableDatabase(); 75 db.beginTransaction(); 76 long rowId = -1; 77 try { 78 // Check if we are trying to re-use an existing id. 79 Object value = values.get(Alarm.Columns._ID); 80 if (value != null) { 81 int id = (Integer) value; 82 if (id > -1) { 83 final Cursor cursor = db 84 .query("alarms", new String[]{Alarm.Columns._ID}, "_id = ?", 85 new String[]{id + ""}, null, null, null); 86 if (cursor.moveToFirst()) { 87 // Record exists. Remove the id so sqlite can generate a new one. 88 values.putNull(Alarm.Columns._ID); 89 } 90 } 91 } 92 93 rowId = db.insert("alarms", Alarm.Columns.MESSAGE, values); 94 db.setTransactionSuccessful(); 95 } finally { 96 db.endTransaction(); 97 } 98 if (rowId < 0) { 99 throw new SQLException("Failed to insert row"); 100 } 101 if (Log.LOGV) Log.v("Added alarm rowId = " + rowId); 102 103 return ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, rowId); 104 } 105 } 106