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.SQLException; 23 import android.database.sqlite.SQLiteDatabase; 24 import android.database.sqlite.SQLiteOpenHelper; 25 import android.net.Uri; 26 27 /** 28 * Helper class for opening the database from multiple providers. Also provides 29 * some common functionality. 30 */ 31 class AlarmDatabaseHelper extends SQLiteOpenHelper { 32 33 private static final String DATABASE_NAME = "alarms.db"; 34 private static final int DATABASE_VERSION = 5; 35 AlarmDatabaseHelper(Context context)36 public AlarmDatabaseHelper(Context context) { 37 super(context, DATABASE_NAME, null, DATABASE_VERSION); 38 } 39 40 @Override onCreate(SQLiteDatabase db)41 public void onCreate(SQLiteDatabase db) { 42 db.execSQL("CREATE TABLE alarms (" + 43 "_id INTEGER PRIMARY KEY," + 44 "hour INTEGER, " + 45 "minutes INTEGER, " + 46 "daysofweek INTEGER, " + 47 "alarmtime INTEGER, " + 48 "enabled INTEGER, " + 49 "vibrate INTEGER, " + 50 "message TEXT, " + 51 "alert TEXT);"); 52 53 // insert default alarms 54 String insertMe = "INSERT INTO alarms " + 55 "(hour, minutes, daysofweek, alarmtime, enabled, vibrate, " + 56 " message, alert) VALUES "; 57 db.execSQL(insertMe + "(8, 30, 31, 0, 0, 1, '', '');"); 58 db.execSQL(insertMe + "(9, 00, 96, 0, 0, 1, '', '');"); 59 } 60 61 @Override onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion)62 public void onUpgrade(SQLiteDatabase db, int oldVersion, 63 int currentVersion) { 64 if (Log.LOGV) Log.v( 65 "Upgrading alarms database from version " + 66 oldVersion + " to " + currentVersion + 67 ", which will destroy all old data"); 68 db.execSQL("DROP TABLE IF EXISTS alarms"); 69 onCreate(db); 70 } 71 commonInsert(ContentValues values)72 Uri commonInsert(ContentValues values) { 73 SQLiteDatabase db = getWritableDatabase(); 74 long rowId = db.insert("alarms", Alarm.Columns.MESSAGE, values); 75 if (rowId < 0) { 76 throw new SQLException("Failed to insert row"); 77 } 78 if (Log.LOGV) Log.v("Added alarm rowId = " + rowId); 79 80 return ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, rowId); 81 } 82 } 83