/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.deskclock;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

/**
 * Helper class for opening the database from multiple providers.  Also provides
 * some common functionality.
 */
class AlarmDatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "alarms.db";
    private static final int DATABASE_VERSION = 5;

    public AlarmDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE alarms (" +
                   "_id INTEGER PRIMARY KEY," +
                   "hour INTEGER, " +
                   "minutes INTEGER, " +
                   "daysofweek INTEGER, " +
                   "alarmtime INTEGER, " +
                   "enabled INTEGER, " +
                   "vibrate INTEGER, " +
                   "message TEXT, " +
                   "alert TEXT);");

        // insert default alarms
        String insertMe = "INSERT INTO alarms " +
                "(hour, minutes, daysofweek, alarmtime, enabled, vibrate, " +
                " message, alert) VALUES ";
        db.execSQL(insertMe + "(8, 30, 31, 0, 0, 1, '', '');");
        db.execSQL(insertMe + "(9, 00, 96, 0, 0, 1, '', '');");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion,
            int currentVersion) {
        if (Log.LOGV) Log.v(
                "Upgrading alarms database from version " +
                oldVersion + " to " + currentVersion +
                ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS alarms");
        onCreate(db);
    }

    Uri commonInsert(ContentValues values) {
        SQLiteDatabase db = getWritableDatabase();
        db.beginTransaction();
        long rowId = -1;
        try {
            // Check if we are trying to re-use an existing id.
            Object value = values.get(Alarm.Columns._ID);
            if (value != null) {
                int id = (Integer) value;
                if (id > -1) {
                    final Cursor cursor = db
                            .query("alarms", new String[]{Alarm.Columns._ID}, "_id = ?",
                                    new String[]{id + ""}, null, null, null);
                    if (cursor.moveToFirst()) {
                        // Record exists. Remove the id so sqlite can generate a new one.
                        values.putNull(Alarm.Columns._ID);
                    }
                }
            }

            rowId = db.insert("alarms", Alarm.Columns.MESSAGE, values);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
        if (rowId < 0) {
            throw new SQLException("Failed to insert row");
        }
        if (Log.LOGV) Log.v("Added alarm rowId = " + rowId);

        return ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, rowId);
    }
}

@@ -1177,16 +1177,71 @@
 ms.db%22;%0A
+    private static final String TABLE_NAME = %22alarms%22;%0A
     priv
@@ -1497,22 +1497,34 @@
 E TABLE 
-alarms
+%22 + TABLE_NAME + %22
  (%22 +%0A  
@@ -1540,20 +1540,37 @@
         
-%22_id
+Alarm.Columns._ID + %22
  INTEGER
@@ -1609,13 +1609,30 @@
     
-%22hour
+Alarm.Columns.HOUR + %22
  INT
@@ -1664,16 +1664,33 @@
     
-%22minutes
+Alarm.Columns.MINUTES + %22
  INT
@@ -1722,19 +1722,38 @@
     
-%22daysofweek
+Alarm.Columns.DAYS_OF_WEEK + %22
  INT
@@ -1785,18 +1785,36 @@
     
-%22a
+A
 larm
-time
+.Columns.ALARM_TIME + %22
  INT
@@ -1846,16 +1846,33 @@
     
-%22enabled
+Alarm.Columns.ENABLED + %22
  INT
@@ -1904,16 +1904,33 @@
     
-%22vibrate
+Alarm.Columns.VIBRATE + %22
  INT
@@ -1962,16 +1962,33 @@
     
-%22message
+Alarm.Columns.MESSAGE + %22
  TEX
@@ -2017,14 +2017,31 @@
     
-%22alert
+Alarm.Columns.ALERT + %22
  TEX
@@ -2100,153 +2100,394 @@
 ing 
-insertMe = %22INSERT INTO alarms %22 +%0A                %22(hour, minutes, daysofweek, alarmtime, enabled, vibrate, %22 +%0A                %22 message, alert
+cs = %22, %22; //coma and space%0A        String insertMe = %22INSERT INTO %22 + TABLE_NAME +%0A                %22 (%22 + Alarm.Columns.HOUR + cs + Alarm.Columns.MINUTES + cs +%0A                Alarm.Columns.DAYS_OF_WEEK + cs + Alarm.Columns.ALARM_TIME + cs +%0A                Alarm.Columns.ENABLED + cs + Alarm.Columns.VIBRATE + cs +%0A                Alarm.Columns.MESSAGE + cs + Alarm.Columns.ALERT + %22
 ) VA
@@ -2977,23 +2977,30 @@
  EXISTS 
-alarms%22
+%22 + TABLE_NAME
 );%0A     
@@ -3497,24 +3497,26 @@
  .query(
-%22alarms%22
+TABLE_NAME
 , new St
@@ -3545,13 +3545,66 @@
 ID%7D,
- %22_id
+%0A                                    Alarm.Columns._ID + %22
  = ?
@@ -3966,24 +3966,26 @@
 .insert(
-%22alarms%22
+TABLE_NAME
 , Alarm.

Remove hardcoded strings from AlarmDatabaseHelper