--- /dev/null
+package ro.ieval.unical;
+
+import android.content.Context;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+
+final class AlarmOpenHelper extends SQLiteOpenHelper{
+ public static final String TIME = "time";
+ public static final String EVENT = "event";
+ public static final String TABLE = "alarms";
+
+ private static final int VERSION = 1;
+ private static final String DB = "alarms";
+
+ AlarmOpenHelper (final Context context) {
+ super(context, DB, null, VERSION);
+ }
+
+ @Override
+ public void onCreate(final SQLiteDatabase db){
+ db.execSQL("CREATE TABLE " + TABLE + " (" +
+ TIME + " INTEGER PRIMARY KEY, " +
+ EVENT + " INTEGER NOT NULL);");
+ }
+
+ @Override
+ public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion){
+ // do nothing
+ }
+}
--- /dev/null
+package ro.ieval.unical;
+
+import android.database.Cursor;
+
+public final class Application extends android.app.Application {
+ public AlarmOpenHelper alarmOpenHelper;
+
+ @Override
+ public void onCreate(){
+ alarmOpenHelper = new AlarmOpenHelper(this);
+ final Cursor cur = alarmOpenHelper.getReadableDatabase().query(AlarmOpenHelper.TABLE, null, null, null, null, null, null);
+ cur.moveToFirst();
+ do {
+ Utils.setAlarm(this, cur.getLong(0), cur.getLong(1));
+ cur.moveToNext();
+ } while(!cur.isAfterLast());
+ }
+}
import android.app.AlarmManager;
import android.app.PendingIntent;
+import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
+import android.database.SQLException;
+import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.ViewGroup;
public static String formatDate(final int year, final int month, final int day){
return String.format(Locale.ENGLISH, "%4d-%02d-%02d", year, month, day);
}
-
- public static void setAlarm(final GregorianCalendar cal, final Context context) {
- final PendingIntent pi = PendingIntent.getActivity(context, 1234, new Intent(context, AlarmReceiverActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);
- final AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
- am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
+
+ public static void addAlarm(final Application application, final long time, final long eventId) throws SQLException{
+ final SQLiteDatabase db = application.alarmOpenHelper.getWritableDatabase();
+ final ContentValues values = new ContentValues(2);
+ values.put(AlarmOpenHelper.TIME, time);
+ values.put(AlarmOpenHelper.EVENT, eventId);
+ db.insertOrThrow(AlarmOpenHelper.TABLE, null, values);
+ setAlarm(application, time, eventId);
+ }
+
+ public static void deleteAlarm(final Application application, final long time) throws SQLException{
+ final SQLiteDatabase db = application.alarmOpenHelper.getWritableDatabase();
+ db.delete(AlarmOpenHelper.TABLE, AlarmOpenHelper.TIME + " = " + time, new String[]{});
+ unsetAlarm(application, time);
+ }
+
+ public static void setAlarm(final Context context, final long time, final long eventId){
+ final Intent intent = new Intent(context, AlarmReceiverActivity.class);
+ intent.setAction(Long.toString(time));
+ intent.putExtra(AlarmReceiverActivity.EXTRA_EVENT, eventId);
+ final AlarmManager man = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+ man.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getActivity(context, 0, intent, 0));
+ }
+
+ public static void unsetAlarm(final Context context, final long time){
+ final Intent intent = new Intent(context, AlarmReceiverActivity.class);
+ intent.setAction(Long.toString(time));
+ final AlarmManager man = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+ man.cancel(PendingIntent.getActivity(context, 0, intent, 0));
}
}