diff options
Diffstat (limited to 'src/com/android/deskclock/provider/ClockDatabaseHelper.java')
-rw-r--r-- | src/com/android/deskclock/provider/ClockDatabaseHelper.java | 112 |
1 files changed, 97 insertions, 15 deletions
diff --git a/src/com/android/deskclock/provider/ClockDatabaseHelper.java b/src/com/android/deskclock/provider/ClockDatabaseHelper.java index b6fc90017..11bd43cf1 100644 --- a/src/com/android/deskclock/provider/ClockDatabaseHelper.java +++ b/src/com/android/deskclock/provider/ClockDatabaseHelper.java @@ -16,6 +16,7 @@ package com.android.deskclock.provider; +import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; @@ -29,6 +30,7 @@ import com.android.deskclock.LogUtils; import com.android.deskclock.data.Weekdays; import java.util.Calendar; +import java.util.UUID; /** * Helper class for opening the database from multiple providers. Also provides @@ -57,11 +59,26 @@ class ClockDatabaseHelper extends SQLiteOpenHelper { */ private static final int VERSION_8 = 8; + /** + * Added increasing alarm volume mode + */ + private static final int VERSION_9 = 10; + + /** + * Added change profile + */ + private static final int VERSION_10 = 11; + + /** + * Removed change profile + */ + private static final int VERSION_11 = 12; + // This creates a default alarm at 8:30 for every Mon,Tue,Wed,Thu,Fri - private static final String DEFAULT_ALARM_1 = "(8, 30, 31, 0, 1, '', NULL, 0);"; + private static final String DEFAULT_ALARM_1 = "(8, 30, 31, 0, 1, '', NULL, 0, 0);"; // This creates a default alarm at 9:30 for every Sat,Sun - private static final String DEFAULT_ALARM_2 = "(9, 00, 96, 0, 1, '', NULL, 0);"; + private static final String DEFAULT_ALARM_2 = "(9, 00, 96, 0, 1, '', NULL, 0, 0);"; // Database and table names static final String DATABASE_NAME = "alarms.db"; @@ -70,8 +87,8 @@ class ClockDatabaseHelper extends SQLiteOpenHelper { static final String INSTANCES_TABLE_NAME = "alarm_instances"; private static final String SELECTED_CITIES_TABLE_NAME = "selected_cities"; - private static void createAlarmsTable(SQLiteDatabase db) { - db.execSQL("CREATE TABLE " + ALARMS_TABLE_NAME + " (" + + private static void createAlarmsTable(SQLiteDatabase db, String alarmsTableName) { + db.execSQL("CREATE TABLE " + alarmsTableName + " (" + ClockContract.AlarmsColumns._ID + " INTEGER PRIMARY KEY," + ClockContract.AlarmsColumns.HOUR + " INTEGER NOT NULL, " + ClockContract.AlarmsColumns.MINUTES + " INTEGER NOT NULL, " + @@ -80,12 +97,13 @@ class ClockDatabaseHelper extends SQLiteOpenHelper { ClockContract.AlarmsColumns.VIBRATE + " INTEGER NOT NULL, " + ClockContract.AlarmsColumns.LABEL + " TEXT NOT NULL, " + ClockContract.AlarmsColumns.RINGTONE + " TEXT, " + - ClockContract.AlarmsColumns.DELETE_AFTER_USE + " INTEGER NOT NULL DEFAULT 0);"); + ClockContract.AlarmsColumns.DELETE_AFTER_USE + " INTEGER NOT NULL DEFAULT 0, " + + ClockContract.AlarmsColumns.INCREASING_VOLUME + " INTEGER NOT NULL DEFAULT 0);"); LogUtils.i("Alarms Table created"); } - private static void createInstanceTable(SQLiteDatabase db) { - db.execSQL("CREATE TABLE " + INSTANCES_TABLE_NAME + " (" + + private static void createInstanceTable(SQLiteDatabase db, String instanceTableName) { + db.execSQL("CREATE TABLE " + instanceTableName + " (" + ClockContract.InstancesColumns._ID + " INTEGER PRIMARY KEY," + ClockContract.InstancesColumns.YEAR + " INTEGER NOT NULL, " + ClockContract.InstancesColumns.MONTH + " INTEGER NOT NULL, " + @@ -98,19 +116,19 @@ class ClockDatabaseHelper extends SQLiteOpenHelper { ClockContract.InstancesColumns.ALARM_STATE + " INTEGER NOT NULL, " + ClockContract.InstancesColumns.ALARM_ID + " INTEGER REFERENCES " + ALARMS_TABLE_NAME + "(" + ClockContract.AlarmsColumns._ID + ") " + - "ON UPDATE CASCADE ON DELETE CASCADE" + - ");"); + "ON UPDATE CASCADE ON DELETE CASCADE, " + + ClockContract.InstancesColumns.INCREASING_VOLUME + " INTEGER NOT NULL DEFAULT 0);"); LogUtils.i("Instance table created"); } public ClockDatabaseHelper(Context context) { - super(context, DATABASE_NAME, null, VERSION_8); + super(context, DATABASE_NAME, null, VERSION_11); } @Override public void onCreate(SQLiteDatabase db) { - createAlarmsTable(db); - createInstanceTable(db); + createAlarmsTable(db, ALARMS_TABLE_NAME); + createInstanceTable(db, INSTANCES_TABLE_NAME); // insert default alarms LogUtils.i("Inserting default alarms"); @@ -123,7 +141,8 @@ class ClockDatabaseHelper extends SQLiteOpenHelper { ClockContract.AlarmsColumns.VIBRATE + cs + ClockContract.AlarmsColumns.LABEL + cs + ClockContract.AlarmsColumns.RINGTONE + cs + - ClockContract.AlarmsColumns.DELETE_AFTER_USE + ") VALUES "; + ClockContract.AlarmsColumns.DELETE_AFTER_USE + cs + + ClockContract.AlarmsColumns.INCREASING_VOLUME + ") VALUES "; db.execSQL(insertMe + DEFAULT_ALARM_1); db.execSQL(insertMe + DEFAULT_ALARM_2); } @@ -142,8 +161,8 @@ class ClockDatabaseHelper extends SQLiteOpenHelper { db.execSQL("DROP TABLE IF EXISTS " + INSTANCES_TABLE_NAME + ";"); // Create new alarms table and copy over the data - createAlarmsTable(db); - createInstanceTable(db); + createAlarmsTable(db, ALARMS_TABLE_NAME); + createInstanceTable(db, INSTANCES_TABLE_NAME); LogUtils.i("Copying old alarms to new table"); final String[] OLD_TABLE_COLUMNS = { @@ -155,6 +174,7 @@ class ClockDatabaseHelper extends SQLiteOpenHelper { "vibrate", "message", "alert", + "incvol" }; try (Cursor cursor = db.query(OLD_ALARMS_TABLE_NAME, OLD_TABLE_COLUMNS, null, null, null, null, null)) { @@ -176,6 +196,7 @@ class ClockDatabaseHelper extends SQLiteOpenHelper { alarm.alert = TextUtils.isEmpty(alertString) ? null : Uri.parse(alertString); } + alarm.increasingVolume = cursor.getInt(8) == 1; // Save new version of alarm and create alarm instance for it db.insert(ALARMS_TABLE_NAME, null, Alarm.createContentValues(alarm)); @@ -189,6 +210,67 @@ class ClockDatabaseHelper extends SQLiteOpenHelper { LogUtils.i("Dropping old alarm table"); db.execSQL("DROP TABLE IF EXISTS " + OLD_ALARMS_TABLE_NAME + ";"); + return; + } + + if (oldVersion < VERSION_9) { + db.execSQL("ALTER TABLE " + ALARMS_TABLE_NAME + + " ADD COLUMN " + ClockContract.AlarmsColumns.INCREASING_VOLUME + + " INTEGER NOT NULL DEFAULT 0;"); + db.execSQL("ALTER TABLE " + INSTANCES_TABLE_NAME + + " ADD COLUMN " + ClockContract.InstancesColumns.INCREASING_VOLUME + + " INTEGER NOT NULL DEFAULT 0;"); + } + + if (oldVersion < VERSION_10) { + db.execSQL("ALTER TABLE " + ALARMS_TABLE_NAME + + " ADD COLUMN profile" + + " TEXT NOT NULL DEFAULT '';"); + db.execSQL("ALTER TABLE " + INSTANCES_TABLE_NAME + + " ADD COLUMN profile" + + " TEXT NOT NULL DEFAULT '';"); + } + + if (oldVersion < VERSION_11) { + LogUtils.i("Copying alarms to temporary table"); + final String TEMP_ALARMS_TABLE_NAME = ALARMS_TABLE_NAME + "_temp"; + final String TEMP_INSTANCES_TABLE_NAME = INSTANCES_TABLE_NAME + "_temp"; + createAlarmsTable(db, TEMP_ALARMS_TABLE_NAME); + createInstanceTable(db, TEMP_INSTANCES_TABLE_NAME); + final String[] OLD_TABLE_COLUMNS = { + ClockContract.AlarmsColumns._ID, + ClockContract.AlarmsColumns.HOUR, + ClockContract.AlarmsColumns.MINUTES, + ClockContract.AlarmsColumns.DAYS_OF_WEEK, + ClockContract.AlarmsColumns.ENABLED, + ClockContract.AlarmsColumns.VIBRATE, + ClockContract.AlarmsColumns.LABEL, + ClockContract.AlarmsColumns.RINGTONE, + ClockContract.AlarmsColumns.DELETE_AFTER_USE, + ClockContract.AlarmsColumns.INCREASING_VOLUME + }; + + try (Cursor cursor = db.query(ALARMS_TABLE_NAME, OLD_TABLE_COLUMNS, + null, null, null, null, null)) { + final Calendar currentTime = Calendar.getInstance(); + while (cursor != null && cursor.moveToNext()) { + final Alarm alarm = new Alarm(cursor); + // Save new version of alarm and create alarm instance for it + db.insert(TEMP_ALARMS_TABLE_NAME, null, + Alarm.createContentValues(alarm)); + if (alarm.enabled) { + AlarmInstance newInstance = alarm.createInstanceAfter(currentTime); + db.insert(TEMP_INSTANCES_TABLE_NAME, null, + AlarmInstance.createContentValues(newInstance)); + } + } + } + db.execSQL("DROP TABLE IF EXISTS " + ALARMS_TABLE_NAME + ";"); + db.execSQL("DROP TABLE IF EXISTS " + INSTANCES_TABLE_NAME + ";"); + db.execSQL("ALTER TABLE " + TEMP_ALARMS_TABLE_NAME + + " RENAME TO " + ALARMS_TABLE_NAME + ";"); + db.execSQL("ALTER TABLE " + TEMP_INSTANCES_TABLE_NAME + + " RENAME TO " + INSTANCES_TABLE_NAME + ";"); } } |