예제 #1
0
  @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 + ";");
    }
  }
예제 #2
0
  long fixAlarmInsert(ContentValues values) {
    // Why are we doing this? Is this not a programming bug if we try to
    // insert an already used id?
    SQLiteDatabase db = getWritableDatabase();
    db.beginTransaction();
    long rowId = -1;
    try {
      // Check if we are trying to re-use an existing id.
      Object value = values.get(ClockContract.AlarmsColumns._ID);
      if (value != null) {
        long id = (Long) value;
        if (id > -1) {
          final Cursor cursor =
              db.query(
                  ALARMS_TABLE_NAME,
                  new String[] {ClockContract.AlarmsColumns._ID},
                  ClockContract.AlarmsColumns._ID + " = ?",
                  new String[] {id + ""},
                  null,
                  null,
                  null);
          if (cursor.moveToFirst()) {
            // Record exists. Remove the id so sqlite can generate a new one.
            values.putNull(ClockContract.AlarmsColumns._ID);
          }
        }
      }

      rowId = db.insert(ALARMS_TABLE_NAME, ClockContract.AlarmsColumns.RINGTONE, values);
      db.setTransactionSuccessful();
    } finally {
      db.endTransaction();
    }
    if (rowId < 0) {
      throw new SQLException("Failed to insert row");
    }
    LogUtils.v("Added alarm rowId = " + rowId);

    return rowId;
  }