@Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
    LogUtils.v("Upgrading alarms database from version " + oldVersion + " to " + currentVersion);

    if (oldVersion <= VERSION_6) {
      // These were not used in DB_VERSION_6, so we can just drop them.
      db.execSQL("DROP TABLE IF EXISTS " + INSTANCES_TABLE_NAME + ";");
      db.execSQL("DROP TABLE IF EXISTS " + CITIES_TABLE_NAME + ";");

      // Create new alarms table and copy over the data
      createAlarmsTable(db);
      createInstanceTable(db);
      createCitiesTable(db);

      LogUtils.i("Copying old alarms to new table");
      String[] OLD_TABLE_COLUMNS = {
        "_id", "hour", "minutes", "daysofweek", "enabled", "vibrate", "message", "alert",
      };
      Cursor cursor =
          db.query(OLD_ALARMS_TABLE_NAME, OLD_TABLE_COLUMNS, null, null, null, null, null);
      Calendar currentTime = Calendar.getInstance();
      while (cursor.moveToNext()) {
        Alarm alarm = new Alarm();
        alarm.id = cursor.getLong(0);
        alarm.hour = cursor.getInt(1);
        alarm.minutes = cursor.getInt(2);
        alarm.daysOfWeek = new DaysOfWeek(cursor.getInt(3));
        alarm.enabled = cursor.getInt(4) == 1;
        alarm.vibrate = cursor.getInt(5) == 1;
        alarm.label = cursor.getString(6);

        String alertString = cursor.getString(7);
        if ("silent".equals(alertString)) {
          alarm.alert = Alarm.NO_RINGTONE_URI;
        } else {
          alarm.alert = TextUtils.isEmpty(alertString) ? null : Uri.parse(alertString);
        }

        // Save new version of alarm and create alarminstance for it
        db.insert(ALARMS_TABLE_NAME, null, Alarm.createContentValues(alarm));
        if (alarm.enabled) {
          AlarmInstance newInstance = alarm.createInstanceAfter(currentTime);
          db.insert(INSTANCES_TABLE_NAME, null, AlarmInstance.createContentValues(newInstance));
        }
      }
      cursor.close();

      LogUtils.i("Dropping old alarm table");
      db.execSQL("DROP TABLE IF EXISTS " + OLD_ALARMS_TABLE_NAME + ";");
    }
  }
 private static void createInstanceTable(SQLiteDatabase db) {
   db.execSQL(
       "CREATE TABLE "
           + INSTANCES_TABLE_NAME
           + " ("
           + ClockContract.InstancesColumns._ID
           + " INTEGER PRIMARY KEY,"
           + ClockContract.InstancesColumns.YEAR
           + " INTEGER NOT NULL, "
           + ClockContract.InstancesColumns.MONTH
           + " INTEGER NOT NULL, "
           + ClockContract.InstancesColumns.DAY
           + " INTEGER NOT NULL, "
           + ClockContract.InstancesColumns.HOUR
           + " INTEGER NOT NULL, "
           + ClockContract.InstancesColumns.MINUTES
           + " INTEGER NOT NULL, "
           + ClockContract.InstancesColumns.VIBRATE
           + " INTEGER NOT NULL, "
           + ClockContract.InstancesColumns.LABEL
           + " TEXT NOT NULL, "
           + ClockContract.InstancesColumns.RINGTONE
           + " TEXT, "
           + 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"
           + ");");
   LogUtils.i("Instance table created");
 }
 private static void createAlarmsTable(SQLiteDatabase db) {
   db.execSQL(
       "CREATE TABLE "
           + ALARMS_TABLE_NAME
           + " ("
           + ClockContract.AlarmsColumns._ID
           + " INTEGER PRIMARY KEY,"
           + ClockContract.AlarmsColumns.HOUR
           + " INTEGER NOT NULL, "
           + ClockContract.AlarmsColumns.MINUTES
           + " INTEGER NOT NULL, "
           + ClockContract.AlarmsColumns.DAYS_OF_WEEK
           + " INTEGER NOT NULL, "
           + ClockContract.AlarmsColumns.ENABLED
           + " INTEGER NOT NULL, "
           + 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);");
   LogUtils.i("Alarms Table created");
 }
  @Override
  public void onCreate(SQLiteDatabase db) {
    createAlarmsTable(db);
    createInstanceTable(db);
    createCitiesTable(db);

    // insert default alarms
    LogUtils.i("Inserting default alarms");
    String cs = ", "; // comma and space
    String insertMe =
        "INSERT INTO "
            + ALARMS_TABLE_NAME
            + " ("
            + ClockContract.AlarmsColumns.HOUR
            + cs
            + ClockContract.AlarmsColumns.MINUTES
            + cs
            + ClockContract.AlarmsColumns.DAYS_OF_WEEK
            + cs
            + ClockContract.AlarmsColumns.ENABLED
            + cs
            + ClockContract.AlarmsColumns.VIBRATE
            + cs
            + ClockContract.AlarmsColumns.LABEL
            + cs
            + ClockContract.AlarmsColumns.RINGTONE
            + cs
            + ClockContract.AlarmsColumns.DELETE_AFTER_USE
            + ") VALUES ";
    db.execSQL(insertMe + DEFAULT_ALARM_1);
    db.execSQL(insertMe + DEFAULT_ALARM_2);
  }
 private static void createCitiesTable(SQLiteDatabase db) {
   db.execSQL(
       "CREATE TABLE "
           + CITIES_TABLE_NAME
           + " ("
           + ClockContract.CitiesColumns.CITY_ID
           + " TEXT PRIMARY KEY,"
           + ClockContract.CitiesColumns.CITY_NAME
           + " TEXT NOT NULL, "
           + ClockContract.CitiesColumns.TIMEZONE_NAME
           + " TEXT NOT NULL, "
           + ClockContract.CitiesColumns.TIMEZONE_OFFSET
           + " INTEGER NOT NULL);");
   LogUtils.i("Cities table created");
 }