public List<Alarm> getAll() { List<Alarm> alarmList = new ArrayList<>(); Cursor cursor = getCursor(); if (cursor.moveToFirst()) { // COLUMN_ALARM_ID // COLUMN_ALARM_ACTIVE // COLUMN_ALARM_TIME // COLUMN_ALARM_DAYS // COLUMN_ALARM_MUSIC // COLUMN_ALARM_VIBRATE // COLUMN_ALARM_NAME do { Alarm alarm = new Alarm(); alarm.setId(cursor.getInt(0)); alarm.setActive(cursor.getInt(1) == 1); alarm.setTime(cursor.getString(2)); byte[] buff = cursor.getBlob(3); ByteArrayInputStream inputStream = new ByteArrayInputStream(buff); try { ObjectInputStream objectInputString = new ObjectInputStream(inputStream); Alarm.Day[] repeatDays; Object object = objectInputString.readObject(); if (object instanceof Alarm.Day[]) { repeatDays = (Alarm.Day[]) object; alarm.setDays(repeatDays); } } catch (Exception e) { e.printStackTrace(); } alarm.setMusic(cursor.getString(4)); alarm.setVibrate(cursor.getInt(5) == 1); alarm.setName(cursor.getString(6)); alarm.setMusicType(cursor.getInt(7) == 1); alarm.setMusicName(cursor.getString(8)); alarmList.add(alarm); } while (cursor.moveToNext()); } cursor.close(); // Sort alarms by time int size = alarmList.size(); Alarm[] alarms = new Alarm[size]; for (int i = 0; i < size; i++) { alarms[i] = alarmList.get(i); } Arrays.sort( alarms, new Comparator<Alarm>() { @Override public int compare(Alarm lhs, Alarm rhs) { return lhs.getTimeString().compareTo(rhs.getTimeString()); } }); alarmList.clear(); for (int i = 0; i < size; i++) { alarmList.add(alarms[i]); } return alarmList; }
public Alarm getAlarmById(int id) { String[] columns = new String[] { COLUMN_ALARM_ID, COLUMN_ALARM_ACTIVE, COLUMN_ALARM_TIME, COLUMN_ALARM_DAYS, COLUMN_ALARM_MUSIC, COLUMN_ALARM_VIBRATE, COLUMN_ALARM_NAME, COLUMN_ALARM_MUSICTYPE, COLUMN_ALARM_MUSICNAME }; Cursor c = getDatabase() .query(ALARM_TABLE, columns, COLUMN_ALARM_ID + "=" + id, null, null, null, null); Alarm alarm = null; if (c.moveToFirst()) { alarm = new Alarm(); alarm.setId(c.getInt(0)); alarm.setActive(c.getInt(1) == 1); alarm.setTime(c.getString(2)); byte[] repeatDaysBytes = c.getBlob(3); ByteArrayInputStream inputStream = new ByteArrayInputStream(repeatDaysBytes); try { ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); Alarm.Day[] repeatDays; Object object = objectInputStream.readObject(); if (object instanceof Alarm.Day[]) { repeatDays = (Alarm.Day[]) object; alarm.setDays(repeatDays); } } catch (Exception e) { e.printStackTrace(); } alarm.setMusic(c.getString(4)); alarm.setVibrate(c.getInt(5) == 1); alarm.setName(c.getString(6)); alarm.setMusicType(c.getInt(7) == 1); alarm.setMusicName(c.getString(8)); } c.close(); return alarm; }
public int update(Alarm alarm) { ContentValues values = new ContentValues(); values.put(COLUMN_ALARM_MUSIC, alarm.getMusic()); values.put(COLUMN_ALARM_TIME, alarm.getTimeString()); try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(alarm.getDays()); byte[] buff = bos.toByteArray(); values.put(COLUMN_ALARM_DAYS, buff); } catch (Exception e) { } values.put(COLUMN_ALARM_NAME, alarm.getName()); values.put(COLUMN_ALARM_VIBRATE, alarm.getVibrate()); values.put(COLUMN_ALARM_ACTIVE, alarm.getActive()); values.put(COLUMN_ALARM_MUSICTYPE, alarm.getMusicType()); values.put(COLUMN_ALARM_MUSICNAME, alarm.getMusicName()); return getDatabase().update(ALARM_TABLE, values, COLUMN_ALARM_ID + "=" + alarm.getId(), null); }
public int deleteEntry(Alarm alarm) { return deleteEntry(alarm.getId()); }